Skip to content

Commit

Permalink
Change API for discriminants
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Aug 9, 2024
1 parent 8011d3e commit f0e6c56
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion rust_bindings/fuzz/fuzz_targets/create_discriminant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ use libfuzzer_sys::{arbitrary::Unstructured, fuzz_target};
fuzz_target!(|data: &[u8]| {
let mut unstructured = Unstructured::new(data);
let seed: [u8; 10] = unstructured.arbitrary().unwrap();
create_discriminant::<64>(&seed);
let mut disc = [0; 64];
assert!(create_discriminant(&seed, &mut disc));
});
3 changes: 2 additions & 1 deletion rust_bindings/fuzz/fuzz_targets/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ fuzz_target!(|data: &[u8]| {
let mut default_el = [0; 100];
default_el[0] = 0x08;
let proof = prove(&genesis_challenge, &default_el, 1024, 231).unwrap();
let disc = create_discriminant::<128>(&genesis_challenge).unwrap();
let mut disc = [0; 128];
assert!(create_discriminant(&genesis_challenge, &mut disc));
let valid = verify_n_wesolowski(&disc, &default_el, &proof, 231, 0);
assert!(valid);
});
3 changes: 2 additions & 1 deletion rust_bindings/fuzz/fuzz_targets/verify_n_wesolowski.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use libfuzzer_sys::{arbitrary::Unstructured, fuzz_target};
fuzz_target!(|data: &[u8]| {
let mut unstructured = Unstructured::new(data);
let seed: [u8; 10] = unstructured.arbitrary().unwrap();
let Some(disc) = create_discriminant::<64>(&seed) else {
let mut disc = [0; 64];
if !create_discriminant(&seed, &mut disc) {
return;
};
let element: [u8; 100] = unstructured.arbitrary().unwrap();
Expand Down
18 changes: 8 additions & 10 deletions rust_bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ mod bindings {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

pub fn create_discriminant<const SIZE: usize>(seed: &[u8]) -> Option<[u8; SIZE]> {
pub fn create_discriminant(seed: &[u8], result: &mut [u8]) -> bool {
// SAFETY: The length of each individual array is passed in as to prevent buffer overflows.
// Exceptions are handled on the C++ side and None is returned if so.
unsafe {
let mut result = [0; SIZE];
if !bindings::create_discriminant_wrapper(
bindings::create_discriminant_wrapper(
seed.as_ptr(),
seed.len(),
SIZE * 8,
result.len() * 8,
result.as_mut_ptr(),
) {
return None;
}
Some(result)
)
}
}

Expand Down Expand Up @@ -93,7 +89,8 @@ mod tests {
let mut discriminants = Vec::new();

for seed in seeds {
let discriminant = create_discriminant::<64>(&seed).unwrap();
let mut discriminant = [0; 64];
assert!(create_discriminant(&seed, &mut discriminant));
discriminants.push(discriminant);
}

Expand Down Expand Up @@ -125,7 +122,8 @@ mod tests {
let mut default_el = [0; 100];
default_el[0] = 0x08;

let disc = create_discriminant::<128>(&genesis_challenge).unwrap();
let mut disc = [0; 128];
assert!(create_discriminant(&genesis_challenge, &mut disc));
let proof = prove(&genesis_challenge, &default_el, 1024, 231).unwrap();
let valid = verify_n_wesolowski(&disc, &default_el, &proof, 231, 0);
assert!(valid);
Expand Down

0 comments on commit f0e6c56

Please sign in to comment.