Class CryptoRistretto
- Namespace
- LibSodium
- Assembly
- LibSodium.Net.dll
Constants and cryptographic operations for the Ristretto255 group.
public static class CryptoRistretto
- Inheritance
-
CryptoRistretto
- Inherited Members
Remarks
🧂 Based on libsodium’s Ristretto255 API: https://doc.libsodium.org/advanced/ristretto255
Fields
HashLen
Length of a hash input required for point derivation in bytes (64).
public const int HashLen = 64
Field Value
NonReducedScalarLen
Length of a non-reduced scalar input in bytes (64).
public const int NonReducedScalarLen = 64
Field Value
PointLen
Length of a Ristretto255 encoded point in bytes (32).
public const int PointLen = 32
Field Value
ScalarLen
Length of a Ristretto255 scalar in bytes (32).
public const int ScalarLen = 32
Field Value
Methods
AddPoints(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Adds two Ristretto255 points: resultPoint = point1 + point2.
public static void AddPoints(ReadOnlySpan<byte> point1, ReadOnlySpan<byte> point2, Span<byte> resultPoint)
Parameters
point1ReadOnlySpan<byte>The first input point (32 bytes).
point2ReadOnlySpan<byte>The second input point (32 bytes).
resultPointSpan<byte>The buffer to receive the result (32 bytes).
Exceptions
- ArgumentException
Thrown if any buffer is not exactly 32 bytes.
- LibSodiumException
Thrown if the operation fails internally.
AddScalars(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Adds two scalars modulo the Ristretto255 group order: resultScalar = scalar1 + scalar2.
public static void AddScalars(ReadOnlySpan<byte> scalar1, ReadOnlySpan<byte> scalar2, Span<byte> resultScalar)
Parameters
scalar1ReadOnlySpan<byte>The first scalar operand (32 bytes).
scalar2ReadOnlySpan<byte>The second scalar operand (32 bytes).
resultScalarSpan<byte>The buffer to receive the sum (32 bytes).
Exceptions
- ArgumentException
Thrown if any of the buffers are not exactly 32 bytes.
ComplementScalar(ReadOnlySpan<byte>, Span<byte>)
Computes the complement of a scalar modulo the Ristretto255 group order: resultScalar = l - inputScalar.
public static void ComplementScalar(ReadOnlySpan<byte> inputScalar, Span<byte> resultScalar)
Parameters
inputScalarReadOnlySpan<byte>The scalar to complement (32 bytes).
resultScalarSpan<byte>The buffer to receive the complemented scalar (32 bytes).
Exceptions
- ArgumentException
Thrown if
inputScalarorresultScalaris not exactly 32 bytes.
DerivePointFromHash(SecureMemory<byte>, SecureMemory<byte>)
Derives a Ristretto255 point from a 64-byte uniformly random hash input.
public static void DerivePointFromHash(SecureMemory<byte> hash, SecureMemory<byte> point)
Parameters
hashSecureMemory<byte>A 64-byte uniformly random input.
pointSecureMemory<byte>The buffer to receive the resulting point. Must be exactly 32 bytes.
Exceptions
- ArgumentException
Thrown if
pointis not 32 bytes orhashis not 64 bytes.- LibSodiumException
Thrown if the operation fails internally.
DerivePointFromHash(ReadOnlySpan<byte>, Span<byte>)
Derives a Ristretto255 point from a 64-byte uniformly random hash input.
public static void DerivePointFromHash(ReadOnlySpan<byte> hash, Span<byte> point)
Parameters
hashReadOnlySpan<byte>A 64-byte uniformly random input.
pointSpan<byte>The buffer to receive the resulting point. Must be exactly 32 bytes.
Exceptions
- ArgumentException
Thrown if
pointis not 32 bytes orhashis not 64 bytes.- LibSodiumException
Thrown if the operation fails internally.
GenerateRandomPoint(Span<byte>)
Generates a uniformly random valid Ristretto255 point.
public static void GenerateRandomPoint(Span<byte> point)
Parameters
Exceptions
- ArgumentException
Thrown if
pointis not exactly 32 bytes.- LibSodiumException
Thrown if the operation fails internally.
GenerateRandomScalar(SecureMemory<byte>)
Generates a uniformly random scalar modulo the Ristretto255 group order (32 bytes). This is typically used to generate a random private key.
public static void GenerateRandomScalar(SecureMemory<byte> resultScalar)
Parameters
resultScalarSecureMemory<byte>The buffer to receive the scalar (32 bytes, secret).
Exceptions
- ArgumentException
Thrown if
resultScalaris not 32 bytes.- LibSodiumException
Thrown if the operation fails internally.
GenerateRandomScalar(Span<byte>)
Generates a uniformly random scalar modulo the Ristretto255 group order (32 bytes). This is typically used to generate a random private key.
public static void GenerateRandomScalar(Span<byte> resultScalar)
Parameters
Exceptions
- ArgumentException
Thrown if
resultScalaris not exactly 32 bytes.- LibSodiumException
Thrown if the operation fails internally.
InvertScalar(ReadOnlySpan<byte>, Span<byte>)
Computes the multiplicative inverse of a scalar modulo the Ristretto255 group order: resultScalar = inverse(inputScalar).
public static void InvertScalar(ReadOnlySpan<byte> inputScalar, Span<byte> resultScalar)
Parameters
inputScalarReadOnlySpan<byte>The scalar to invert (32 bytes).
resultScalarSpan<byte>The buffer to receive the inverted scalar (32 bytes).
Exceptions
- ArgumentException
Thrown if
inputScalarorresultScalaris not exactly 32 bytes.- LibSodiumException
Thrown if the operation fails internally or the scalar is not invertible.
IsValidPoint(ReadOnlySpan<byte>)
Checks whether the given 32-byte buffer is a valid Ristretto255 encoded point.
public static bool IsValidPoint(ReadOnlySpan<byte> point)
Parameters
pointReadOnlySpan<byte>The encoded point to validate. Must be exactly 32 bytes.
Returns
- bool
trueif the point is valid; otherwise,false.
Exceptions
- ArgumentException
Thrown if
pointis not exactly 32 bytes.
MultiplyScalars(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Multiplies two scalars modulo the Ristretto255 group order: resultScalar = scalar1 Ă— scalar2.
public static void MultiplyScalars(ReadOnlySpan<byte> scalar1, ReadOnlySpan<byte> scalar2, Span<byte> resultScalar)
Parameters
scalar1ReadOnlySpan<byte>The first scalar operand (32 bytes).
scalar2ReadOnlySpan<byte>The second scalar operand (32 bytes).
resultScalarSpan<byte>The buffer to receive the product (32 bytes).
Exceptions
- ArgumentException
Thrown if any of the buffers are not exactly 32 bytes.
NegateScalar(SecureMemory<byte>, SecureMemory<byte>)
Computes the additive inverse of a scalar modulo the Ristretto255 group order: resultScalar = -inputScalar.
public static void NegateScalar(SecureMemory<byte> inputScalar, SecureMemory<byte> resultScalar)
Parameters
inputScalarSecureMemory<byte>The scalar to negate (32 bytes).
resultScalarSecureMemory<byte>The buffer to receive the negated scalar (32 bytes).
Exceptions
- ArgumentException
Thrown if
inputScalarorresultScalaris not exactly 32 bytes.
NegateScalar(ReadOnlySpan<byte>, Span<byte>)
Computes the additive inverse of a scalar modulo the Ristretto255 group order: resultScalar = -inputScalar.
public static void NegateScalar(ReadOnlySpan<byte> inputScalar, Span<byte> resultScalar)
Parameters
inputScalarReadOnlySpan<byte>The scalar to negate (32 bytes).
resultScalarSpan<byte>The buffer to receive the negated scalar (32 bytes).
Exceptions
- ArgumentException
Thrown if
inputScalarorresultScalaris not exactly 32 bytes.
ReduceScalar(SecureMemory<byte>, SecureMemory<byte>)
Reduces a 64-byte non-reduced scalar to a canonical 32-byte scalar modulo the Ristretto255 group order. This is typically used after hashing or key derivation to produce a valid private key.
public static void ReduceScalar(SecureMemory<byte> nonReducedScalar, SecureMemory<byte> resultScalar)
Parameters
nonReducedScalarSecureMemory<byte>The 64-byte input scalar to reduce (typically secret).
resultScalarSecureMemory<byte>The buffer to receive the reduced scalar (also secret, 32 bytes).
Exceptions
- ArgumentException
Thrown if the input or output buffers have incorrect length.
- LibSodiumException
Thrown if the operation fails internally.
ReduceScalar(ReadOnlySpan<byte>, Span<byte>)
Reduces a 64-byte non-reduced scalar to a canonical 32-byte scalar modulo the Ristretto255 group order. This is typically used after hashing to produce a valid private key.
public static void ReduceScalar(ReadOnlySpan<byte> nonReducedScalar, Span<byte> resultScalar)
Parameters
nonReducedScalarReadOnlySpan<byte>The 64-byte input scalar to reduce.
resultScalarSpan<byte>The buffer to receive the reduced scalar (32 bytes).
Exceptions
- ArgumentException
Thrown if
nonReducedScalaris not 64 bytes orresultScalaris not 32 bytes.- LibSodiumException
Thrown if the operation fails internally.
ScalarMultiply(SecureMemory<byte>, SecureMemory<byte>, SecureMemory<byte>)
Computes the scalar multiplication of a Ristretto255 point by a scalar: resultPoint = scalar * point.
public static void ScalarMultiply(SecureMemory<byte> scalar, SecureMemory<byte> point, SecureMemory<byte> resultPoint)
Parameters
scalarSecureMemory<byte>The scalar to multiply (32 bytes).
pointSecureMemory<byte>The Ristretto255 point to multiply (32 bytes).
resultPointSecureMemory<byte>The buffer to receive the resulting point (32 bytes).
Exceptions
- ArgumentException
Thrown if
scalar,point, orresultPointdo not have the correct length.- LibSodiumException
Thrown if the operation fails internally.
ScalarMultiply(SecureMemory<byte>, ReadOnlySpan<byte>, SecureMemory<byte>)
Computes the scalar multiplication of a Ristretto255 point by a scalar: resultPoint = scalar * point.
public static void ScalarMultiply(SecureMemory<byte> scalar, ReadOnlySpan<byte> point, SecureMemory<byte> resultPoint)
Parameters
scalarSecureMemory<byte>The scalar to multiply (32 bytes).
pointReadOnlySpan<byte>The Ristretto255 point to multiply (32 bytes).
resultPointSecureMemory<byte>The buffer to receive the resulting point (32 bytes).
Exceptions
- ArgumentException
Thrown if
scalar,point, orresultPointdo not have the correct length.- LibSodiumException
Thrown if the operation fails internally.
ScalarMultiply(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Computes the scalar multiplication of a Ristretto255 point by a scalar: resultPoint = scalar * point.
public static void ScalarMultiply(ReadOnlySpan<byte> scalar, ReadOnlySpan<byte> point, Span<byte> resultPoint)
Parameters
scalarReadOnlySpan<byte>The scalar to multiply (32 bytes).
pointReadOnlySpan<byte>The Ristretto255 point to multiply (32 bytes).
resultPointSpan<byte>The buffer to receive the resulting point (32 bytes).
Exceptions
- ArgumentException
Thrown if
scalar,point, orresultPointdo not have the correct length.- LibSodiumException
Thrown if the operation fails internally.
ScalarMultiplyBase(SecureMemory<byte>, Span<byte>)
Computes the scalar multiplication of the Ristretto255 base point by a scalar: resultPoint = scalar * base_point.
Typically used to derive a public key from a private scalar.
public static void ScalarMultiplyBase(SecureMemory<byte> scalar, Span<byte> resultPoint)
Parameters
scalarSecureMemory<byte>The private scalar (32 bytes).
resultPointSpan<byte>The buffer to receive the resulting point (32 bytes).
Exceptions
- ArgumentException
Thrown if
scalarorresultPointdo not have the correct length.- LibSodiumException
Thrown if the operation fails internally.
ScalarMultiplyBase(ReadOnlySpan<byte>, Span<byte>)
Computes the scalar multiplication of the Ristretto255 base point by a scalar: resultPoint = scalar * base_point.
Typically used to derive a public key from a private scalar.
public static void ScalarMultiplyBase(ReadOnlySpan<byte> scalar, Span<byte> resultPoint)
Parameters
scalarReadOnlySpan<byte>The scalar to multiply (32 bytes). This is typically a private key.
resultPointSpan<byte>The buffer to receive the resulting point (32 bytes). This is typically the corresponding public key.
Exceptions
- ArgumentException
Thrown if
scalarorresultPointdo not have the correct length.- LibSodiumException
Thrown if the operation fails internally.
SubtractPoints(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Subtracts one Ristretto255 point from another: resultPoint = point1 - point2.
public static void SubtractPoints(ReadOnlySpan<byte> point1, ReadOnlySpan<byte> point2, Span<byte> resultPoint)
Parameters
point1ReadOnlySpan<byte>The point to subtract from (32 bytes).
point2ReadOnlySpan<byte>The point to subtract (32 bytes).
resultPointSpan<byte>The buffer to receive the result (32 bytes).
Exceptions
- ArgumentException
Thrown if any buffer is not exactly 32 bytes.
- LibSodiumException
Thrown if the operation fails internally.
SubtractScalars(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Subtracts one scalar from another modulo the Ristretto255 group order: resultScalar = scalar1 - scalar2.
public static void SubtractScalars(ReadOnlySpan<byte> scalar1, ReadOnlySpan<byte> scalar2, Span<byte> resultScalar)
Parameters
scalar1ReadOnlySpan<byte>The scalar to subtract from (32 bytes).
scalar2ReadOnlySpan<byte>The scalar to subtract (32 bytes).
resultScalarSpan<byte>The buffer to receive the result (32 bytes).
Exceptions
- ArgumentException
Thrown if any of the buffers are not exactly 32 bytes.