2 require 'secretsharing/shamir'
4 DEFAULT_SECRET_BITLENGTH = 128
6 class TestShamir < Test::Unit::TestCase
8 assert_raise( ArgumentError ) { SecretSharing::Shamir.new }
9 s1 = SecretSharing::Shamir.new(5)
12 assert_false(s1.secret_set?)
13 s2 = SecretSharing::Shamir.new(5, 3)
16 assert_false(s2.secret_set?)
19 def test_create_random_secret
20 s = SecretSharing::Shamir.new(5)
21 s.create_random_secret()
22 assert_true(s.secret_set?)
23 assert_not_nil(s.secret)
24 assert_not_nil(s.shares)
25 assert_equal(s.shares.class, Array)
26 assert_equal(s.shares.length, 5)
27 assert_equal(s.shares[0].class, SecretSharing::Shamir::Share)
28 assert_equal(s.secret_bitlength, DEFAULT_SECRET_BITLENGTH)
30 # can only be called once
31 assert_raise( RunTimeError) { s.create_random_secret() }
33 s2 = SecretSharing::Shamir.new(7)
34 s2.create_random_secret(256)
35 assert_equal(s2.secret_bitlength, 256)
38 def test_recover_secret_k_eq_n
39 s = SecretSharing::Shamir.new(5)
40 s.create_random_secret()
42 s2 = SecretSharing::Shamir.new(5)
43 s2.shares << s.shares[0]
44 assert_equal(s2.shares.length, 1)
45 assert_false(s2.secret_set?)
47 # adding the same share raises an error
48 assert_raise( RunTimeError ) { s2.shares << s.shares[0] }
50 s2.shares << s.shares[1]
51 assert_false(s2.secret_set?)
52 s2.shares << s.shares[2]
53 assert_false(s2.secret_set?)
54 s2.shares << s.shares[3]
55 assert_false(s2.secret_set?)
56 s2.shares << s.shares[4]
57 assert_true(s2.secret_set?)
58 assert_equal(s.secret, s2.secret)
61 def test_recover_secret_k_le_n
62 s = SecretSharing::Shamir.new(5, 3)
63 s.create_random_secret()
65 s2 = SecretSharing::Shamir.new(5, 3)
66 s2.shares << s.shares[0]
67 assert_equal(s2.shares.length, 1)
68 assert_false(s2.secret_set?)
71 s2.shares << s.shares[1]
72 assert_false(s2.secret_set?)
73 s2.shares << s.shares[2]
74 assert_true(s2.secret_set?)
75 assert_equal(s.secret, s2.secret)
77 # adding more shares than needed raises an error
78 assert_raise( RunTimeError ) { s2.shares << s.shares[3] }