Skip to content

Commit

Permalink
feat: New brillig field operations and refactor of binary operations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant authored Mar 14, 2024
1 parent 321f149 commit eb69504
Show file tree
Hide file tree
Showing 12 changed files with 546 additions and 256 deletions.
3 changes: 3 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 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
125 changes: 124 additions & 1 deletion noir/noir-repo/acvm-repo/acir/codegen/acir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,31 @@ namespace Circuit {
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 @@ -1317,6 +1335,41 @@ Circuit::BinaryFieldOp::Div serde::Deserializable<Circuit::BinaryFieldOp::Div>::
return obj;
}

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 serde::deserialization_error("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) {
Expand Down Expand Up @@ -1352,6 +1405,76 @@ Circuit::BinaryFieldOp::Equals serde::Deserializable<Circuit::BinaryFieldOp::Equ
return obj;
}

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 serde::deserialization_error("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 serde::deserialization_error("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) {
Expand Down
7 changes: 7 additions & 0 deletions noir/noir-repo/acvm-repo/brillig/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,16 @@ pub enum BinaryFieldOp {
Add,
Sub,
Mul,
/// Field division
Div,
/// Integer division
IntegerDiv,
/// (==) equal
Equals,
/// (<) Field less than
LessThan,
/// (<=) field less or equal
LessThanEquals,
}

/// Binary fixed-length integer expressions
Expand Down
9 changes: 9 additions & 0 deletions noir/noir-repo/acvm-repo/brillig_vm/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ pub(crate) fn evaluate_binary_field_op(
BinaryFieldOp::Sub => a - b,
BinaryFieldOp::Mul => a * b,
BinaryFieldOp::Div => a / b,
BinaryFieldOp::IntegerDiv => {
let a_big = BigUint::from_bytes_be(&a.to_be_bytes());
let b_big = BigUint::from_bytes_be(&b.to_be_bytes());

let result = a_big / b_big;
FieldElement::from_be_bytes_reduce(&result.to_bytes_be())
}
BinaryFieldOp::Equals => (a == b).into(),
BinaryFieldOp::LessThan => (a < b).into(),
BinaryFieldOp::LessThanEquals => (a <= b).into(),
}
}

Expand Down
Loading

0 comments on commit eb69504

Please sign in to comment.