Class: Schnorr::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/schnorr/signature.rb

Overview

Instances of this class represents Schnorr signatures, which are simply a pair of integers named ‘r` and `s`.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r, s) ⇒ Signature

Returns a new instance of Signature.

Parameters:



14
15
16
17
18
# File 'lib/schnorr/signature.rb', line 14

def initialize(r, s)
  @r, @s = r, s
  r.is_a?(Integer) or raise ArgumentError, 'r is not an integer.'
  s.is_a?(Integer) or raise ArgumentError, 's is not an integer.'
end

Instance Attribute Details

#rObject (readonly)

Returns the value of attribute r.



9
10
11
# File 'lib/schnorr/signature.rb', line 9

def r
  @r
end

#sObject (readonly)

Returns the value of attribute s.



10
11
12
# File 'lib/schnorr/signature.rb', line 10

def s
  @s
end

Class Method Details

.decode(string) ⇒ Signature

Parse a string to a Schnorr::Signature.

Parameters:

  • string (String)

    signature string with binary format.

Returns:

Raises:



23
24
25
26
27
28
# File 'lib/schnorr/signature.rb', line 23

def self.decode(string)
  raise InvalidSignatureError, 'Invalid schnorr signature length.' unless string.bytesize == 64
  r = string[0...32].unpack('H*').first.to_i(16)
  s = string[32..-1].unpack('H*').first.to_i(16)
  new(r, s)
end

Instance Method Details

#encodeString

Encode signature to string.

Returns:

  • (String)

    encoded signature.



32
33
34
# File 'lib/schnorr/signature.rb', line 32

def encode
  ECDSA::Format::IntegerOctetString.encode(r, 32) + ECDSA::Format::IntegerOctetString.encode(s, 32)
end