Skip to content

Commit

Permalink
feat!: restrict noir word size to u32 (#5180)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*



## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
Maddiaa0 authored Jun 5, 2024
1 parent fe3ae09 commit bdb2bc6
Show file tree
Hide file tree
Showing 40 changed files with 207 additions and 161 deletions.
9 changes: 4 additions & 5 deletions acvm-repo/acir/tests/test_program_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn complex_brillig_foreign_call() {
brillig::Opcode::Const {
destination: MemoryAddress(0),
value: FieldElement::from(32_usize),
bit_size: 64,
bit_size: 32,
},
brillig::Opcode::CalldataCopy {
destination_address: MemoryAddress(1),
Expand Down Expand Up @@ -335,17 +335,16 @@ fn complex_brillig_foreign_call() {
Program { functions: vec![circuit], unconstrained_functions: vec![brillig_bytecode] };

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,
50, 204, 162, 100, 249, 119, 137, 226, 193, 16, 251, 169, 50, 204, 235, 170, 41, 139, 214,
130, 42, 82, 253, 168, 253, 23, 222, 25, 236, 58, 176, 237, 20, 234, 207, 107, 45, 78, 184,
55, 27, 124, 191, 104, 42, 111, 40, 121, 15, 94, 163, 77, 128, 65, 5, 0, 0,
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,
];

assert_eq!(bytes, expected_serialization)
Expand Down
4 changes: 4 additions & 0 deletions acvm-repo/acir_field/src/field_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ impl<F: PrimeField> AcirField for FieldElement<F> {
(self.num_bits() <= 64).then(|| self.to_u128() as u64)
}

fn try_to_u32(&self) -> Option<u32> {
(self.num_bits() <= 32).then(|| self.to_u128() as u32)
}

/// Computes the inverse or returns zero if the inverse does not exist
/// Before using this FieldElement, please ensure that this behavior is necessary
fn inverse(&self) -> FieldElement<F> {
Expand Down
2 changes: 2 additions & 0 deletions acvm-repo/acir_field/src/generic_ark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub trait AcirField:

fn try_to_u64(&self) -> Option<u64>;

fn try_to_u32(&self) -> Option<u32>;

/// Computes the inverse or returns zero if the inverse does not exist
/// Before using this FieldElement, please ensure that this behavior is necessary
fn inverse(&self) -> Self;
Expand Down
6 changes: 3 additions & 3 deletions acvm-repo/acvm_js/test/shared/complex_foreign_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export const bytecode = Uint8Array.from([
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, 50, 204, 162, 100, 249, 119, 137, 226, 193,
16, 251, 169, 50, 204, 235, 170, 41, 139, 214, 130, 42, 82, 253, 168, 253, 23, 222, 25, 236, 58, 176, 237, 20, 234,
207, 107, 45, 78, 184, 55, 27, 124, 191, 104, 42, 111, 40, 121, 15, 94, 163, 77, 128, 65, 5, 0, 0,
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,
]);
export const initialWitnessMap: WitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000001'],
Expand Down
91 changes: 64 additions & 27 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
mod tests {
use acir::{AcirField, FieldElement};
use acvm_blackbox_solver::StubbedBlackBoxSolver;
const BRILLIG_MEMORY_ADDRESSING_BIT_SIZE: u32 = 32;

use super::*;

Expand Down Expand Up @@ -927,7 +928,7 @@ mod tests {

#[test]
fn cmp_binary_ops() {
let bit_size = 32;
let bit_size = BRILLIG_MEMORY_ADDRESSING_BIT_SIZE;
let calldata: Vec<FieldElement> =
vec![(2u128).into(), (2u128).into(), (0u128).into(), (5u128).into(), (6u128).into()];
let calldata_size = calldata.len();
Expand Down Expand Up @@ -1029,7 +1030,7 @@ mod tests {
/// i += 1;
/// }
fn brillig_write_memory(item_count: usize) -> Vec<MemoryValue<FieldElement>> {
let bit_size = 64;
let bit_size = BRILLIG_MEMORY_ADDRESSING_BIT_SIZE;
let r_i = MemoryAddress::from(0);
let r_len = MemoryAddress::from(1);
let r_tmp = MemoryAddress::from(2);
Expand Down Expand Up @@ -1083,11 +1084,11 @@ mod tests {

let memory = brillig_write_memory(5);
let expected =
vec![(0u64).into(), (1u64).into(), (2u64).into(), (3u64).into(), (4u64).into()];
vec![(0u32).into(), (1u32).into(), (2u32).into(), (3u32).into(), (4u32).into()];
assert_eq!(memory, expected);

let memory = brillig_write_memory(1024);
let expected: Vec<_> = (0..1024).map(|i: u64| i.into()).collect();
let expected: Vec<_> = (0..1024).map(|i: u32| i.into()).collect();
assert_eq!(memory, expected);
}

Expand All @@ -1102,7 +1103,7 @@ mod tests {
/// i += 1;
/// }
fn brillig_sum_memory(memory: Vec<FieldElement>) -> FieldElement {
let bit_size = 64;
let bit_size = 32;
let r_i = MemoryAddress::from(0);
let r_len = MemoryAddress::from(1);
let r_sum = MemoryAddress::from(2);
Expand Down Expand Up @@ -1198,7 +1199,7 @@ mod tests {
/// }
/// Note we represent a 100% in-stack optimized form in brillig
fn brillig_recursive_write_memory<F: AcirField>(size: usize) -> Vec<MemoryValue<F>> {
let bit_size = 64;
let bit_size = BRILLIG_MEMORY_ADDRESSING_BIT_SIZE;
let r_i = MemoryAddress::from(0);
let r_len = MemoryAddress::from(1);
let r_tmp = MemoryAddress::from(2);
Expand Down Expand Up @@ -1265,11 +1266,11 @@ mod tests {

let memory = brillig_recursive_write_memory::<FieldElement>(5);
let expected =
vec![(0u64).into(), (1u64).into(), (2u64).into(), (3u64).into(), (4u64).into()];
vec![(0u32).into(), (1u32).into(), (2u32).into(), (3u32).into(), (4u32).into()];
assert_eq!(memory, expected);

let memory = brillig_recursive_write_memory::<FieldElement>(1024);
let expected: Vec<_> = (0..1024).map(|i: u64| i.into()).collect();
let expected: Vec<_> = (0..1024).map(|i: u32| i.into()).collect();
assert_eq!(memory, expected);
}

Expand Down Expand Up @@ -1301,7 +1302,11 @@ mod tests {

let double_program = vec![
// Load input address with value 5
Opcode::Const { destination: r_input, value: (5u128).into(), bit_size: 32 },
Opcode::Const {
destination: r_input,
value: (5u128).into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// Call foreign function "double" with the input address
Opcode::ForeignCall {
function: "double".into(),
Expand Down Expand Up @@ -1362,9 +1367,17 @@ mod tests {
offset: 0,
},
// input = 0
Opcode::Const { destination: r_input, value: 2_usize.into(), bit_size: 64 },
Opcode::Const {
destination: r_input,
value: 2_usize.into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// output = 0
Opcode::Const { destination: r_output, value: 2_usize.into(), bit_size: 64 },
Opcode::Const {
destination: r_output,
value: 2_usize.into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// *output = matrix_2x2_transpose(*input)
Opcode::ForeignCall {
function: "matrix_2x2_transpose".into(),
Expand Down Expand Up @@ -1444,24 +1457,28 @@ mod tests {
offset: 0,
},
// input_pointer = 4
Opcode::Const { destination: r_input_pointer, value: (4u128).into(), bit_size: 64 },
Opcode::Const {
destination: r_input_pointer,
value: (4u128).into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// input_size = input_string.len() (constant here)
Opcode::Const {
destination: r_input_size,
value: input_string.len().into(),
bit_size: 64,
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// output_pointer = 4 + input_size
Opcode::Const {
destination: r_output_pointer,
value: (4 + input_string.len()).into(),
bit_size: 64,
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// output_size = input_size * 2
Opcode::Const {
destination: r_output_size,
value: (input_string.len() * 2).into(),
bit_size: 64,
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// output_pointer[0..output_size] = string_double(input_pointer[0...input_size])
Opcode::ForeignCall {
Expand Down Expand Up @@ -1538,9 +1555,17 @@ mod tests {
offset: 0,
},
// input = 0
Opcode::Const { destination: r_input, value: (2u128).into(), bit_size: 64 },
Opcode::Const {
destination: r_input,
value: (2u128).into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// output = 0
Opcode::Const { destination: r_output, value: (6u128).into(), bit_size: 64 },
Opcode::Const {
destination: r_output,
value: (6u128).into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// *output = matrix_2x2_transpose(*input)
Opcode::ForeignCall {
function: "matrix_2x2_transpose".into(),
Expand Down Expand Up @@ -1629,11 +1654,23 @@ mod tests {
offset: 0,
},
// input = 3
Opcode::Const { destination: r_input_a, value: (3u128).into(), bit_size: 64 },
Opcode::Const {
destination: r_input_a,
value: (3u128).into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// input = 7
Opcode::Const { destination: r_input_b, value: (7u128).into(), bit_size: 64 },
Opcode::Const {
destination: r_input_b,
value: (7u128).into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// output = 0
Opcode::Const { destination: r_output, value: (0u128).into(), bit_size: 64 },
Opcode::Const {
destination: r_output,
value: (0u128).into(),
bit_size: BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
},
// *output = matrix_2x2_transpose(*input)
Opcode::ForeignCall {
function: "matrix_2x2_transpose".into(),
Expand Down Expand Up @@ -1718,28 +1755,28 @@ mod tests {
let v2_ptr: usize = 0usize;
let mut memory = v2.clone();
let v2_start = memory.len();
memory.extend(vec![MemoryValue::from(v2_ptr), v2.len().into(), MemoryValue::from(1_usize)]);
memory.extend(vec![MemoryValue::from(v2_ptr), v2.len().into(), MemoryValue::from(1_u32)]);
let a4_ptr = memory.len();
memory.extend(a4.clone());
let a4_start = memory.len();
memory.extend(vec![MemoryValue::from(a4_ptr), MemoryValue::from(1_usize)]);
memory.extend(vec![MemoryValue::from(a4_ptr), MemoryValue::from(1_u32)]);
let v6_ptr = memory.len();
memory.extend(v6.clone());
let v6_start = memory.len();
memory.extend(vec![MemoryValue::from(v6_ptr), v6.len().into(), MemoryValue::from(1_usize)]);
memory.extend(vec![MemoryValue::from(v6_ptr), v6.len().into(), MemoryValue::from(1_u32)]);
let a9_ptr = memory.len();
memory.extend(a9.clone());
let a9_start = memory.len();
memory.extend(vec![MemoryValue::from(a9_ptr), MemoryValue::from(1_usize)]);
memory.extend(vec![MemoryValue::from(a9_ptr), MemoryValue::from(1_u32)]);
// finally we add the contents of the outer array
let outer_ptr = memory.len();
let outer_array = vec![
MemoryValue::new_field(FieldElement::from(1u128)),
MemoryValue::from(v2.len()),
MemoryValue::from(v2.len() as u32),
MemoryValue::from(v2_start),
MemoryValue::from(a4_start),
MemoryValue::new_field(FieldElement::from(5u128)),
MemoryValue::from(v6.len()),
MemoryValue::from(v6.len() as u32),
MemoryValue::from(v6_start),
MemoryValue::from(a9_start),
];
Expand Down Expand Up @@ -1770,7 +1807,7 @@ mod tests {
}))
.chain(vec![
// input = 0
Opcode::Const { destination: r_input, value: (outer_ptr).into(), bit_size: 64 },
Opcode::Const { destination: r_input, value: (outer_ptr).into(), bit_size: 32 },
// some_function(input)
Opcode::ForeignCall {
function: "flat_sum".into(),
Expand Down
14 changes: 7 additions & 7 deletions acvm-repo/brillig_vm/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use acir::{brillig::MemoryAddress, AcirField};
use num_bigint::BigUint;
use num_traits::{One, Zero};

pub const MEMORY_ADDRESSING_BIT_SIZE: u32 = 64;
pub const MEMORY_ADDRESSING_BIT_SIZE: u32 = 32;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum MemoryValue<F> {
Expand Down Expand Up @@ -167,18 +167,18 @@ impl<F: AcirField> From<usize> for MemoryValue<F> {
}
}

impl<F: AcirField> From<u64> for MemoryValue<F> {
fn from(value: u64) -> Self {
MemoryValue::new_integer(value.into(), 64)
}
}

impl<F: AcirField> From<u32> for MemoryValue<F> {
fn from(value: u32) -> Self {
MemoryValue::new_integer(value.into(), 32)
}
}

impl<F: AcirField> From<u64> for MemoryValue<F> {
fn from(value: u64) -> Self {
MemoryValue::new_integer(value.into(), 64)
}
}

impl<F: AcirField> From<u8> for MemoryValue<F> {
fn from(value: u8) -> Self {
MemoryValue::new_integer(value.into(), 8)
Expand Down
2 changes: 1 addition & 1 deletion aztec_macros/src/transforms/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn transform_event(
// Also update the type! It might have a different length now than the placeholder.
interner.push_expr_type(
selector_literal_id,
Type::String(Box::new(Type::Constant(signature.len() as u64))),
Type::String(Box::new(Type::Constant(signature.len() as u32))),
);
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions aztec_macros/src/transforms/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub fn get_storage_serialized_length(
traits: &[TraitId],
typ: &Type,
interner: &NodeInterner,
) -> Result<u64, AztecMacroError> {
) -> Result<u32, AztecMacroError> {
let (struct_name, maybe_stored_in_state) = match typ {
Type::Struct(struct_type, generics) => {
Ok((struct_type.borrow().name.0.contents.clone(), generics.first()))
Expand Down Expand Up @@ -394,7 +394,7 @@ pub fn assign_storage_slots(
)),
}?;

let mut storage_slot: u64 = 1;
let mut storage_slot: u32 = 1;
for (index, (_, expr_id)) in storage_constructor_expression.fields.iter().enumerate() {
let fields = storage_struct
.borrow()
Expand Down
2 changes: 1 addition & 1 deletion aztec_macros/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ pub fn get_serialized_length(
trait_name: &str,
typ: &Type,
interner: &NodeInterner,
) -> Result<u64, MacroError> {
) -> Result<u32, MacroError> {
let serialized_trait_impl_kind = traits
.iter()
.find_map(|&trait_id| {
Expand Down
6 changes: 3 additions & 3 deletions compiler/noirc_evaluator/src/brillig/brillig_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ use acvm::{
use debug_show::DebugShow;

/// The Brillig VM does not apply a limit to the memory address space,
/// As a convention, we take use 64 bits. This means that we assume that
/// memory has 2^64 memory slots.
pub(crate) const BRILLIG_MEMORY_ADDRESSING_BIT_SIZE: u32 = 64;
/// As a convention, we take use 32 bits. This means that we assume that
/// memory has 2^32 memory slots.
pub(crate) const BRILLIG_MEMORY_ADDRESSING_BIT_SIZE: u32 = 32;

// Registers reserved in runtime for special purposes.
pub(crate) enum ReservedRegisters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ impl BrilligContext {

let current_revert_data_pointer = ctx.allocate_register();
ctx.mov_instruction(current_revert_data_pointer, revert_data.pointer);
let revert_data_id =
ctx.make_usize_constant_instruction((error_selector as u128).into());
let revert_data_id = ctx.make_constant_instruction((error_selector as u128).into(), 64);
ctx.store_instruction(current_revert_data_pointer, revert_data_id.address);

ctx.codegen_usize_op_in_place(current_revert_data_pointer, BrilligBinaryOp::Add, 1);
Expand Down
Loading

0 comments on commit bdb2bc6

Please sign in to comment.