elastic_elgamal::group

Trait ScalarOps

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

Source

const SCALAR_SIZE: usize

Byte size of a serialized Self::Scalar.

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 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.

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.

Implementors§

Source§

impl ScalarOps for Curve25519Subgroup

Source§

const SCALAR_SIZE: usize = 32usize

Source§

type Scalar = Scalar

Source§

impl ScalarOps for Ristretto

Source§

const SCALAR_SIZE: usize = 32usize

Source§

type Scalar = Scalar

Source§

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

Source§

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

Source§

type Scalar = <C as CurveArithmetic>::Scalar