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.
|
|
|
|
|
|
2021-01-25 13:20:36 -08:00
|
|
|
use crate::errors::InternalPakeError;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
use curve25519_dalek::{
|
2021-01-25 13:20:36 -08:00
|
|
|
constants::RISTRETTO_BASEPOINT_POINT,
|
2020-06-05 09:35:14 -07:00
|
|
|
ristretto::{CompressedRistretto, RistrettoPoint},
|
|
|
|
|
scalar::Scalar,
|
2021-06-15 17:31:12 -07:00
|
|
|
traits::Identity,
|
2020-06-05 09:35:14 -07:00
|
|
|
};
|
|
|
|
|
use generic_array::{
|
|
|
|
|
typenum::{U32, U64},
|
2021-07-31 01:02:02 +02:00
|
|
|
GenericArray,
|
2020-06-05 09:35:14 -07:00
|
|
|
};
|
2021-01-25 13:20:36 -08:00
|
|
|
use std::convert::TryInto;
|
2020-11-03 21:44:00 +00:00
|
|
|
|
2021-02-11 18:10:48 -08:00
|
|
|
use rand::{CryptoRng, RngCore};
|
2021-07-12 12:33:19 -07:00
|
|
|
|
2021-07-31 01:02:02 +02:00
|
|
|
use super::Group;
|
2020-06-05 09:35:14 -07:00
|
|
|
|
|
|
|
|
/// 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))
|
|
|
|
|
}
|
2021-06-15 17:31:12 -07:00
|
|
|
fn random_nonzero_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
|
|
|
|
|
loop {
|
|
|
|
|
let scalar = {
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
|
{
|
|
|
|
|
let mut scalar_bytes = [0u8; 64];
|
|
|
|
|
rng.fill_bytes(&mut scalar_bytes);
|
|
|
|
|
Scalar::from_bytes_mod_order_wide(&scalar_bytes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests need an exact conversion from bytes to scalar, sampling only 32 bytes from rng
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
{
|
|
|
|
|
let mut scalar_bytes = [0u8; 32];
|
|
|
|
|
rng.fill_bytes(&mut scalar_bytes);
|
|
|
|
|
Scalar::from_bytes_mod_order(scalar_bytes)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if scalar != Scalar::zero() {
|
|
|
|
|
break scalar;
|
|
|
|
|
}
|
2021-01-25 13:20:36 -08:00
|
|
|
}
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
2021-07-06 13:32:02 +02:00
|
|
|
fn scalar_as_bytes(scalar: Self::Scalar) -> GenericArray<u8, Self::ScalarLen> {
|
|
|
|
|
scalar.to_bytes().into()
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
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)
|
2020-09-19 19:16:30 -04:00
|
|
|
.decompress()
|
|
|
|
|
.ok_or(InternalPakeError::PointError)
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
// serialization of a group element
|
2020-07-03 16:21:11 -04:00
|
|
|
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen> {
|
2020-06-05 09:35:14 -07:00
|
|
|
let c = self.compress();
|
|
|
|
|
*GenericArray::from_slice(c.as_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UniformBytesLen = U64;
|
2021-07-06 14:33:13 +02:00
|
|
|
fn hash_to_curve(
|
|
|
|
|
uniform_bytes: &GenericArray<u8, Self::UniformBytesLen>,
|
|
|
|
|
) -> Result<Self, InternalPakeError> {
|
2020-09-20 09:58:54 -04:00
|
|
|
// https://caniuse.rs/features/array_gt_32_impls
|
|
|
|
|
let bits: [u8; 64] = {
|
|
|
|
|
let mut bytes = [0u8; 64];
|
|
|
|
|
bytes.copy_from_slice(uniform_bytes);
|
|
|
|
|
bytes
|
|
|
|
|
};
|
2021-07-06 14:32:04 +02:00
|
|
|
Ok(RistrettoPoint::from_uniform_bytes(&bits))
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-25 13:20:36 -08:00
|
|
|
fn base_point() -> Self {
|
|
|
|
|
RISTRETTO_BASEPOINT_POINT
|
2020-06-05 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-25 13:20:36 -08:00
|
|
|
fn mult_by_slice(&self, scalar: &GenericArray<u8, Self::ScalarLen>) -> Self {
|
|
|
|
|
let arr: [u8; 32] = scalar.as_slice().try_into().expect("Wrong length");
|
|
|
|
|
self * Scalar::from_bits(arr)
|
2020-08-19 17:18:25 -04:00
|
|
|
}
|
2021-06-15 17:31:12 -07:00
|
|
|
|
|
|
|
|
/// Returns if the group element is equal to the identity (1)
|
|
|
|
|
fn is_identity(&self) -> bool {
|
|
|
|
|
self == &Self::identity()
|
|
|
|
|
}
|
2021-07-12 12:33:19 -07:00
|
|
|
|
|
|
|
|
fn ct_equal(&self, other: &Self) -> bool {
|
|
|
|
|
constant_time_eq::constant_time_eq(&self.to_arr(), &other.to_arr())
|
|
|
|
|
}
|
2020-08-19 17:18:25 -04:00
|
|
|
}
|