libsodium_crypto_scalarmult (libsodium v3.0.1)

View Source

Elliptic curve point multiplication with the default primitive, X25519.

base/1 computes a bytes/0 byte public key from a scalarbytes/0 byte secret key, and crypto_scalarmult/2 computes a shared secret from a secret key and another party's public key.

Do not use the shared secret directly as a key: hash it together with both public keys, or use libsodium_crypto_kx instead.

AliceSK = libsodium_randombytes:buf(libsodium_crypto_scalarmult:scalarbytes()),
BobSK = libsodium_randombytes:buf(libsodium_crypto_scalarmult:scalarbytes()),
AlicePK = libsodium_crypto_scalarmult:base(AliceSK),
BobPK = libsodium_crypto_scalarmult:base(BobSK),
Shared = libsodium_crypto_scalarmult:crypto_scalarmult(AliceSK, BobPK),
Shared = libsodium_crypto_scalarmult:crypto_scalarmult(BobSK, AlicePK).

Summary

Types

n()

A crypto_scalarmult_SCALARBYTES (32-byte) binary.

p()

A crypto_scalarmult_BYTES (32-byte) binary.

q()

A crypto_scalarmult_BYTES (32-byte) binary.

Functions

Compute the public key for the secret scalar N by multiplying the base point. Returns -1 on failure.

Return crypto_scalarmult_bytes(), in bytes.

Multiply the point P by the secret scalar N, producing a shared secret. Returns -1 when P is a low-order point.

Return crypto_scalarmult_primitive() as an atom.

Return crypto_scalarmult_scalarbytes(), in bytes.

Types

n()

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

A crypto_scalarmult_SCALARBYTES (32-byte) binary.

p()

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

A crypto_scalarmult_BYTES (32-byte) binary.

q()

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

A crypto_scalarmult_BYTES (32-byte) binary.

Functions

base(N)

-spec base(N) -> Result when N :: n(), Result :: q() | -1.

Compute the public key for the secret scalar N by multiplying the base point. Returns -1 on failure.

bytes()

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

Return crypto_scalarmult_bytes(), in bytes.

crypto_scalarmult(N, P)

-spec crypto_scalarmult(N, P) -> Result when N :: n(), P :: p(), Result :: q() | -1.

Multiply the point P by the secret scalar N, producing a shared secret. Returns -1 when P is a low-order point.

primitive()

-spec primitive() -> Result when Result :: curve25519.

Return crypto_scalarmult_primitive() as an atom.

scalarbytes()

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

Return crypto_scalarmult_scalarbytes(), in bytes.