Skip to content

Commit

Permalink
Merge branch 'master' into kw/update-bb
Browse files Browse the repository at this point in the history
* master:
  chore: remove messagepack serialization logic (#2865)
  chore: improve workspace clean (#2870)
  fix: finer bit size in bound constrain (#2869)
  feat: Contract events in artifacts (#2873)
  • Loading branch information
TomAFrench committed Sep 28, 2023
2 parents b7a5648 + c556878 commit 8ce02c5
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 108 deletions.
23 changes: 0 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions acvm-repo/acir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ acir_field.workspace = true
brillig.workspace = true
serde.workspace = true
thiserror.workspace = true
rmp-serde = { version = "1.1.0", optional = true }
flate2 = "1.0.24"
bincode.workspace = true

Expand All @@ -30,4 +29,3 @@ strum_macros = "0.24"
default = ["bn254"]
bn254 = ["acir_field/bn254", "brillig/bn254"]
bls12_381 = ["acir_field/bls12_381", "brillig/bls12_381"]
serialize-messagepack = ["rmp-serde"]
19 changes: 0 additions & 19 deletions acvm-repo/acir/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,6 @@ impl Circuit {
PublicInputs(public_inputs)
}

#[cfg(feature = "serialize-messagepack")]
pub fn write<W: std::io::Write>(&self, writer: W) -> std::io::Result<()> {
let buf = rmp_serde::to_vec(&self).unwrap();
let mut deflater = flate2::write::DeflateEncoder::new(writer, Compression::best());
deflater.write_all(&buf).unwrap();

Ok(())
}
#[cfg(feature = "serialize-messagepack")]
pub fn read<R: std::io::Read>(reader: R) -> std::io::Result<Self> {
let mut deflater = flate2::read::DeflateDecoder::new(reader);
let mut buf_d = Vec::new();
deflater.read_to_end(&mut buf_d).unwrap();
let circuit = rmp_serde::from_slice(buf_d.as_slice()).unwrap();
Ok(circuit)
}

#[cfg(not(feature = "serialize-messagepack"))]
pub fn write<W: std::io::Write>(&self, writer: W) -> std::io::Result<()> {
let buf = bincode::serialize(&self).unwrap();
let mut encoder = flate2::write::GzEncoder::new(writer, Compression::default());
Expand All @@ -151,7 +133,6 @@ impl Circuit {
Ok(())
}

#[cfg(not(feature = "serialize-messagepack"))]
pub fn read<R: std::io::Read>(reader: R) -> std::io::Result<Self> {
let mut gz_decoder = flate2::read::GzDecoder::new(reader);
let mut buf_d = Vec::new();
Expand Down
43 changes: 0 additions & 43 deletions acvm-repo/acir/src/native_types/witness_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ use thiserror::Error;

use crate::native_types::Witness;

#[cfg(feature = "serialize-messagepack")]
#[derive(Debug, Error)]
enum SerializationError {
#[error(transparent)]
MsgpackEncode(#[from] rmp_serde::encode::Error),

#[error(transparent)]
MsgpackDecode(#[from] rmp_serde::decode::Error),

#[error(transparent)]
Deflate(#[from] std::io::Error),
}

#[cfg(not(feature = "serialize-messagepack"))]
#[derive(Debug, Error)]
enum SerializationError {
#[error(transparent)]
Expand Down Expand Up @@ -92,20 +78,6 @@ impl From<BTreeMap<Witness, FieldElement>> for WitnessMap {
}
}

#[cfg(feature = "serialize-messagepack")]
impl TryFrom<WitnessMap> for Vec<u8> {
type Error = WitnessMapError;

fn try_from(val: WitnessMap) -> Result<Self, Self::Error> {
let buf = rmp_serde::to_vec(&val).map_err(|err| WitnessMapError(err.into()))?;
let mut deflater = flate2::write::DeflateEncoder::new(buf.as_slice(), Compression::best());
let mut buf_c = Vec::new();
deflater.read_to_end(&mut buf_c).map_err(|err| WitnessMapError(err.into()))?;
Ok(buf_c)
}
}

#[cfg(not(feature = "serialize-messagepack"))]
impl TryFrom<WitnessMap> for Vec<u8> {
type Error = WitnessMapError;

Expand All @@ -118,21 +90,6 @@ impl TryFrom<WitnessMap> for Vec<u8> {
}
}

#[cfg(feature = "serialize-messagepack")]
impl TryFrom<&[u8]> for WitnessMap {
type Error = WitnessMapError;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let mut deflater = flate2::bufread::DeflateDecoder::new(bytes);
let mut buf_d = Vec::new();
deflater.read_to_end(&mut buf_d).map_err(|err| WitnessMapError(err.into()))?;
let witness_map =
rmp_serde::from_slice(buf_d.as_slice()).map_err(|err| WitnessMapError(err.into()))?;
Ok(Self(witness_map))
}
}

#[cfg(not(feature = "serialize-messagepack"))]
impl TryFrom<&[u8]> for WitnessMap {
type Error = WitnessMapError;

Expand Down
3 changes: 2 additions & 1 deletion acvm-repo/acvm_js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"build": "bash ./build.sh",
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
"test:browser": "web-test-runner",
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0"
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0",
"clean": "chmod u+w web nodejs && rm -rf web nodejs"
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_driver/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::BTreeMap;

use acvm::acir::circuit::Circuit;
use fm::FileId;
use noirc_abi::Abi;
use noirc_abi::{Abi, ContractEvent};
use noirc_errors::debug_info::DebugInfo;

use super::debug::DebugFile;
Expand Down Expand Up @@ -34,6 +34,11 @@ pub struct CompiledContract {
/// stored in this `Vector`.
pub functions: Vec<ContractFunction>,

/// All the events defined inside the contract scope.
/// An event is a struct value that can be emitted via oracles
/// by any contract function during execution.
pub events: Vec<ContractEvent>,

pub file_map: BTreeMap<FileId, DebugFile>,
}

Expand Down
17 changes: 15 additions & 2 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use clap::Args;
use debug::filter_relevant_files;
use fm::FileId;
use noirc_abi::{AbiParameter, AbiType};
use noirc_abi::{AbiParameter, AbiType, ContractEvent};
use noirc_errors::{CustomDiagnostic, FileDiagnostic};
use noirc_evaluator::{create_circuit, into_abi_params};
use noirc_frontend::graph::{CrateId, CrateName};
Expand Down Expand Up @@ -285,7 +285,20 @@ fn compile_contract_inner(
let debug_infos: Vec<_> = functions.iter().map(|function| function.debug.clone()).collect();
let file_map = filter_relevant_files(&debug_infos, &context.file_manager);

Ok(CompiledContract { name: contract.name, functions, file_map })
Ok(CompiledContract {
name: contract.name,
events: contract
.events
.iter()
.map(|event_id| {
let typ = context.def_interner.get_struct(*event_id);
let typ = typ.borrow();
ContractEvent::from_struct_type(context, &typ)
})
.collect(),
functions,
file_map,
})
} else {
Err(errors)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,18 +596,19 @@ impl GeneratedAcir {
// we now have lhs+offset <= rhs <=> lhs_offset <= rhs_offset

let bit_size = bit_size_u128(rhs_offset);
// r = 2^bit_size - rhs_offset
// r = 2^bit_size - rhs_offset -1, is of bit size 'bit_size' by construtction
let r = (1_u128 << bit_size) - rhs_offset - 1;
// however, since it is a constant, we can compute it's actual bit size
let r_bit_size = bit_size_u128(r);
// witness = lhs_offset + r
assert!(bits + bit_size < FieldElement::max_num_bits()); //we need to ensure lhs_offset + r does not overflow
assert!(bits + r_bit_size < FieldElement::max_num_bits()); //we need to ensure lhs_offset + r does not overflow
let mut aor = lhs_offset;
aor.q_c += FieldElement::from(r);
let witness = self.get_or_create_witness(&aor);
// lhs_offset<=rhs_offset <=> lhs_offset + r < rhs_offset + r = 2^bit_size <=> witness < 2^bit_size
self.range_constraint(witness, bit_size)?;
return Ok(());
}

// General case: lhs_offset<=rhs <=> rhs-lhs_offset>=0 <=> rhs-lhs_offset is a 'bits' bit integer
let sub_expression = rhs - &lhs_offset; //rhs-lhs_offset
let w = self.create_witness_for_expression(&sub_expression);
Expand Down
21 changes: 17 additions & 4 deletions compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::graph::CrateId;
use crate::hir::def_collector::dc_crate::{CompilationError, DefCollector};
use crate::hir::Context;
use crate::node_interner::{FuncId, NodeInterner};
use crate::node_interner::{FuncId, NodeInterner, StructId};
use crate::parser::{parse_program, ParsedModule, ParserError};
use crate::token::{FunctionAttribute, TestScope};
use crate::token::{FunctionAttribute, SecondaryAttribute, TestScope};
use arena::{Arena, Index};
use fm::{FileId, FileManager};
use noirc_errors::Location;
Expand Down Expand Up @@ -182,8 +182,20 @@ impl CrateDefMap {
})
.collect();

let events = module
.type_definitions()
.filter_map(|id| {
id.as_type().filter(|struct_id| {
interner
.struct_attributes(struct_id)
.iter()
.any(|attr| attr == &SecondaryAttribute::Event)
})
})
.collect();

let name = self.get_module_path(id, module.parent);
Some(Contract { name, location: module.location, functions })
Some(Contract { name, location: module.location, functions, events })
} else {
None
}
Expand Down Expand Up @@ -236,13 +248,14 @@ pub struct ContractFunctionMeta {
pub is_entry_point: bool,
}

/// A 'contract' in Noir source code with the given name and functions.
/// A 'contract' in Noir source code with a given name, functions and events.
/// This is not an AST node, it is just a convenient form to return for CrateDefMap::get_all_contracts.
pub struct Contract {
/// To keep `name` semi-unique, it is prefixed with the names of parent modules via CrateDefMap::get_module_path
pub name: String,
pub location: Location,
pub functions: Vec<ContractFunctionMeta>,
pub events: Vec<StructId>,
}

/// Given a FileId, fetch the File, from the FileManager and parse it's content
Expand Down
4 changes: 4 additions & 0 deletions compiler/noirc_frontend/src/hir/def_map/module_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ impl ModuleData {
self.scope.find_name(name)
}

pub fn type_definitions(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
self.definitions.types().values().map(|(id, _)| *id)
}

/// Return an iterator over all definitions defined within this module,
/// excluding any type definitions.
pub fn value_definitions(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
Expand Down
4 changes: 4 additions & 0 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ impl Attribute {
["contract_library_method"] => {
Attribute::Secondary(SecondaryAttribute::ContractLibraryMethod)
}
["event"] => Attribute::Secondary(SecondaryAttribute::Event),
["deprecated", name] => {
if !name.starts_with('"') && !name.ends_with('"') {
return Err(LexerErrorKind::MalformedFuncAttribute {
Expand Down Expand Up @@ -544,6 +545,7 @@ pub enum SecondaryAttribute {
// is a helper method for a contract and should not be seen as
// the entry point.
ContractLibraryMethod,
Event,
Custom(String),
}

Expand All @@ -556,6 +558,7 @@ impl fmt::Display for SecondaryAttribute {
}
SecondaryAttribute::Custom(ref k) => write!(f, "#[{k}]"),
SecondaryAttribute::ContractLibraryMethod => write!(f, "#[contract_library_method]"),
SecondaryAttribute::Event => write!(f, "#[event]"),
}
}
}
Expand All @@ -578,6 +581,7 @@ impl AsRef<str> for SecondaryAttribute {
SecondaryAttribute::Deprecated(None) => "",
SecondaryAttribute::Custom(string) => string,
SecondaryAttribute::ContractLibraryMethod => "",
SecondaryAttribute::Event => "",
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::hir_def::{
function::{FuncMeta, HirFunction},
stmt::HirStatement,
};
use crate::token::Attributes;
use crate::token::{Attributes, SecondaryAttribute};
use crate::{
ContractFunctionType, FunctionDefinition, Generics, Shared, TypeAliasType, TypeBinding,
TypeBindings, TypeVariable, TypeVariableId, TypeVariableKind, Visibility,
Expand All @@ -32,6 +32,8 @@ pub struct TraitImplKey {
// pub generics: Generics - TODO
}

type StructAttributes = Vec<SecondaryAttribute>;

/// The node interner is the central storage location of all nodes in Noir's Hir (the
/// various node types can be found in hir_def). The interner is also used to collect
/// extra information about the Hir, such as the type of each node, information about
Expand Down Expand Up @@ -73,6 +75,7 @@ pub struct NodeInterner {
// methods from impls to the type.
structs: HashMap<StructId, Shared<StructType>>,

struct_attributes: HashMap<StructId, StructAttributes>,
// Type Aliases map.
//
// Map type aliases to the actual type.
Expand Down Expand Up @@ -365,6 +368,7 @@ impl Default for NodeInterner {
definitions: vec![],
id_to_type: HashMap::new(),
structs: HashMap::new(),
struct_attributes: HashMap::new(),
type_aliases: Vec::new(),
traits: HashMap::new(),
trait_implementations: HashMap::new(),
Expand Down Expand Up @@ -456,6 +460,7 @@ impl NodeInterner {

let new_struct = StructType::new(struct_id, name, typ.struct_def.span, no_fields, generics);
self.structs.insert(struct_id, Shared::new(new_struct));
self.struct_attributes.insert(struct_id, typ.struct_def.attributes.clone());
struct_id
}

Expand Down Expand Up @@ -678,6 +683,10 @@ impl NodeInterner {
&self.function_modifiers[func_id].attributes
}

pub fn struct_attributes(&self, struct_id: &StructId) -> &StructAttributes {
&self.struct_attributes[struct_id]
}

/// Returns the interned statement corresponding to `stmt_id`
pub fn statement(&self, stmt_id: &StmtId) -> HirStatement {
let def =
Expand Down
2 changes: 1 addition & 1 deletion compiler/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
"test:node": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
"test:browser": "web-test-runner",
"clean": "rm -rf ./nodejs ./web ./target ./result",
"clean": "chmod u+w web nodejs && rm -rf ./nodejs ./web ./target ./result",
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
# Nix flakes cannot build more than one derivation in one command (see https://github.com/NixOS/nix/issues/5591)
# so we use `symlinkJoin` to build everything as the "all" package.
all = pkgs.symlinkJoin { name = "all"; paths = [ nargo noir_wasm noirc_abi_wasm acvm_js ]; };
all_wasm = pkgs.symlinkJoin { name = "all_wasm"; paths = [ noir_wasm noirc_abi_wasm acvm_js ]; };

# We also export individual packages to enable `nix build .#nargo -L`, etc.
inherit nargo;
Expand Down
Loading

0 comments on commit 8ce02c5

Please sign in to comment.