libsodium_crypto_core_ed25519 (libsodium v3.0.1)

View Source

Low-level arithmetic on the edwards25519 curve and on scalars modulo its prime group order L, for building custom protocols.

Points are bytes/0 byte compressed encodings and scalars are scalarbytes/0 byte little-endian integers. is_valid_point/1 accepts only canonical encodings of points on the main subgroup; add/2 and sub/2 only reject encodings that are not on the curve, so check untrusted input first. Scalars are used as given: there is no clamping, unlike libsodium_crypto_scalarmult_ed25519.

scalar_reduce/1 reduces a nonreducedscalarbytes/0 byte value modulo L, and from_uniform/1 maps uniformbytes/0 bytes (such as a hash) to a point. Prefer libsodium_crypto_core_ristretto255 for new protocols, as it avoids the cofactor pitfalls of edwards25519.

P = libsodium_crypto_core_ed25519:random(),
Q = libsodium_crypto_core_ed25519:random(),
1 = libsodium_crypto_core_ed25519:is_valid_point(P),
R = libsodium_crypto_core_ed25519:add(P, Q),
P = libsodium_crypto_core_ed25519:sub(R, Q),
S = libsodium_crypto_core_ed25519:scalar_random(),
One = libsodium_crypto_core_ed25519:scalar_mul(S, libsodium_crypto_core_ed25519:scalar_invert(S)),
One = <<1, 0:248>>.

Summary

Types

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

h()

A crypto_core_ed25519_HASHBYTES (64-byte) binary.

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

A crypto_core_ed25519_NONREDUCEDSCALARBYTES (64-byte) binary.

p()

A crypto_core_ed25519_BYTES (32-byte) binary.

q()

A crypto_core_ed25519_BYTES (32-byte) binary.

r()

A crypto_core_ed25519_BYTES (32-byte) binary.

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

s()

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

x()

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

y()

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

z()

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

Functions

Return the point P + Q, or -1 when either input is not a point on the curve.

Return crypto_core_ed25519_bytes(), in bytes.

from_hash(H) deprecated

Map the hashbytes/0 byte string H to a point. Deprecated and undocumented by libsodium; use from_uniform/1 instead.

Map the uniformbytes/0 byte string R (such as the output of a hash function) to a point on the main subgroup using Elligator 2.

Return crypto_core_ed25519_hashbytes(), in bytes.

Return 1 when P is the canonical encoding of a point on the main subgroup (excluding small-order points), otherwise 0.

Return crypto_core_ed25519_nonreducedscalarbytes(), in bytes.

Return a random point on the main subgroup.

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_ed25519_scalarbytes(), in bytes.

Return the point P - Q, or -1 when either input is not a point on the curve.

Return crypto_core_ed25519_uniformbytes(), in bytes.

Types

comp()

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

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

h()

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

A crypto_core_ed25519_HASHBYTES (64-byte) binary.

neg()

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

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

nonreduced_scalar()

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

A crypto_core_ed25519_NONREDUCEDSCALARBYTES (64-byte) binary.

p()

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

A crypto_core_ed25519_BYTES (32-byte) binary.

q()

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

A crypto_core_ed25519_BYTES (32-byte) binary.

r()

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

A crypto_core_ed25519_BYTES (32-byte) binary.

recip()

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

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

s()

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

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

x()

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

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

y()

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

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

z()

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

A crypto_core_ed25519_SCALARBYTES (32-byte) binary.

Functions

add(P, Q)

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

Return the point P + Q, or -1 when either input is not a point on the curve.

bytes()

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

Return crypto_core_ed25519_bytes(), in bytes.

from_hash(H)

This function is deprecated. Undocumented and deprecated by libsodium; use from_uniform/1..
-spec from_hash(H) -> Result when H :: h(), Result :: p() | -1.

Map the hashbytes/0 byte string H to a point. Deprecated and undocumented by libsodium; use from_uniform/1 instead.

from_uniform(R)

-spec from_uniform(R) -> Result when R :: r(), Result :: p() | -1.

Map the uniformbytes/0 byte string R (such as the output of a hash function) to a point on the main subgroup using Elligator 2.

hashbytes()

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

Return crypto_core_ed25519_hashbytes(), in bytes.

is_valid_point(P)

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

Return 1 when P is the canonical encoding of a point on the main subgroup (excluding small-order points), otherwise 0.

nonreducedscalarbytes()

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

Return crypto_core_ed25519_nonreducedscalarbytes(), in bytes.

random()

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

Return a random point on the main subgroup.

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_ed25519_scalarbytes(), in bytes.

sub(P, Q)

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

Return the point P - Q, or -1 when either input is not a point on the curve.

uniformbytes()

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

Return crypto_core_ed25519_uniformbytes(), in bytes.