Module: FROST::Hash

Defined in:
lib/frost/hash.rb

Overview

Class Method Summary collapse

Class Method Details

.h1(msg, context) ⇒ Integer

H1 hash function.

Parameters:

  • msg (String)

    The message to be hashed.

  • context (FROST::Context)

    FROST context.

Returns:

  • (Integer)


12
13
14
# File 'lib/frost/hash.rb', line 12

def h1(msg, context)
  hash_to_field(msg, context, "rho")
end

.h2(msg, context) ⇒ Integer

H2 hash function.

Parameters:

  • msg (String)

    The message to be hashed.

  • context (FROST::Context)

    FROST context.

Returns:

  • (Integer)


20
21
22
23
24
25
26
# File 'lib/frost/hash.rb', line 20

def h2(msg, context)
  if context.taproot?
    tagged_hash('BIP0340/challenge', msg)
  else
    hash_to_field(msg, context, "chal")
  end
end

.h3(msg, context) ⇒ Integer

H3 hash function.

Parameters:

  • msg (String)

    The message to be hashed.

  • context (FROST::Context)

    FROST context.

Returns:

  • (Integer)


32
33
34
# File 'lib/frost/hash.rb', line 32

def h3(msg, context)
  hash_to_field(msg, context, "nonce")
end

.h4(msg, context) ⇒ String

H4 hash function.

Parameters:

  • msg (String)

    The message to be hashed.

  • context (FROST::Context)

    FROST context.

Returns:

  • (String)

    The hash value.



40
41
42
# File 'lib/frost/hash.rb', line 40

def h4(msg, context)
  hash(msg, context, "msg")
end

.h5(msg, context) ⇒ String

H5 hash function.

Parameters:

  • msg (String)

    The message to be hashed.

  • context (FROST::Context)

    FROST context.

Returns:

  • (String)

    The hash value.



48
49
50
# File 'lib/frost/hash.rb', line 48

def h5(msg, context)
  hash(msg, context, "com")
end

.hash(msg, context, tag) ⇒ Object



71
72
73
74
# File 'lib/frost/hash.rb', line 71

def hash(msg, context, tag)
  raise ArgumentError "context must be FROST::Context." unless context.is_a?(FROST::Context)
  Digest::SHA256.digest(context.ctx_string + tag + msg)
end

.hash_to_field(msg, context, tag) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/frost/hash.rb', line 60

def hash_to_field(msg, context, tag)
  raise ArgumentError "context must be FROST::Context." unless context.is_a?(FROST::Context)
  h2c = case context.group
        when ECDSA::Group::Secp256k1
          H2C.get(H2C::Suite::SECP256K1_XMDSHA256_SSWU_NU_, context.ctx_string + tag)
        when ECDSA::Group::Secp256r1
          H2C.get(H2C::Suite::P256_XMDSHA256_SSWU_NU_, context.ctx_string + tag)
        end
  h2c.hash_to_field(msg, 1, context.group.order).first
end

.hdkg(msg, context) ⇒ Integer

Hash function for a FROST ciphersuite, used for the DKG.

Parameters:

  • msg (String)

    The message to be hashed.

  • context (FROST::Context)

    FROST context.

Returns:

  • (Integer)

    The hash value.



56
57
58
# File 'lib/frost/hash.rb', line 56

def hdkg(msg, context)
  hash_to_field(msg, context, "dkg")
end

.tagged_hash(tag, msg) ⇒ Object



76
77
78
79
# File 'lib/frost/hash.rb', line 76

def tagged_hash(tag, msg)
  tag_hash = Digest::SHA256.digest(tag)
  Digest::SHA256.hexdigest(tag_hash + tag_hash + msg).to_i(16)
end