pub trait ScalarOps {
type Scalar: Copy + Default + From<u64> + From<Self::Scalar> + Neg<Output = Self::Scalar> + Add<Output = Self::Scalar> + for<'a> Add<&'a Self::Scalar, Output = Self::Scalar> + Sub<Output = Self::Scalar> + Mul<Output = Self::Scalar> + for<'a> Mul<&'a Self::Scalar, Output = Self::Scalar> + PartialEq + Zeroize + Debug;
const SCALAR_SIZE: usize;
// Required methods
fn generate_scalar<R: CryptoRng + RngCore>(rng: &mut R) -> Self::Scalar;
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar;
fn serialize_scalar(scalar: &Self::Scalar, buffer: &mut [u8]);
fn deserialize_scalar(buffer: &[u8]) -> Option<Self::Scalar>;
// Provided methods
fn scalar_from_random_bytes(source: RandomBytesProvider<'_>) -> Self::Scalar { ... }
fn invert_scalars(scalars: &mut [Self::Scalar]) { ... }
}
Expand description
Helper trait for Group
that describes operations on group scalars.
Required Associated Constants§
Sourceconst SCALAR_SIZE: usize
const SCALAR_SIZE: usize
Byte size of a serialized Self::Scalar
.
Required Associated Types§
Sourcetype Scalar: Copy + Default + From<u64> + From<Self::Scalar> + Neg<Output = Self::Scalar> + Add<Output = Self::Scalar> + for<'a> Add<&'a Self::Scalar, Output = Self::Scalar> + Sub<Output = Self::Scalar> + Mul<Output = Self::Scalar> + for<'a> Mul<&'a Self::Scalar, Output = Self::Scalar> + PartialEq + Zeroize + Debug
type Scalar: Copy + Default + From<u64> + From<Self::Scalar> + Neg<Output = Self::Scalar> + Add<Output = Self::Scalar> + for<'a> Add<&'a Self::Scalar, Output = Self::Scalar> + Sub<Output = Self::Scalar> + Mul<Output = Self::Scalar> + for<'a> Mul<&'a Self::Scalar, Output = Self::Scalar> + PartialEq + Zeroize + Debug
Scalar type. As per Group
contract, scalars must form a prime field.
Arithmetic operations on scalars requested here must be constant-time.
Required Methods§
Sourcefn generate_scalar<R: CryptoRng + RngCore>(rng: &mut R) -> Self::Scalar
fn generate_scalar<R: CryptoRng + RngCore>(rng: &mut R) -> Self::Scalar
Generates a random scalar based on the provided CSPRNG. This operation must be constant-time.
Sourcefn invert_scalar(scalar: Self::Scalar) -> Self::Scalar
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar
Inverts the scalar
, which is guaranteed to be non-zero. This operation does not
need to be constant-time.
Sourcefn serialize_scalar(scalar: &Self::Scalar, buffer: &mut [u8])
fn serialize_scalar(scalar: &Self::Scalar, buffer: &mut [u8])
Serializes the scalar into the provided buffer
, which is guaranteed to have length
Self::SCALAR_SIZE
.
Sourcefn deserialize_scalar(buffer: &[u8]) -> Option<Self::Scalar>
fn deserialize_scalar(buffer: &[u8]) -> Option<Self::Scalar>
Deserializes the scalar from buffer
, which is guaranteed to have length
Self::SCALAR_SIZE
. This method returns None
if the buffer
does not correspond to a representation of a valid scalar.
Provided Methods§
Sourcefn scalar_from_random_bytes(source: RandomBytesProvider<'_>) -> Self::Scalar
fn scalar_from_random_bytes(source: RandomBytesProvider<'_>) -> Self::Scalar
Generates a scalar from a source
of random bytes. This operation must be constant-time.
The source
is guaranteed to return any necessary number of bytes.
§Default implementation
- Create a ChaCha RNG using 32 bytes read from
source
as the seed. - Call
Self::generate_scalar()
with the created RNG.
Sourcefn invert_scalars(scalars: &mut [Self::Scalar])
fn invert_scalars(scalars: &mut [Self::Scalar])
Inverts scalars in a batch. This operation does not need to be constant-time.
§Default implementation
Inverts every scalar successively.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.