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

129 lines
4.0 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
use core::convert::TryInto;
2021-10-14 20:09:00 +02:00
use core::ops::Add;
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;
use digest::{Digest, FixedOutputReset};
2021-12-23 07:50:48 +01:00
use generic_array::typenum::{U1, U32, U64};
use generic_array::{ArrayLength, GenericArray};
2021-10-14 20:09:00 +02:00
use rand_core::{CryptoRng, RngCore};
2021-09-09 01:56:54 -07:00
2021-12-23 07:50:48 +01:00
use super::Group;
2021-12-25 22:54:27 +01:00
use crate::{Error, Result};
2021-09-09 01:56:54 -07:00
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")]
/// The implementation of such a subgroup for Ristretto
impl Group for RistrettoPoint {
const SUITE_ID: usize = 0x0001;
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
2021-12-23 21:58:00 +01:00
fn hash_to_curve<H: BlockSizeUser + Digest + FixedOutputReset, D: ArrayLength<u8> + Add<U1>>(
2021-12-23 07:50:48 +01:00
msg: &[u8],
dst: GenericArray<u8, D>,
2021-12-25 22:54:27 +01:00
) -> Result<Self>
2021-12-23 07:50:48 +01:00
where
<D as Add<U1>>::Output: ArrayLength<u8>,
{
let uniform_bytes = super::expand::expand_message_xmd::<H, U64, _, _>(Some(msg), dst)?;
Ok(RistrettoPoint::from_uniform_bytes(
uniform_bytes
.as_slice()
.try_into()
2021-12-25 22:54:27 +01:00
.map_err(|_| Error::HashToCurveError)?,
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
fn hash_to_scalar<
'a,
2021-12-23 21:58:00 +01:00
H: BlockSizeUser + Digest + FixedOutputReset,
2021-12-23 07:50:48 +01:00
D: ArrayLength<u8> + Add<U1>,
I: IntoIterator<Item = &'a [u8]>,
>(
input: I,
dst: GenericArray<u8, D>,
2021-12-25 22:54:27 +01:00
) -> Result<Self::Scalar>
2021-12-23 07:50:48 +01:00
where
<D as Add<U1>>::Output: ArrayLength<u8>,
{
let uniform_bytes = super::expand::expand_message_xmd::<H, U64, _, _>(input, dst)?;
Ok(Scalar::from_bytes_mod_order_wide(
uniform_bytes
.as_slice()
.try_into()
2021-12-25 22:54:27 +01:00
.map_err(|_| Error::HashToCurveError)?,
2021-12-23 07:50:48 +01:00
))
}
type Scalar = Scalar;
type ScalarLen = U32;
fn from_scalar_slice_unchecked(
scalar_bits: &GenericArray<u8, Self::ScalarLen>,
2021-12-25 22:54:27 +01:00
) -> Result<Self::Scalar> {
2021-12-23 07:50:48 +01:00
Ok(Scalar::from_bytes_mod_order(*scalar_bits.as_ref()))
}
2021-09-27 18:29:08 -07:00
2021-12-23 07:50:48 +01:00
fn random_nonzero_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Scalar {
loop {
let scalar = {
let mut scalar_bytes = [0u8; 64];
rng.fill_bytes(&mut scalar_bytes);
Scalar::from_bytes_mod_order_wide(&scalar_bytes)
};
if scalar != Scalar::zero() {
break scalar;
2021-09-09 01:56:54 -07:00
}
}
2021-12-23 07:50:48 +01:00
}
2021-12-23 07:50:48 +01:00
fn scalar_as_bytes(scalar: Self::Scalar) -> GenericArray<u8, Self::ScalarLen> {
scalar.to_bytes().into()
}
2021-12-23 07:50:48 +01:00
fn scalar_invert(scalar: &Self::Scalar) -> Self::Scalar {
scalar.invert()
}
2021-09-09 01:56:54 -07:00
2021-12-23 07:50:48 +01:00
// The byte length necessary to represent group elements
type ElemLen = U32;
fn from_element_slice_unchecked(
element_bits: &GenericArray<u8, Self::ElemLen>,
2021-12-25 22:54:27 +01:00
) -> Result<Self> {
2021-12-23 07:50:48 +01:00
CompressedRistretto::from_slice(element_bits)
.decompress()
2021-12-25 22:54:27 +01:00
.ok_or(Error::PointError)
2021-12-23 07:50:48 +01:00
}
// serialization of a group element
fn to_arr(&self) -> GenericArray<u8, Self::ElemLen> {
self.compress().to_bytes().into()
}
2021-09-09 01:56:54 -07:00
2021-12-23 07:50:48 +01:00
fn base_point() -> Self {
RISTRETTO_BASEPOINT_POINT
}
2021-09-09 01:56:54 -07:00
2021-12-23 07:50:48 +01:00
fn identity() -> Self {
<Self as Identity>::identity()
}
2021-09-09 01:56:54 -07:00
2021-12-23 07:50:48 +01:00
fn scalar_zero() -> Self::Scalar {
Self::Scalar::zero()
2021-09-27 18:29:08 -07:00
}
2021-09-09 01:56:54 -07:00
}