Module: Schnorr::Util

Included in:
Schnorr, MuSig2, MuSig2::KeyAggContext, MuSig2::SessionContext
Defined in:
lib/schnorr/util.rb

Instance Method Summary collapse

Instance Method Details

#hex2bin(str) ⇒ String

If str is a hex value, it is converted to binary. Otherwise, it is returned as is.

Parameters:

Returns:



17
18
19
# File 'lib/schnorr/util.rb', line 17

def hex2bin(str)
  hex_string?(str) ? [str].pack('H*') : str
end

#hex_string?(str) ⇒ Boolean

Check whether str is hex string or not.

Parameters:

Returns:

  • (Boolean)


7
8
9
10
11
12
# File 'lib/schnorr/util.rb', line 7

def hex_string?(str)
  return false if str.bytes.any? { |b| b > 127 }
  return false if str.length % 2 != 0
  hex_chars = str.chars.to_a
  hex_chars.all? { |c| c =~ /[0-9a-fA-F]/ }
end

#pad_scalar(scalar) ⇒ Integer

Pad scalar with the group order so that its bit length is fixed, mitigating a timing leak of the bit length of a secret scalar. Since (k + i * n)G = kG, this does not change the resulting point. Only use this for secret scalars; padding a public scalar just costs an extra doubling.

Parameters:

  • scalar (Integer)

    A secret scalar.

Returns:



34
35
36
37
38
# File 'lib/schnorr/util.rb', line 34

def pad_scalar(scalar)
  k = scalar + GROUP.order
  k += GROUP.order if k.bit_length == GROUP.order.bit_length
  k
end

#string2point(str) ⇒ ECDSA::Point

Convert str to the point of elliptic curve.

Parameters:

  • str (String)

    A byte string for point.

Returns:



24
25
26
# File 'lib/schnorr/util.rb', line 24

def string2point(str)
  ECDSA::Format::PointOctetString.decode(str, GROUP)
end