Module: JOSE::JWA::X25519_CryptoRb

Extended by:
X25519_CryptoRb
Included in:
X25519_CryptoRb
Defined in:
lib/jose/jwa/x25519_cryptorb.rb

Instance Method Summary collapse

Instance Method Details

#coerce_montgomery_u!(pk) ⇒ Object



35
36
37
38
39
# File 'lib/jose/jwa/x25519_cryptorb.rb', line 35

def coerce_montgomery_u!(pk)
  return pk if pk.is_a?(X25519::MontgomeryU)
  pk = JOSE::JWA::X25519.coerce_coordinate_bytes!(u) if not pk.respond_to?(:bytesize)
  return X25519::MontgomeryU.new(pk)
end

#coerce_scalar!(sk) ⇒ Object



41
42
43
44
45
# File 'lib/jose/jwa/x25519_cryptorb.rb', line 41

def coerce_scalar!(sk)
  return sk if sk.is_a?(X25519::Scalar)
  sk = JOSE::JWA::X25519.coerce_scalar_bytes!(sk) if not sk.respond_to?(:bytesize)
  return X25519::Scalar.new(sk)
end

#curve25519(k, u) ⇒ Object



5
6
7
8
9
# File 'lib/jose/jwa/x25519_cryptorb.rb', line 5

def curve25519(k, u)
  k = coerce_scalar!(k)
  u = coerce_montgomery_u!(u)
  return k.diffie_hellman(u)
end

#keypair(sk = nil) ⇒ Object



20
21
22
23
24
25
# File 'lib/jose/jwa/x25519_cryptorb.rb', line 20

def keypair(sk = nil)
  sk ||= X25519::Scalar.generate()
  scalar = coerce_scalar!(sk)
  pk = sk_to_pk(scalar)
  return pk, scalar.to_bytes()
end

#shared_secret(pk, sk) ⇒ Object



27
28
29
# File 'lib/jose/jwa/x25519_cryptorb.rb', line 27

def shared_secret(pk, sk)
  return x25519(sk, pk)
end

#sk_to_pk(sk) ⇒ Object



31
32
33
# File 'lib/jose/jwa/x25519_cryptorb.rb', line 31

def sk_to_pk(sk)
  return x25519_base(sk)
end

#x25519(sk, pk) ⇒ Object



11
12
13
# File 'lib/jose/jwa/x25519_cryptorb.rb', line 11

def x25519(sk, pk)
  return curve25519(sk, pk).to_bytes()
end

#x25519_base(sk) ⇒ Object



15
16
17
18
# File 'lib/jose/jwa/x25519_cryptorb.rb', line 15

def x25519_base(sk)
  scalar = coerce_scalar!(sk)
  return scalar.public_key.to_bytes()
end