Trait elastic_elgamal::group::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 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 Associated Constants§
sourceconst SCALAR_SIZE: usize
const SCALAR_SIZE: usize
Byte size of a serialized Self::Scalar
.
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.