Files
voprf-vx/src/group/mod.rs
T

130 lines
4.7 KiB
Rust
Raw Normal View History

2023-05-22 23:04:39 -07:00
// Copyright (c) Meta Platforms, Inc. and affiliates.
2021-09-09 01:56:54 -07:00
//
2023-05-22 23:04:39 -07:00
// This source code is dual-licensed under either the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree or the Apache
2021-09-27 18:53:06 -07:00
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
2023-05-22 23:04:39 -07:00
// of this source tree. You may select, at your option, one of the above-listed
// licenses.
2021-09-09 01:56:54 -07:00
//! Defines the Group trait to specify the underlying prime order group
2021-09-09 01:56:54 -07:00
2022-01-21 22:52:09 +01:00
mod elliptic_curve;
2021-12-23 07:50:48 +01:00
#[cfg(feature = "ristretto255")]
mod ristretto;
2021-09-09 01:56:54 -07:00
use core::ops::{Add, Mul, Sub};
2021-12-23 01:17:03 +01:00
2021-12-23 21:58:00 +01:00
use digest::core_api::BlockSizeUser;
2023-02-08 09:18:04 +01:00
use digest::{FixedOutput, HashMarker};
2025-04-15 22:30:56 +02:00
use generic_array::typenum::{IsLess, IsLessOrEqual, Sum, U256};
2021-12-23 01:17:03 +01:00
use generic_array::{ArrayLength, GenericArray};
2021-10-14 20:09:00 +02:00
use rand_core::{CryptoRng, RngCore};
2022-01-18 12:34:28 +01:00
#[cfg(feature = "ristretto255")]
pub use ristretto::Ristretto255;
2022-01-28 01:38:17 +01:00
use subtle::{Choice, ConstantTimeEq};
2021-09-09 01:56:54 -07:00
use zeroize::Zeroize;
2022-04-01 21:18:32 +02:00
use crate::{InternalError, Result};
2021-12-23 01:17:03 +01:00
2021-09-09 01:56:54 -07:00
/// A prime-order subgroup of a base field (EC, prime-order field ...). This
2024-01-11 11:58:36 -08:00
/// subgroup is noted additively — as in the RFC — in this trait.
2025-04-15 22:30:56 +02:00
pub trait Group
where
// `VoprfClientLen`, `PoprfClientLen`, `VoprfServerLen`, `PoprfServerLen`
Self::ScalarLen: Add<Self::ElemLen>,
2025-11-04 14:40:16 -05:00
Sum<Self::ScalarLen, Self::ElemLen>: ArrayLength,
2025-04-15 22:30:56 +02:00
// `ProofLen`
Self::ScalarLen: Add<Self::ScalarLen>,
2025-11-04 14:40:16 -05:00
Sum<Self::ScalarLen, Self::ScalarLen>: ArrayLength,
2025-04-15 22:30:56 +02:00
{
2022-01-18 12:34:28 +01:00
/// The type of group elements
2022-02-13 04:00:11 -08:00
type Elem: ConstantTimeEq
+ Copy
2022-01-18 12:34:28 +01:00
+ Zeroize
2022-01-21 22:52:09 +01:00
+ for<'a> Add<&'a Self::Elem, Output = Self::Elem>
+ for<'a> Mul<&'a Self::Scalar, Output = Self::Elem>;
2021-09-09 01:56:54 -07:00
2022-01-18 12:34:28 +01:00
/// The byte length necessary to represent group elements
2025-11-04 14:40:16 -05:00
type ElemLen: ArrayLength + 'static;
2021-09-09 01:56:54 -07:00
/// The type of base field scalars
2022-01-21 22:52:09 +01:00
type Scalar: ConstantTimeEq
+ Copy
2022-01-21 22:52:09 +01:00
+ Zeroize
+ for<'a> Add<&'a Self::Scalar, Output = Self::Scalar>
2022-01-21 22:52:09 +01:00
+ for<'a> Mul<&'a Self::Scalar, Output = Self::Scalar>
+ for<'a> Sub<&'a Self::Scalar, Output = Self::Scalar>;
2022-01-18 12:34:28 +01:00
2021-09-09 01:56:54 -07:00
/// The byte length necessary to represent scalars
2025-11-04 14:40:16 -05:00
type ScalarLen: ArrayLength + 'static;
2021-09-27 18:29:08 -07:00
2022-01-25 05:55:02 +01:00
/// Transforms a password and domain separation tag (DST) into a curve point
///
/// # Errors
/// [`Error::Input`](crate::Error::Input) if the `input` is empty or longer
/// then [`u16::MAX`].
2023-02-08 09:18:04 +01:00
fn hash_to_curve<H>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Elem, InternalError>
2022-01-21 22:52:09 +01:00
where
2023-02-08 09:18:04 +01:00
H: BlockSizeUser + Default + FixedOutput + HashMarker,
2022-04-01 21:18:32 +02:00
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize>;
2021-09-27 18:29:08 -07:00
2022-01-18 12:34:28 +01:00
/// Hashes a slice of pseudo-random bytes to a scalar
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Input`](crate::Error::Input) if the `input` is empty or longer
/// then [`u16::MAX`].
2023-02-08 09:18:04 +01:00
fn hash_to_scalar<H>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Scalar, InternalError>
2022-01-21 22:52:09 +01:00
where
2023-02-08 09:18:04 +01:00
H: BlockSizeUser + Default + FixedOutput + HashMarker,
2022-04-01 21:18:32 +02:00
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize>;
2021-09-27 18:29:08 -07:00
2022-01-18 12:34:28 +01:00
/// Get the base point for the group
fn base_elem() -> Self::Elem;
2021-09-09 01:56:54 -07:00
2022-01-18 12:34:28 +01:00
/// Returns the identity group element
fn identity_elem() -> Self::Elem;
2021-09-27 18:29:08 -07:00
2022-02-13 04:00:11 -08:00
/// Returns `true` if the element is equal to the identity element
fn is_identity_elem(elem: Self::Elem) -> Choice {
Self::identity_elem().ct_eq(&elem)
}
2022-01-18 12:34:28 +01:00
/// Serializes the `self` group element
fn serialize_elem(elem: Self::Elem) -> GenericArray<u8, Self::ElemLen>;
2021-09-27 18:29:08 -07:00
2021-12-23 01:17:03 +01:00
/// Return an element from its fixed-length bytes representation. If the
/// element is the identity element, return an error.
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`](crate::Error::Deserialization) if the element
/// is not a valid point on the group or the identity element.
2022-01-28 01:38:17 +01:00
fn deserialize_elem(element_bits: &[u8]) -> Result<Self::Elem>;
2021-09-09 01:56:54 -07:00
2022-01-18 12:34:28 +01:00
/// picks a scalar at random
fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar;
2022-01-18 12:34:28 +01:00
/// The multiplicative inverse of this scalar
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar;
2021-09-09 01:56:54 -07:00
2022-01-28 01:38:17 +01:00
/// Returns `true` if the scalar is zero.
fn is_zero_scalar(scalar: Self::Scalar) -> Choice;
2021-09-27 18:29:08 -07:00
/// Returns the scalar representing zero
2022-01-18 12:34:28 +01:00
#[cfg(test)]
fn zero_scalar() -> Self::Scalar;
/// Serializes a scalar to bytes
fn serialize_scalar(scalar: Self::Scalar) -> GenericArray<u8, Self::ScalarLen>;
2021-09-27 18:29:08 -07:00
2022-01-18 12:34:28 +01:00
/// Return a scalar from its fixed-length bytes representation. If the
/// scalar is zero or invalid, then return an error.
2022-01-25 05:55:02 +01:00
///
/// # Errors
/// [`Error::Deserialization`](crate::Error::Deserialization) if the scalar
/// is not a valid point on the group or zero.
2022-01-28 01:38:17 +01:00
fn deserialize_scalar(scalar_bits: &[u8]) -> Result<Self::Scalar>;
2021-09-09 01:56:54 -07:00
}
2021-09-27 18:29:08 -07:00
#[cfg(test)]
mod tests;