Class: X25519::Scalar

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Scalar

Create an X25519 scalar object from a bytestring

Parameters:

  • bytes (String)

    32-byte random secret scalar



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

.generateObject

Securely generate a random scalar



10
11
12
# File 'lib/x25519/scalar.rb', line 10

def self.generate
  new(SecureRandom.random_bytes(X25519::KEY_SIZE))
end

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

Parameters:

  • montgomery_u (X25519::MontgomeryU)

    coordinate of the public key/point to perform D-H with

Returns:

Raises:

  • (TypeError)


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

#inspectObject

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_keyX25519::MontgomeryU Also known as: multiply_base

Fixed-base scalar multiplication. Calculates a public key from a private scalar

Returns:



40
41
42
# File 'lib/x25519/scalar.rb', line 40

def public_key
  MontgomeryU.new(X25519.calculate_public_key(@scalar_bytes))
end

#to_bytesString

Return a bytestring representation of this scalar

Returns:

  • (String)

    scalar converted to a bytestring



48
49
50
# File 'lib/x25519/scalar.rb', line 48

def to_bytes
  @scalar_bytes
end