Module: RSA::ACC::Functions

Included in:
MembershipProof, PoE, PoE, PoKE2, PoKE2, RSA::Accumulator
Defined in:
lib/rsa/acc/functions.rb

Instance Method Summary collapse

Instance Method Details

#blake2_hash(*params) ⇒ Integer

Computes hash value from params.

Parameters:

  • params (Array[Integer])

Returns:

  • (Integer)

    hash value.



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.

Parameters:

  • params (Array[Integer])

Returns:

  • (Integer)

    prime number of challenge.



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]

Parameters:

  • x (Integer)
  • y (Integer)

Returns:

  • (Array[Integer, Integer])

    Bezout coefficients



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.

Parameters:

  • elements (Array[String])

    a list of element.

Returns:

  • (Integer)

    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.

Parameters:

  • Array[String] (Array[String] elements an element to be converted.)

    elements an element to be converted.

Returns:

  • (Integer)

    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)

Parameters:

  • w1 (Integer)

    first witness.

  • w2 (Integer)

    second witness.

  • x (Integer)
  • y (Integer)

Returns:

  • (Integer)

    w1^b * w2^a

Raises:

  • (ArgumentError)


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