Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush committed Jan 24, 2023
1 parent a3c1ed0 commit 0b2e079
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/commitment/blake2s/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::CommitmentScheme;
use crate::Error;
use ark_std::rand::Rng;
use blake2::Blake2s as b2s;
use blake2::Blake2s256 as b2s;
use digest::Digest;

pub struct Commitment;
Expand Down
9 changes: 4 additions & 5 deletions src/prf/blake2s/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ mod test {

use crate::prf::blake2s::{constraints::evaluate_blake2s, Blake2s as B2SPRF};
use ark_relations::r1cs::ConstraintSystem;
use blake2::VarBlake2s;
use blake2::Blake2s256;
use digest::{Digest, FixedOutput};

use super::Blake2sGadget;
use ark_r1cs_std::prelude::*;
Expand Down Expand Up @@ -483,15 +484,13 @@ mod test {
let mut rng = ark_std::test_rng();

for input_len in (0..32).chain((32..256).filter(|a| a % 8 == 0)) {
use digest::*;
let mut h = VarBlake2s::new_keyed(&[], 32);
let mut h = Blake2s256::new();

let data: Vec<u8> = (0..input_len).map(|_| rng.gen()).collect();

h.update(&data);

let mut hash_result = Vec::with_capacity(digest::VariableOutput::output_size(&h));
h.finalize_variable(|res| hash_result.extend_from_slice(res));
let hash_result = h.finalize_fixed();

let cs = ConstraintSystem::<Fr>::new_ref();

Expand Down
54 changes: 8 additions & 46 deletions src/prf/blake2s/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::Vec;
use ark_std::convert::TryFrom;
use blake2::{Blake2s as B2s, VarBlake2s};
use blake2::{Blake2s256 as B2s, Blake2sMac};
use digest::Digest;

use super::PRF;
Expand Down Expand Up @@ -31,58 +30,21 @@ impl PRF for Blake2s {

#[derive(Clone)]
pub struct Blake2sWithParameterBlock {
pub digest_length: u8,
pub key_length: u8,
pub fan_out: u8,
pub depth: u8,
pub leaf_length: u32,
pub node_offset: u32,
pub xof_digest_length: u16,
pub node_depth: u8,
pub inner_length: u8,
pub output_size: u8,
pub key_size: u8,
pub salt: [u8; 8],
pub personalization: [u8; 8],
}

impl Blake2sWithParameterBlock {
pub fn parameters(&self) -> [u32; 8] {
let mut parameters = [0; 8];
parameters[0] = u32::from_le_bytes([
self.digest_length,
self.key_length,
self.fan_out,
self.depth,
]);
parameters[1] = self.leaf_length;
parameters[2] = self.node_offset;
parameters[3] = u32::from_le_bytes([
self.xof_digest_length as u8,
(self.xof_digest_length >> 8) as u8,
self.node_depth,
self.inner_length,
]);

let salt_bytes_1 = <[u8; 4]>::try_from(&self.salt[0..4]).unwrap();
let salt_bytes_2 = <[u8; 4]>::try_from(&self.salt[4..8]).unwrap();
let personalization_bytes_1 = <[u8; 4]>::try_from(&self.personalization[0..4]).unwrap();
let personalization_bytes_2 = <[u8; 4]>::try_from(&self.personalization[4..8]).unwrap();

parameters[4] = u32::from_le_bytes(salt_bytes_1);
parameters[5] = u32::from_le_bytes(salt_bytes_2);
parameters[6] = u32::from_le_bytes(personalization_bytes_1);
parameters[7] = u32::from_le_bytes(personalization_bytes_2);

parameters
}

pub fn evaluate(&self, input: &[u8]) -> Vec<u8> {
use digest::*;
use digest::{typenum::U32, FixedOutput, Update};
let eval_time = start_timer!(|| "Blake2sWithParameterBlock::Eval");
let mut h = VarBlake2s::with_parameter_block(&self.parameters());
let mut h =
Blake2sMac::<U32>::new_with_salt_and_personal(&[], &self.salt, &self.personalization)
.unwrap();
h.update(input.as_ref());
end_timer!(eval_time);
let mut buf = Vec::with_capacity(digest::VariableOutput::output_size(&h));
h.finalize_variable(|res| buf.extend_from_slice(res));
buf
h.finalize_fixed().into_iter().collect()
}
}
2 changes: 1 addition & 1 deletion src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod test {
use ark_ec::Group;
use ark_ed_on_bls12_381::EdwardsProjective as JubJub;
use ark_std::{test_rng, vec::Vec, UniformRand};
use blake2::Blake2s;
use blake2::Blake2s256 as Blake2s;

fn sign_and_verify<S: SignatureScheme>(message: &[u8]) {
let rng = &mut test_rng();
Expand Down

0 comments on commit 0b2e079

Please sign in to comment.