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

feat: Allow nested arrays and vectors in Brillig foreign calls #4404

Closed
Closed
Show file tree
Hide file tree
Changes from 5 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
1,348 changes: 792 additions & 556 deletions barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp

Large diffs are not rendered by default.

1,250 changes: 724 additions & 526 deletions noir/acvm-repo/acir/codegen/acir.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion noir/acvm-repo/acir/src/circuit/black_box_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl BlackBoxFunc {
BlackBoxFunc::RecursiveAggregation => "recursive_aggregation",
BlackBoxFunc::EcdsaSecp256r1 => "ecdsa_secp256r1",
BlackBoxFunc::BigIntAdd => "bigint_add",
BlackBoxFunc::BigIntSub => "bigint_neg",
BlackBoxFunc::BigIntSub => "bigint_sub",
BlackBoxFunc::BigIntMul => "bigint_mul",
BlackBoxFunc::BigIntDiv => "bigint_div",
BlackBoxFunc::BigIntFromLeBytes => "bigint_from_le_bytes",
Expand Down
6 changes: 5 additions & 1 deletion noir/acvm-repo/acir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ mod reflection {
path::{Path, PathBuf},
};

use brillig::{BinaryFieldOp, BinaryIntOp, BlackBoxOp, Opcode as BrilligOpcode, ValueOrArray};
use brillig::{
BinaryFieldOp, BinaryIntOp, BlackBoxOp, HeapValueType, Opcode as BrilligOpcode,
ValueOrArray,
};
use serde_reflection::{Tracer, TracerConfig};

use crate::{
Expand Down Expand Up @@ -69,6 +72,7 @@ mod reflection {
tracer.trace_simple_type::<BlackBoxOp>().unwrap();
tracer.trace_simple_type::<Directive>().unwrap();
tracer.trace_simple_type::<ValueOrArray>().unwrap();
tracer.trace_simple_type::<HeapValueType>().unwrap();

let registry = tracer.registry().unwrap();

Expand Down
38 changes: 25 additions & 13 deletions noir/acvm-repo/acir/tests/test_program_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use acir::{
native_types::{Expression, Witness},
};
use acir_field::FieldElement;
use brillig::{HeapArray, MemoryAddress, ValueOrArray};
use brillig::{HeapArray, HeapValueType, MemoryAddress, ValueOrArray};

#[test]
fn addition_circuit() {
Expand Down Expand Up @@ -186,7 +186,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],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
},
brillig::Opcode::Stop { return_data_offset: 0, return_data_size: 1 },
],
Expand All @@ -205,10 +207,10 @@ fn simple_brillig_foreign_call() {

let expected_serialization: Vec<u8> = vec![
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 173, 143, 177, 10, 192, 32, 12, 68, 207, 148, 150, 118,
234, 175, 216, 63, 232, 207, 116, 232, 226, 32, 226, 247, 171, 24, 225, 6, 113, 209, 7, 33,
199, 5, 194, 221, 9, 192, 160, 178, 145, 102, 154, 247, 234, 182, 115, 60, 102, 221, 47,
203, 121, 69, 59, 20, 246, 78, 254, 198, 149, 231, 80, 253, 187, 248, 249, 48, 106, 205,
220, 189, 187, 144, 33, 24, 144, 0, 93, 119, 243, 238, 108, 1, 0, 0,
234, 175, 216, 63, 232, 207, 116, 232, 226, 32, 226, 247, 171, 24, 33, 72, 112, 209, 7, 33,
199, 13, 199, 221, 9, 192, 160, 178, 9, 45, 105, 222, 203, 223, 206, 241, 152, 117, 89, 86,
246, 37, 222, 80, 216, 149, 254, 141, 43, 223, 193, 250, 119, 241, 243, 97, 180, 90, 75,
232, 189, 91, 243, 73, 24, 132, 1, 9, 251, 174, 12, 242, 132, 1, 0, 0,
];

assert_eq!(bytes, expected_serialization)
Expand Down Expand Up @@ -272,11 +274,20 @@ fn complex_brillig_foreign_call() {
ValueOrArray::HeapArray(HeapArray { pointer: 0.into(), size: 3 }),
ValueOrArray::MemoryAddress(MemoryAddress::from(1)),
],
input_value_types: vec![
HeapValueType::Array { size: 3, value_types: vec![HeapValueType::Simple] },
HeapValueType::Simple,
],
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,
],
},
brillig::Opcode::Stop { return_data_offset: 32, return_data_size: 5 },
],
Expand All @@ -294,14 +305,15 @@ fn complex_brillig_foreign_call() {
let bytes = Circuit::serialize_circuit(&circuit);

let expected_serialization: Vec<u8> = vec![
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 83, 81, 10, 131, 48, 12, 125, 105, 215, 205, 125,
237, 10, 131, 237, 0, 221, 78, 224, 93, 196, 63, 69, 63, 61, 190, 5, 95, 177, 6, 193, 15,
43, 104, 32, 164, 9, 175, 201, 107, 146, 22, 0, 4, 147, 216, 160, 134, 103, 161, 159, 74,
196, 149, 180, 126, 159, 252, 36, 95, 46, 127, 20, 71, 115, 1, 142, 246, 0, 142, 113, 31,
78, 58, 239, 156, 115, 201, 218, 63, 187, 242, 127, 110, 65, 93, 208, 59, 253, 7, 109, 193,
56, 104, 223, 170, 239, 80, 120, 16, 83, 102, 225, 250, 247, 14, 243, 46, 138, 170, 253,
76, 234, 86, 93, 219, 55, 245, 96, 21, 84, 83, 253, 36, 231, 47, 173, 217, 184, 19, 227,
47, 204, 207, 119, 26, 40, 76, 164, 251, 178, 144, 17, 127, 189, 34, 151, 201, 4, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 84, 73, 14, 131, 48, 12, 28, 39, 77, 75, 79, 253,
66, 165, 246, 1, 129, 23, 240, 23, 196, 13, 4, 71, 158, 79, 16, 70, 4, 19, 184, 16, 36, 24,
201, 114, 150, 73, 60, 178, 157, 36, 0, 8, 35, 180, 51, 197, 99, 226, 185, 143, 137, 151,
179, 183, 199, 144, 82, 188, 187, 236, 89, 26, 213, 13, 52, 234, 19, 52, 78, 253, 112, 209,
122, 199, 172, 75, 212, 252, 233, 192, 251, 121, 56, 51, 206, 158, 60, 127, 177, 79, 120,
29, 236, 191, 34, 239, 16, 252, 120, 181, 200, 172, 193, 220, 135, 36, 226, 190, 189, 152,
69, 83, 183, 85, 217, 105, 65, 149, 50, 127, 222, 248, 47, 246, 105, 231, 220, 0, 255, 203,
9, 113, 40, 192, 219, 226, 126, 48, 167, 209, 32, 32, 68, 97, 45, 96, 129, 30, 205, 70,
116, 33, 17, 5, 0, 0,
];

assert_eq!(bytes, expected_serialization)
Expand Down
13 changes: 13 additions & 0 deletions noir/acvm-repo/acvm/tests/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use acir::{

use acvm::pwg::{ACVMStatus, ErrorLocation, ForeignCallWaitInfo, OpcodeResolutionError, ACVM};
use acvm_blackbox_solver::StubbedBlackBoxSolver;
use brillig_vm::brillig::HeapValueType;

// Reenable these test cases once we move the brillig implementation of inversion down into the acvm stdlib.

Expand Down Expand Up @@ -69,7 +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],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
},
BrilligOpcode::Stop { return_data_offset: 0, return_data_size: 3 },
],
Expand Down Expand Up @@ -196,12 +199,16 @@ fn double_inversion_brillig_oracle() {
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(1))],
destination_value_types: vec![HeapValueType::Simple],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
},
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(3))],
destination_value_types: vec![HeapValueType::Simple],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(2))],
input_value_types: vec![HeapValueType::Simple],
},
BrilligOpcode::Stop { return_data_offset: 0, return_data_size: 5 },
],
Expand Down Expand Up @@ -327,12 +334,16 @@ fn oracle_dependent_execution() {
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(1))],
destination_value_types: vec![HeapValueType::Simple],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
},
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(3))],
destination_value_types: vec![HeapValueType::Simple],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(2))],
input_value_types: vec![HeapValueType::Simple],
},
BrilligOpcode::Stop { return_data_offset: 0, return_data_size: 4 },
],
Expand Down Expand Up @@ -453,7 +464,9 @@ fn brillig_oracle_predicate() {
BrilligOpcode::ForeignCall {
function: "invert".into(),
destinations: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(1))],
destination_value_types: vec![HeapValueType::Simple],
inputs: vec![ValueOrArray::MemoryAddress(MemoryAddress::from(0))],
input_value_types: vec![HeapValueType::Simple],
},
],
predicate: Some(Expression::default()),
Expand Down
14 changes: 7 additions & 7 deletions noir/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, 83, 81, 10, 131, 48, 12, 125, 105, 215, 205, 125, 237, 10, 131, 237, 0, 221,
78, 224, 93, 196, 63, 69, 63, 61, 190, 5, 95, 177, 6, 193, 15, 43, 104, 32, 164, 9, 175, 201, 107, 146, 22, 0, 4, 147,
216, 160, 134, 103, 161, 159, 74, 196, 149, 180, 126, 159, 252, 36, 95, 46, 127, 20, 71, 115, 1, 142, 246, 0, 142,
113, 31, 78, 58, 239, 156, 115, 201, 218, 63, 187, 242, 127, 110, 65, 93, 208, 59, 253, 7, 109, 193, 56, 104, 223,
170, 239, 80, 120, 16, 83, 102, 225, 250, 247, 14, 243, 46, 138, 170, 253, 76, 234, 86, 93, 219, 55, 245, 96, 21, 84,
83, 253, 36, 231, 47, 173, 217, 184, 19, 227, 47, 204, 207, 119, 26, 40, 76, 164, 251, 178, 144, 17, 127, 189, 34,
151, 201, 4, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 84, 73, 14, 131, 48, 12, 28, 39, 77, 75, 79, 253, 66, 165, 246, 1, 129, 23,
240, 23, 196, 13, 4, 71, 158, 79, 16, 70, 4, 19, 184, 16, 36, 24, 201, 114, 150, 73, 60, 178, 157, 36, 0, 8, 35, 180,
51, 197, 99, 226, 185, 143, 137, 151, 179, 183, 199, 144, 82, 188, 187, 236, 89, 26, 213, 13, 52, 234, 19, 52, 78,
253, 112, 209, 122, 199, 172, 75, 212, 252, 233, 192, 251, 121, 56, 51, 206, 158, 60, 127, 177, 79, 120, 29, 236, 191,
34, 239, 16, 252, 120, 181, 200, 172, 193, 220, 135, 36, 226, 190, 189, 152, 69, 83, 183, 85, 217, 105, 65, 149, 50,
127, 222, 248, 47, 246, 105, 231, 220, 0, 255, 203, 9, 113, 40, 192, 219, 226, 126, 48, 167, 209, 32, 32, 68, 97, 45,
96, 129, 30, 205, 70, 116, 33, 17, 5, 0, 0,
]);
export const initialWitnessMap: WitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000001'],
Expand Down
6 changes: 3 additions & 3 deletions noir/acvm-repo/acvm_js/test/shared/foreign_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { WitnessMap } from '@noir-lang/acvm_js';
// See `simple_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, 173, 143, 177, 10, 192, 32, 12, 68, 207, 148, 150, 118, 234, 175, 216, 63, 232,
207, 116, 232, 226, 32, 226, 247, 171, 24, 225, 6, 113, 209, 7, 33, 199, 5, 194, 221, 9, 192, 160, 178, 145, 102, 154,
247, 234, 182, 115, 60, 102, 221, 47, 203, 121, 69, 59, 20, 246, 78, 254, 198, 149, 231, 80, 253, 187, 248, 249, 48,
106, 205, 220, 189, 187, 144, 33, 24, 144, 0, 93, 119, 243, 238, 108, 1, 0, 0,
207, 116, 232, 226, 32, 226, 247, 171, 24, 33, 72, 112, 209, 7, 33, 199, 13, 199, 221, 9, 192, 160, 178, 9, 45, 105,
222, 203, 223, 206, 241, 152, 117, 89, 86, 246, 37, 222, 80, 216, 149, 254, 141, 43, 223, 193, 250, 119, 241, 243, 97,
180, 90, 75, 232, 189, 91, 243, 73, 24, 132, 1, 9, 251, 174, 12, 242, 132, 1, 0, 0,
]);
export const initialWitnessMap: WitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000005'],
Expand Down
4 changes: 3 additions & 1 deletion noir/acvm-repo/brillig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ mod value;

pub use black_box::BlackBoxOp;
pub use foreign_call::{ForeignCallParam, ForeignCallResult};
pub use opcodes::{BinaryFieldOp, BinaryIntOp, HeapArray, HeapVector, MemoryAddress, ValueOrArray};
pub use opcodes::{
BinaryFieldOp, BinaryIntOp, HeapArray, HeapValueType, HeapVector, MemoryAddress, ValueOrArray,
};
pub use opcodes::{BrilligOpcode as Opcode, Label};
pub use value::Typ;
pub use value::Value;
26 changes: 26 additions & 0 deletions noir/acvm-repo/brillig/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ impl From<usize> for MemoryAddress {
}
}

/// Describes the memory layout for an array/vector element
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum HeapValueType {
// A single field element is enough to represent the value
Simple,
// The value read should be interpreted as a pointer to a heap array, which
// consists of a pointer to a slice of memory of size elements, and a
// reference count
Array { value_types: Vec<HeapValueType>, size: usize },
// The value read should be interpreted as a pointer to a heap vector, which
// consists of a pointer to a slice of memory, a number of elements in that
// slice, and a reference count
Vector { value_types: Vec<HeapValueType> },
}

impl HeapValueType {
pub fn all_simple(types: &[HeapValueType]) -> bool {
types.iter().all(|typ| matches!(typ, HeapValueType::Simple))
}
}

/// A fixed-sized array starting from a Brillig memory location.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Copy)]
pub struct HeapArray {
Expand Down Expand Up @@ -118,8 +139,13 @@ pub enum BrilligOpcode {
function: String,
/// Destination addresses (may be single values or memory pointers).
destinations: Vec<ValueOrArray>,
/// Destination value types
destination_value_types: Vec<HeapValueType>,
/// Input addresses (may be single values or memory pointers).
inputs: Vec<ValueOrArray>,
/// Input value types (for heap allocated structures indicates how to
/// retrieve the elements)
input_value_types: Vec<HeapValueType>,
},
Mov {
destination: MemoryAddress,
Expand Down
Loading
Loading