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:



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

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

#r ⇒ Object (readonly)

Returns the value of attribute r.



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

def r
  @r
end

#s ⇒ Object (readonly)

Returns the value of attribute s.



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

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:



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

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

#encode ⇒ String

Encode signature to string.

Returns:

  • (String) —

    encoded signature.



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

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