Class: RingSig::PublicKey
- Inherits:
-
Object
- Object
- RingSig::PublicKey
- Defined in:
- lib/ring_sig/public_key.rb
Overview
Instances of this class represent a public ECDSA key.
Instance Attribute Summary collapse
- #group ⇒ ECDSA::Group readonly
-
#point ⇒ ECDSA::Point
readonly
The elliptical curve point of this public key.
Class Method Summary collapse
-
.from_hex(hex_string, opts = {}) ⇒ PublicKey
Creates a new instance of PublicKey from a hex string.
-
.from_octet(octet_string, opts = {}) ⇒ PublicKey
Creates a new instance of PublicKey from an octet string.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if the public keys are equal.
-
#initialize(point, opts = {}) ⇒ PublicKey
constructor
Creates a new instance of PublicKey.
-
#public_key ⇒ PublicKey
Self.
-
#to_hex(opts = {}) ⇒ String
Encodes this public key into an octet string.
-
#to_octet(opts = {}) ⇒ String
Encodes this public key into a hex string.
Constructor Details
#initialize(point, opts = {}) ⇒ PublicKey
Creates a new instance of RingSig::PublicKey.
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
#group ⇒ ECDSA::Group (readonly)
11 12 13 |
# File 'lib/ring_sig/public_key.rb', line 11 def group @group end |
#point ⇒ ECDSA::Point (readonly)
The elliptical curve point of this public key.
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.
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.
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.
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_key ⇒ PublicKey
Returns self.
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.
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.
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 |