Class: RingSig::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/ring_sig/public_key.rb

Overview

Instances of this class represent a public ECDSA key.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(point, hasher) ⇒ PublicKey

Creates a new instance of RingSig::PublicKey.

Parameters:

  • point (ECDSA::Point)
  • hasher (Hasher)

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/ring_sig/public_key.rb', line 17

def initialize(point, hasher)
  raise ArgumentError, "Point is not an ECDSA::Point" unless point.is_a?(ECDSA::Point)
  raise ArgumentError, "Point is not on the group's curve" unless hasher.group.include?(point)

  @point = point
  @hasher = hasher
end

Instance Attribute Details

#hasherHasher (readonly)

Returns:



11
12
13
# File 'lib/ring_sig/public_key.rb', line 11

def hasher
  @hasher
end

#pointECDSA::Point (readonly)

The elliptical curve point of this public key.

Returns:

  • (ECDSA::Point)


8
9
10
# File 'lib/ring_sig/public_key.rb', line 8

def point
  @point
end

Class Method Details

.from_hex(hex_string, hasher) ⇒ PublicKey

Creates a new instance of RingSig::PublicKey from a hex string.

Parameters:

  • hex_string (String)
  • hasher (Hasher)

Returns:



30
31
32
# File 'lib/ring_sig/public_key.rb', line 30

def self.from_hex(hex_string, hasher)
  self.from_octet([hex_string].pack('H*'), hasher)
end

.from_octet(octet_string, hasher) ⇒ PublicKey

Creates a new instance of RingSig::PublicKey from an octet string.

Parameters:

  • octet_string (String)
  • hasher (Hasher)

Returns:



39
40
41
42
# File 'lib/ring_sig/public_key.rb', line 39

def self.from_octet(octet_string, hasher)
  point = ECDSA::Format::PointOctetString.decode(octet_string, hasher.group)
  PublicKey.new(point, hasher)
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if the public keys are equal.

Returns:

  • (Boolean)

    true if the public keys are equal.



76
77
78
79
# File 'lib/ring_sig/public_key.rb', line 76

def ==(other)
  return false unless other.is_a?(PublicKey)
  point == other.point && hasher == other.hasher
end

#public_keyPublicKey

Returns self.

Returns:



71
72
73
# File 'lib/ring_sig/public_key.rb', line 71

def public_key
  self
end

#to_hex(opts = {}) ⇒ String

Encodes this public key into an octet string. The encoded data contains only the point. It does not contain the hasher.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :compression (Boolean) — default: true

Returns:

  • (String)

Raises:

  • (ArgumentError)


50
51
52
53
54
55
# File 'lib/ring_sig/public_key.rb', line 50

def to_hex(opts = {})
  compression = opts.delete(:compression) { true }
  raise ArgumentError, "Unknown opts: #{opts.keys.join(', ')}" unless opts.empty?

  to_octet(compression: compression).unpack('H*').first
end

#to_octet(opts = {}) ⇒ String

Encodes this public key into a hex string. The encoded data contains only the point. It does not contain the hasher.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :compression (Boolean) — default: true

Returns:

  • (String)

Raises:

  • (ArgumentError)


63
64
65
66
67
68
# File 'lib/ring_sig/public_key.rb', line 63

def to_octet(opts = {})
  compression = opts.delete(:compression) { true }
  raise ArgumentError, "Unknown opts: #{opts.keys.join(', ')}" unless opts.empty?

  ECDSA::Format::PointOctetString.encode(point, compression: compression)
end