Skip to content

Commit

Permalink
fix: Move BigInt modulus checks to runtime in brillig (noir-lang#5374)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves noir-lang#5373

## Summary\*

Bigint modulus was checked but never written. Since changing
bigint::to_le_bytes to write a modulus ID would require a breaking
change, this fixes the modulus issue without changing the opcode spec,
by completely offloading modulus checks to the runtime itself.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
sirasistant authored Jul 11, 2024
1 parent f7c4bdb commit 741d339
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 44 deletions.
4 changes: 2 additions & 2 deletions acvm-repo/blackbox_solver/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct BigIntSolver {
}

impl BigIntSolver {
pub(crate) fn get_bigint(
pub fn get_bigint(
&self,
id: u32,
func: BlackBoxFunc,
Expand All @@ -32,7 +32,7 @@ impl BigIntSolver {
.cloned()
}

pub(crate) fn get_modulus(
pub fn get_modulus(
&self,
id: u32,
func: BlackBoxFunc,
Expand Down
8 changes: 8 additions & 0 deletions acvm-repo/brillig_vm/src/black_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ impl BrilligBigintSolver {
rhs: u32,
func: BlackBoxFunc,
) -> Result<u32, BlackBoxResolutionError> {
let modulus_lhs = self.bigint_solver.get_modulus(lhs, func)?;
let modulus_rhs = self.bigint_solver.get_modulus(rhs, func)?;
if modulus_lhs != modulus_rhs {
return Err(BlackBoxResolutionError::Failed(
func,
"moduli should be identical in BigInt operation".to_string(),
));
}
let id = self.create_bigint_id();
self.bigint_solver.bigint_op(lhs, rhs, id, func)?;
Ok(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use acvm::{
use crate::brillig::brillig_ir::{
brillig_variable::{BrilligVariable, BrilligVector, SingleAddrVariable},
debug_show::DebugToString,
BrilligBinaryOp, BrilligContext,
BrilligContext,
};

/// Transforms SSA's black box function calls into the corresponding brillig instructions
Expand Down Expand Up @@ -239,11 +239,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
BlackBoxFunc::RecursiveAggregation => {}
BlackBoxFunc::BigIntAdd => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntAdd {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -257,11 +256,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntSub => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntSub {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -275,11 +273,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntMul => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntMul {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -293,11 +290,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntDiv => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntDiv {
lhs: lhs.address,
rhs: rhs.address,
Expand Down Expand Up @@ -416,27 +412,3 @@ fn convert_array_or_vector<F: AcirField + DebugToString>(
),
}
}

fn prepare_bigint_output<F: AcirField + DebugToString>(
brillig_context: &mut BrilligContext<F>,
lhs_modulus: &SingleAddrVariable,
rhs_modulus: &SingleAddrVariable,
modulus_id: &SingleAddrVariable,
) {
// Check moduli
let condition = brillig_context.allocate_register();
let condition_adr = SingleAddrVariable { address: condition, bit_size: 1 };
brillig_context.binary_instruction(
*lhs_modulus,
*rhs_modulus,
condition_adr,
BrilligBinaryOp::Equals,
);
brillig_context.codegen_constrain(
condition_adr,
Some("moduli should be identical in BigInt operation".to_string()),
);
brillig_context.deallocate_register(condition);

brillig_context.mov_instruction(modulus_id.address, lhs_modulus.address);
}
6 changes: 1 addition & 5 deletions tooling/debugger/ignored-tests.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
bigint
brillig_references
brillig_to_bytes_integration
debug_logs
fold_after_inlined_calls
fold_basic
Expand All @@ -12,7 +10,5 @@ fold_fibonacci
fold_numeric_generic_poseidon
is_unconstrained
macros
modulus
references
regression_4709
to_bytes_integration
regression_4709

0 comments on commit 741d339

Please sign in to comment.