Skip to content

Instantly share code, notes, and snippets.

@ntalbott
Created January 12, 2015 20:01
Show Gist options
  • Save ntalbott/2c7fe4d48b2295463a65 to your computer and use it in GitHub Desktop.
Save ntalbott/2c7fe4d48b2295463a65 to your computer and use it in GitHub Desktop.
Verify secretsharing permutations
#!/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