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

148 lines
5.6 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
use digest::block_api::BlockSizeUser;
2023-02-08 09:18:04 +01:00
use digest::{FixedOutput, HashMarker};
use hybrid_array::typenum::{IsGreaterOrEqual, IsLess, IsLessOrEqual, Prod, Sum, True, U2, U256};
use hybrid_array::{Array, ArraySize};
use rand_core::{TryCryptoRng, TryRng};
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>,
Sum<Self::ScalarLen, Self::ElemLen>: ArraySize,
2025-04-15 22:30:56 +02:00
// `ProofLen`
Self::ScalarLen: Add<Self::ScalarLen>,
Sum<Self::ScalarLen, Self::ScalarLen>: ArraySize,
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
type ElemLen: ArraySize + '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
type ScalarLen: ArraySize + 'static;
/// Security parameter `k` in bytes (i.e. `k / 8`), as defined in
/// [RFC 9380 §8](https://www.rfc-editor.org/rfc/rfc9380#section-8).
///
/// Used to enforce `H::OutputSize >= 2 * SecurityLevel` in
/// `hash_to_curve` and `hash_to_scalar`, which corresponds to the
/// `expand_message` requirement `len_in_bytes = 2 * k / 8`.
type SecurityLevel: ArraySize;
/// The OKM length for hash_to_scalar (>= ScalarLen, used by hash_to_field).
type OkmLen: ArraySize + hybrid_array::typenum::NonZero;
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,
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize, Output = True>,
Self::SecurityLevel: Mul<U2>,
H::OutputSize: IsGreaterOrEqual<Prod<Self::SecurityLevel, U2>, Output = True>;
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,
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize, Output = True>,
Self::SecurityLevel: Mul<U2>,
H::OutputSize: IsGreaterOrEqual<Prod<Self::SecurityLevel, U2>, Output = True>;
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) -> Array<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
2026-01-25 23:37:55 +01:00
/// Picks a scalar at random.
///
/// # Errors
/// [`Error::Rng`](crate::Error::Rng) if the random number generator fails.
fn random_scalar<R: TryRng + TryCryptoRng>(rng: &mut R) -> Result<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) -> Array<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;