Skip to content

Commit

Permalink
Merge ff36923 into 9341113
Browse files Browse the repository at this point in the history
  • Loading branch information
AztecBot authored Jul 30, 2024
2 parents 9341113 + ff36923 commit bc95569
Show file tree
Hide file tree
Showing 43 changed files with 1,924 additions and 1,254 deletions.
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e44ef7042c87d3c78a14413ad7d54e4ed642ad89
a26419f00f5f082a9ed856346addf6276fbdb4d7
1,874 changes: 1,175 additions & 699 deletions acvm-repo/acir/codegen/acir.cpp

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions acvm-repo/acir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ mod reflection {

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

Expand Down Expand Up @@ -81,6 +81,8 @@ mod reflection {
tracer.trace_simple_type::<HeapValueType>().unwrap();
tracer.trace_simple_type::<AssertionPayload<FieldElement>>().unwrap();
tracer.trace_simple_type::<ExpressionOrMemory<FieldElement>>().unwrap();
tracer.trace_simple_type::<BitSize>().unwrap();
tracer.trace_simple_type::<IntegerBitSize>().unwrap();

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

Expand Down
33 changes: 16 additions & 17 deletions 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::{AcirField, FieldElement};
use brillig::{HeapArray, HeapValueType, MemoryAddress, ValueOrArray};
use brillig::{BitSize, HeapArray, HeapValueType, IntegerBitSize, MemoryAddress, ValueOrArray};

#[test]
fn addition_circuit() {
Expand Down Expand Up @@ -204,12 +204,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, 144, 193, 10, 192, 32, 8, 134, 117, 99, 99, 236,
182, 55, 105, 111, 176, 151, 217, 161, 75, 135, 136, 30, 63, 42, 82, 144, 8, 47, 245, 65,
252, 230, 47, 162, 34, 52, 174, 242, 144, 226, 131, 148, 255, 18, 206, 125, 164, 102, 142,
23, 215, 245, 50, 114, 222, 173, 15, 80, 38, 65, 217, 108, 39, 61, 7, 30, 115, 11, 223,
186, 248, 251, 160, 221, 170, 146, 64, 191, 39, 215, 60, 3, 47, 3, 99, 171, 188, 84, 164,
1, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 173, 80, 49, 10, 192, 32, 12, 52, 45, 45, 165, 155, 63,
209, 31, 248, 25, 7, 23, 7, 17, 223, 175, 96, 2, 65, 162, 139, 30, 132, 203, 221, 65, 72,
2, 170, 227, 107, 5, 216, 63, 200, 164, 57, 200, 115, 200, 102, 15, 22, 206, 205, 50, 124,
223, 107, 108, 128, 155, 106, 113, 217, 141, 252, 10, 25, 225, 103, 121, 136, 197, 167,
188, 250, 213, 76, 75, 158, 22, 178, 10, 176, 188, 242, 119, 164, 1, 0, 0,
];

assert_eq!(bytes, expected_serialization)
Expand Down Expand Up @@ -239,7 +238,7 @@ fn complex_brillig_foreign_call() {
brillig::Opcode::Const {
destination: MemoryAddress(0),
value: FieldElement::from(32_usize),
bit_size: 32,
bit_size: BitSize::Integer(IntegerBitSize::U32),
},
brillig::Opcode::CalldataCopy {
destination_address: MemoryAddress(1),
Expand Down Expand Up @@ -308,15 +307,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, 75, 10, 131, 64, 12, 77, 102, 90, 43, 221, 245,
6, 133, 246, 0, 211, 158, 192, 187, 136, 59, 69, 151, 158, 94, 116, 48, 131, 241, 233, 70,
28, 65, 3, 195, 155, 79, 62, 47, 9, 25, 166, 81, 210, 97, 177, 236, 239, 130, 70, 208, 223,
91, 154, 75, 208, 205, 4, 221, 62, 249, 113, 60, 95, 238, 40, 142, 230, 2, 28, 237, 1, 28,
73, 245, 255, 132, 253, 142, 217, 151, 168, 245, 179, 43, 243, 115, 163, 113, 190, 18, 57,
63, 4, 83, 44, 180, 55, 50, 180, 28, 188, 153, 224, 196, 122, 175, 111, 112, 68, 24, 65,
116, 178, 40, 89, 254, 93, 162, 120, 48, 196, 126, 170, 12, 243, 186, 106, 202, 162, 181,
160, 138, 84, 63, 106, 255, 133, 119, 6, 187, 14, 108, 59, 133, 250, 243, 90, 139, 19, 238,
205, 6, 223, 47, 154, 202, 27, 74, 222, 3, 234, 73, 242, 82, 65, 5, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 84, 75, 10, 132, 48, 12, 77, 90, 199, 145, 217,
205, 13, 6, 102, 14, 208, 241, 4, 222, 69, 220, 41, 186, 244, 248, 90, 140, 24, 159, 5, 23,
86, 208, 7, 37, 253, 228, 243, 146, 144, 50, 77, 200, 198, 197, 178, 127, 136, 52, 34, 253,
189, 165, 53, 102, 221, 66, 164, 59, 134, 63, 199, 243, 229, 206, 226, 104, 110, 192, 209,
158, 192, 145, 84, 255, 47, 216, 239, 152, 125, 137, 90, 63, 27, 152, 159, 132, 166, 249,
74, 229, 252, 20, 153, 97, 161, 189, 145, 161, 237, 224, 173, 128, 19, 235, 189, 126, 192,
17, 97, 4, 177, 75, 162, 101, 154, 187, 84, 113, 97, 136, 255, 82, 89, 150, 109, 211, 213,
85, 111, 65, 21, 233, 126, 213, 254, 7, 239, 12, 118, 104, 171, 161, 63, 176, 144, 46, 7,
244, 246, 124, 191, 105, 41, 241, 92, 246, 1, 235, 222, 207, 212, 69, 5, 0, 0,
];

assert_eq!(bytes, expected_serialization)
Expand Down
32 changes: 24 additions & 8 deletions acvm-repo/acir_field/src/field_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,25 @@ impl<'de, T: ark_ff::PrimeField> Deserialize<'de> for FieldElement<T> {

impl<F: PrimeField> From<u128> for FieldElement<F> {
fn from(a: u128) -> FieldElement<F> {
let result = match F::from_str(&a.to_string()) {
Ok(result) => result,
Err(_) => panic!("Cannot convert u128 as a string to a field element"),
};
FieldElement(result)
FieldElement(F::from(a))
}
}

impl<F: PrimeField> From<usize> for FieldElement<F> {
fn from(a: usize) -> FieldElement<F> {
FieldElement::from(a as u128)
FieldElement::from(a as u64)
}
}

impl<F: PrimeField> From<u64> for FieldElement<F> {
fn from(a: u64) -> FieldElement<F> {
FieldElement(F::from(a))
}
}

impl<F: PrimeField> From<u32> for FieldElement<F> {
fn from(a: u32) -> FieldElement<F> {
FieldElement(F::from(a))
}
}

Expand Down Expand Up @@ -265,8 +273,16 @@ impl<F: PrimeField> AcirField for FieldElement<F> {
}

fn to_u128(self) -> u128 {
let bytes = self.to_be_bytes();
u128::from_be_bytes(bytes[16..32].try_into().unwrap())
let as_bigint = self.0.into_bigint();
let limbs = as_bigint.as_ref();

let mut result = limbs[0] as u128;
if limbs.len() > 1 {
let high_limb = limbs[1] as u128;
result += high_limb << 64;
}

result
}

fn try_into_u128(self) -> Option<u128> {
Expand Down
3 changes: 2 additions & 1 deletion acvm-repo/acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ fn extract_failure_payload_from_memory<F: AcirField>(
let error_selector = ErrorSelector::new(
revert_values_iter
.next()
.copied()
.expect("Incorrect revert data size")
.try_into()
.expect("Error selector is not u64"),
Expand All @@ -273,7 +274,7 @@ fn extract_failure_payload_from_memory<F: AcirField>(
STRING_ERROR_SELECTOR => {
// If the error selector is 0, it means the error is a string
let string = revert_values_iter
.map(|memory_value| {
.map(|&memory_value| {
let as_u8: u8 = memory_value.try_into().expect("String item is not u8");
as_u8 as char
})
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, 75, 10, 131, 64, 12, 77, 102, 90, 43, 221, 245, 6, 133, 246, 0, 211, 158,
192, 187, 136, 59, 69, 151, 158, 94, 116, 48, 131, 241, 233, 70, 28, 65, 3, 195, 155, 79, 62, 47, 9, 25, 166, 81, 210,
97, 177, 236, 239, 130, 70, 208, 223, 91, 154, 75, 208, 205, 4, 221, 62, 249, 113, 60, 95, 238, 40, 142, 230, 2, 28,
237, 1, 28, 73, 245, 255, 132, 253, 142, 217, 151, 168, 245, 179, 43, 243, 115, 163, 113, 190, 18, 57, 63, 4, 83, 44,
180, 55, 50, 180, 28, 188, 153, 224, 196, 122, 175, 111, 112, 68, 24, 65, 116, 178, 40, 89, 254, 93, 162, 120, 48,
196, 126, 170, 12, 243, 186, 106, 202, 162, 181, 160, 138, 84, 63, 106, 255, 133, 119, 6, 187, 14, 108, 59, 133, 250,
243, 90, 139, 19, 238, 205, 6, 223, 47, 154, 202, 27, 74, 222, 3, 234, 73, 242, 82, 65, 5, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 84, 75, 10, 132, 48, 12, 77, 90, 199, 145, 217, 205, 13, 6, 102, 14, 208, 241,
4, 222, 69, 220, 41, 186, 244, 248, 90, 140, 24, 159, 5, 23, 86, 208, 7, 37, 253, 228, 243, 146, 144, 50, 77, 200,
198, 197, 178, 127, 136, 52, 34, 253, 189, 165, 53, 102, 221, 66, 164, 59, 134, 63, 199, 243, 229, 206, 226, 104, 110,
192, 209, 158, 192, 145, 84, 255, 47, 216, 239, 152, 125, 137, 90, 63, 27, 152, 159, 132, 166, 249, 74, 229, 252, 20,
153, 97, 161, 189, 145, 161, 237, 224, 173, 128, 19, 235, 189, 126, 192, 17, 97, 4, 177, 75, 162, 101, 154, 187, 84,
113, 97, 136, 255, 82, 89, 150, 109, 211, 213, 85, 111, 65, 21, 233, 126, 213, 254, 7, 239, 12, 118, 104, 171, 161,
63, 176, 144, 46, 7, 244, 246, 124, 191, 105, 41, 241, 92, 246, 1, 235, 222, 207, 212, 69, 5, 0, 0,
]);
export const initialWitnessMap: WitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000001'],
Expand Down
8 changes: 4 additions & 4 deletions acvm-repo/acvm_js/test/shared/foreign_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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, 144, 193, 10, 192, 32, 8, 134, 117, 99, 99, 236, 182, 55, 105, 111, 176, 151,
217, 161, 75, 135, 136, 30, 63, 42, 82, 144, 8, 47, 245, 65, 252, 230, 47, 162, 34, 52, 174, 242, 144, 226, 131, 148,
255, 18, 206, 125, 164, 102, 142, 23, 215, 245, 50, 114, 222, 173, 15, 80, 38, 65, 217, 108, 39, 61, 7, 30, 115, 11,
223, 186, 248, 251, 160, 221, 170, 146, 64, 191, 39, 215, 60, 3, 47, 3, 99, 171, 188, 84, 164, 1, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 173, 80, 49, 10, 192, 32, 12, 52, 45, 45, 165, 155, 63, 209, 31, 248, 25, 7, 23, 7,
17, 223, 175, 96, 2, 65, 162, 139, 30, 132, 203, 221, 65, 72, 2, 170, 227, 107, 5, 216, 63, 200, 164, 57, 200, 115,
200, 102, 15, 22, 206, 205, 50, 124, 223, 107, 108, 128, 155, 106, 113, 217, 141, 252, 10, 25, 225, 103, 121, 136,
197, 167, 188, 250, 213, 76, 75, 158, 22, 178, 10, 176, 188, 242, 119, 164, 1, 0, 0,
]);
export const initialWitnessMap: WitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000005'],
Expand Down
2 changes: 1 addition & 1 deletion acvm-repo/brillig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ pub use foreign_call::{ForeignCallParam, ForeignCallResult};
pub use opcodes::{
BinaryFieldOp, BinaryIntOp, HeapArray, HeapValueType, HeapVector, MemoryAddress, ValueOrArray,
};
pub use opcodes::{BrilligOpcode as Opcode, Label};
pub use opcodes::{BitSize, BrilligOpcode as Opcode, IntegerBitSize, Label};
91 changes: 85 additions & 6 deletions acvm-repo/brillig/src/opcodes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::black_box::BlackBoxOp;
use acir_field::{AcirField, FieldElement};
use acir_field::AcirField;
use serde::{Deserialize, Serialize};

pub type Label = usize;
Expand All @@ -24,7 +24,7 @@ impl From<usize> for MemoryAddress {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum HeapValueType {
// A single field element is enough to represent the value with a given bit size
Simple(u32),
Simple(BitSize),
// 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
Expand All @@ -41,7 +41,7 @@ impl HeapValueType {
}

pub fn field() -> HeapValueType {
HeapValueType::Simple(FieldElement::max_num_bits())
HeapValueType::Simple(BitSize::Field)
}
}

Expand All @@ -65,6 +65,85 @@ pub struct HeapVector {
pub size: MemoryAddress,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Copy, PartialOrd, Ord)]
pub enum IntegerBitSize {
U0, // Uninitialized
U1,
U8,
U16,
U32,
U64,
U128,
}

impl From<IntegerBitSize> for u32 {
fn from(bit_size: IntegerBitSize) -> u32 {
match bit_size {
IntegerBitSize::U0 => 0,
IntegerBitSize::U1 => 1,
IntegerBitSize::U8 => 8,
IntegerBitSize::U16 => 16,
IntegerBitSize::U32 => 32,
IntegerBitSize::U64 => 64,
IntegerBitSize::U128 => 128,
}
}
}

impl TryFrom<u32> for IntegerBitSize {
type Error = &'static str;

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(IntegerBitSize::U0),
1 => Ok(IntegerBitSize::U1),
8 => Ok(IntegerBitSize::U8),
16 => Ok(IntegerBitSize::U16),
32 => Ok(IntegerBitSize::U32),
64 => Ok(IntegerBitSize::U64),
128 => Ok(IntegerBitSize::U128),
_ => Err("Invalid bit size"),
}
}
}

impl std::fmt::Display for IntegerBitSize {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
IntegerBitSize::U0 => write!(f, "null"),
IntegerBitSize::U1 => write!(f, "bool"),
IntegerBitSize::U8 => write!(f, "u8"),
IntegerBitSize::U16 => write!(f, "u16"),
IntegerBitSize::U32 => write!(f, "u32"),
IntegerBitSize::U64 => write!(f, "u64"),
IntegerBitSize::U128 => write!(f, "u128"),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Copy, PartialOrd, Ord)]
pub enum BitSize {
Field,
Integer(IntegerBitSize),
}

impl BitSize {
pub fn to_u32<F: AcirField>(self) -> u32 {
match self {
BitSize::Field => F::max_num_bits(),
BitSize::Integer(bit_size) => bit_size.into(),
}
}

pub fn try_from_u32<F: AcirField>(value: u32) -> Result<Self, &'static str> {
if value == F::max_num_bits() {
Ok(BitSize::Field)
} else {
Ok(BitSize::Integer(IntegerBitSize::try_from(value)?))
}
}
}

/// Lays out various ways an external foreign call's input and output data may be interpreted inside Brillig.
/// This data can either be an individual value or memory.
///
Expand Down Expand Up @@ -105,14 +184,14 @@ pub enum BrilligOpcode<F> {
BinaryIntOp {
destination: MemoryAddress,
op: BinaryIntOp,
bit_size: u32,
bit_size: IntegerBitSize,
lhs: MemoryAddress,
rhs: MemoryAddress,
},
Cast {
destination: MemoryAddress,
source: MemoryAddress,
bit_size: u32,
bit_size: BitSize,
},
JumpIfNot {
condition: MemoryAddress,
Expand Down Expand Up @@ -141,7 +220,7 @@ pub enum BrilligOpcode<F> {
},
Const {
destination: MemoryAddress,
bit_size: u32,
bit_size: BitSize,
value: F,
},
Return,
Expand Down
Loading

0 comments on commit bc95569

Please sign in to comment.