Skip to content

Commit

Permalink
Add hash_grow, hash_trim
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed May 6, 2024
1 parent 166b1da commit d02dbca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crypto/fcmps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub(crate) use gadgets::*;
mod circuit;
pub(crate) use circuit::*;

pub mod tree;

#[cfg(test)]
mod tests;

Expand Down
40 changes: 40 additions & 0 deletions crypto/fcmps/src/tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use transcript::Transcript;

use multiexp::multiexp_vartime;
use ciphersuite::Ciphersuite;

use generalized_bulletproofs::Generators;

/// Add children to an existing hash.
///
/// For a new hash, pass the hash initialization point as the existing hash.
pub fn hash_grow<T: Transcript, C: Ciphersuite>(
generators: &Generators<T, C>,
existing_hash: C::G,
offset: usize,
children: &[C::F],
) -> Option<C::G> {
let mut pairs = Vec::with_capacity(children.len());
for (i, child) in children.iter().enumerate() {
pairs.push((*child, *generators.g_bold_slice().get(offset + i)?));
}
Some(existing_hash + multiexp_vartime(&pairs))
}

/// Remove children from an existing hash.
///
/// This should only be called when the amount of children removed is less than the amount of
/// children remaining. If less children remain, calling `hash_grow` on a new hash with the
/// remaining children will be faster.
pub fn hash_trim<T: Transcript, C: Ciphersuite>(
generators: &Generators<T, C>,
existing_hash: C::G,
offset: usize,
children: &[C::F],
) -> Option<C::G> {
let mut pairs = Vec::with_capacity(children.len());
for (i, child) in children.iter().enumerate() {
pairs.push((*child, *generators.g_bold_slice().get(offset + i)?));
}
Some(existing_hash - multiexp_vartime(&pairs))
}

0 comments on commit d02dbca

Please sign in to comment.