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, opts = {}) ⇒ PublicKey

Creates a new instance of RingSig::PublicKey.

Parameters:

  • point (ECDSA::Point)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :group (ECDSA::Group)

Raises:

  • (ArgumentError)


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

def initialize(point, opts = {})
  @group = opts.delete(:group) { RingSig.default_group }
  raise ArgumentError, "Unknown opts: #{opts.keys.join(', ')}" unless opts.empty?

  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 group.include?(point)

  @point = point
end

Instance Attribute Details

#groupECDSA::Group (readonly)

Returns:

  • (ECDSA::Group)


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

def group
  @group
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, opts = {}) ⇒ PublicKey

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

Parameters:

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

Options Hash (opts):

  • :group (ECDSA::Group)

Returns:

Raises:

  • (ArgumentError)


34
35
36
37
38
39
# File 'lib/ring_sig/public_key.rb', line 34

def self.from_hex(hex_string, opts = {})
  group = opts.delete(:group) { RingSig.default_group }
  raise ArgumentError, "Unknown opts: #{opts.keys.join(', ')}" unless opts.empty?

  self.from_octet([hex_string].pack('H*'), group: group)
end

.from_octet(octet_string, opts = {}) ⇒ PublicKey

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

Parameters:

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

Options Hash (opts):

  • :group (ECDSA::Group)

Returns:

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
# File 'lib/ring_sig/public_key.rb', line 47

def self.from_octet(octet_string, opts = {})
  group = opts.delete(:group) { RingSig.default_group }
  raise ArgumentError, "Unknown opts: #{opts.keys.join(', ')}" unless opts.empty?

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

Instance Method Details

#==(other) ⇒ Boolean

Returns true if the public keys are equal.

Returns:

  • (Boolean)

    true if the public keys are equal.



87
88
89
90
# File 'lib/ring_sig/public_key.rb', line 87

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

#public_keyPublicKey

Returns self.

Returns:



82
83
84
# File 'lib/ring_sig/public_key.rb', line 82

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 group.

Parameters:

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

Options Hash (opts):

  • :compression (Boolean)

Returns:

  • (String)

Raises:

  • (ArgumentError)


61
62
63
64
65
66
# File 'lib/ring_sig/public_key.rb', line 61

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 group.

Parameters:

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

Options Hash (opts):

  • :compression (Boolean)

Returns:

  • (String)

Raises:

  • (ArgumentError)


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

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