2020-06-05 09:35:14 -07:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
//
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
|
|
|
|
|
|
|
|
|
use crate::errors::InternalPakeError;
|
|
|
|
|
|
|
|
|
|
use curve25519_dalek::{
|
|
|
|
|
edwards::{CompressedEdwardsY, EdwardsPoint},
|
|
|
|
|
ristretto::{CompressedRistretto, RistrettoPoint},
|
|
|
|
|
scalar::Scalar,
|
|
|
|
|
};
|
|
|
|
|
use generic_array::{
|
|
|
|
|
typenum::{U32, U64},
|
|
|
|
|
ArrayLength, GenericArray,
|
|
|
|
|
};
|
|
|
|
|
use rand_core::{CryptoRng, RngCore};
|
2020-06-15 07:13:18 -04:00
|
|
|
|
2020-06-05 09:35:14 -07:00
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
|
|
|
|
|
|
use std::ops::Mul;
|
|
|
|
|
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.
|
|
|
|
|
pub trait Group: Sized + for<'a> Mul<&'a <Self as Group>::Scalar, Output = Self> {
|
|
|
|
|
/// The type of base field scalars
|
|
|
|
|
type Scalar: Zeroize;
|
|
|
|
|
/// The byte length necessary to represent scalars
|
|
|
|
|
type ScalarLen: ArrayLength<u8>;
|
|
|
|
|
/// Return a scalat from its fixed-length bytes representation
|
|
|
|
|
fn from_scalar_slice(
|
|
|
|
|
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
|
|
|
|
|
) -> Result<Self::Scalar, InternalPakeError>;
|
|
|
|
|
/// picks a scalar at random
|
|
|
|
|
fn random_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>;
|
|
|
|
|
/// Return an element from its fixed-length bytes representation
|
|
|
|
|
fn from_element_slice(
|
|
|
|
|
element_bits: &GenericArray<u8, Self::ElemLen>,
|
|
|
|
|
) -> Result<Self, InternalPakeError>;
|
|
|
|
|
/// Serializes the `self` group element
|
|
|
|
|
fn to_bytes(&self) -> GenericArray<u8, Self::ElemLen>;
|
|
|
|
|
|
|
|
|
|
/// Hashes points presumed to be uniformly random to the curve. The
|
|
|
|
|
/// impl is allowed to perform additional hashes if it needs to, but this
|
|
|
|
|
/// may not be necessary as this function is going to be called with the
|
|
|
|
|
/// output of a kdf.
|
|
|
|
|
type UniformBytesLen: ArrayLength<u8>;
|
|
|
|
|
fn hash_to_curve(uniform_bytes: &GenericArray<u8, Self::UniformBytesLen>) -> Self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The implementation of such a subgroup for Ristretto
|
|
|
|
|
impl Group for RistrettoPoint {
|
|
|
|
|
type Scalar = Scalar;
|
|
|
|
|
type ScalarLen = U32;
|
|
|
|
|
fn from_scalar_slice(
|
|
|
|
|
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
|
|
|
|
|
) -> Result<Self::Scalar, InternalPakeError> {
|
|
|
|
|
let mut bits = [0u8; 32];
|
|
|
|
|
bits.copy_from_slice(scalar_bits);
|
|
|
|
|
Ok(Scalar::from_bytes_mod_order(bits))
|
|
|
|
|
}
|
|
|
|
|
fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
|
|
|
|
|
Scalar::random(rng)
|
|
|
|
|
}
|
|
|
|
|
fn scalar_as_bytes(scalar: &Self::Scalar) -> &GenericArray<u8, Self::ScalarLen> {
|
|
|
|
|
GenericArray::from_slice(scalar.as_bytes())
|
|
|
|
|
}
|
|
|
|
|
fn scalar_invert(scalar: &Self::Scalar) -> Self::Scalar {
|
|
|
|
|
scalar.invert()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The byte length necessary to represent group elements
|
|
|
|
|
type ElemLen = U32;
|
|
|
|
|
fn from_element_slice(
|
|
|
|
|
element_bits: &GenericArray<u8, Self::ElemLen>,
|
|
|
|
|
) -> Result<Self, InternalPakeError> {
|
|
|
|
|
CompressedRistretto::from_slice(element_bits)
|
|
|
|
|
.decompress()
|
|
|
|
|
.ok_or_else(|| InternalPakeError::PointError)
|
|
|
|
|
}
|
|
|
|
|
// serialization of a group element
|
|
|
|
|
fn to_bytes(&self) -> GenericArray<u8, Self::ElemLen> {
|
|
|
|
|
let c = self.compress();
|
|
|
|
|
*GenericArray::from_slice(c.as_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UniformBytesLen = U64;
|
|
|
|
|
fn hash_to_curve(uniform_bytes: &GenericArray<u8, Self::UniformBytesLen>) -> Self {
|
2020-07-03 16:12:14 -04:00
|
|
|
// This is because RistrettoPoint is on an obsolete sha2 version, see https://github.com/dalek-cryptography/curve25519-dalek/pull/327
|
2020-06-05 09:35:14 -07:00
|
|
|
let mut bits = [0u8; 64];
|
2020-06-15 07:13:18 -04:00
|
|
|
let mut hasher = sha2::Sha512::new();
|
|
|
|
|
hasher.update(uniform_bytes);
|
|
|
|
|
bits.copy_from_slice(&hasher.finalize());
|
|
|
|
|
|
|
|
|
|
RistrettoPoint::from_uniform_bytes(&bits)
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The implementation of such a subgroup for points on the large Curve25519-subgroup
|
|
|
|
|
impl Group for EdwardsPoint {
|
|
|
|
|
type Scalar = Scalar;
|
|
|
|
|
type ScalarLen = U32;
|
|
|
|
|
fn from_scalar_slice(
|
|
|
|
|
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
|
|
|
|
|
) -> Result<Self::Scalar, InternalPakeError> {
|
|
|
|
|
let mut bits = [0u8; 32];
|
|
|
|
|
bits.copy_from_slice(scalar_bits);
|
|
|
|
|
Ok(Scalar::from_bytes_mod_order(bits))
|
|
|
|
|
}
|
|
|
|
|
fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
|
|
|
|
|
Scalar::random(rng)
|
|
|
|
|
}
|
|
|
|
|
fn scalar_as_bytes(scalar: &Self::Scalar) -> &GenericArray<u8, Self::ScalarLen> {
|
|
|
|
|
GenericArray::from_slice(scalar.as_bytes())
|
|
|
|
|
}
|
|
|
|
|
fn scalar_invert(scalar: &Self::Scalar) -> Self::Scalar {
|
|
|
|
|
scalar.invert()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The byte length necessary to represent group elements
|
|
|
|
|
type ElemLen = U32;
|
|
|
|
|
fn from_element_slice(
|
|
|
|
|
element_bits: &GenericArray<u8, Self::ElemLen>,
|
|
|
|
|
) -> Result<Self, InternalPakeError> {
|
|
|
|
|
CompressedEdwardsY::from_slice(element_bits)
|
|
|
|
|
.decompress()
|
|
|
|
|
.ok_or_else(|| InternalPakeError::PointError)
|
|
|
|
|
}
|
|
|
|
|
// serialization of a group element
|
|
|
|
|
fn to_bytes(&self) -> GenericArray<u8, Self::ElemLen> {
|
|
|
|
|
let c = self.compress();
|
|
|
|
|
*GenericArray::from_slice(c.as_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UniformBytesLen = U64;
|
|
|
|
|
fn hash_to_curve(uniform_bytes: &GenericArray<u8, Self::UniformBytesLen>) -> Self {
|
|
|
|
|
let mut result = [0u8; 32];
|
|
|
|
|
let mut counter = 0;
|
|
|
|
|
let mut wrapped_point: Option<EdwardsPoint> = None;
|
|
|
|
|
|
|
|
|
|
while wrapped_point.is_none() {
|
|
|
|
|
result.copy_from_slice(
|
|
|
|
|
&Sha256::new()
|
|
|
|
|
.chain(&uniform_bytes[..32])
|
|
|
|
|
.chain(&[counter])
|
2020-06-15 07:13:18 -04:00
|
|
|
.finalize()[..32],
|
2020-06-05 09:35:14 -07:00
|
|
|
);
|
|
|
|
|
wrapped_point = CompressedEdwardsY::from_slice(&result).decompress();
|
|
|
|
|
counter += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wrapped_point
|
|
|
|
|
.expect("guarded by loop exit condition")
|
|
|
|
|
.mul_by_cofactor()
|
|
|
|
|
}
|
|
|
|
|
}
|