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

100 lines
3.6 KiB
Rust
Raw Normal View History

2021-09-09 01:56:54 -07:00
// Copyright (c) Facebook, Inc. and its affiliates.
//
2021-09-27 18:53:06 -07:00
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree and the Apache
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
// of this source tree.
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;
2022-01-21 22:52:09 +01:00
use digest::OutputSizeUser;
use generic_array::typenum::{IsLess, IsLessOrEqual, 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;
2021-10-14 20:09:00 +02:00
use subtle::ConstantTimeEq;
2021-09-09 01:56:54 -07:00
use zeroize::Zeroize;
2022-01-18 12:34:28 +01:00
use crate::voprf::Mode;
2022-01-21 22:52:09 +01:00
use crate::{CipherSuite, Result};
2022-01-18 12:34:28 +01:00
pub(crate) const STR_HASH_TO_SCALAR: [u8; 13] = *b"HashToScalar-";
pub(crate) const STR_HASH_TO_GROUP: [u8; 12] = *b"HashToGroup-";
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
/// subgroup is noted additively — as in the draft RFC — in this trait.
2022-01-18 12:34:28 +01:00
pub trait Group {
/// The type of group elements
type Elem: Copy
+ 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: ArrayLength<u8> + '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: ArrayLength<u8> + 'static;
2021-09-27 18:29:08 -07:00
2022-01-18 12:34:28 +01:00
/// transforms a password and domain separation tag (DST) into a curve point
2022-01-21 22:52:09 +01:00
fn hash_to_curve<CS: CipherSuite>(msg: &[&[u8]], mode: Mode) -> Result<Self::Elem>
where
<CS::Hash as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<CS::Hash as BlockSizeUser>::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-21 22:52:09 +01:00
fn hash_to_scalar<CS: CipherSuite>(input: &[&[u8]], mode: Mode) -> Result<Self::Scalar>
where
<CS::Hash as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<CS::Hash as BlockSizeUser>::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-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-18 12:34:28 +01:00
fn deserialize_elem(element_bits: &GenericArray<u8, Self::ElemLen>) -> 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
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.
fn deserialize_scalar(scalar_bits: &GenericArray<u8, Self::ScalarLen>) -> Result<Self::Scalar>;
2021-09-09 01:56:54 -07:00
}
2021-09-27 18:29:08 -07:00
#[cfg(test)]
mod tests;