-
Notifications
You must be signed in to change notification settings - Fork 382
feat: build simple dictionary from inspecting ACIR program #5264
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8d9a380
feat: add fuzzer for Noir programs
TomAFrench b37f324
chore: restructure code and improve integer strategies
TomAFrench c398302
feat: hook fuzzing up to test functions
TomAFrench a26edb3
chore: restrict usage of proptest in `noirc_abi`
TomAFrench 8454474
chore: restrict visibility
TomAFrench 4d06fa0
Merge branch 'master' into tf/fuzzer-mvp
TomAFrench fbb97ae
chore: fuzz stdlib
TomAFrench c36130e
chore: use `proptest`'s value tree implementations
TomAFrench cb2a885
chore: allow test functions with args in elaborator
TomAFrench c5ffa8f
chore: revert changes to `noirc_abi`
TomAFrench 8913296
chore: nit
TomAFrench 7d4e1cc
chore: delete commented code
TomAFrench 0dea198
chore: nits
TomAFrench 4488450
chore: remove explicit counterexample tracking
TomAFrench b065e27
feat: build simple dictionary from inspecting ACIR program
TomAFrench 438f35e
Merge branch 'master' into tf/build-dictionary
TomAFrench 7bae217
chore: avoid panics in bitshifts when building dictionary
TomAFrench 71bbcbb
Update tooling/fuzzer/src/strategies/uint.rs
TomAFrench 030e1cb
Update test_programs/noir_test_success/fuzzer_checks/src/main.nr
TomAFrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [package] | ||
| name = "fuzzer_checks" | ||
| type = "bin" | ||
| authors = [""] | ||
| [dependencies] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
|
|
||
| #[test(should_fail_with = "42 is not allowed")] | ||
| fn finds_magic_value(x: u32) { | ||
| let x = x as u64; | ||
| assert(2 * x != 42, "42 is not allowed"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| //! This module defines how to build a dictionary of values which are likely to be correspond | ||
| //! to significant inputs during fuzzing by inspecting the [Program] being fuzzed. | ||
| //! | ||
| //! This dictionary can be fed into the fuzzer's [strategy][proptest::strategy::Strategy] in order to bias it towards | ||
| //! generating these values to ensure they get proper coverage. | ||
| use std::collections::HashSet; | ||
|
|
||
| use acvm::{ | ||
| acir::{ | ||
| circuit::{ | ||
| brillig::{BrilligBytecode, BrilligInputs}, | ||
| directives::Directive, | ||
| opcodes::{BlackBoxFuncCall, FunctionInput}, | ||
| Circuit, Opcode, Program, | ||
| }, | ||
| native_types::Expression, | ||
| }, | ||
| brillig_vm::brillig::Opcode as BrilligOpcode, | ||
| AcirField, | ||
| }; | ||
|
|
||
| /// Constructs a [HashSet<F>] of values pulled from a [Program<F>] which are likely to be correspond | ||
| /// to significant inputs during fuzzing. | ||
| pub(super) fn build_dictionary_from_program<F: AcirField>(program: &Program<F>) -> HashSet<F> { | ||
| let constrained_dictionaries = program.functions.iter().map(build_dictionary_from_circuit); | ||
| let unconstrained_dictionaries = | ||
| program.unconstrained_functions.iter().map(build_dictionary_from_unconstrained_function); | ||
| let dictionaries = constrained_dictionaries.chain(unconstrained_dictionaries); | ||
|
|
||
| let mut constants: HashSet<F> = HashSet::new(); | ||
| for dictionary in dictionaries { | ||
| constants.extend(dictionary); | ||
| } | ||
| constants | ||
| } | ||
|
|
||
| fn build_dictionary_from_circuit<F: AcirField>(circuit: &Circuit<F>) -> HashSet<F> { | ||
| let mut constants: HashSet<F> = HashSet::new(); | ||
|
|
||
| fn insert_expr<F: AcirField>(dictionary: &mut HashSet<F>, expr: &Expression<F>) { | ||
| let quad_coefficients = expr.mul_terms.iter().map(|(k, _, _)| *k); | ||
| let linear_coefficients = expr.linear_combinations.iter().map(|(k, _)| *k); | ||
| let coefficients = linear_coefficients.chain(quad_coefficients); | ||
|
|
||
| dictionary.extend(coefficients.clone()); | ||
| dictionary.insert(expr.q_c); | ||
|
|
||
| // We divide the constant term by any coefficients in the expression to aid solving constraints such as `2 * x - 4 == 0`. | ||
| let scaled_constants = coefficients.map(|coefficient| expr.q_c / coefficient); | ||
| dictionary.extend(scaled_constants); | ||
| } | ||
|
|
||
| fn insert_array_len<F: AcirField, T>(dictionary: &mut HashSet<F>, array: &[T]) { | ||
| let array_length = array.len() as u128; | ||
| dictionary.insert(F::from(array_length)); | ||
| dictionary.insert(F::from(array_length - 1)); | ||
| } | ||
|
|
||
| for opcode in &circuit.opcodes { | ||
| match opcode { | ||
| Opcode::AssertZero(expr) | ||
| | Opcode::Call { predicate: Some(expr), .. } | ||
| | Opcode::MemoryOp { predicate: Some(expr), .. } | ||
| | Opcode::Directive(Directive::ToLeRadix { a: expr, .. }) => { | ||
| insert_expr(&mut constants, expr) | ||
| } | ||
|
|
||
| Opcode::MemoryInit { init, .. } => insert_array_len(&mut constants, init), | ||
|
|
||
| Opcode::BrilligCall { inputs, predicate, .. } => { | ||
| for input in inputs { | ||
| match input { | ||
| BrilligInputs::Single(expr) => insert_expr(&mut constants, expr), | ||
| BrilligInputs::Array(exprs) => { | ||
| exprs.iter().for_each(|expr| insert_expr(&mut constants, expr)); | ||
| insert_array_len(&mut constants, exprs); | ||
| } | ||
| BrilligInputs::MemoryArray(_) => (), | ||
| } | ||
| } | ||
| if let Some(predicate) = predicate { | ||
| insert_expr(&mut constants, predicate) | ||
| } | ||
| } | ||
|
|
||
| Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE { | ||
| input: FunctionInput { num_bits, .. }, | ||
| }) => { | ||
| let field = 1u128.wrapping_shl(*num_bits); | ||
| constants.insert(F::from(field)); | ||
| constants.insert(F::from(field - 1)); | ||
| } | ||
| _ => (), | ||
| } | ||
| } | ||
|
|
||
| constants | ||
| } | ||
|
|
||
| fn build_dictionary_from_unconstrained_function<F: AcirField>( | ||
| function: &BrilligBytecode<F>, | ||
| ) -> HashSet<F> { | ||
| let mut constants: HashSet<F> = HashSet::new(); | ||
|
|
||
| for opcode in &function.bytecode { | ||
| match opcode { | ||
| BrilligOpcode::Cast { bit_size, .. } => { | ||
| let field = 1u128.wrapping_shl(*bit_size); | ||
| constants.insert(F::from(field)); | ||
| constants.insert(F::from(field - 1)); | ||
| } | ||
| BrilligOpcode::Const { bit_size, value, .. } => { | ||
| constants.insert(*value); | ||
|
|
||
| let field = 1u128.wrapping_shl(*bit_size); | ||
| constants.insert(F::from(field)); | ||
| constants.insert(F::from(field - 1)); | ||
| } | ||
| _ => (), | ||
| } | ||
| } | ||
|
|
||
| constants | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.