Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added size check to Bowe-Hopwood gadget evaluate method #120

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading