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: Runtime brillig bigint id assignment #5369

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions acvm-repo/blackbox_solver/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
pub struct BigIntSolver {
bigint_id_to_value: HashMap<u32, BigUint>,
bigint_id_to_modulus: HashMap<u32, BigUint>,
last_id: usize,
}

impl BigIntSolver {
Expand Down Expand Up @@ -45,6 +46,13 @@
))
.cloned()
}

pub fn create_bigint_id(&mut self) -> u32 {
let output = self.last_id as u32;
self.last_id += 1;
output
}

pub fn bigint_from_bytes(
&mut self,
inputs: &[u8],
Expand Down Expand Up @@ -84,7 +92,7 @@
}
BlackBoxFunc::BigIntMul => lhs * rhs,
BlackBoxFunc::BigIntDiv => {
lhs * rhs.modpow(&(&modulus - BigUint::from(2_u32)), &modulus)

Check warning on line 95 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
28 changes: 18 additions & 10 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 @@ -270,38 +270,46 @@
BlackBoxOp::BigIntAdd { lhs, rhs, output } => {
let lhs = memory.read(*lhs).try_into().unwrap();
let rhs = memory.read(*rhs).try_into().unwrap();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_op(lhs, rhs, output, BlackBoxFunc::BigIntAdd)?;
let new_id = bigint_solver.create_bigint_id();
bigint_solver.bigint_op(lhs, rhs, new_id, BlackBoxFunc::BigIntAdd)?;
memory.write(*output, new_id.into());
Ok(())
}
BlackBoxOp::BigIntSub { lhs, rhs, output } => {
let lhs = memory.read(*lhs).try_into().unwrap();
let rhs = memory.read(*rhs).try_into().unwrap();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_op(lhs, rhs, output, BlackBoxFunc::BigIntSub)?;
let new_id = bigint_solver.create_bigint_id();
bigint_solver.bigint_op(lhs, rhs, new_id, BlackBoxFunc::BigIntSub)?;
memory.write(*output, new_id.into());
Ok(())
}
BlackBoxOp::BigIntMul { lhs, rhs, output } => {
let lhs = memory.read(*lhs).try_into().unwrap();
let rhs = memory.read(*rhs).try_into().unwrap();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_op(lhs, rhs, output, BlackBoxFunc::BigIntMul)?;
let new_id = bigint_solver.create_bigint_id();
bigint_solver.bigint_op(lhs, rhs, new_id, BlackBoxFunc::BigIntMul)?;
memory.write(*output, new_id.into());
Ok(())
}
BlackBoxOp::BigIntDiv { lhs, rhs, output } => {
let lhs = memory.read(*lhs).try_into().unwrap();
let rhs = memory.read(*rhs).try_into().unwrap();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_op(lhs, rhs, output, BlackBoxFunc::BigIntDiv)?;
let new_id = bigint_solver.create_bigint_id();
bigint_solver.bigint_op(lhs, rhs, new_id, BlackBoxFunc::BigIntDiv)?;
memory.write(*output, new_id.into());
Ok(())
}
BlackBoxOp::BigIntFromLeBytes { inputs, modulus, output } => {
let input = read_heap_vector(memory, inputs);
let input: Vec<u8> = input.iter().map(|x| x.try_into().unwrap()).collect();
let modulus = read_heap_vector(memory, modulus);
let modulus: Vec<u8> = modulus.iter().map(|x| x.try_into().unwrap()).collect();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_from_bytes(&input, &modulus, output)?;
let next_id = bigint_solver.create_bigint_id();
bigint_solver.bigint_from_bytes(&input, &modulus, next_id)?;
println!("Built bigint from bytes: {:?} {:?} with id {}", input, modulus, next_id);
sirasistant marked this conversation as resolved.
Show resolved Hide resolved

memory.write(*output, next_id.into());

Ok(())
}
BlackBoxOp::BigIntToLeBytes { input, output } => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(
brillig_context,
lhs_modulus,
rhs_modulus,
output,
modulus_id,
);
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 @@ -267,13 +261,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(
brillig_context,
lhs_modulus,
rhs_modulus,
output,
modulus_id,
);
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 @@ -291,13 +279,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(
brillig_context,
lhs_modulus,
rhs_modulus,
output,
modulus_id,
);
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 @@ -315,13 +297,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(
brillig_context,
lhs_modulus,
rhs_modulus,
output,
modulus_id,
);
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 All @@ -341,8 +317,6 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
{
let inputs_vector = convert_array_or_vector(brillig_context, inputs, bb_func);
let modulus_vector = convert_array_or_vector(brillig_context, modulus, bb_func);
let output_id = brillig_context.get_new_bigint_id();
brillig_context.const_instruction(*output, F::from(output_id as u128));
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntFromLeBytes {
inputs: inputs_vector.to_heap_vector(),
modulus: modulus_vector.to_heap_vector(),
Expand Down Expand Up @@ -447,7 +421,6 @@ fn prepare_bigint_output<F: AcirField + DebugToString>(
brillig_context: &mut BrilligContext<F>,
lhs_modulus: &SingleAddrVariable,
rhs_modulus: &SingleAddrVariable,
output: &SingleAddrVariable,
modulus_id: &SingleAddrVariable,
) {
// Check moduli
Expand All @@ -464,8 +437,6 @@ fn prepare_bigint_output<F: AcirField + DebugToString>(
Some("moduli should be identical in BigInt operation".to_string()),
);
brillig_context.deallocate_register(condition);
// Set output id
let output_id = brillig_context.get_new_bigint_id();
brillig_context.const_instruction(*output, F::from(output_id as u128));

brillig_context.mov_instruction(modulus_id.address, lhs_modulus.address);
}
8 changes: 0 additions & 8 deletions compiler/noirc_evaluator/src/brillig/brillig_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ pub(crate) struct BrilligContext<F> {
next_section: usize,
/// IR printer
debug_show: DebugShow,
/// Counter for generating bigint ids in unconstrained functions
bigint_new_id: u32,
}

impl<F: AcirField + DebugToString> BrilligContext<F> {
Expand All @@ -105,15 +103,9 @@ impl<F: AcirField + DebugToString> BrilligContext<F> {
section_label: 0,
next_section: 1,
debug_show: DebugShow::new(enable_debug_trace),
bigint_new_id: 0,
}
}

pub(crate) fn get_new_bigint_id(&mut self) -> u32 {
let result = self.bigint_new_id;
self.bigint_new_id += 1;
result
}
/// Adds a brillig instruction to the brillig byte code
fn push_opcode(&mut self, opcode: BrilligOpcode<F>) {
self.obj.push_opcode(opcode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
section_label: 0,
next_section: 1,
debug_show: DebugShow::new(false),
bigint_new_id: 0,
};

context.codegen_entry_point(&arguments, &return_parameters);
Expand Down Expand Up @@ -153,7 +152,7 @@
}

for (i, bit_size) in arguments.iter().flat_map(flat_bit_sizes).enumerate() {
// Calldatacopy tags everything with field type, so when downcast when necessary

Check warning on line 155 in compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Calldatacopy)
if bit_size < F::max_num_bits() {
self.cast_instruction(
SingleAddrVariable::new(MemoryAddress(MAX_STACK_SIZE + i), bit_size),
Expand Down
Loading