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
|
|
|
|
2021-09-29 17:02:40 -07:00
|
|
|
//! Defines the Group trait to specify the underlying prime order group
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2021-10-14 20:09:00 +02:00
|
|
|
#[cfg(any(
|
|
|
|
|
feature = "ristretto255_u64",
|
|
|
|
|
feature = "ristretto255_u32",
|
|
|
|
|
feature = "ristretto255_fiat_u64",
|
|
|
|
|
feature = "ristretto255_fiat_u32",
|
|
|
|
|
feature = "ristretto255_simd",
|
|
|
|
|
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-21 20:17:02 +01:00
|
|
|
cfg_ristretto! {
|
|
|
|
|
mod ristretto;
|
|
|
|
|
}
|
2021-09-09 01:56:54 -07:00
|
|
|
|
|
|
|
|
use crate::errors::InternalError;
|
2021-09-13 16:02:09 -07:00
|
|
|
use core::ops::{Add, Mul, Sub};
|
2021-10-06 00:53:18 +02:00
|
|
|
use digest::{BlockInput, Digest};
|
2021-10-14 20:09:00 +02:00
|
|
|
use generic_array::{typenum::U1, ArrayLength, GenericArray};
|
|
|
|
|
use rand_core::{CryptoRng, RngCore};
|
|
|
|
|
use subtle::ConstantTimeEq;
|
2021-09-09 01:56:54 -07:00
|
|
|
use zeroize::Zeroize;
|
|
|
|
|
|
|
|
|
|
/// 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.
|
2021-09-13 16:02:09 -07:00
|
|
|
pub trait Group:
|
|
|
|
|
Copy
|
|
|
|
|
+ Sized
|
2021-10-14 20:09:00 +02:00
|
|
|
+ ConstantTimeEq
|
2021-09-13 16:02:09 -07:00
|
|
|
+ for<'a> Mul<&'a <Self as Group>::Scalar, Output = Self>
|
|
|
|
|
+ for<'a> Add<&'a Self, Output = Self>
|
|
|
|
|
{
|
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>
|
|
|
|
|
const SUITE_ID: usize;
|
|
|
|
|
|
|
|
|
|
/// transforms a password and domain separation tag (DST) into a curve point
|
2021-10-14 20:09:00 +02:00
|
|
|
fn hash_to_curve<H: BlockInput + Digest, D: ArrayLength<u8> + Add<U1>>(
|
|
|
|
|
msg: &[u8],
|
|
|
|
|
dst: GenericArray<u8, D>,
|
|
|
|
|
) -> Result<Self, InternalError>
|
|
|
|
|
where
|
|
|
|
|
<D as Add<U1>>::Output: ArrayLength<u8>;
|
2021-09-09 01:56:54 -07:00
|
|
|
|
|
|
|
|
/// Hashes a slice of pseudo-random bytes to a scalar
|
2021-10-16 01:56:21 +02:00
|
|
|
fn hash_to_scalar<
|
|
|
|
|
'a,
|
|
|
|
|
H: BlockInput + Digest,
|
|
|
|
|
D: ArrayLength<u8> + Add<U1>,
|
|
|
|
|
I: IntoIterator<Item = &'a [u8]>,
|
|
|
|
|
>(
|
|
|
|
|
input: I,
|
2021-10-14 20:09:00 +02:00
|
|
|
dst: GenericArray<u8, D>,
|
|
|
|
|
) -> Result<Self::Scalar, InternalError>
|
|
|
|
|
where
|
|
|
|
|
<D as Add<U1>>::Output: ArrayLength<u8>;
|
2021-09-09 01:56:54 -07:00
|
|
|
|
|
|
|
|
/// The type of base field scalars
|
2021-09-13 16:02:09 -07:00
|
|
|
type Scalar: Zeroize
|
|
|
|
|
+ Copy
|
2021-10-14 20:09:00 +02:00
|
|
|
+ ConstantTimeEq
|
2021-09-13 16:02:09 -07:00
|
|
|
+ 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>;
|
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
|
|
|
|
|
|
|
|
/// Return a scalar from its fixed-length bytes representation, without
|
|
|
|
|
/// checking if the scalar is zero.
|
|
|
|
|
fn from_scalar_slice_unchecked(
|
2021-09-09 01:56:54 -07:00
|
|
|
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
|
|
|
|
|
) -> Result<Self::Scalar, InternalError>;
|
2021-09-27 18:29:08 -07:00
|
|
|
|
|
|
|
|
/// Return a scalar from its fixed-length bytes representation. If the scalar
|
|
|
|
|
/// is zero, then return an error.
|
2021-10-11 02:51:12 +02:00
|
|
|
fn from_scalar_slice<'a>(
|
|
|
|
|
scalar_bits: impl Into<&'a GenericArray<u8, Self::ScalarLen>>,
|
2021-09-27 18:29:08 -07:00
|
|
|
) -> Result<Self::Scalar, InternalError> {
|
2021-10-11 02:51:12 +02:00
|
|
|
let scalar = Self::from_scalar_slice_unchecked(scalar_bits.into())?;
|
2021-10-14 20:09:00 +02:00
|
|
|
if scalar.ct_eq(&Self::scalar_zero()).into() {
|
2021-09-27 18:29:08 -07:00
|
|
|
return Err(InternalError::ZeroScalarError);
|
|
|
|
|
}
|
|
|
|
|
Ok(scalar)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 01:56:54 -07:00
|
|
|
/// picks a scalar at random
|
|
|
|
|
fn random_nonzero_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar;
|
|
|
|
|
/// Serializes a scalar to bytes
|
|
|
|
|
fn scalar_as_bytes(scalar: Self::Scalar) -> GenericArray<u8, Self::ScalarLen>;
|
|
|
|
|
/// The multiplicative inverse of this scalar
|
|
|
|
|
fn scalar_invert(scalar: &Self::Scalar) -> Self::Scalar;
|
|
|
|
|
|
|
|
|
|
/// The byte length necessary to represent group elements
|
|
|
|
|
type ElemLen: ArrayLength<u8> + 'static;
|
2021-09-27 18:29:08 -07:00
|
|
|
|
|
|
|
|
/// Return an element from its fixed-length bytes representation. This is
|
|
|
|
|
/// the unchecked version, which does not check for deserializing the identity
|
|
|
|
|
/// element
|
|
|
|
|
fn from_element_slice_unchecked(
|
2021-09-09 01:56:54 -07:00
|
|
|
element_bits: &GenericArray<u8, Self::ElemLen>,
|
|
|
|
|
) -> Result<Self, InternalError>;
|
2021-09-27 18:29:08 -07:00
|
|
|
|
|
|
|
|
/// Return an element from its fixed-length bytes representation. If the element
|
|
|
|
|
/// is the identity element, return an error.
|
2021-10-11 02:51:12 +02:00
|
|
|
fn from_element_slice<'a>(
|
|
|
|
|
element_bits: impl Into<&'a GenericArray<u8, Self::ElemLen>>,
|
2021-09-27 18:29:08 -07:00
|
|
|
) -> Result<Self, InternalError> {
|
2021-10-11 02:51:12 +02:00
|
|
|
let elem = Self::from_element_slice_unchecked(element_bits.into())?;
|
2021-09-27 18:29:08 -07:00
|
|
|
|
2021-10-14 20:09:00 +02:00
|
|
|
if Self::ct_eq(&elem, &<Self as Group>::identity()).into() {
|
2021-09-27 18:29:08 -07:00
|
|
|
// found the identity element
|
|
|
|
|
return Err(InternalError::PointError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(elem)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 01:56:54 -07:00
|
|
|
/// Serializes the `self` group element
|
|
|
|
|
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen>;
|
|
|
|
|
|
|
|
|
|
/// Get the base point for the group
|
|
|
|
|
fn base_point() -> Self;
|
|
|
|
|
|
|
|
|
|
/// Returns if the group element is equal to the identity (1)
|
2021-09-13 16:02:09 -07:00
|
|
|
fn is_identity(&self) -> bool {
|
2021-10-14 20:09:00 +02:00
|
|
|
self.ct_eq(&<Self as Group>::identity()).into()
|
2021-09-13 16:02:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the identity group element
|
|
|
|
|
fn identity() -> Self;
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2021-09-27 18:29:08 -07:00
|
|
|
/// Returns the scalar representing zero
|
|
|
|
|
fn scalar_zero() -> Self::Scalar;
|
|
|
|
|
|
2021-09-28 19:44:57 -07:00
|
|
|
/// Set the contents of self to the identity value
|
|
|
|
|
fn zeroize(&mut self) {
|
|
|
|
|
*self = <Self as Group>::identity();
|
|
|
|
|
}
|
2021-09-09 01:56:54 -07:00
|
|
|
}
|
2021-09-27 18:29:08 -07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|