class X25519::Scalar

X25519 private keys

Scalars are the integer component of scalar multiplication, multiplied against an elliptic curve point.

Public Class Methods

generate() click to toggle source

Securely generate a random scalar

# File lib/x25519-termux/scalar.rb, line 10
def self.generate
  new(SecureRandom.random_bytes(X25519::KEY_SIZE))
end
new(bytes) click to toggle source

Create an X25519 scalar object from a bytestring

@param bytes [String] 32-byte random secret scalar

# File lib/x25519-termux/scalar.rb, line 17
def initialize(bytes)
  X25519.validate_key_bytes(bytes)
  @scalar_bytes = bytes
end

Public Instance Methods

diffie_hellman(montgomery_u) click to toggle source

Variable-base scalar multiplication a.k.a. Diffie-Hellman

This can be used to obtain a shared secret from a public key

@param montgomery_u [X25519::MontgomeryU] coordinate of the public key/point to perform D-H with

@return [X25519::MontgomeryU] resulting point (i.e. D-H shared secret)

# File lib/x25519-termux/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
Also aliased as: multiply
inspect() click to toggle source

String inspection that does not leak the private scalar

# File lib/x25519-termux/scalar.rb, line 52
def inspect
  to_s
end
multiply(montgomery_u)
Alias for: diffie_hellman
multiply_base()
Alias for: public_key
public_key() click to toggle source

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

@return [X25519::MontgomeryU] resulting point (i.e. public key)

# File lib/x25519-termux/scalar.rb, line 39
def public_key
  MontgomeryU.new(X25519.calculate_public_key(@scalar_bytes))
end
Also aliased as: multiply_base
to_bytes() click to toggle source

Return a bytestring representation of this scalar

@return [String] scalar converted to a bytestring

# File lib/x25519-termux/scalar.rb, line 47
def to_bytes
  @scalar_bytes
end