Module: RSA::ACC::Functions
- Included in:
- MembershipProof, PoE, PoE, PoKE2, PoKE2, RSA::Accumulator
- Defined in:
- lib/rsa/acc/functions.rb
Instance Method Summary collapse
-
#blake2_hash(*params) ⇒ Integer
Computes hash value from
params
. -
#compute_challenge(*params) ⇒ Integer
Computes a challenge from
params
. -
#egcd(x, y) ⇒ Array[Integer, Integer]
Computes Bezout coefficients.
-
#elements_to_prime(elements) ⇒ Integer
Converts a list of elements to an product of prime numbers.
-
#hash_to_prime(element) ⇒ Integer
Convert element to prime number.
-
#shamir_trick(w1, w2, x, y, modulus) ⇒ Integer
Computes (xy) th root of g given xth and yth roots of g.
Instance Method Details
#blake2_hash(*params) ⇒ Integer
Computes hash value from params
.
68 69 70 |
# File 'lib/rsa/acc/functions.rb', line 68 def blake2_hash(*params) RbNaCl::Hash.blake2b(params.map{|p|even_hex(p)}.join).unpack("H*").first.to_i(16) end |
#compute_challenge(*params) ⇒ Integer
Computes a challenge from params
.
61 62 63 |
# File 'lib/rsa/acc/functions.rb', line 61 def compute_challenge(*params) hash_to_prime(params.map{|p|even_hex(p)}.join) end |
#egcd(x, y) ⇒ Array[Integer, Integer]
Computes Bezout coefficients. see: github.com/dryruby/rsa.rb/blob/b1366970d31dba0078fd06d9f5d3ddd4952fb087/lib/rsa/math.rb#L143
52 53 54 55 56 |
# File 'lib/rsa/acc/functions.rb', line 52 def egcd(x, y) return [0, 1] if x.modulo(y).zero? a, b = egcd(y, x.modulo(y)) [b, a - b * x.div(y)] end |
#elements_to_prime(elements) ⇒ Integer
Converts a list of elements to an product of prime numbers.
28 29 30 |
# File 'lib/rsa/acc/functions.rb', line 28 def elements_to_prime(elements) elements.map{|e|hash_to_prime(e)}.inject(:*) end |
#hash_to_prime(element) ⇒ Integer
Convert element to prime number.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rsa/acc/functions.rb', line 12 def hash_to_prime(element) nonce = 0 loop do candidate = RbNaCl::Hash.blake2b(element + even_hex(nonce)).unpack("C*") candidate[-1] |= 1 candidate = candidate.pack('c*').unpack("H*").first.to_i(16) if candidate.to_bn.prime? return candidate end nonce += 1 end end |
#shamir_trick(w1, w2, x, y, modulus) ⇒ Integer
Computes (xy) th root of g given xth and yth roots of g. x and y is co-prime. (a, b) ← Bezout(x, y)
40 41 42 43 44 45 |
# File 'lib/rsa/acc/functions.rb', line 40 def shamir_trick(w1, w2, x, y, modulus) raise ArgumentError, 'w1^x != w2^y' unless w1.pow(x, modulus) == w2.pow(y, modulus) a, b = egcd(x, y) raise ArgumentError, 'Inputs does not co-prime.' unless a * x + b * y == 1 (w1.pow(b, modulus) * w2.pow(a, modulus)) % modulus end |