Skip to content

Commit

Permalink
Default insead of Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
¨Jeff committed Jun 11, 2023
1 parent e9ce287 commit 23f1772
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ff/src/fields/field_hashers/expander/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ impl<H: ExtendableOutput + Clone + Default> Expander for ExpanderXof<H> {
}
}

pub(super) struct ExpanderXmd<T: DynDigest + Clone> {
pub(super) hasher: T,
pub(super) struct ExpanderXmd<H: DynDigest + Default + Clone> {
pub(super) hasher: H,
pub(super) dst: Vec<u8>,
pub(super) block_size: usize,
}

static Z_PAD: [u8; 256] = [0u8; 256];

impl<T: DynDigest + Clone> ExpanderXmd<T> {
impl<H: DynDigest + Default + Clone> ExpanderXmd<H> {
fn construct_dst_prime(&self) -> Vec<u8> {
let mut dst_prime = if self.dst.len() > MAX_DST_LENGTH {
let mut hasher = self.hasher.clone();
Expand All @@ -100,7 +100,7 @@ impl<T: DynDigest + Clone> ExpanderXmd<T> {
}
}

impl<T: DynDigest + Clone> Expander for ExpanderXmd<T> {
impl<H: DynDigest + Default + Clone> Expander for ExpanderXmd<H> {
fn expand(&self, msg: &[u8], n: usize) -> Vec<u8> {
let mut hasher = self.hasher.clone();
// output size of the hash function, e.g. 32 bytes = 256 bits for sha2::Sha256
Expand All @@ -118,13 +118,15 @@ impl<T: DynDigest + Clone> Expander for ExpanderXmd<T> {
assert!(n < (1 << 16), "Length should be smaller than 2^16");
let lib_str: [u8; 2] = (n as u16).to_be_bytes();

let mut hasher = H::default();
hasher.update(&Z_PAD[0..self.block_size]);
hasher.update(msg);
hasher.update(&lib_str);
hasher.update(&[0u8]);
hasher.update(&dst_prime);
let b0 = hasher.finalize_reset();

let mut hasher = H::default();
hasher.update(&b0);
hasher.update(&[1u8]);
hasher.update(&dst_prime);
Expand All @@ -133,6 +135,7 @@ impl<T: DynDigest + Clone> Expander for ExpanderXmd<T> {
let mut uniform_bytes: Vec<u8> = Vec::with_capacity(n);
uniform_bytes.extend_from_slice(&bi);
for i in 2..=ell {
let mut hasher = H::default();
// update the hasher with xor of b_0 and b_i elements
for (l, r) in b0.iter().zip(bi.iter()) {
hasher.update(&[*l ^ *r]);
Expand Down

0 comments on commit 23f1772

Please sign in to comment.