Skip to content

Commit

Permalink
Merge 71f3da3 into 9bf2dcb
Browse files Browse the repository at this point in the history
  • Loading branch information
AztecBot authored Sep 13, 2024
2 parents 9bf2dcb + 71f3da3 commit fbb9e51
Show file tree
Hide file tree
Showing 22 changed files with 267 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79995c84e0f88c1ee72876fd21c50c33830597fc
bcec12dbf79b658406dc21083f8fdeef8962085e
56 changes: 55 additions & 1 deletion acvm-repo/acir/codegen/acir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,16 @@ namespace Program {
static BinaryIntOp bincodeDeserialize(std::vector<uint8_t>);
};

struct Not {
Program::MemoryAddress destination;
Program::MemoryAddress source;
Program::IntegerBitSize bit_size;

friend bool operator==(const Not&, const Not&);
std::vector<uint8_t> bincodeSerialize() const;
static Not bincodeDeserialize(std::vector<uint8_t>);
};

struct Cast {
Program::MemoryAddress destination;
Program::MemoryAddress source;
Expand Down Expand Up @@ -727,7 +737,7 @@ namespace Program {
static Stop bincodeDeserialize(std::vector<uint8_t>);
};

std::variant<BinaryFieldOp, BinaryIntOp, Cast, JumpIfNot, JumpIf, Jump, CalldataCopy, Call, Const, IndirectConst, Return, ForeignCall, Mov, ConditionalMov, Load, Store, BlackBox, Trap, Stop> value;
std::variant<BinaryFieldOp, BinaryIntOp, Not, Cast, JumpIfNot, JumpIf, Jump, CalldataCopy, Call, Const, IndirectConst, Return, ForeignCall, Mov, ConditionalMov, Load, Store, BlackBox, Trap, Stop> value;

friend bool operator==(const BrilligOpcode&, const BrilligOpcode&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down Expand Up @@ -5110,6 +5120,50 @@ Program::BrilligOpcode::BinaryIntOp serde::Deserializable<Program::BrilligOpcode
return obj;
}

namespace Program {

inline bool operator==(const BrilligOpcode::Not &lhs, const BrilligOpcode::Not &rhs) {
if (!(lhs.destination == rhs.destination)) { return false; }
if (!(lhs.source == rhs.source)) { return false; }
if (!(lhs.bit_size == rhs.bit_size)) { return false; }
return true;
}

inline std::vector<uint8_t> BrilligOpcode::Not::bincodeSerialize() const {
auto serializer = serde::BincodeSerializer();
serde::Serializable<BrilligOpcode::Not>::serialize(*this, serializer);
return std::move(serializer).bytes();
}

inline BrilligOpcode::Not BrilligOpcode::Not::bincodeDeserialize(std::vector<uint8_t> input) {
auto deserializer = serde::BincodeDeserializer(input);
auto value = serde::Deserializable<BrilligOpcode::Not>::deserialize(deserializer);
if (deserializer.get_buffer_offset() < input.size()) {
throw serde::deserialization_error("Some input bytes were not read");
}
return value;
}

} // end of namespace Program

template <>
template <typename Serializer>
void serde::Serializable<Program::BrilligOpcode::Not>::serialize(const Program::BrilligOpcode::Not &obj, Serializer &serializer) {
serde::Serializable<decltype(obj.destination)>::serialize(obj.destination, serializer);
serde::Serializable<decltype(obj.source)>::serialize(obj.source, serializer);
serde::Serializable<decltype(obj.bit_size)>::serialize(obj.bit_size, serializer);
}

template <>
template <typename Deserializer>
Program::BrilligOpcode::Not serde::Deserializable<Program::BrilligOpcode::Not>::deserialize(Deserializer &deserializer) {
Program::BrilligOpcode::Not obj;
obj.destination = serde::Deserializable<decltype(obj.destination)>::deserialize(deserializer);
obj.source = serde::Deserializable<decltype(obj.source)>::deserialize(deserializer);
obj.bit_size = serde::Deserializable<decltype(obj.bit_size)>::deserialize(deserializer);
return obj;
}

namespace Program {

inline bool operator==(const BrilligOpcode::Cast &lhs, const BrilligOpcode::Cast &rhs) {
Expand Down
34 changes: 17 additions & 17 deletions acvm-repo/acir/tests/test_program_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ 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, 81, 49, 10, 128, 48, 12, 108, 196, 138, 224, 230,
75, 226, 15, 252, 140, 131, 139, 131, 136, 239, 111, 161, 9, 28, 165, 205, 210, 28, 132,
36, 119, 16, 114, 9, 133, 130, 53, 7, 73, 29, 37, 107, 143, 80, 238, 148, 204, 99, 56, 200,
111, 22, 227, 190, 83, 93, 16, 146, 193, 112, 22, 225, 34, 168, 205, 142, 174, 241, 218,
206, 179, 121, 49, 188, 109, 57, 84, 191, 159, 255, 122, 63, 235, 199, 189, 190, 197, 237,
13, 45, 1, 20, 245, 146, 30, 92, 2, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 173, 81, 73, 10, 192, 48, 8, 140, 165, 91, 160, 183,
126, 196, 254, 160, 159, 233, 161, 151, 30, 74, 200, 251, 19, 136, 130, 132, 196, 75, 28,
16, 199, 17, 212, 65, 112, 5, 123, 14, 32, 190, 80, 230, 90, 130, 181, 155, 50, 142, 225,
2, 187, 89, 40, 239, 157, 106, 2, 82, 116, 138, 51, 118, 239, 171, 222, 108, 232, 218, 139,
125, 198, 179, 113, 83, 188, 29, 57, 86, 226, 239, 23, 159, 63, 104, 63, 238, 213, 45, 237,
108, 244, 18, 195, 174, 252, 193, 92, 2, 0, 0,
];

assert_eq!(bytes, expected_serialization)
Expand Down Expand Up @@ -338,17 +338,17 @@ 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, 85, 81, 14, 194, 48, 8, 133, 118, 206, 26, 255, 60,
129, 137, 30, 160, 211, 11, 120, 23, 227, 159, 70, 63, 61, 190, 146, 209, 140, 177, 46,
251, 24, 77, 182, 151, 44, 116, 45, 16, 120, 64, 139, 208, 34, 252, 63, 228, 245, 134, 165,
99, 73, 251, 30, 250, 72, 186, 55, 150, 113, 30, 26, 180, 243, 21, 75, 197, 232, 86, 16,
163, 47, 16, 35, 136, 250, 47, 176, 222, 150, 117, 49, 229, 207, 103, 230, 167, 130, 118,
190, 106, 254, 223, 178, 12, 154, 104, 50, 114, 48, 28, 188, 30, 82, 247, 236, 180, 23, 62,
171, 236, 178, 185, 202, 27, 194, 216, 119, 36, 54, 142, 35, 185, 149, 203, 233, 18, 131,
34, 220, 48, 167, 38, 176, 191, 18, 181, 168, 5, 63, 178, 179, 8, 123, 232, 186, 234, 254,
126, 125, 158, 143, 175, 87, 148, 74, 51, 194, 73, 172, 207, 234, 28, 149, 157, 182, 149,
144, 15, 70, 78, 23, 51, 122, 83, 190, 15, 208, 181, 70, 122, 152, 126, 56, 83, 244, 10,
181, 6, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 85, 81, 14, 194, 48, 8, 133, 118, 186, 53, 241,
207, 11, 152, 232, 1, 58, 189, 128, 119, 49, 254, 105, 244, 211, 227, 59, 50, 154, 49, 214,
100, 31, 163, 201, 246, 146, 133, 174, 5, 10, 15, 72, 17, 122, 52, 221, 135, 188, 222, 177,
116, 44, 105, 223, 195, 24, 73, 247, 206, 50, 46, 67, 139, 118, 190, 98, 169, 24, 221, 6,
98, 244, 5, 98, 4, 81, 255, 21, 214, 219, 178, 46, 166, 252, 249, 204, 252, 84, 208, 207,
215, 158, 255, 107, 150, 141, 38, 154, 140, 28, 76, 7, 111, 132, 212, 61, 65, 201, 116, 86,
217, 101, 115, 11, 226, 62, 99, 223, 145, 88, 56, 205, 228, 102, 127, 239, 53, 6, 69, 184,
97, 78, 109, 96, 127, 37, 106, 81, 11, 126, 100, 103, 17, 14, 48, 116, 213, 227, 243, 254,
190, 158, 63, 175, 40, 149, 102, 132, 179, 88, 95, 212, 57, 42, 59, 109, 43, 33, 31, 140,
156, 46, 102, 244, 230, 124, 31, 97, 104, 141, 244, 48, 253, 1, 180, 46, 168, 159, 181, 6,
0, 0,
];

assert_eq!(bytes, expected_serialization)
Expand Down
16 changes: 8 additions & 8 deletions acvm-repo/acvm_js/test/shared/complex_foreign_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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, 85, 81, 14, 194, 48, 8, 133, 118, 206, 26, 255, 60, 129, 137, 30, 160, 211,
11, 120, 23, 227, 159, 70, 63, 61, 190, 146, 209, 140, 177, 46, 251, 24, 77, 182, 151, 44, 116, 45, 16, 120, 64, 139,
208, 34, 252, 63, 228, 245, 134, 165, 99, 73, 251, 30, 250, 72, 186, 55, 150, 113, 30, 26, 180, 243, 21, 75, 197, 232,
86, 16, 163, 47, 16, 35, 136, 250, 47, 176, 222, 150, 117, 49, 229, 207, 103, 230, 167, 130, 118, 190, 106, 254, 223,
178, 12, 154, 104, 50, 114, 48, 28, 188, 30, 82, 247, 236, 180, 23, 62, 171, 236, 178, 185, 202, 27, 194, 216, 119,
36, 54, 142, 35, 185, 149, 203, 233, 18, 131, 34, 220, 48, 167, 38, 176, 191, 18, 181, 168, 5, 63, 178, 179, 8, 123,
232, 186, 234, 254, 126, 125, 158, 143, 175, 87, 148, 74, 51, 194, 73, 172, 207, 234, 28, 149, 157, 182, 149, 144, 15,
70, 78, 23, 51, 122, 83, 190, 15, 208, 181, 70, 122, 152, 126, 56, 83, 244, 10, 181, 6, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 213, 85, 81, 14, 194, 48, 8, 133, 118, 186, 53, 241, 207, 11, 152, 232, 1, 58, 189,
128, 119, 49, 254, 105, 244, 211, 227, 59, 50, 154, 49, 214, 100, 31, 163, 201, 246, 146, 133, 174, 5, 10, 15, 72, 17,
122, 52, 221, 135, 188, 222, 177, 116, 44, 105, 223, 195, 24, 73, 247, 206, 50, 46, 67, 139, 118, 190, 98, 169, 24,
221, 6, 98, 244, 5, 98, 4, 81, 255, 21, 214, 219, 178, 46, 166, 252, 249, 204, 252, 84, 208, 207, 215, 158, 255, 107,
150, 141, 38, 154, 140, 28, 76, 7, 111, 132, 212, 61, 65, 201, 116, 86, 217, 101, 115, 11, 226, 62, 99, 223, 145, 88,
56, 205, 228, 102, 127, 239, 53, 6, 69, 184, 97, 78, 109, 96, 127, 37, 106, 81, 11, 126, 100, 103, 17, 14, 48, 116,
213, 227, 243, 254, 190, 158, 63, 175, 40, 149, 102, 132, 179, 88, 95, 212, 57, 42, 59, 109, 43, 33, 31, 140, 156, 46,
102, 244, 230, 124, 31, 97, 104, 141, 244, 48, 253, 1, 180, 46, 168, 159, 181, 6, 0, 0,
]);
export const initialWitnessMap: WitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000001'],
Expand Down
10 changes: 5 additions & 5 deletions acvm-repo/acvm_js/test/shared/foreign_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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, 81, 49, 10, 128, 48, 12, 108, 196, 138, 224, 230, 75, 226, 15, 252, 140, 131,
139, 131, 136, 239, 111, 161, 9, 28, 165, 205, 210, 28, 132, 36, 119, 16, 114, 9, 133, 130, 53, 7, 73, 29, 37, 107,
143, 80, 238, 148, 204, 99, 56, 200, 111, 22, 227, 190, 83, 93, 16, 146, 193, 112, 22, 225, 34, 168, 205, 142, 174,
241, 218, 206, 179, 121, 49, 188, 109, 57, 84, 191, 159, 255, 122, 63, 235, 199, 189, 190, 197, 237, 13, 45, 1, 20,
245, 146, 30, 92, 2, 0, 0,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 173, 81, 73, 10, 192, 48, 8, 140, 165, 91, 160, 183, 126, 196, 254, 160, 159, 233,
161, 151, 30, 74, 200, 251, 19, 136, 130, 132, 196, 75, 28, 16, 199, 17, 212, 65, 112, 5, 123, 14, 32, 190, 80, 230,
90, 130, 181, 155, 50, 142, 225, 2, 187, 89, 40, 239, 157, 106, 2, 82, 116, 138, 51, 118, 239, 171, 222, 108, 232,
218, 139, 125, 198, 179, 113, 83, 188, 29, 57, 86, 226, 239, 23, 159, 63, 104, 63, 238, 213, 45, 237, 108, 244, 18,
195, 174, 252, 193, 92, 2, 0, 0,
]);
export const initialWitnessMap: WitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000005'],
Expand Down
5 changes: 5 additions & 0 deletions acvm-repo/brillig/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ pub enum BrilligOpcode<F> {
lhs: MemoryAddress,
rhs: MemoryAddress,
},
Not {
destination: MemoryAddress,
source: MemoryAddress,
bit_size: IntegerBitSize,
},
Cast {
destination: MemoryAddress,
source: MemoryAddress,
Expand Down
93 changes: 93 additions & 0 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
self.increment_program_counter()
}
}
Opcode::Not { destination, source, bit_size } => {
if let Err(error) = self.process_not(*source, *destination, *bit_size) {
self.fail(error)
} else {
self.increment_program_counter()
}
}
Opcode::Cast { destination: destination_address, source: source_address, bit_size } => {
let source_value = self.memory.read(*source_address);
let casted_value = self.cast(*bit_size, source_value);
Expand Down Expand Up @@ -709,6 +716,36 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
Ok(())
}

fn process_not(
&mut self,
source: MemoryAddress,
destination: MemoryAddress,
op_bit_size: IntegerBitSize,
) -> Result<(), String> {
let (value, bit_size) = self
.memory
.read(source)
.extract_integer()
.ok_or("Not opcode source is not an integer")?;

if bit_size != op_bit_size {
return Err(format!(
"Not opcode source bit size {} does not match expected bit size {}",
bit_size, op_bit_size
));
}

let negated_value = if let IntegerBitSize::U128 = bit_size {
!value
} else {
let bit_size: u32 = bit_size.into();
let mask = (1_u128 << bit_size as u128) - 1;
(!value) & mask
};
self.memory.write(destination, MemoryValue::new_integer(negated_value, bit_size));
Ok(())
}

/// Casts a value to a different bit size.
fn cast(&self, target_bit_size: BitSize, source_value: MemoryValue<F>) -> MemoryValue<F> {
match (source_value, target_bit_size) {
Expand Down Expand Up @@ -952,6 +989,62 @@ mod tests {
assert_eq!(casted_value.to_field(), (2_u128.pow(8) - 1).into());
}

#[test]
fn not_opcode() {
let calldata: Vec<FieldElement> = vec![(1_usize).into()];

let opcodes = &[
Opcode::Const {
destination: MemoryAddress(0),
bit_size: BitSize::Integer(IntegerBitSize::U32),
value: FieldElement::from(1u64),
},
Opcode::Const {
destination: MemoryAddress(1),
bit_size: BitSize::Integer(IntegerBitSize::U32),
value: FieldElement::from(0u64),
},
Opcode::CalldataCopy {
destination_address: MemoryAddress(0),
size_address: MemoryAddress(0),
offset_address: MemoryAddress(1),
},
Opcode::Cast {
destination: MemoryAddress::from(1),
source: MemoryAddress::from(0),
bit_size: BitSize::Integer(IntegerBitSize::U128),
},
Opcode::Not {
destination: MemoryAddress::from(1),
source: MemoryAddress::from(1),
bit_size: IntegerBitSize::U128,
},
Opcode::Stop { return_data_offset: 1, return_data_size: 1 },
];
let mut vm = VM::new(calldata, opcodes, vec![], &StubbedBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
let status = vm.process_opcode();
assert_eq!(status, VMStatus::Finished { return_data_offset: 1, return_data_size: 1 });

let VM { memory, .. } = vm;

let (negated_value, _) = memory
.read(MemoryAddress::from(1))
.extract_integer()
.expect("Expected integer as the output of Not");
assert_eq!(negated_value, !1_u128);
}

#[test]
fn mov_opcode() {
let calldata: Vec<FieldElement> = vec![(1u128).into(), (2u128).into(), (3u128).into()];
Expand Down
18 changes: 6 additions & 12 deletions compiler/noirc_evaluator/src/brillig/brillig_ir/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ impl<F: AcirField + DebugToString, Registers: RegisterAllocator> BrilligContext<
result: SingleAddrVariable,
) {
self.debug_show.not_instruction(input.address, input.bit_size, result.address);
// Compile !x as ((-1) - x)
let u_max = F::from(2_u128).pow(&F::from(input.bit_size as u128)) - F::one();
let max = self.make_constant(u_max, input.bit_size);

self.binary(max, input, result, BrilligBinaryOp::Sub);
self.deallocate_single_addr(max);
assert_eq!(input.bit_size, result.bit_size, "Not operands should have the same bit size");
self.push_opcode(BrilligOpcode::Not {
destination: result.address,
source: input.address,
bit_size: input.bit_size.try_into().unwrap(),
});
}

/// Utility method to perform a binary instruction with a memory address
Expand Down Expand Up @@ -418,12 +418,6 @@ impl<F: AcirField + DebugToString, Registers: RegisterAllocator> BrilligContext<
var
}

fn make_constant(&mut self, constant: F, bit_size: u32) -> SingleAddrVariable {
let var = SingleAddrVariable::new(self.allocate_register(), bit_size);
self.constant(var.address, var.bit_size, constant, false);
var
}

/// Returns a register which holds the value of an usize constant
pub(crate) fn make_usize_constant_instruction(&mut self, constant: F) -> SingleAddrVariable {
let register = self.allocate_register();
Expand Down
2 changes: 1 addition & 1 deletion scripts/install_bb.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION="0.51.1"
VERSION="0.55.0"

BBUP_PATH=~/.bb/bbup

Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/bench_2_to_17/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "bench_2_to_17"
type = "bin"
authors = [""]
compiler_version = ">=0.33.0"

[dependencies]
1 change: 1 addition & 0 deletions test_programs/execution_success/bench_2_to_17/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "3"
8 changes: 8 additions & 0 deletions test_programs/execution_success/bench_2_to_17/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::hash::poseidon2;

global len = 2450 * 2;
fn main(x: Field) {
let ped_input = [x; len];
let mut val = poseidon2::Poseidon2::hash(ped_input, len);
assert(val != 0);
}
7 changes: 7 additions & 0 deletions test_programs/execution_success/fold_2_to_17/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "fold_2_to_17"
type = "bin"
authors = [""]
compiler_version = ">=0.25.0"

[dependencies]
1 change: 1 addition & 0 deletions test_programs/execution_success/fold_2_to_17/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "2"
16 changes: 16 additions & 0 deletions test_programs/execution_success/fold_2_to_17/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::hash::poseidon2;

global len = 2450 * 2 - 240; // for just under 2^17 gates
fn main(x: Field) {
let ped_input = [x; len];
let mut val = poseidon2::Poseidon2::hash(ped_input, len);
let z = foo(x);
assert(val == z);
}

#[fold]
fn foo(x: Field) -> Field {
let ped_input = [x; len];
let mut val = poseidon2::Poseidon2::hash(ped_input, len);
val
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "single_verify_proof"
type = "bin"
authors = [""]
compiler_version = ">=0.24.0"

[dependencies]
Loading

0 comments on commit fbb9e51

Please sign in to comment.