Class: RingSig::PrivateKey
- Inherits:
-
Object
- Object
- RingSig::PrivateKey
- Defined in:
- lib/ring_sig/private_key.rb
Overview
Instances of this class represent a private ECDSA key.
Instance Attribute Summary collapse
- #hasher ⇒ Hasher readonly
- #public_key ⇒ PublicKey readonly
-
#value ⇒ Integer
readonly
The integer value of this private key.
Class Method Summary collapse
-
.from_hex(hex_string, hasher) ⇒ PrivateKey
Creates a new instance of PrivateKey from a hex string.
-
.from_octet(octet_string, hasher) ⇒ PrivateKey
Creates a new instance of PrivateKey from an octet string.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if the private keys are equal.
-
#initialize(value, hasher) ⇒ PrivateKey
constructor
Creates a new instance of PrivateKey.
-
#key_image ⇒ ECDSA::Point
The key image.
-
#point ⇒ ECDSA::Point
The public key's point.
-
#sign(message, foreign_keys) ⇒ Array(Signature, Array<PublicKey>)
Signs a message with this key's private key and a set of foreign public keys.
-
#to_hex ⇒ String
Encodes this private key into an octet string.
-
#to_octet ⇒ String
Encodes this public key into a hex string.
Constructor Details
#initialize(value, hasher) ⇒ PrivateKey
Creates a new instance of RingSig::PrivateKey.
21 22 23 24 25 26 27 28 29 |
# File 'lib/ring_sig/private_key.rb', line 21 def initialize(value, hasher) raise ArgumentError, "Value is not an integer" unless value.is_a?(Integer) raise ArgumentError, "Value is too small" if value < 1 raise ArgumentError, "Value is too large" if value >= hasher.group.order @value = value @hasher = hasher @public_key = PublicKey.new(hasher.group.generator * value, hasher) end |
Instance Attribute Details
#hasher ⇒ Hasher (readonly)
15 16 17 |
# File 'lib/ring_sig/private_key.rb', line 15 def hasher @hasher end |
#public_key ⇒ PublicKey (readonly)
12 13 14 |
# File 'lib/ring_sig/private_key.rb', line 12 def public_key @public_key end |
#value ⇒ Integer (readonly)
The integer value of this private key. A number between 0 and the group's order (non-inclusive).
9 10 11 |
# File 'lib/ring_sig/private_key.rb', line 9 def value @value end |
Class Method Details
.from_hex(hex_string, hasher) ⇒ PrivateKey
Creates a new instance of RingSig::PrivateKey from a hex string.
36 37 38 |
# File 'lib/ring_sig/private_key.rb', line 36 def self.from_hex(hex_string, hasher) self.from_octet([hex_string].pack('H*'), hasher) end |
.from_octet(octet_string, hasher) ⇒ PrivateKey
Creates a new instance of RingSig::PrivateKey from an octet string.
45 46 47 48 |
# File 'lib/ring_sig/private_key.rb', line 45 def self.from_octet(octet_string, hasher) value = ECDSA::Format::FieldElementOctetString.decode(octet_string, hasher.group.field) PrivateKey.new(value, hasher) end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if the private keys are equal.
106 107 108 109 |
# File 'lib/ring_sig/private_key.rb', line 106 def ==(other) return false unless other.is_a?(PrivateKey) value == other.value && hasher == other.hasher end |
#key_image ⇒ ECDSA::Point
Returns the key image.
96 97 98 |
# File 'lib/ring_sig/private_key.rb', line 96 def key_image @key_image ||= hasher.hash_point(point) * value end |
#point ⇒ ECDSA::Point
Returns the public key's point.
101 102 103 |
# File 'lib/ring_sig/private_key.rb', line 101 def point public_key.point end |
#sign(message, foreign_keys) ⇒ Array(Signature, Array<PublicKey>)
Signs a message with this key's private key and a set of foreign public keys. The resulting signature can be verified against the ordered set of all public keys used for creating this signature. The signature will also contain a key_image which will be the same for all messages signed with this key.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ring_sig/private_key.rb', line 76 def sign(, foreign_keys) raise ArgumentError "Foreign keys must all have the same hasher" unless foreign_keys.all?{ |e| e.hasher == hasher } = hasher.hash_string() seed = hasher.hash_array([value, ]) all_keys = hasher.shuffle([self] + foreign_keys, seed) q_array, w_array = generate_q_w(all_keys, seed) ll_array, rr_array = generate_ll_rr(all_keys, q_array, w_array) challenge = hasher.hash_array([] + ll_array + rr_array) c_array, r_array = generate_c_r(all_keys, q_array, w_array, challenge) public_keys = all_keys.map(&:public_key) signature = Signature.new(key_image, c_array, r_array, hasher) [signature, public_keys] end |
#to_hex ⇒ String
Encodes this private key into an octet string. The encoded data contains only the value. It does not contain the hasher.
54 55 56 |
# File 'lib/ring_sig/private_key.rb', line 54 def to_hex to_octet.unpack('H*').first end |
#to_octet ⇒ String
Encodes this public key into a hex string. The encoded data contains only the value. It does not contain the hasher.
62 63 64 |
# File 'lib/ring_sig/private_key.rb', line 62 def to_octet ECDSA::Format::FieldElementOctetString.encode(value, hasher.group.field) end |