Skip to content

Commit

Permalink
Directly store cs from volecommit into signature and avoid heap alloc…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
sebastinas committed Nov 9, 2024
1 parent ee411e8 commit 29a638c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 46 deletions.
38 changes: 15 additions & 23 deletions src/faest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
universal_hashing::{VoleHasherInit, VoleHasherProcess},
utils::Reader,
vc::VectorCommitment,
vole::{volecommit, volereconstruct},
vole::{volecommit, volereconstruct, VoleCommitmentCRef},
ByteEncoding, Error,
};

Expand Down Expand Up @@ -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<I, T>(chall1: &mut [u8], mu: &[u8], hcom: &[u8], c: I, iv: &[u8])
where
I: Iterator<Item = T>,
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
Expand Down Expand Up @@ -341,17 +338,11 @@ where
h3_reader.read(iv);
}

fn hash_challenge_1<I, T>(chall1: &mut [u8], mu: &[u8], hcom: &[u8], c: I, iv: &[u8])
where
I: Iterator<Item = T>,
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);
}
Expand Down Expand Up @@ -413,18 +404,19 @@ fn sign<P, O>(
let mut iv = IV::default();
RO::<P>::hash_r_iv(&mut r, &mut iv, &sk.owf_key, &mu, rho);

let (hcom, decom, c, u, gv) =
volecommit::<<O::BaseParams as BaseParameters>::VC, P::Tau, O::LHATBYTES>(&r, &iv);
let volecommit_cs =
&mut signature[..O::LHATBYTES::USIZE * (<P::Tau as TauParameters>::Tau::USIZE - 1)];
let (hcom, decom, u, gv) = volecommit::<
<O::BaseParams as BaseParameters>::VC,
P::Tau,
O::LHATBYTES,
>(VoleCommitmentCRef::new(volecommit_cs), &r, &iv);
let mut chall1 =
GenericArray::<u8, <<O as OWFParameters>::BaseParams as BaseParameters>::Chall1>::default();
RO::<P>::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::<P>::hash_challenge_1(&mut chall1, &mu, &hcom, volecommit_cs, &iv);

let signature =
&mut signature[O::LHATBYTES::USIZE * (<P::Tau as TauParameters>::Tau::USIZE - 1)..];
let (u_t, hv) = {
let vole_hasher = VoleHasher::<P>::new_vole_hasher(&chall1);
let u_t = vole_hasher.process(&u);
Expand Down Expand Up @@ -542,7 +534,7 @@ where
let mut chall1 =
GenericArray::<u8, <<O as OWFParameters>::BaseParams as BaseParameters>::Chall1>::default();
let c = &sigma[..O::LHATBYTES::USIZE * (<P::Tau as TauParameters>::Tau::USIZE - 1)];
RO::<P>::hash_challenge_1(&mut chall1, &mu, &hcom, [c].into_iter(), iv);
RO::<P>::hash_challenge_1(&mut chall1, &mu, &hcom, c, iv);

let vole_hasher = VoleHasher::<P>::new_vole_hasher(&chall1);
let def = GenericArray::default();
Expand Down
7 changes: 0 additions & 7 deletions src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -713,7 +712,6 @@ pub(crate) struct Tau128Small;

impl TauParameters for Tau128Small {
type Tau = U11;
type TauMinus1 = Diff<Self::Tau, U1>;
type K0 = U12;
type K1 = U11;
type Tau0 = U7;
Expand All @@ -725,7 +723,6 @@ pub(crate) struct Tau128Fast;

impl TauParameters for Tau128Fast {
type Tau = U16;
type TauMinus1 = Diff<Self::Tau, U1>;
type K0 = U8;
type K1 = U8;
type Tau0 = U8;
Expand All @@ -737,7 +734,6 @@ pub(crate) struct Tau192Small;

impl TauParameters for Tau192Small {
type Tau = U16;
type TauMinus1 = Diff<Self::Tau, U1>;
type K0 = U12;
type K1 = U12;
type Tau0 = U8;
Expand All @@ -749,7 +745,6 @@ pub(crate) struct Tau192Fast;

impl TauParameters for Tau192Fast {
type Tau = U24;
type TauMinus1 = Diff<Self::Tau, U1>;
type K0 = U8;
type K1 = U8;
type Tau0 = U12;
Expand All @@ -761,7 +756,6 @@ pub(crate) struct Tau256Small;

impl TauParameters for Tau256Small {
type Tau = U22;
type TauMinus1 = Diff<Self::Tau, U1>;
type K0 = U12;
type K1 = U11;
type Tau0 = U14;
Expand All @@ -773,7 +767,6 @@ pub(crate) struct Tau256Fast;

impl TauParameters for Tau256Fast {
type Tau = U32;
type TauMinus1 = Diff<Self::Tau, U1>;
type K0 = U8;
type K1 = U8;
type Tau0 = U16;
Expand Down
85 changes: 69 additions & 16 deletions src/vole.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::iter::zip;
use std::{
iter::zip,
marker::PhantomData,
ops::{Index, IndexMut},
};

use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};

Expand Down Expand Up @@ -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<LH>);

impl<'a, LH> Index<usize> 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<usize> 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<VC, Tau, LH>(
mut c: VoleCommitmentCRef<LH>,
r: &GenericArray<u8, VC::Lambda>,
iv: &IV,
) -> (
Expand All @@ -64,7 +101,6 @@ pub fn volecommit<VC, Tau, LH>(
Tau::Tau,
>,
>,
Box<GenericArray<GenericArray<u8, LH>, Tau::TauMinus1>>,
Box<GenericArray<u8, LH>>,
Box<GenericArray<Vec<GenericArray<u8, LH>>, Tau::Tau>>,
)
Expand All @@ -77,7 +113,6 @@ where
let mut decom = GenericArray::default_boxed();
let mut u0 = GenericArray::<u8, LH>::default_boxed();
let mut v = GenericArray::default_boxed();
let mut c = GenericArray::<GenericArray<u8, LH>, Tau::TauMinus1>::default_boxed();

let mut hasher = VC::RO::h1_init();
for i in 0..Tau::Tau::USIZE {
Expand All @@ -100,7 +135,7 @@ where
}
}

(hasher.finish().read_into(), decom, c, u0, v)
(hasher.finish().read_into(), decom, u0, v)
}

#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -181,6 +216,24 @@ mod test {
u: Vec<u8>,
}

fn volecommit<VC, Tau, LH>(
r: &GenericArray<u8, VC::Lambda>,
iv: &IV,
) -> (
GenericArray<u8, VC::LambdaTimes2>,
Box<GenericArray<u8, LH>>,
)
where
Tau: TauParameters,
VC: VectorCommitment,
LH: ArrayLength,
{
let mut c = vec![0; LH::USIZE * (Tau::Tau::USIZE - 1)];
let ret =
super::volecommit::<VC, Tau, LH>(VoleCommitmentCRef::new(c.as_mut_slice()), r, iv);
(ret.0, ret.2)
}

#[test]
fn volecommit_test() {
let database: Vec<DataVoleCommit> = read_test_data("DataVoleCommit.json");
Expand All @@ -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<FAEST128fParameters>,
Expand All @@ -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::<
Expand All @@ -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<FAESTEM128fParameters>,
Expand All @@ -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 {
Expand All @@ -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<FAEST192fParameters>,
Expand All @@ -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::<
Expand All @@ -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<FAESTEM192fParameters>,
Expand All @@ -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 {
Expand All @@ -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<FAEST256fParameters>,
Expand All @@ -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 =
Expand All @@ -303,7 +356,7 @@ mod test {
LH<FAESTEM256sParameters>,
>(&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::<
Expand All @@ -312,7 +365,7 @@ mod test {
LH<FAESTEM256fParameters>,
>(&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);
}
}
}
Expand Down

0 comments on commit 29a638c

Please sign in to comment.