Class: X25519::Scalar
- Inherits:
-
Object
- Object
- X25519::Scalar
- Defined in:
- lib/x25519/scalar.rb
Overview
X25519 private keys
Scalars are the integer component of scalar multiplication, multiplied against an elliptic curve point.
Class Method Summary collapse
-
.generate ⇒ Object
Securely generate a random scalar.
Instance Method Summary collapse
-
#diffie_hellman(montgomery_u) ⇒ X25519::MontgomeryU
(also: #multiply)
Variable-base scalar multiplication a.k.a.
-
#initialize(bytes) ⇒ Scalar
constructor
Create an X25519 scalar object from a bytestring.
-
#inspect ⇒ Object
String inspection that does not leak the private scalar.
-
#public_key ⇒ X25519::MontgomeryU
(also: #multiply_base)
Fixed-base scalar multiplication.
-
#to_bytes ⇒ String
Return a bytestring representation of this scalar.
Constructor Details
#initialize(bytes) ⇒ Scalar
Create an X25519 scalar object from a bytestring
17 18 19 20 |
# File 'lib/x25519/scalar.rb', line 17 def initialize(bytes) X25519.validate_key_bytes(bytes) @scalar_bytes = bytes end |
Class Method Details
Instance Method Details
#diffie_hellman(montgomery_u) ⇒ X25519::MontgomeryU Also known as: multiply
Variable-base scalar multiplication a.k.a. Diffie-Hellman
This can be used to obtain a shared secret from a public key
29 30 31 32 33 |
# File 'lib/x25519/scalar.rb', line 29 def diffie_hellman(montgomery_u) raise TypeError, "expected X25519::MontgomeryU, got #{montgomery_u}" unless montgomery_u.is_a?(MontgomeryU) MontgomeryU.new(X25519.diffie_hellman(@scalar_bytes, montgomery_u.to_bytes)) end |
#inspect ⇒ Object
String inspection that does not leak the private scalar
53 54 55 |
# File 'lib/x25519/scalar.rb', line 53 def inspect to_s end |
#public_key ⇒ X25519::MontgomeryU Also known as: multiply_base
Fixed-base scalar multiplication. Calculates a public key from a private scalar
40 41 42 |
# File 'lib/x25519/scalar.rb', line 40 def public_key MontgomeryU.new(X25519.calculate_public_key(@scalar_bytes)) end |
#to_bytes ⇒ String
Return a bytestring representation of this scalar
48 49 50 |
# File 'lib/x25519/scalar.rb', line 48 def to_bytes @scalar_bytes end |