Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush authored Jan 9, 2024
2 parents 99cca29 + 2ee4fd6 commit 0b9c04a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Pending

- [\#120](https://github.com/arkworks-rs/crypto-primitives/pull/120) Add input size check to `bowe_hopwood::CRHGadget::evaluate`.

### Breaking changes

### Features
Expand Down
39 changes: 39 additions & 0 deletions src/crh/bowe_hopwood/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ where
parameters: &Self::ParametersVar,
input: &Self::InputVar,
) -> Result<Self::OutputVar, SynthesisError> {
if (input.len() * 8) > W::WINDOW_SIZE * W::NUM_WINDOWS * CHUNK_SIZE {
panic!(
"incorrect input bitlength {:?} for window params {:?}x{:?}x{}",
input.len() * 8,
W::WINDOW_SIZE,
W::NUM_WINDOWS,
CHUNK_SIZE,
);
}

// Pad the input if it is not the current length.
let mut input_in_bits: Vec<Boolean<_>> = input
.iter()
Expand Down Expand Up @@ -267,4 +277,33 @@ mod test {
assert_eq!(primitive_result, result_var.value().unwrap());
assert!(cs.is_satisfied().unwrap());
}

#[should_panic]
#[test]
fn test_input_size_check() {
// Pick parameters that are far too small for a CRH
#[derive(Clone, PartialEq, Eq, Hash)]
pub(super) struct TooSmallWindow;
impl pedersen::Window for TooSmallWindow {
const WINDOW_SIZE: usize = 1;
const NUM_WINDOWS: usize = 1;
}
type TestCRH = bowe_hopwood::CRH<EdwardsConfig, TooSmallWindow>;
type TestCRHGadget = bowe_hopwood::constraints::CRHGadget<EdwardsConfig, FqVar>;

let rng = &mut test_rng();
let cs = ConstraintSystem::<Fr>::new_ref();

let (_, input_var) = generate_u8_input(cs.clone(), 189, rng);
println!("number of constraints for input: {}", cs.num_constraints());

let parameters = TestCRH::setup(rng).unwrap();
let parameters_var =
<TestCRHGadget as CRHSchemeGadget<TestCRH, Fr>>::ParametersVar::new_witness(
ark_relations::ns!(cs, "parameters_var"),
|| Ok(&parameters),
)
.unwrap();
let _ = TestCRHGadget::evaluate(&parameters_var, &input_var).unwrap();
}
}
4 changes: 2 additions & 2 deletions src/crh/bowe_hopwood/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ impl<P: TECurveConfig, W: pedersen::Window> CRHScheme for CRH<P, W> {

if (input.len() * 8) > W::WINDOW_SIZE * W::NUM_WINDOWS * CHUNK_SIZE {
panic!(
"incorrect input length {:?} for window params {:?}x{:?}x{}",
input.len(),
"incorrect input bitlength {:?} for window params {:?}x{:?}x{}",
input.len() * 8,
W::WINDOW_SIZE,
W::NUM_WINDOWS,
CHUNK_SIZE,
Expand Down

0 comments on commit 0b9c04a

Please sign in to comment.