Skip to content

Commit

Permalink
feat: Sync from aztec-packages (#4657)
Browse files Browse the repository at this point in the history
Automated pull of Noir development from
[aztec-packages](https://github.com/AztecProtocol/aztec-packages).
BEGIN_COMMIT_OVERRIDE
chore: Dont double check num bits in brillig vm
(AztecProtocol/aztec-packages#5489)
chore: unify noir macros flow
(AztecProtocol/aztec-packages#5461)
feat!: Brillig typed memory
(AztecProtocol/aztec-packages#5395)
chore: add AvmContextInputs
(AztecProtocol/aztec-packages#5396)
END_COMMIT_OVERRIDE

---------

Co-authored-by: sirasistant <[email protected]>
  • Loading branch information
AztecBot and sirasistant authored Mar 27, 2024
1 parent b87654e commit 10148c5
Show file tree
Hide file tree
Showing 46 changed files with 1,886 additions and 1,617 deletions.
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
208abbb63af4c9a3f25d723fe1c49e82aa461061
a18288d9b8f3057b9e79362d922da656dacf22a9
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,245 changes: 600 additions & 645 deletions acvm-repo/acir/codegen/acir.cpp

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions acvm-repo/acir/tests/test_program_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ fn simple_brillig_foreign_call() {
brillig::Opcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
destination_value_types: vec![HeapValueType::Simple],
destination_value_types: vec![HeapValueType::field()],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
input_value_types: vec![HeapValueType::field()],
},
brillig::Opcode::Stop { return_data_offset: 0, return_data_size: 1 },
],
Expand All @@ -211,11 +211,11 @@ fn simple_brillig_foreign_call() {
let bytes = Program::serialize_program(&program);

let expected_serialization: Vec<u8> = vec![
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 173, 143, 65, 10, 192, 32, 12, 4, 77, 10, 165, 244, 214,
159, 216, 31, 244, 51, 61, 120, 241, 32, 226, 251, 85, 140, 176, 136, 122, 209, 129, 144,
176, 9, 97, 151, 84, 225, 74, 69, 50, 31, 48, 35, 85, 251, 164, 235, 53, 94, 218, 247, 75,
163, 95, 150, 12, 153, 179, 227, 191, 114, 195, 222, 216, 240, 59, 63, 75, 221, 251, 208,
106, 207, 232, 150, 65, 100, 53, 33, 2, 22, 232, 178, 27, 144, 1, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 173, 144, 61, 10, 192, 48, 8, 133, 53, 133, 82, 186,
245, 38, 233, 13, 122, 153, 14, 93, 58, 132, 144, 227, 135, 252, 41, 56, 36, 46, 201, 7,
162, 168, 200, 123, 34, 52, 142, 28, 72, 245, 38, 106, 9, 247, 30, 202, 118, 142, 27, 215,
221, 178, 82, 175, 33, 15, 133, 189, 163, 159, 57, 197, 252, 251, 195, 235, 188, 230, 186,
16, 65, 255, 12, 239, 92, 131, 89, 149, 198, 77, 3, 10, 9, 119, 8, 198, 242, 152, 1, 0, 0,
];

assert_eq!(bytes, expected_serialization)
Expand Down Expand Up @@ -264,8 +264,8 @@ fn complex_brillig_foreign_call() {
},
brillig::Opcode::Const {
destination: MemoryAddress(0),
value: brillig::Value::from(32_usize),
bit_size: 32,
value: FieldElement::from(32_usize),
bit_size: 64,
},
brillig::Opcode::CalldataCopy {
destination_address: MemoryAddress(1),
Expand All @@ -280,18 +280,18 @@ fn complex_brillig_foreign_call() {
ValueOrArray::MemoryAddress(MemoryAddress::from(1)),
],
input_value_types: vec![
HeapValueType::Array { size: 3, value_types: vec![HeapValueType::Simple] },
HeapValueType::Simple,
HeapValueType::Array { size: 3, value_types: vec![HeapValueType::field()] },
HeapValueType::field(),
],
destinations: vec![
ValueOrArray::HeapArray(HeapArray { pointer: 0.into(), size: 3 }),
ValueOrArray::MemoryAddress(MemoryAddress::from(35)),
ValueOrArray::MemoryAddress(MemoryAddress::from(36)),
],
destination_value_types: vec![
HeapValueType::Array { size: 3, value_types: vec![HeapValueType::Simple] },
HeapValueType::Simple,
HeapValueType::Simple,
HeapValueType::Array { size: 3, value_types: vec![HeapValueType::field()] },
HeapValueType::field(),
HeapValueType::field(),
],
},
brillig::Opcode::Stop { return_data_offset: 32, return_data_size: 5 },
Expand All @@ -311,15 +311,15 @@ fn complex_brillig_foreign_call() {
let bytes = Program::serialize_program(&program);

let expected_serialization: Vec<u8> = vec![
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 84, 65, 14, 132, 32, 12, 108, 101, 117, 205, 222,
246, 7, 38, 187, 15, 96, 247, 5, 254, 197, 120, 211, 232, 209, 231, 139, 113, 136, 181, 65,
47, 98, 162, 147, 52, 20, 24, 202, 164, 45, 48, 205, 200, 157, 49, 124, 227, 44, 129, 207,
152, 75, 120, 94, 137, 209, 30, 195, 143, 227, 197, 178, 103, 105, 76, 110, 160, 209, 156,
160, 209, 247, 195, 69, 235, 29, 179, 46, 81, 243, 103, 2, 239, 231, 225, 44, 117, 150, 97,
254, 196, 152, 99, 157, 176, 87, 168, 188, 147, 224, 121, 20, 209, 180, 254, 109, 70, 75,
47, 178, 186, 251, 37, 116, 86, 93, 219, 55, 245, 96, 20, 85, 75, 253, 8, 255, 171, 246,
121, 231, 220, 4, 249, 237, 132, 56, 28, 224, 109, 113, 223, 180, 164, 50, 165, 0, 137, 17,
72, 139, 88, 97, 4, 173, 98, 132, 157, 33, 5, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 84, 93, 10, 131, 48, 12, 78, 218, 233, 100, 111,
187, 193, 96, 59, 64, 231, 9, 188, 139, 248, 166, 232, 163, 167, 23, 11, 126, 197, 24, 250,
34, 86, 208, 64, 72, 218, 252, 125, 36, 105, 153, 22, 42, 60, 51, 116, 235, 217, 64, 103,
156, 37, 5, 191, 10, 210, 29, 163, 63, 167, 203, 229, 206, 194, 104, 110, 128, 209, 158,
128, 49, 236, 195, 69, 231, 157, 114, 46, 73, 251, 103, 35, 239, 231, 225, 57, 243, 156,
227, 252, 132, 44, 112, 79, 176, 125, 84, 223, 73, 248, 145, 152, 69, 149, 4, 107, 233,
114, 90, 119, 145, 85, 237, 151, 192, 89, 247, 221, 208, 54, 163, 85, 174, 26, 234, 87,
232, 63, 101, 103, 21, 55, 169, 216, 73, 72, 249, 5, 197, 234, 132, 123, 179, 35, 247, 155,
214, 246, 102, 20, 73, 204, 72, 168, 123, 191, 161, 25, 66, 136, 159, 187, 53, 5, 0, 0,
];

assert_eq!(bytes, expected_serialization)
Expand Down
20 changes: 10 additions & 10 deletions acvm-repo/acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use acir::{
brillig::{ForeignCallParam, ForeignCallResult, Value},
brillig::{ForeignCallParam, ForeignCallResult},
circuit::{
brillig::{Brillig, BrilligInputs, BrilligOutputs},
opcodes::BlockId,
Expand All @@ -11,7 +11,7 @@ use acir::{
FieldElement,
};
use acvm_blackbox_solver::BlackBoxFunctionSolver;
use brillig_vm::{VMStatus, VM};
use brillig_vm::{MemoryValue, VMStatus, VM};

use crate::{pwg::OpcodeNotSolvable, OpcodeResolutionError};

Expand Down Expand Up @@ -73,15 +73,15 @@ impl<'b, B: BlackBoxFunctionSolver> BrilligSolver<'b, B> {
acir_index: usize,
) -> Result<Self, OpcodeResolutionError> {
// Set input values
let mut calldata: Vec<Value> = Vec::new();
let mut calldata: Vec<FieldElement> = Vec::new();
// Each input represents an expression or array of expressions to evaluate.
// Iterate over each input and evaluate the expression(s) associated with it.
// Push the results into memory.
// If a certain expression is not solvable, we stall the ACVM and do not proceed with Brillig VM execution.
for input in &brillig.inputs {
match input {
BrilligInputs::Single(expr) => match get_value(expr, initial_witness) {
Ok(value) => calldata.push(value.into()),
Ok(value) => calldata.push(value),
Err(_) => {
return Err(OpcodeResolutionError::OpcodeNotSolvable(
OpcodeNotSolvable::ExpressionHasTooManyUnknowns(expr.clone()),
Expand All @@ -92,7 +92,7 @@ impl<'b, B: BlackBoxFunctionSolver> BrilligSolver<'b, B> {
// Attempt to fetch all array input values
for expr in expr_arr.iter() {
match get_value(expr, initial_witness) {
Ok(value) => calldata.push(value.into()),
Ok(value) => calldata.push(value),
Err(_) => {
return Err(OpcodeResolutionError::OpcodeNotSolvable(
OpcodeNotSolvable::ExpressionHasTooManyUnknowns(expr.clone()),
Expand All @@ -110,7 +110,7 @@ impl<'b, B: BlackBoxFunctionSolver> BrilligSolver<'b, B> {
.block_value
.get(&memory_index)
.expect("All memory is initialized on creation");
calldata.push((*memory_value).into());
calldata.push(*memory_value);
}
}
}
Expand All @@ -122,11 +122,11 @@ impl<'b, B: BlackBoxFunctionSolver> BrilligSolver<'b, B> {
Ok(Self { vm, acir_index })
}

pub fn get_memory(&self) -> &[Value] {
pub fn get_memory(&self) -> &[MemoryValue] {
self.vm.get_memory()
}

pub fn write_memory_at(&mut self, ptr: usize, value: Value) {
pub fn write_memory_at(&mut self, ptr: usize, value: MemoryValue) {
self.vm.write_memory_at(ptr, value);
}

Expand Down Expand Up @@ -206,13 +206,13 @@ impl<'b, B: BlackBoxFunctionSolver> BrilligSolver<'b, B> {
for output in brillig.outputs.iter() {
match output {
BrilligOutputs::Simple(witness) => {
insert_value(witness, memory[current_ret_data_idx].to_field(), witness_map)?;
insert_value(witness, memory[current_ret_data_idx].value, witness_map)?;
current_ret_data_idx += 1;
}
BrilligOutputs::Array(witness_arr) => {
for witness in witness_arr.iter() {
let value = memory[current_ret_data_idx];
insert_value(witness, value.to_field(), witness_map)?;
insert_value(witness, value.value, witness_map)?;
current_ret_data_idx += 1;
}
}
Expand Down
41 changes: 18 additions & 23 deletions acvm-repo/acvm/tests/solver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;

use acir::{
brillig::{BinaryFieldOp, MemoryAddress, Opcode as BrilligOpcode, Value, ValueOrArray},
brillig::{BinaryFieldOp, MemoryAddress, Opcode as BrilligOpcode, ValueOrArray},
circuit::{
brillig::{Brillig, BrilligInputs, BrilligOutputs},
opcodes::{BlockId, MemOp},
Expand Down Expand Up @@ -70,9 +70,9 @@ fn inversion_brillig_oracle_equivalence() {
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(1))],
destination_value_types: vec![HeapValueType::Simple],
destination_value_types: vec![HeapValueType::field()],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
input_value_types: vec![HeapValueType::field()],
},
BrilligOpcode::Stop { return_data_offset: 0, return_data_size: 3 },
],
Expand Down Expand Up @@ -120,8 +120,7 @@ fn inversion_brillig_oracle_equivalence() {
assert_eq!(foreign_call_wait_info.inputs.len(), 1, "Should be waiting for a single input");

// As caller of VM, need to resolve foreign calls
let foreign_call_result =
Value::from(foreign_call_wait_info.inputs[0].unwrap_value().to_field().inverse());
let foreign_call_result = foreign_call_wait_info.inputs[0].unwrap_field().inverse();
// Alter Brillig oracle opcode with foreign call resolution
acvm.resolve_pending_foreign_call(foreign_call_result.into());

Expand Down Expand Up @@ -199,16 +198,16 @@ fn double_inversion_brillig_oracle() {
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(1))],
destination_value_types: vec![HeapValueType::Simple],
destination_value_types: vec![HeapValueType::field()],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
input_value_types: vec![HeapValueType::field()],
},
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(3))],
destination_value_types: vec![HeapValueType::Simple],
destination_value_types: vec![HeapValueType::field()],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(2))],
input_value_types: vec![HeapValueType::Simple],
input_value_types: vec![HeapValueType::field()],
},
BrilligOpcode::Stop { return_data_offset: 0, return_data_size: 5 },
],
Expand Down Expand Up @@ -257,8 +256,7 @@ fn double_inversion_brillig_oracle() {
acvm.get_pending_foreign_call().expect("should have a brillig foreign call request");
assert_eq!(foreign_call_wait_info.inputs.len(), 1, "Should be waiting for a single input");

let x_plus_y_inverse =
Value::from(foreign_call_wait_info.inputs[0].unwrap_value().to_field().inverse());
let x_plus_y_inverse = foreign_call_wait_info.inputs[0].unwrap_field().inverse();

// Resolve Brillig foreign call
acvm.resolve_pending_foreign_call(x_plus_y_inverse.into());
Expand All @@ -275,8 +273,7 @@ fn double_inversion_brillig_oracle() {
acvm.get_pending_foreign_call().expect("should have a brillig foreign call request");
assert_eq!(foreign_call_wait_info.inputs.len(), 1, "Should be waiting for a single input");

let i_plus_j_inverse =
Value::from(foreign_call_wait_info.inputs[0].unwrap_value().to_field().inverse());
let i_plus_j_inverse = foreign_call_wait_info.inputs[0].unwrap_field().inverse();
assert_ne!(x_plus_y_inverse, i_plus_j_inverse);

// Alter Brillig oracle opcode
Expand Down Expand Up @@ -334,16 +331,16 @@ fn oracle_dependent_execution() {
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(1))],
destination_value_types: vec![HeapValueType::Simple],
destination_value_types: vec![HeapValueType::field()],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
input_value_types: vec![HeapValueType::field()],
},
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(3))],
destination_value_types: vec![HeapValueType::Simple],
destination_value_types: vec![HeapValueType::field()],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(2))],
input_value_types: vec![HeapValueType::Simple],
input_value_types: vec![HeapValueType::field()],
},
BrilligOpcode::Stop { return_data_offset: 0, return_data_size: 4 },
],
Expand Down Expand Up @@ -389,8 +386,7 @@ fn oracle_dependent_execution() {
assert_eq!(foreign_call_wait_info.inputs.len(), 1, "Should be waiting for a single input");

// Resolve Brillig foreign call
let x_inverse =
Value::from(foreign_call_wait_info.inputs[0].unwrap_value().to_field().inverse());
let x_inverse = foreign_call_wait_info.inputs[0].unwrap_field().inverse();
acvm.resolve_pending_foreign_call(x_inverse.into());

// After filling data request, continue solving
Expand All @@ -406,8 +402,7 @@ fn oracle_dependent_execution() {
assert_eq!(foreign_call_wait_info.inputs.len(), 1, "Should be waiting for a single input");

// Resolve Brillig foreign call
let y_inverse =
Value::from(foreign_call_wait_info.inputs[0].unwrap_value().to_field().inverse());
let y_inverse = foreign_call_wait_info.inputs[0].unwrap_field().inverse();
acvm.resolve_pending_foreign_call(y_inverse.into());

// We've resolved all the brillig foreign calls so we should be able to complete execution now.
Expand Down Expand Up @@ -464,9 +459,9 @@ fn brillig_oracle_predicate() {
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(1))],
destination_value_types: vec![HeapValueType::Simple],
destination_value_types: vec![HeapValueType::field()],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
input_value_types: vec![HeapValueType::field()],
},
],
predicate: Some(Expression::default()),
Expand Down
4 changes: 2 additions & 2 deletions acvm-repo/acvm_js/src/foreign_call/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub(super) fn encode_foreign_call_inputs(
let inputs = js_sys::Array::default();
for input in foreign_call_inputs {
let input_array = js_sys::Array::default();
for value in input.values() {
let hex_js_string = field_element_to_js_string(&value.to_field());
for value in input.fields() {
let hex_js_string = field_element_to_js_string(&value);
input_array.push(&hex_js_string);
}
inputs.push(&input_array);
Expand Down
8 changes: 4 additions & 4 deletions acvm-repo/acvm_js/src/foreign_call/outputs.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use acvm::brillig_vm::brillig::{ForeignCallParam, ForeignCallResult, Value};
use acvm::brillig_vm::brillig::{ForeignCallParam, ForeignCallResult};
use wasm_bindgen::JsValue;

use crate::js_witness_map::js_value_to_field_element;

fn decode_foreign_call_output(output: JsValue) -> Result<ForeignCallParam, String> {
if output.is_string() {
let value = Value::from(js_value_to_field_element(output)?);
let value = js_value_to_field_element(output)?;
Ok(ForeignCallParam::Single(value))
} else if output.is_array() {
let output = js_sys::Array::from(&output);

let mut values: Vec<Value> = Vec::with_capacity(output.length() as usize);
let mut values: Vec<_> = Vec::with_capacity(output.length() as usize);
for elem in output.iter() {
values.push(Value::from(js_value_to_field_element(elem)?));
values.push(js_value_to_field_element(elem)?);
}
Ok(ForeignCallParam::Array(values))
} else {
Expand Down
14 changes: 7 additions & 7 deletions acvm-repo/acvm_js/test/shared/complex_foreign_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { WitnessMap } from '@noir-lang/acvm_js';

// See `complex_brillig_foreign_call` integration test in `acir/tests/test_program_serialization.rs`.
export const bytecode = Uint8Array.from([
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 84, 65, 14, 132, 32, 12, 108, 101, 117, 205, 222, 246, 7, 38, 187, 15, 96,
247, 5, 254, 197, 120, 211, 232, 209, 231, 139, 113, 136, 181, 65, 47, 98, 162, 147, 52, 20, 24, 202, 164, 45, 48,
205, 200, 157, 49, 124, 227, 44, 129, 207, 152, 75, 120, 94, 137, 209, 30, 195, 143, 227, 197, 178, 103, 105, 76, 110,
160, 209, 156, 160, 209, 247, 195, 69, 235, 29, 179, 46, 81, 243, 103, 2, 239, 231, 225, 44, 117, 150, 97, 254, 196,
152, 99, 157, 176, 87, 168, 188, 147, 224, 121, 20, 209, 180, 254, 109, 70, 75, 47, 178, 186, 251, 37, 116, 86, 93,
219, 55, 245, 96, 20, 85, 75, 253, 8, 255, 171, 246, 121, 231, 220, 4, 249, 237, 132, 56, 28, 224, 109, 113, 223, 180,
164, 50, 165, 0, 137, 17, 72, 139, 88, 97, 4, 173, 98, 132, 157, 33, 5, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 84, 93, 10, 131, 48, 12, 78, 218, 233, 100, 111, 187, 193, 96, 59, 64, 231, 9,
188, 139, 248, 166, 232, 163, 167, 23, 11, 126, 197, 24, 250, 34, 86, 208, 64, 72, 218, 252, 125, 36, 105, 153, 22,
42, 60, 51, 116, 235, 217, 64, 103, 156, 37, 5, 191, 10, 210, 29, 163, 63, 167, 203, 229, 206, 194, 104, 110, 128,
209, 158, 128, 49, 236, 195, 69, 231, 157, 114, 46, 73, 251, 103, 35, 239, 231, 225, 57, 243, 156, 227, 252, 132, 44,
112, 79, 176, 125, 84, 223, 73, 248, 145, 152, 69, 149, 4, 107, 233, 114, 90, 119, 145, 85, 237, 151, 192, 89, 247,
221, 208, 54, 163, 85, 174, 26, 234, 87, 232, 63, 101, 103, 21, 55, 169, 216, 73, 72, 249, 5, 197, 234, 132, 123, 179,
35, 247, 155, 214, 246, 102, 20, 73, 204, 72, 168, 123, 191, 161, 25, 66, 136, 159, 187, 53, 5, 0, 0,
]);
export const initialWitnessMap: WitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000001'],
Expand Down
Loading

0 comments on commit 10148c5

Please sign in to comment.