libsodium_crypto_core_ristretto255 (libsodium v3.0.1)

View Source

Low-level arithmetic on the ristretto255 prime-order group (RFC 9496) and on scalars modulo its order L, for building custom protocols.

Ristretto255 is built on edwards25519 but has no cofactor: every valid bytes/0 byte encoding is canonical and represents a distinct group element, so there are no small-order points to reject. Encodings of edwards25519 points are not interchangeable with ristretto255 encodings.

Scalars are scalarbytes/0 byte little-endian integers and are used as given (no clamping). from_hash/1 maps hashbytes/0 bytes of uniform data (such as a SHA-512 digest) to a group element, and scalar_reduce/1 reduces a nonreducedscalarbytes/0 byte value modulo L.

P = libsodium_crypto_core_ristretto255:random(),
Q = libsodium_crypto_core_ristretto255:from_hash(libsodium_randombytes:buf(64)),
1 = libsodium_crypto_core_ristretto255:is_valid_point(Q),
R = libsodium_crypto_core_ristretto255:add(P, Q),
P = libsodium_crypto_core_ristretto255:sub(R, Q),
S = libsodium_crypto_core_ristretto255:scalar_random(),
One = libsodium_crypto_core_ristretto255:scalar_mul(S, libsodium_crypto_core_ristretto255:scalar_invert(S)),
One = <<1, 0:248>>.

Summary

Types

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

h()

A crypto_core_ristretto255_HASHBYTES (64-byte) binary.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

A crypto_core_ristretto255_NONREDUCEDSCALARBYTES (64-byte) binary.

p()

A crypto_core_ristretto255_BYTES (32-byte) binary.

q()

A crypto_core_ristretto255_BYTES (32-byte) binary.

r()

A crypto_core_ristretto255_BYTES (32-byte) binary.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

s()

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

x()

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

y()

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

z()

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

Functions

Return the element P + Q, or -1 when either input is not a valid encoding.

Return crypto_core_ristretto255_bytes(), in bytes.

Map the hashbytes/0 byte uniform string H (such as a SHA-512 digest) to a group element.

Return crypto_core_ristretto255_hashbytes(), in bytes.

Return 1 when P is a valid canonical ristretto255 encoding, otherwise 0.

Return crypto_core_ristretto255_nonreducedscalarbytes(), in bytes.

Return a random group element.

Return X + Y mod L.

Return 1 - S mod L, so that S + Comp = 1 (mod L).

Return the multiplicative inverse of the scalar S modulo L, or -1 when S is zero.

Return X * Y mod L.

Return -S mod L.

Return a random scalar in [0, L).

Reduce the nonreducedscalarbytes/0 byte little-endian integer S modulo L. S should be sampled from at least 317 bits (such as a 64-byte hash) for the result to be close to uniform.

Return X - Y mod L.

Return crypto_core_ristretto255_scalarbytes(), in bytes.

Return the element P - Q, or -1 when either input is not a valid encoding.

Types

comp()

-type comp() :: <<_:256>>.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

h()

-type h() :: <<_:512>>.

A crypto_core_ristretto255_HASHBYTES (64-byte) binary.

neg()

-type neg() :: <<_:256>>.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

nonreduced_scalar()

-type nonreduced_scalar() :: <<_:512>>.

A crypto_core_ristretto255_NONREDUCEDSCALARBYTES (64-byte) binary.

p()

-type p() :: <<_:256>>.

A crypto_core_ristretto255_BYTES (32-byte) binary.

q()

-type q() :: <<_:256>>.

A crypto_core_ristretto255_BYTES (32-byte) binary.

r()

-type r() :: <<_:256>>.

A crypto_core_ristretto255_BYTES (32-byte) binary.

recip()

-type recip() :: <<_:256>>.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

s()

-type s() :: <<_:256>>.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

x()

-type x() :: <<_:256>>.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

y()

-type y() :: <<_:256>>.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

z()

-type z() :: <<_:256>>.

A crypto_core_ristretto255_SCALARBYTES (32-byte) binary.

Functions

add(P, Q)

-spec add(P, Q) -> Result when P :: p(), Q :: q(), Result :: r() | -1.

Return the element P + Q, or -1 when either input is not a valid encoding.

bytes()

-spec bytes() -> Result when Result :: 32.

Return crypto_core_ristretto255_bytes(), in bytes.

from_hash(H)

-spec from_hash(H) -> Result when H :: h(), Result :: p() | -1.

Map the hashbytes/0 byte uniform string H (such as a SHA-512 digest) to a group element.

hashbytes()

-spec hashbytes() -> Result when Result :: 64.

Return crypto_core_ristretto255_hashbytes(), in bytes.

is_valid_point(P)

-spec is_valid_point(P) -> Result when P :: p(), Result :: 0 | 1.

Return 1 when P is a valid canonical ristretto255 encoding, otherwise 0.

nonreducedscalarbytes()

-spec nonreducedscalarbytes() -> Result when Result :: 64.

Return crypto_core_ristretto255_nonreducedscalarbytes(), in bytes.

random()

-spec random() -> Result when Result :: p().

Return a random group element.

scalar_add(X, Y)

-spec scalar_add(X, Y) -> Result when X :: x(), Y :: y(), Result :: z().

Return X + Y mod L.

scalar_complement(S)

-spec scalar_complement(S) -> Result when S :: s(), Result :: comp().

Return 1 - S mod L, so that S + Comp = 1 (mod L).

scalar_invert(S)

-spec scalar_invert(S) -> Result when S :: s(), Result :: recip() | -1.

Return the multiplicative inverse of the scalar S modulo L, or -1 when S is zero.

scalar_mul(X, Y)

-spec scalar_mul(X, Y) -> Result when X :: x(), Y :: y(), Result :: z().

Return X * Y mod L.

scalar_negate(S)

-spec scalar_negate(S) -> Result when S :: s(), Result :: neg().

Return -S mod L.

scalar_random()

-spec scalar_random() -> Result when Result :: r().

Return a random scalar in [0, L).

scalar_reduce(S)

-spec scalar_reduce(S) -> Result when S :: nonreduced_scalar(), Result :: r().

Reduce the nonreducedscalarbytes/0 byte little-endian integer S modulo L. S should be sampled from at least 317 bits (such as a 64-byte hash) for the result to be close to uniform.

scalar_sub(X, Y)

-spec scalar_sub(X, Y) -> Result when X :: x(), Y :: y(), Result :: z().

Return X - Y mod L.

scalarbytes()

-spec scalarbytes() -> Result when Result :: 32.

Return crypto_core_ristretto255_scalarbytes(), in bytes.

sub(P, Q)

-spec sub(P, Q) -> Result when P :: p(), Q :: q(), Result :: r() | -1.

Return the element P - Q, or -1 when either input is not a valid encoding.