Skip to content

Commit

Permalink
Merge branch 'master' into ad/try-fix-acir
Browse files Browse the repository at this point in the history
  • Loading branch information
ludamad authored Mar 14, 2024
2 parents bcb300d + eb69504 commit 8e12a7f
Show file tree
Hide file tree
Showing 16 changed files with 684 additions and 262 deletions.
2 changes: 1 addition & 1 deletion avm-transpiler/src/opcodes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// All AVM opcodes
/// Keep updated with TS and yellow paper!
#[derive(PartialEq, Copy, Clone)]
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum AvmOpcode {
// Compute
ADD,
Expand Down
20 changes: 20 additions & 0 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
BinaryFieldOp::Mul => AvmOpcode::MUL,
BinaryFieldOp::Div => AvmOpcode::FDIV,
BinaryFieldOp::Equals => AvmOpcode::EQ,
BinaryFieldOp::LessThan => AvmOpcode::LT,
BinaryFieldOp::LessThanEquals => AvmOpcode::LTE,
BinaryFieldOp::IntegerDiv => AvmOpcode::DIV,
};
avm_instrs.push(AvmInstruction {
opcode: avm_opcode,
Expand All @@ -58,6 +61,10 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
},
],
});
// Brillig currently expects comparison instructions to return an u1 (for us, an u8).
if avm_opcode == AvmOpcode::EQ {
avm_instrs.push(generate_cast_instruction(destination.to_usize() as u32, destination.to_usize() as u32, AvmTypeTag::UINT8));
}
}
BrilligOpcode::BinaryIntOp {
destination,
Expand Down Expand Up @@ -114,6 +121,10 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
},
],
});
// Brillig currently expects comparison instructions to return an u1 (for us, an u8).
if avm_opcode == AvmOpcode::EQ || avm_opcode == AvmOpcode::LT || avm_opcode == AvmOpcode::LTE {
avm_instrs.push(generate_cast_instruction(destination.to_usize() as u32, destination.to_usize() as u32, AvmTypeTag::UINT8));
}
}
BrilligOpcode::CalldataCopy { destination_address, size, offset } => {
avm_instrs.push(AvmInstruction {
Expand Down Expand Up @@ -1005,6 +1016,15 @@ fn map_brillig_pcs_to_avm_pcs(initial_offset: usize, brillig: &Brillig) -> Vec<u
for i in 0..brillig.bytecode.len() - 1 {
let num_avm_instrs_for_this_brillig_instr = match &brillig.bytecode[i] {
BrilligOpcode::Const { bit_size: 254, .. } => 2,
// Brillig currently expects comparison instructions to return an u1 (for us, an u8).
BrilligOpcode::BinaryIntOp {
op: BinaryIntOp::Equals | BinaryIntOp::LessThan | BinaryIntOp::LessThanEquals,
..
} => 2,
BrilligOpcode::BinaryFieldOp {
op: BinaryFieldOp::Equals,
..
} => 2,
_ => 1,
};
// next Brillig pc will map to an AVM pc offset by the
Expand Down
143 changes: 142 additions & 1 deletion barretenberg/cpp/src/barretenberg/dsl/acir_format/serde/acir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,31 @@ struct BinaryFieldOp {
static Div bincodeDeserialize(std::vector<uint8_t>);
};

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

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

std::variant<Add, Sub, Mul, Div, Equals> value;
struct LessThan {
friend bool operator==(const LessThan&, const LessThan&);
std::vector<uint8_t> bincodeSerialize() const;
static LessThan bincodeDeserialize(std::vector<uint8_t>);
};

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

std::variant<Add, Sub, Mul, Div, IntegerDiv, Equals, LessThan, LessThanEquals> value;

friend bool operator==(const BinaryFieldOp&, const BinaryFieldOp&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down Expand Up @@ -1418,6 +1436,47 @@ Circuit::BinaryFieldOp::Div serde::Deserializable<Circuit::BinaryFieldOp::Div>::

namespace Circuit {

inline bool operator==(const BinaryFieldOp::IntegerDiv& lhs, const BinaryFieldOp::IntegerDiv& rhs)
{
return true;
}

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

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

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::BinaryFieldOp::IntegerDiv>::serialize(const Circuit::BinaryFieldOp::IntegerDiv& obj,
Serializer& serializer)
{}

template <>
template <typename Deserializer>
Circuit::BinaryFieldOp::IntegerDiv serde::Deserializable<Circuit::BinaryFieldOp::IntegerDiv>::deserialize(
Deserializer& deserializer)
{
Circuit::BinaryFieldOp::IntegerDiv obj;
return obj;
}

namespace Circuit {

inline bool operator==(const BinaryFieldOp::Equals& lhs, const BinaryFieldOp::Equals& rhs)
{
return true;
Expand Down Expand Up @@ -1459,6 +1518,88 @@ Circuit::BinaryFieldOp::Equals serde::Deserializable<Circuit::BinaryFieldOp::Equ

namespace Circuit {

inline bool operator==(const BinaryFieldOp::LessThan& lhs, const BinaryFieldOp::LessThan& rhs)
{
return true;
}

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

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

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::BinaryFieldOp::LessThan>::serialize(const Circuit::BinaryFieldOp::LessThan& obj,
Serializer& serializer)
{}

template <>
template <typename Deserializer>
Circuit::BinaryFieldOp::LessThan serde::Deserializable<Circuit::BinaryFieldOp::LessThan>::deserialize(
Deserializer& deserializer)
{
Circuit::BinaryFieldOp::LessThan obj;
return obj;
}

namespace Circuit {

inline bool operator==(const BinaryFieldOp::LessThanEquals& lhs, const BinaryFieldOp::LessThanEquals& rhs)
{
return true;
}

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

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

} // end of namespace Circuit

template <>
template <typename Serializer>
void serde::Serializable<Circuit::BinaryFieldOp::LessThanEquals>::serialize(
const Circuit::BinaryFieldOp::LessThanEquals& obj, Serializer& serializer)
{}

template <>
template <typename Deserializer>
Circuit::BinaryFieldOp::LessThanEquals serde::Deserializable<Circuit::BinaryFieldOp::LessThanEquals>::deserialize(
Deserializer& deserializer)
{
Circuit::BinaryFieldOp::LessThanEquals obj;
return obj;
}

namespace Circuit {

inline bool operator==(const BinaryIntOp& lhs, const BinaryIntOp& rhs)
{
if (!(lhs.value == rhs.value)) {
Expand Down
18 changes: 18 additions & 0 deletions barretenberg/cpp/src/barretenberg/ultra_honk/oink_prover.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
#pragma once
// clang-format off
/* )\ /|
* .-/'-|_/ |
* __ __,-' ( / \/
* .-'" "'-..__,-'"" -o.`-._
* / '/
* *--._ ./ _.--
* | _.-'
* : .-/
* \ )_ /
* \ _) / \(
* `. /-.___.---'( / \\
* ( / \\ \( L\
* \( L\ \\
* \\ \\
* L\ L\
*/
// clang-format on
#include <utility>

#include "barretenberg/flavor/goblin_ultra.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ contract AvmTest {
use crate::Note;

// Libs
use dep::aztec::prelude::Map;
use dep::aztec::state_vars::PublicMutable;
use dep::aztec::protocol_types::{address::{AztecAddress, EthAddress}, constants::L1_TO_L2_MESSAGE_LENGTH};
use dep::aztec::protocol_types::abis::function_selector::FunctionSelector;
use dep::aztec::protocol_types::{hash::pedersen_hash, traits::{ToField}};
use dep::compressed_string::CompressedString;

// avm lib
Expand All @@ -32,6 +34,7 @@ contract AvmTest {
struct Storage {
single: PublicMutable<Field>,
list: PublicMutable<Note>,
map: Map<AztecAddress, PublicMutable<u32>>,
}

#[aztec(public-vm)]
Expand Down Expand Up @@ -61,6 +64,26 @@ contract AvmTest {
note.serialize()
}

#[aztec(public-vm)]
fn setStorageMap(to: AztecAddress, amount: u32) -> pub Field {
storage.map.at(to).write(amount);
// returns storage slot for key
pedersen_hash([storage.map.storage_slot, to.to_field()], 0)
}

#[aztec(public-vm)]
fn addStorageMap(to: AztecAddress, amount: u32) -> pub Field {
let new_balance = storage.map.at(to).read().add(amount);
storage.map.at(to).write(new_balance);
// returns storage slot for key
pedersen_hash([storage.map.storage_slot, to.to_field()], 0)
}

#[aztec(public-vm)]
fn readStorageMap(address: AztecAddress) -> pub u32 {
storage.map.at(address).read()
}

#[aztec(public-vm)]
fn addArgsReturn(argA: Field, argB: Field) -> pub Field {
argA + argB
Expand Down
Loading

0 comments on commit 8e12a7f

Please sign in to comment.