diff --git a/src/faest.rs b/src/faest.rs index ff43ff5..e4eccb2 100644 --- a/src/faest.rs +++ b/src/faest.rs @@ -11,7 +11,7 @@ use crate::{ universal_hashing::{VoleHasherInit, VoleHasherProcess}, utils::Reader, vc::VectorCommitment, - vole::{volecommit, volereconstruct}, + vole::{volecommit, volereconstruct, VoleCommitmentCRef}, ByteEncoding, Error, }; @@ -308,10 +308,7 @@ trait FaestHash { /// Generate `r` and `iv` fn hash_r_iv(r: &mut [u8], iv: &mut IV, key: &[u8], mu: &[u8], rho: &[u8]); /// Generate first challange - fn hash_challenge_1(chall1: &mut [u8], mu: &[u8], hcom: &[u8], c: I, iv: &[u8]) - where - I: Iterator, - T: AsRef<[u8]>; + fn hash_challenge_1(chall1: &mut [u8], mu: &[u8], hcom: &[u8], c: &[u8], iv: &[u8]); /// Generate second challenge fn hash_challenge_2(chall2: &mut [u8], chall1: &[u8], u_t: &[u8], hv: &[u8], d: &[u8]); /// Generate third challenge @@ -341,17 +338,11 @@ where h3_reader.read(iv); } - fn hash_challenge_1(chall1: &mut [u8], mu: &[u8], hcom: &[u8], c: I, iv: &[u8]) - where - I: Iterator, - T: AsRef<[u8]>, - { + fn hash_challenge_1(chall1: &mut [u8], mu: &[u8], hcom: &[u8], c: &[u8], iv: &[u8]) { let mut h2_hasher = Self::h2_init(); h2_hasher.update(mu); h2_hasher.update(hcom); - for buf in c { - h2_hasher.update(buf.as_ref()); - } + h2_hasher.update(c); h2_hasher.update(iv); h2_hasher.finish().read(chall1); } @@ -413,18 +404,19 @@ fn sign( let mut iv = IV::default(); RO::

::hash_r_iv(&mut r, &mut iv, &sk.owf_key, &mu, rho); - let (hcom, decom, c, u, gv) = - volecommit::<::VC, P::Tau, O::LHATBYTES>(&r, &iv); + let volecommit_cs = + &mut signature[..O::LHATBYTES::USIZE * (::Tau::USIZE - 1)]; + let (hcom, decom, u, gv) = volecommit::< + ::VC, + P::Tau, + O::LHATBYTES, + >(VoleCommitmentCRef::new(volecommit_cs), &r, &iv); let mut chall1 = GenericArray::::BaseParams as BaseParameters>::Chall1>::default(); - RO::

::hash_challenge_1(&mut chall1, &mu, &hcom, c.iter(), &iv); - - // write c and drop it - let mut signature = signature.as_mut_slice(); - for x in c.into_iter() { - signature.write_all(&x).unwrap(); - } + RO::

::hash_challenge_1(&mut chall1, &mu, &hcom, volecommit_cs, &iv); + let signature = + &mut signature[O::LHATBYTES::USIZE * (::Tau::USIZE - 1)..]; let (u_t, hv) = { let vole_hasher = VoleHasher::

::new_vole_hasher(&chall1); let u_t = vole_hasher.process(&u); @@ -542,7 +534,7 @@ where let mut chall1 = GenericArray::::BaseParams as BaseParameters>::Chall1>::default(); let c = &sigma[..O::LHATBYTES::USIZE * (::Tau::USIZE - 1)]; - RO::

::hash_challenge_1(&mut chall1, &mu, &hcom, [c].into_iter(), iv); + RO::

::hash_challenge_1(&mut chall1, &mu, &hcom, c, iv); let vole_hasher = VoleHasher::

::new_vole_hasher(&chall1); let def = GenericArray::default(); diff --git a/src/parameter.rs b/src/parameter.rs index e60e52f..4b85f14 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -681,7 +681,6 @@ impl OWFParameters for OWF256EM { pub(crate) trait TauParameters { type Tau: ArrayLength; - type TauMinus1: ArrayLength; type K0: ArrayLength; type K1: ArrayLength; type Tau0: ArrayLength; @@ -713,7 +712,6 @@ pub(crate) struct Tau128Small; impl TauParameters for Tau128Small { type Tau = U11; - type TauMinus1 = Diff; type K0 = U12; type K1 = U11; type Tau0 = U7; @@ -725,7 +723,6 @@ pub(crate) struct Tau128Fast; impl TauParameters for Tau128Fast { type Tau = U16; - type TauMinus1 = Diff; type K0 = U8; type K1 = U8; type Tau0 = U8; @@ -737,7 +734,6 @@ pub(crate) struct Tau192Small; impl TauParameters for Tau192Small { type Tau = U16; - type TauMinus1 = Diff; type K0 = U12; type K1 = U12; type Tau0 = U8; @@ -749,7 +745,6 @@ pub(crate) struct Tau192Fast; impl TauParameters for Tau192Fast { type Tau = U24; - type TauMinus1 = Diff; type K0 = U8; type K1 = U8; type Tau0 = U12; @@ -761,7 +756,6 @@ pub(crate) struct Tau256Small; impl TauParameters for Tau256Small { type Tau = U22; - type TauMinus1 = Diff; type K0 = U12; type K1 = U11; type Tau0 = U14; @@ -773,7 +767,6 @@ pub(crate) struct Tau256Fast; impl TauParameters for Tau256Fast { type Tau = U32; - type TauMinus1 = Diff; type K0 = U8; type K1 = U8; type Tau0 = U16; diff --git a/src/vole.rs b/src/vole.rs index a2cb290..9bf7525 100644 --- a/src/vole.rs +++ b/src/vole.rs @@ -1,4 +1,8 @@ -use std::iter::zip; +use std::{ + iter::zip, + marker::PhantomData, + ops::{Index, IndexMut}, +}; use generic_array::{typenum::Unsigned, ArrayLength, GenericArray}; @@ -48,8 +52,41 @@ where (r[(d % 2) * n].clone(), v) } +/// Reference to storage area in signature for all `c`s. +pub(crate) struct VoleCommitmentCRef<'a, LH>(&'a mut [u8], PhantomData); + +impl<'a, LH> Index for VoleCommitmentCRef<'a, LH> +where + LH: ArrayLength, +{ + type Output = [u8]; + + fn index(&self, index: usize) -> &Self::Output { + &self.0[index * LH::USIZE..(index + 1) * LH::USIZE] + } +} + +impl<'a, LH> IndexMut for VoleCommitmentCRef<'a, LH> +where + LH: ArrayLength, +{ + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.0[index * LH::USIZE..(index + 1) * LH::USIZE] + } +} + +impl<'a, LH> VoleCommitmentCRef<'a, LH> +where + LH: ArrayLength, +{ + pub(crate) fn new(buffer: &'a mut [u8]) -> Self { + Self(buffer, PhantomData) + } +} + #[allow(clippy::type_complexity)] pub fn volecommit( + mut c: VoleCommitmentCRef, r: &GenericArray, iv: &IV, ) -> ( @@ -64,7 +101,6 @@ pub fn volecommit( Tau::Tau, >, >, - Box, Tau::TauMinus1>>, Box>, Box>, Tau::Tau>>, ) @@ -77,7 +113,6 @@ where let mut decom = GenericArray::default_boxed(); let mut u0 = GenericArray::::default_boxed(); let mut v = GenericArray::default_boxed(); - let mut c = GenericArray::, Tau::TauMinus1>::default_boxed(); let mut hasher = VC::RO::h1_init(); for i in 0..Tau::Tau::USIZE { @@ -100,7 +135,7 @@ where } } - (hasher.finish().read_into(), decom, c, u0, v) + (hasher.finish().read_into(), decom, u0, v) } #[allow(clippy::type_complexity)] @@ -181,6 +216,24 @@ mod test { u: Vec, } + fn volecommit( + r: &GenericArray, + iv: &IV, + ) -> ( + GenericArray, + Box>, + ) + where + Tau: TauParameters, + VC: VectorCommitment, + LH: ArrayLength, + { + let mut c = vec![0; LH::USIZE * (Tau::Tau::USIZE - 1)]; + let ret = + super::volecommit::(VoleCommitmentCRef::new(c.as_mut_slice()), r, iv); + (ret.0, ret.2) + } + #[test] fn volecommit_test() { let database: Vec = read_test_data("DataVoleCommit.json"); @@ -196,7 +249,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } else { let res = volecommit::< VC, @@ -206,7 +259,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } } else if data.k0[0] == 12 { let res = volecommit::< @@ -217,7 +270,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } else { let res = volecommit::< VC, @@ -227,7 +280,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } } else if data.lambdabytes[0] == 24 { if data.u.len() == 458 { @@ -240,7 +293,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } else { let res = volecommit::< VC, @@ -250,7 +303,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } } else if data.k0[0] == 12 { let res = volecommit::< @@ -261,7 +314,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } else { let res = volecommit::< VC, @@ -271,7 +324,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } } else if data.u.len() == 566 { if data.k0[0] == 12 { @@ -283,7 +336,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } else { let res = volecommit::< VC, @@ -293,7 +346,7 @@ mod test { &GenericArray::generate(|idx| idx as u8), &IV::default() ); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } } else if data.k0[0] == 12 { let res = @@ -303,7 +356,7 @@ mod test { LH, >(&GenericArray::generate(|idx| idx as u8), &IV::default()); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } else { let res = volecommit::< @@ -312,7 +365,7 @@ mod test { LH, >(&GenericArray::generate(|idx| idx as u8), &IV::default()); assert_eq!(res.0.as_slice(), &data.hcom); - assert_eq!(res.3.as_slice(), &data.u); + assert_eq!(res.1.as_slice(), &data.u); } } }