Created
January 12, 2015 20:01
-
-
Save ntalbott/2c7fe4d48b2295463a65 to your computer and use it in GitHub Desktop.
Verify secretsharing permutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "securerandom" | |
TESTS = (ARGV[0] || 1).to_i | |
N = (ARGV[1] || 4).to_i | |
K = (ARGV[2] || 2).to_i | |
BYTES = (ARGV[3] || 24).to_i | |
puts "Running #{TESTS} tests of n=#{N} k=#{K} with bytelength of #{BYTES}..." | |
puts | |
flunked = [] | |
permutations = (0...N).to_a.permutation(K).collect{|e| e.sort}.uniq | |
require "secretsharing" | |
module SecretSharing | |
module Shamir | |
class Share | |
extend SecretSharing::Shamir | |
end | |
end | |
end | |
(1..BYTES).each do |bytes| | |
print "#{bytes}: " | |
TESTS.times do | |
begin | |
bn = OpenSSL::BN.new(SecureRandom.hex(bytes), 16) | |
print " #{bn.num_bits}" | |
c0 = SecretSharing::Shamir::Container.new(N, K) | |
c0.secret = SecretSharing::Shamir::Secret.new(secret: bn) | |
permutations.each do |indexes| | |
c_test = SecretSharing::Shamir::Container.new(K) | |
indexes.each do |i| | |
c_test << c0.shares[i] | |
end | |
abort("Mismatch #{c_test.secret} != #{c0.secret}") unless c_test.secret == c0.secret | |
end | |
rescue OpenSSL::BNError => e | |
print "!" | |
end | |
end | |
puts | |
end | |
puts | |
puts | |
puts "All good?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment