Skip to content

Commit

Permalink
Merge pull request AleoNet#190 from AleoHQ/optimize-pedersen
Browse files Browse the repository at this point in the history
Optimize PedersenCRH
  • Loading branch information
howardwu authored Jun 4, 2021
2 parents e02c412 + 48a13f7 commit d8d9c80
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions algorithms/src/crh/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ use crate::{
errors::CRHError,
traits::{CRHParameters, CRH},
};
use bitvec::{order::Lsb0, view::BitView};
use snarkvm_curves::Group;
use snarkvm_fields::{ConstraintFieldError, Field, ToConstraintField};
use snarkvm_utilities::bytes_to_bits;

use rand::Rng;

#[cfg(feature = "parallel")]
use rayon::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct PedersenCRH<G: Group, S: PedersenSize> {
pub parameters: PedersenCRHParameters<G, S>,
Expand Down Expand Up @@ -75,8 +72,9 @@ impl<G: Group, S: PedersenSize> CRH for PedersenCRH<G, S> {
}

// Compute sum of h_i^{m_i} for all i.
let bits = bytes_to_bits(input).collect::<Vec<_>>();
let mapping = cfg_chunks!(bits, S::WINDOW_SIZE)
let bits = input.view_bits::<Lsb0>();
let result = bits
.chunks(S::WINDOW_SIZE)
.zip(&self.parameters.bases)
.map(|(bits, powers)| {
let mut encoded = G::zero();
Expand All @@ -86,8 +84,8 @@ impl<G: Group, S: PedersenSize> CRH for PedersenCRH<G, S> {
}
}
encoded
});
let result = cfg_reduce!(mapping, G::zero, |a, b| a + &b);
})
.fold(G::zero(), |a, b| a + &b);

Ok(result)
}
Expand Down

0 comments on commit d8d9c80

Please sign in to comment.