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

108 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
2021-12-23 07:50:48 +01:00
#[cfg(any(feature = "ristretto255", feature = "p256",))]
2021-09-09 01:56:54 -07:00
mod expand;
#[cfg(feature = "p256")]
2021-10-14 20:09:00 +02:00
mod p256;
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;
use digest::{Digest, FixedOutputReset};
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;
use crate::Result;
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 {
2021-09-09 01:56:54 -07:00
/// The ciphersuite identifier as dictated by
/// <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-05.txt>
2022-01-18 12:34:28 +01:00
const SUITE_ID: u16;
2021-09-09 01:56:54 -07:00
2022-01-18 12:34:28 +01:00
/// The type of group elements
type Elem: Copy
+ Sized
+ ConstantTimeEq
+ Zeroize
+ for<'a> Mul<&'a Self::Scalar, Output = Self::Elem>
+ for<'a> Add<&'a Self::Elem, 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
type Scalar: Zeroize
+ Copy
2021-10-14 20:09:00 +02:00
+ ConstantTimeEq
+ for<'a> Add<&'a Self::Scalar, Output = Self::Scalar>
+ for<'a> Sub<&'a Self::Scalar, Output = Self::Scalar>
+ for<'a> Mul<&'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
fn hash_to_curve<H: BlockSizeUser + Digest + FixedOutputReset>(
msg: &[&[u8]],
mode: Mode,
) -> Result<Self::Elem>;
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
fn hash_to_scalar<H: BlockSizeUser + Digest + FixedOutputReset>(
input: &[&[u8]],
mode: Mode,
) -> Result<Self::Scalar>;
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;