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-12-23 07:50:48 +01:00
|
|
|
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
|
|
|
|
|
use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint};
|
|
|
|
|
use curve25519_dalek::scalar::Scalar;
|
|
|
|
|
use curve25519_dalek::traits::Identity;
|
2021-12-23 21:58:00 +01:00
|
|
|
use digest::core_api::BlockSizeUser;
|
2022-04-01 21:18:32 +02:00
|
|
|
use digest::Digest;
|
2022-01-21 22:52:09 +01:00
|
|
|
use elliptic_curve::hash2curve::{ExpandMsg, ExpandMsgXmd, Expander};
|
|
|
|
|
use generic_array::typenum::{IsLess, IsLessOrEqual, U256, U32, U64};
|
2022-01-18 12:34:28 +01:00
|
|
|
use generic_array::GenericArray;
|
2021-10-14 20:09:00 +02:00
|
|
|
use rand_core::{CryptoRng, RngCore};
|
2022-01-28 01:38:17 +01:00
|
|
|
use subtle::ConstantTimeEq;
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2022-02-13 04:00:11 -08:00
|
|
|
use super::Group;
|
2022-04-01 21:18:32 +02:00
|
|
|
use crate::{Error, InternalError, Result};
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2022-01-18 12:34:28 +01:00
|
|
|
/// [`Group`] implementation for Ristretto255.
|
2022-01-28 01:38:17 +01:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
|
|
|
|
// `cfg` here is only needed because of a bug in Rust's crate feature documentation. See: https://github.com/rust-lang/rust/issues/83428
|
|
|
|
|
#[cfg(feature = "ristretto255")]
|
2022-01-18 12:34:28 +01:00
|
|
|
pub struct Ristretto255;
|
|
|
|
|
|
2022-01-21 22:52:09 +01:00
|
|
|
#[cfg(feature = "ristretto255-ciphersuite")]
|
|
|
|
|
impl crate::CipherSuite for Ristretto255 {
|
|
|
|
|
const ID: u16 = 0x0001;
|
|
|
|
|
|
|
|
|
|
type Group = Ristretto255;
|
|
|
|
|
|
|
|
|
|
type Hash = sha2::Sha512;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 07:50:48 +01:00
|
|
|
// `cfg` here is only needed because of a bug in Rust's crate feature documentation. See: https://github.com/rust-lang/rust/issues/83428
|
|
|
|
|
#[cfg(feature = "ristretto255")]
|
2022-01-18 12:34:28 +01:00
|
|
|
impl Group for Ristretto255 {
|
|
|
|
|
type Elem = RistrettoPoint;
|
|
|
|
|
|
|
|
|
|
type ElemLen = U32;
|
|
|
|
|
|
|
|
|
|
type Scalar = Scalar;
|
|
|
|
|
|
|
|
|
|
type ScalarLen = U32;
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2021-12-23 07:50:48 +01:00
|
|
|
// Implements the `hash_to_ristretto255()` function from
|
|
|
|
|
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-10.txt
|
2022-04-01 21:18:32 +02:00
|
|
|
fn hash_to_curve<H>(input: &[&[u8]], dst: &[u8]) -> Result<Self::Elem, InternalError>
|
2022-01-21 22:52:09 +01:00
|
|
|
where
|
2022-04-01 21:18:32 +02:00
|
|
|
H: Digest + BlockSizeUser,
|
|
|
|
|
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize>,
|
2022-01-21 22:52:09 +01:00
|
|
|
{
|
|
|
|
|
let mut uniform_bytes = GenericArray::<_, U64>::default();
|
2022-04-01 21:18:32 +02:00
|
|
|
ExpandMsgXmd::<H>::expand_message(input, dst, 64)
|
2022-01-25 05:55:02 +01:00
|
|
|
.map_err(|_| InternalError::Input)?
|
2022-01-21 22:52:09 +01:00
|
|
|
.fill_bytes(&mut uniform_bytes);
|
2022-01-18 12:34:28 +01:00
|
|
|
|
|
|
|
|
Ok(RistrettoPoint::from_uniform_bytes(&uniform_bytes.into()))
|
2021-12-23 07:50:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implements the `HashToScalar()` function from
|
|
|
|
|
// https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-07.html#section-4.1
|
2022-04-01 21:18:32 +02:00
|
|
|
fn hash_to_scalar<H>(input: &[&[u8]], dst: &[u8]) -> Result<Self::Scalar, InternalError>
|
2022-01-21 22:52:09 +01:00
|
|
|
where
|
2022-04-01 21:18:32 +02:00
|
|
|
H: Digest + BlockSizeUser,
|
|
|
|
|
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize>,
|
2022-01-21 22:52:09 +01:00
|
|
|
{
|
|
|
|
|
let mut uniform_bytes = GenericArray::<_, U64>::default();
|
2022-04-01 21:18:32 +02:00
|
|
|
ExpandMsgXmd::<H>::expand_message(input, dst, 64)
|
2022-01-25 05:55:02 +01:00
|
|
|
.map_err(|_| InternalError::Input)?
|
2022-01-21 22:52:09 +01:00
|
|
|
.fill_bytes(&mut uniform_bytes);
|
2021-12-23 07:50:48 +01:00
|
|
|
|
2022-01-21 22:52:09 +01:00
|
|
|
Ok(Scalar::from_bytes_mod_order_wide(&uniform_bytes.into()))
|
2021-12-23 07:50:48 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-18 12:34:28 +01:00
|
|
|
fn base_elem() -> Self::Elem {
|
|
|
|
|
RISTRETTO_BASEPOINT_POINT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn identity_elem() -> Self::Elem {
|
|
|
|
|
RistrettoPoint::identity()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// serialization of a group element
|
|
|
|
|
fn serialize_elem(elem: Self::Elem) -> GenericArray<u8, Self::ElemLen> {
|
|
|
|
|
elem.compress().to_bytes().into()
|
2021-12-23 07:50:48 +01:00
|
|
|
}
|
2021-09-27 18:29:08 -07:00
|
|
|
|
2022-01-28 01:38:17 +01:00
|
|
|
fn deserialize_elem(element_bits: &[u8]) -> Result<Self::Elem> {
|
2022-04-01 21:18:32 +02:00
|
|
|
if element_bits.len() != 32 {
|
|
|
|
|
return Err(Error::Deserialization);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 12:34:28 +01:00
|
|
|
CompressedRistretto::from_slice(element_bits)
|
|
|
|
|
.decompress()
|
|
|
|
|
.filter(|point| point != &RistrettoPoint::identity())
|
2022-01-25 05:55:02 +01:00
|
|
|
.ok_or(Error::Deserialization)
|
2022-01-18 12:34:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
|
2021-12-23 07:50:48 +01:00
|
|
|
loop {
|
2022-04-01 21:18:32 +02:00
|
|
|
let scalar = Scalar::random(rng);
|
2021-12-23 07:50:48 +01:00
|
|
|
|
|
|
|
|
if scalar != Scalar::zero() {
|
|
|
|
|
break scalar;
|
2021-09-09 01:56:54 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-23 07:50:48 +01:00
|
|
|
}
|
2021-09-13 16:02:09 -07:00
|
|
|
|
2022-01-18 12:34:28 +01:00
|
|
|
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar {
|
2021-12-23 07:50:48 +01:00
|
|
|
scalar.invert()
|
|
|
|
|
}
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2022-01-28 01:38:17 +01:00
|
|
|
fn is_zero_scalar(scalar: Self::Scalar) -> subtle::Choice {
|
|
|
|
|
scalar.ct_eq(&Scalar::zero())
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 12:34:28 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
|
fn zero_scalar() -> Self::Scalar {
|
|
|
|
|
Scalar::zero()
|
2021-12-23 07:50:48 +01:00
|
|
|
}
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2022-01-18 12:34:28 +01:00
|
|
|
fn serialize_scalar(scalar: Self::Scalar) -> GenericArray<u8, Self::ScalarLen> {
|
|
|
|
|
scalar.to_bytes().into()
|
2021-12-23 07:50:48 +01:00
|
|
|
}
|
2021-09-09 01:56:54 -07:00
|
|
|
|
2022-01-28 01:38:17 +01:00
|
|
|
fn deserialize_scalar(scalar_bits: &[u8]) -> Result<Self::Scalar> {
|
|
|
|
|
scalar_bits
|
|
|
|
|
.try_into()
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(Scalar::from_canonical_bytes)
|
2022-01-18 12:34:28 +01:00
|
|
|
.filter(|scalar| scalar != &Scalar::zero())
|
2022-01-25 05:55:02 +01:00
|
|
|
.ok_or(Error::Deserialization)
|
2021-09-27 18:29:08 -07:00
|
|
|
}
|
2021-09-09 01:56:54 -07:00
|
|
|
}
|