Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ version = "0.5.0"
alloc = []
danger = []
default = ["ristretto255-ciphersuite", "dep:serde"]
ristretto255 = ["dep:curve25519-dalek", "generic-array/more_lengths"]
ristretto255 = ["dep:curve25519-dalek"]
ristretto255-ciphersuite = ["ristretto255", "dep:sha2"]
serde = ["curve25519-dalek?/serde", "generic-array/serde", "dep:serde"]
std = ["alloc"]
Expand All @@ -33,7 +33,7 @@ elliptic-curve = { version = "0.13", features = [
"sec1",
"voprf",
] }
generic-array = "0.14"
generic-array = "1"
rand_core = { version = "0.6", default-features = false }
serde = { version = "1", default-features = false, features = [
"derive",
Expand All @@ -43,7 +43,7 @@ subtle = { version = "2.3", default-features = false }
zeroize = { version = "1.5", default-features = false }

[dev-dependencies]
generic-array = { version = "0.14", features = ["more_lengths"] }
generic-array = { version = "1" }
hex = "0.4"
p256 = { version = "0.13", default-features = false, features = [
"hash2curve",
Expand Down
5 changes: 3 additions & 2 deletions src/ciphersuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ use digest::core_api::BlockSizeUser;
use digest::{FixedOutput, HashMarker, OutputSizeUser};
use elliptic_curve::VoprfParameters;
use generic_array::typenum::{IsLess, IsLessOrEqual, U256};
use generic_array::ArrayLength;

use crate::Group;

/// Configures the underlying primitives used in VOPRF
pub trait CipherSuite
where
<Self::Hash as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<Self::Hash as BlockSizeUser>::BlockSize>,
ArrayLength + IsLess<U256> + IsLessOrEqual<<Self::Hash as BlockSizeUser>::BlockSize>,
{
/// The ciphersuite identifier as dictated by
/// <https://www.rfc-editor.org/rfc/rfc9497>
Expand All @@ -39,7 +40,7 @@ where
T: Group,
T::Hash: BlockSizeUser + Default + FixedOutput + HashMarker,
<T::Hash as OutputSizeUser>::OutputSize:
IsLess<U256> + IsLessOrEqual<<T::Hash as BlockSizeUser>::BlockSize>,
ArrayLength + IsLess<U256> + IsLessOrEqual<<T::Hash as BlockSizeUser>::BlockSize>,
{
const ID: &'static str = T::ID;

Expand Down
16 changes: 7 additions & 9 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::convert::TryFrom;
use core::ops::Add;

use derive_where::derive_where;
use digest::{Digest, Output};
use digest::{Digest, Output, OutputSizeUser};
use generic_array::sequence::Concat;
use generic_array::typenum::{IsLess, Unsigned, U2, U256, U9};
use generic_array::{ArrayLength, GenericArray};
Expand Down Expand Up @@ -283,7 +283,7 @@ fn compute_composites<
.chain_update(seed_dst.i2osp_2())
.chain_update_multi(&seed_dst.as_dst())
.finalize();
let seed_len = i2osp_2_array(&seed);
let seed_len = i2osp_2_array::<<CS::Hash as OutputSizeUser>::OutputSize>();

let mut m = CS::Group::identity_elem();
let mut z = CS::Group::identity_elem();
Expand Down Expand Up @@ -442,23 +442,23 @@ pub(crate) fn server_evaluate_hash_input<CS: CipherSuite>(
.chain_update(info.as_ref());
}
Ok(hash
.chain_update(i2osp_2(issued_element.as_ref().len()).map_err(|_| Error::Input)?)
.chain_update(i2osp_2(issued_element.as_slice().len()).map_err(|_| Error::Input)?)
.chain_update(issued_element)
.chain_update(STR_FINALIZE)
.finalize())
}

pub(crate) struct Dst<L: ArrayLength<u8>> {
pub(crate) struct Dst<L: ArrayLength> {
dst_1: GenericArray<u8, L>,
dst_2: &'static str,
}

impl<L: ArrayLength<u8>> Dst<L> {
impl<L: ArrayLength> Dst<L> {
pub(crate) fn new<CS, T, TL>(par_1: T, mode: Mode) -> Self
where
CS: CipherSuite,
T: Into<GenericArray<u8, TL>>,
TL: ArrayLength<u8> + Add<U9, Output = L>,
TL: ArrayLength + Add<U9, Output = L>,
{
let par_1 = par_1.into();
// Generates the contextString parameter as defined in
Expand Down Expand Up @@ -518,8 +518,6 @@ pub(crate) fn i2osp_2(input: usize) -> Result<[u8; 2], InternalError> {
.map_err(|_| InternalError::I2osp)
}

pub(crate) fn i2osp_2_array<L: ArrayLength<u8> + IsLess<U256>>(
_: &GenericArray<u8, L>,
) -> GenericArray<u8, U2> {
pub(crate) fn i2osp_2_array<L: ArrayLength + IsLess<U256>>() -> GenericArray<u8, U2> {
L::U16.to_be_bytes().into()
}
4 changes: 1 addition & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

//! Errors which are produced during an execution of the protocol

use displaydoc::Display;

/// [`Result`](core::result::Result) shorthand that uses [`Error`].
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Represents an error in the manipulation of internal cryptographic data
#[derive(Clone, Copy, Debug, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, displaydoc::Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Error {
/// Size of info is longer then [`u16::MAX`].
Info,
Expand Down
14 changes: 10 additions & 4 deletions src/group/elliptic_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use elliptic_curve::group::cofactor::CofactorGroup;
use elliptic_curve::hash2curve::{ExpandMsgXmd, FromOkm, GroupDigest};
use elliptic_curve::sec1::{FromEncodedPoint, ModulusSize, ToEncodedPoint};
use elliptic_curve::{
AffinePoint, Field, FieldBytesSize, Group as _, ProjectivePoint, PublicKey, Scalar, SecretKey,
AffinePoint, Field, FieldBytes, FieldBytesSize, Group as _, ProjectivePoint, PublicKey, Scalar,
SecretKey,
};
use generic_array::typenum::{IsLess, IsLessOrEqual, Sum, U256};
use generic_array::{ArrayLength, GenericArray};
Expand All @@ -31,14 +32,16 @@ where
C: GroupDigest,
ProjectivePoint<Self>: CofactorGroup + ToEncodedPoint<Self>,
ScalarLen<Self>: ModulusSize,
ScalarLen<Self>: ArrayLength,
AffinePoint<Self>: FromEncodedPoint<Self> + ToEncodedPoint<Self>,
Scalar<Self>: FromOkm,
// `VoprfClientLen`, `PoprfClientLen`, `VoprfServerLen`, `PoprfServerLen`
ScalarLen<Self>: Add<ElemLen<Self>>,
Sum<ScalarLen<Self>, ElemLen<Self>>: ArrayLength<u8>,
Sum<ScalarLen<Self>, ElemLen<Self>>: ArrayLength,
// `ProofLen`
ScalarLen<Self>: Add<ScalarLen<Self>>,
Sum<ScalarLen<Self>, ScalarLen<Self>>: ArrayLength<u8>,
Sum<ScalarLen<Self>, ScalarLen<Self>>: ArrayLength,
ElemLen<Self>: ArrayLength,
{
type Elem = ProjectivePoint<Self>;

Expand Down Expand Up @@ -108,7 +111,10 @@ where
}

fn serialize_scalar(scalar: Self::Scalar) -> GenericArray<u8, Self::ScalarLen> {
scalar.into()
let bytes: FieldBytes<Self> = scalar.into();
let mut result = GenericArray::<u8, Self::ScalarLen>::default();
result.as_mut_slice().copy_from_slice(bytes.as_ref());
result
}

fn deserialize_scalar(scalar_bits: &[u8]) -> Result<Self::Scalar> {
Expand Down
8 changes: 4 additions & 4 deletions src/group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pub trait Group
where
// `VoprfClientLen`, `PoprfClientLen`, `VoprfServerLen`, `PoprfServerLen`
Self::ScalarLen: Add<Self::ElemLen>,
Sum<Self::ScalarLen, Self::ElemLen>: ArrayLength<u8>,
Sum<Self::ScalarLen, Self::ElemLen>: ArrayLength,
// `ProofLen`
Self::ScalarLen: Add<Self::ScalarLen>,
Sum<Self::ScalarLen, Self::ScalarLen>: ArrayLength<u8>,
Sum<Self::ScalarLen, Self::ScalarLen>: ArrayLength,
{
/// The type of group elements
type Elem: ConstantTimeEq
Expand All @@ -45,7 +45,7 @@ where
+ for<'a> Mul<&'a Self::Scalar, Output = Self::Elem>;

/// The byte length necessary to represent group elements
type ElemLen: ArrayLength<u8> + 'static;
type ElemLen: ArrayLength + 'static;

/// The type of base field scalars
type Scalar: ConstantTimeEq
Expand All @@ -56,7 +56,7 @@ where
+ for<'a> Sub<&'a Self::Scalar, Output = Self::Scalar>;

/// The byte length necessary to represent scalars
type ScalarLen: ArrayLength<u8> + 'static;
type ScalarLen: ArrayLength + 'static;

/// Transforms a password and domain separation tag (DST) into a curve point
///
Expand Down
15 changes: 11 additions & 4 deletions src/poprf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use core::iter::{self, Map, Repeat, Zip};
use derive_where::derive_where;
use digest::{Digest, Output, OutputSizeUser};
use generic_array::typenum::Unsigned;
use generic_array::GenericArray;
use generic_array::{ArrayLength, GenericArray};
use rand_core::{CryptoRng, RngCore};

use crate::common::{
Expand Down Expand Up @@ -132,7 +132,10 @@ impl<CS: CipherSuite> PoprfClient<CS> {
proof: &Proof<CS>,
pk: <CS::Group as Group>::Elem,
info: Option<&[u8]>,
) -> Result<Output<CS::Hash>> {
) -> Result<Output<CS::Hash>>
where
<<CS as CipherSuite>::Hash as OutputSizeUser>::OutputSize: ArrayLength,
{
let clients = core::array::from_ref(self);
let messages = core::array::from_ref(evaluation_element);

Expand Down Expand Up @@ -167,6 +170,7 @@ impl<CS: CipherSuite> PoprfClient<CS> {
<&'a IC as IntoIterator>::IntoIter: ExactSizeIterator,
&'a IM: 'a + IntoIterator<Item = &'a EvaluationElement<CS>>,
<&'a IM as IntoIterator>::IntoIter: ExactSizeIterator,
<<CS as CipherSuite>::Hash as OutputSizeUser>::OutputSize: ArrayLength,
{
let unblinded_elements = poprf_unblind(clients, messages, pk, proof, info)?;

Expand Down Expand Up @@ -672,7 +676,7 @@ type FinalizeAfterUnblindResult<'a, CS, IE, II> = Map<
Zip<Zip<IE, II>, Repeat<&'a [u8]>>,
fn(
((<<CS as CipherSuite>::Group as Group>::Elem, &[u8]), &[u8]),
) -> Result<GenericArray<u8, <<CS as CipherSuite>::Hash as OutputSizeUser>::OutputSize>>,
) -> Result<Output<<CS as CipherSuite>::Hash>>,
>;

/// Can only fail with [`Error::Batch`] and returned values can only fail with
Expand All @@ -686,7 +690,10 @@ fn finalize_after_unblind<
unblinded_elements: IE,
inputs: II,
info: Option<&'a [u8]>,
) -> Result<FinalizeAfterUnblindResult<'a, CS, IE, II>> {
) -> Result<FinalizeAfterUnblindResult<'a, CS, IE, II>>
where
<<CS as CipherSuite>::Hash as OutputSizeUser>::OutputSize: ArrayLength,
{
if unblinded_elements.len() != inputs.len() {
return Err(Error::Batch);
}
Expand Down
Loading