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

fix: Move BigInt modulus checks to runtime in brillig #5374

Merged
merged 8 commits into from
Jul 11, 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
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 @@
}

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

pub(crate) fn get_modulus(
pub fn get_modulus(
&self,
id: u32,
func: BlackBoxFunc,
Expand Down Expand Up @@ -84,7 +84,7 @@
}
BlackBoxFunc::BigIntMul => lhs * rhs,
BlackBoxFunc::BigIntDiv => {
lhs * rhs.modpow(&(&modulus - BigUint::from(2_u32)), &modulus)

Check warning on line 87 in acvm-repo/blackbox_solver/src/bigint.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (modpow)
} //TODO ensure that modulus is prime
_ => unreachable!("ICE - bigint_op must be called for an operation"),
};
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 @@ -57,10 +57,10 @@
to_u8_vec(read_heap_array(memory, key)).try_into().map_err(|_| {
BlackBoxResolutionError::Failed(bb_func, "Invalid ley length".to_string())
})?;
let ciphertext = aes128_encrypt(&inputs, iv, key)?;

Check warning on line 60 in acvm-repo/brillig_vm/src/black_box.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (ciphertext)

memory.write(outputs.size, ciphertext.len().into());

Check warning on line 62 in acvm-repo/brillig_vm/src/black_box.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (ciphertext)
memory.write_slice(memory.read_ref(outputs.pointer), &to_value_vec(&ciphertext));

Check warning on line 63 in acvm-repo/brillig_vm/src/black_box.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (ciphertext)

Ok(())
}
Expand Down Expand Up @@ -421,6 +421,14 @@
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
Loading