Rust CI / cargo fmt (push) Successful in 3s
Rust CI / cargo clippy (push) Successful in 28s
Rust CI / test (1.87.0 / no backend / no frontend) (push) Successful in 1m26s
Rust CI / test (stable / no backend / no frontend) (push) Successful in 1m18s
Rust CI / test (1.87.0 / no backend / --features danger) (push) Successful in 1m26s
Publish / publish (release) Successful in 17s
Rust CI / test (stable / no backend / --features danger) (push) Successful in 1m17s
Rust CI / test (1.87.0 / no backend / --features serde) (push) Successful in 1m30s
Rust CI / test (stable / no backend / --features serde) (push) Successful in 1m24s
Rust CI / test (1.87.0 / --features ristretto255-ciphersuite / no frontend) (push) Successful in 1m33s
Rust CI / test (stable / --features ristretto255-ciphersuite / no frontend) (push) Successful in 1m28s
Rust CI / test (1.87.0 / --features ristretto255-ciphersuite / --features danger) (push) Successful in 1m34s
Rust CI / test (stable / --features ristretto255-ciphersuite / --features danger) (push) Successful in 1m27s
Rust CI / test (1.87.0 / --features ristretto255-ciphersuite / --features serde) (push) Successful in 1m40s
Rust CI / test (stable / --features ristretto255-ciphersuite / --features serde) (push) Successful in 1m29s
Rust CI / cargo audit (push) Successful in 4s
Rust CI / no-std (thumbv6m-none-eabi / no backend) (push) Successful in 10s
Rust CI / no-std (wasm32-unknown-unknown / no backend) (push) Successful in 11s
Rust CI / no-std (wasm32-unknown-unknown / --features ristretto255-ciphersuite) (push) Successful in 10s
Rust CI / no-std (thumbv6m-none-eabi / --features ristretto255-ciphersuite) (push) Successful in 13s
- Deduplicate serialization with impl_serde_scalar, impl_serde_elem, and impl_serde_scalar_elem macros - Move finalize_after_unblind to common.rs, shared by OPRF and VOPRF - Add shared test helpers (test_all_curves macro, prf function) - Update dependencies to stable releases Reviewed-on: #12 Co-authored-by: UneBaguette <[email protected]> Co-committed-by: UneBaguette <[email protected]>
154 lines
4.7 KiB
Rust
154 lines
4.7 KiB
Rust
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
// Copyright (c) VexaHub and contributors.
|
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
use core::num::NonZeroU16;
|
|
|
|
use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
|
|
use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint};
|
|
use curve25519_dalek::scalar::Scalar;
|
|
use curve25519_dalek::traits::Identity;
|
|
use digest::block_api::BlockSizeUser;
|
|
use digest::{FixedOutput, HashMarker};
|
|
use hash2curve::{ExpandMsg, ExpandMsgXmd, Expander};
|
|
use hybrid_array::Array;
|
|
use hybrid_array::typenum::{
|
|
IsGreaterOrEqual, IsLess, IsLessOrEqual, Prod, True, U2, U16, U32, U256,
|
|
};
|
|
use rand_core::{TryCryptoRng, TryRng};
|
|
use subtle::ConstantTimeEq;
|
|
|
|
use super::Group;
|
|
use crate::{Error, InternalError, Result};
|
|
|
|
/// [`Group`] implementation for Ristretto255.
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
|
pub struct Ristretto255;
|
|
|
|
#[cfg(feature = "ristretto255-ciphersuite")]
|
|
impl crate::CipherSuite for Ristretto255 {
|
|
const ID: &'static [u8] = b"ristretto255-SHA512";
|
|
|
|
type Group = Ristretto255;
|
|
|
|
type Hash = sha2::Sha512;
|
|
}
|
|
|
|
impl Group for Ristretto255 {
|
|
type Elem = RistrettoPoint;
|
|
|
|
type ElemLen = U32;
|
|
|
|
type Scalar = Scalar;
|
|
|
|
type ScalarLen = U32;
|
|
|
|
type SecurityLevel = U16;
|
|
|
|
// Implements the `hash_to_ristretto255()` function from
|
|
// https://www.rfc-editor.org/rfc/rfc9380.html#appendix-B
|
|
fn hash_to_curve<H>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Elem, InternalError>
|
|
where
|
|
H: BlockSizeUser + Default + FixedOutput + HashMarker,
|
|
H::OutputSize: IsLess<U256>
|
|
+ IsLessOrEqual<H::BlockSize, Output = True>
|
|
+ IsGreaterOrEqual<Prod<Self::SecurityLevel, U2>, Output = True>,
|
|
{
|
|
let uniform_bytes = expand_uniform_bytes::<H>(input, dst)?;
|
|
|
|
Ok(RistrettoPoint::from_uniform_bytes(&uniform_bytes))
|
|
}
|
|
|
|
// Implements the `HashToScalar()` function from
|
|
// https://www.rfc-editor.org/rfc/rfc9497#section-4.1
|
|
fn hash_to_scalar<H>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Scalar, InternalError>
|
|
where
|
|
H: BlockSizeUser + Default + FixedOutput + HashMarker,
|
|
H::OutputSize: IsLess<U256>
|
|
+ IsLessOrEqual<H::BlockSize, Output = True>
|
|
+ IsGreaterOrEqual<Prod<Self::SecurityLevel, U2>, Output = True>,
|
|
{
|
|
let uniform_bytes = expand_uniform_bytes::<H>(input, dst)?;
|
|
|
|
Ok(Scalar::from_bytes_mod_order_wide(&uniform_bytes))
|
|
}
|
|
|
|
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) -> Array<u8, Self::ElemLen> {
|
|
elem.compress().to_bytes().into()
|
|
}
|
|
|
|
fn deserialize_elem(element_bits: &[u8]) -> Result<Self::Elem> {
|
|
CompressedRistretto::from_slice(element_bits)
|
|
.map_err(|_| Error::Deserialization)?
|
|
.decompress()
|
|
.filter(|point| point != &RistrettoPoint::identity())
|
|
.ok_or(Error::Deserialization)
|
|
}
|
|
|
|
fn random_scalar<R: TryRng + TryCryptoRng>(rng: &mut R) -> Result<Self::Scalar> {
|
|
loop {
|
|
let mut scalar_bytes = [0u8; 32];
|
|
rng.try_fill_bytes(&mut scalar_bytes)
|
|
.map_err(|_| Error::Rng)?;
|
|
|
|
if let Ok(scalar) = Self::deserialize_scalar(&scalar_bytes) {
|
|
break Ok(scalar);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn invert_scalar(scalar: Self::Scalar) -> Self::Scalar {
|
|
scalar.invert()
|
|
}
|
|
|
|
fn is_zero_scalar(scalar: Self::Scalar) -> subtle::Choice {
|
|
scalar.ct_eq(&Scalar::ZERO)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
fn zero_scalar() -> Self::Scalar {
|
|
Scalar::ZERO
|
|
}
|
|
|
|
fn serialize_scalar(scalar: Self::Scalar) -> Array<u8, Self::ScalarLen> {
|
|
scalar.to_bytes().into()
|
|
}
|
|
|
|
fn deserialize_scalar(scalar_bits: &[u8]) -> Result<Self::Scalar> {
|
|
scalar_bits
|
|
.try_into()
|
|
.ok()
|
|
.and_then(|bytes| Scalar::from_canonical_bytes(bytes).into())
|
|
.filter(|scalar| scalar != &Scalar::ZERO)
|
|
.ok_or(Error::Deserialization)
|
|
}
|
|
}
|
|
|
|
// HELPERS
|
|
|
|
fn expand_uniform_bytes<H>(input: &[&[u8]], dst: &[&[u8]]) -> Result<[u8; 64], InternalError>
|
|
where
|
|
H: BlockSizeUser + Default + FixedOutput + HashMarker,
|
|
H::OutputSize: IsLess<U256>
|
|
+ IsLessOrEqual<H::BlockSize, Output = True>
|
|
+ IsGreaterOrEqual<Prod<U16, U2>, Output = True>,
|
|
{
|
|
let mut uniform_bytes = [0u8; 64];
|
|
|
|
<ExpandMsgXmd<H> as ExpandMsg<U16>>::expand_message(input, dst, NonZeroU16::new(64).unwrap())
|
|
.map_err(|_| InternalError::Input)?
|
|
.fill_bytes(&mut uniform_bytes)
|
|
.map_err(|_| InternalError::Input)?;
|
|
|
|
Ok(uniform_bytes)
|
|
}
|