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 Types§

source

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 Associated Constants§

source

const SCALAR_SIZE: usize

Byte size of a serialized Self::Scalar.

Required Methods§

source

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.

source

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.

source

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.

source

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§

source

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
  1. Create a ChaCha RNG using 32 bytes read from source as the seed.
  2. Call Self::generate_scalar() with the created RNG.
source

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.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl ScalarOps for Curve25519Subgroup

§

type Scalar = Scalar

source§

const SCALAR_SIZE: usize = 32usize

source§

impl ScalarOps for Ristretto

§

type Scalar = Scalar

source§

const SCALAR_SIZE: usize = 32usize

source§

impl<C> ScalarOps for Generic<C>
where C: CurveArithmetic, Scalar<C>: Zeroize,

§

type Scalar = <C as CurveArithmetic>::Scalar

source§

const SCALAR_SIZE: usize = <FieldBytesSize<C> as Unsigned>::USIZE