Skip to content

Commit

Permalink
repro constraint explosion
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Feb 15, 2024
1 parent 3ea26fb commit 58da881
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 117 deletions.
11 changes: 8 additions & 3 deletions noir-projects/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ use avm::AVMContext;
struct Context {
private: Option<&mut PrivateContext>,
public: Option<&mut PublicContext>,
public_vm: Option<&mut AVMContext>,
}

impl Context {
pub fn private(context: &mut PrivateContext) -> Context {
Context { private: Option::some(context), public: Option::none() }
Context { private: Option::some(context), public: Option::none(), public_vm: Option::none() }
}

pub fn public(context: &mut PublicContext) -> Context {
Context { public: Option::some(context), private: Option::none() }
Context { public: Option::some(context), private: Option::none(), public_vm: Option::none() }
}

pub fn public_vm(context: &mut AVMContext) -> Context {
Context { public_vm: Option::some(context), public: Option::none(), private: Option::none() }
}

pub fn none() -> Context {
Context { public: Option::none(), private: Option::none() }
Context { public: Option::none(), private: Option::none(), public_vm: Option::none() }
}
}
14 changes: 10 additions & 4 deletions noir-projects/aztec-nr/aztec/src/oracle/storage.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ pub fn storage_read<N>(storage_slot: Field) -> [Field; N] {
}

#[oracle(storageWrite)]
fn storage_write_oracle<N>(_storage_slot: Field, _values: [Field; N]) -> [Field; N] {}
fn storage_write_oracle(_storage_slot: Field, _value: Field) -> Field {}

unconstrained pub fn storage_write<N>(storage_slot: Field, fields: [Field; N]) {
let _hash = storage_write_oracle(storage_slot, fields);
}
#[oracle(storageWrite)]
pub fn storage_writes<N>(_storage_slot: Field, _value: [Field; N]) -> [Field; N] {}

#[oracle(storageWrite)]
pub fn storage_write(_storage_slot: Field, _value: Field) -> Field {}

// unconstrained pub fn storage_write(storage_slot: Field, field: Field) {
// let _hash = storage_write_oracle(storage_slot, field);
// }
7 changes: 6 additions & 1 deletion noir-projects/aztec-nr/aztec/src/state_vars/public_state.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::context::{Context};
use crate::oracle::storage::storage_read;
use crate::oracle::storage::storage_write;
use crate::oracle::storage::storage_writes;
use dep::std::option::Option;
use dep::protocol_types::traits::{Deserialize, Serialize};
use crate::state_vars::storage::Storage;
Expand Down Expand Up @@ -38,7 +39,11 @@ impl<T> PublicState<T> {
pub fn write<T_SERIALIZED_LEN>(self, value: T) where T: Serialize<T_SERIALIZED_LEN> {
assert(self.context.private.is_none(), "Public state writes only supported in public functions");
let fields = T::serialize(value);
storage_write(self.storage_slot, fields);
// storage_writes(self.storage_slot, fields);
for i in 0..1 {
// Convert this in the public simulator
println(i);
}
}
// docs:end:public_state_struct_write
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ impl<T> StablePublicState<T> {
// This is currently impractical, as public functions are never marked `is_contract_deployment`
// in the `call_context`, only private functions will have this flag set.
let fields = T::serialize(value);
storage_write(self.storage_slot, fields);
for i in 0..fields.len() {
storage_write(self.storage_slot + i, fields[i]);
}
}

pub fn read_public<T_SERIALIZED_LEN>(self) -> T where T: Deserialize<T_SERIALIZED_LEN> {
Expand Down
12 changes: 9 additions & 3 deletions noir-projects/aztec-nr/slow-updates-tree/src/slow_map.nr
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ impl<N,M> SlowMap<N,M> {
after: initial_root,
};
let fields = root_object.serialize();
storage_write(self.storage_slot, fields);
for i in 0..fields.len() {
storage_write(self.storage_slot + i, fields[i]);
}
}

// Reads the "CURRENT" value of the root
Expand Down Expand Up @@ -249,10 +251,14 @@ impl<N,M> SlowMap<N,M> {
fn update_unsafe(self: Self, index: Field, leaf: Leaf, root: Leaf) {
let derived_storage_slot = pedersen_hash([self.storage_slot, index]);
let fields = leaf.serialize();
storage_write(derived_storage_slot, fields);
for i in 0..fields.len() {
storage_write(self.storage_slot + i, fields[i]);
}

let fields = root.serialize();
storage_write(self.storage_slot, fields);
for i in 0..fields.len() {
storage_write(self.storage_slot + i, fields[i]);
}
}
}

Expand Down
210 changes: 110 additions & 100 deletions noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
contract AvmTest {
// Libs
use dep::aztec::protocol_types::address::{AztecAddress, EthAddress};
use dep::aztec::state_vars::public_state::PublicState;

// avm lib
use dep::aztec::avm::{
Expand All @@ -12,141 +13,150 @@ contract AvmTest {
}
};

struct Storage {
owner: PublicState<AztecAddress>
}

#[aztec(private)]
fn constructor() {}

// Public-vm macro will prefix avm to the function name for transpilation
#[aztec(public-vm)]
fn addArgsReturn(argA: Field, argB: Field) -> pub Field {
argA + argB
fn setOwner(owner: AztecAddress) {
storage.owner.write(owner);
}

#[aztec(public-vm)]
fn setOpcodeUint8() -> pub u8 {
8 as u8
}
// Public-vm macro will prefix avm to the function name for transpilation
// #[aztec(public-vm)]
// fn addArgsReturn(argA: Field, argB: Field) -> pub Field {
// argA + argB
// }

// Bit size 16 in Noir is deprecated.
// #[aztec(public-vm)]
// fn setOpcodeUint16() -> pub u16 {
// 60000 as u16
// fn setOpcodeUint8() -> pub u8 {
// 8 as u8
// }

#[aztec(public-vm)]
fn setOpcodeUint32() -> pub u32 {
1 << 30 as u32
}
// // Bit size 16 in Noir is deprecated.
// // #[aztec(public-vm)]
// // fn setOpcodeUint16() -> pub u16 {
// // 60000 as u16
// // }

#[aztec(public-vm)]
fn setOpcodeUint64() -> pub u64 {
1 << 60 as u64
}
// #[aztec(public-vm)]
// fn setOpcodeUint32() -> pub u32 {
// 1 << 30 as u32
// }

// Can't return this since it doesn't fit in a Noir field.
// #[aztec(public-vm)]
// fn setOpcodeUint128() -> pub u128 {
// 1 << 120 as u128
// fn setOpcodeUint64() -> pub u64 {
// 1 << 60 as u64
// }

// Field should fit in 128 bits
// ACIR only supports fields of up to 126 bits!
// Same with internal fields for unconstrained functions, apprently.
#[aztec(public-vm)]
fn setOpcodeSmallField() -> pub Field {
200 as Field
}
// // Can't return this since it doesn't fit in a Noir field.
// // #[aztec(public-vm)]
// // fn setOpcodeUint128() -> pub u128 {
// // 1 << 120 as u128
// // }

// /************************************************************************
// * Hashing functions
// ************************************************************************/
#[aztec(public-vm)]
fn keccak_hash(data: [Field; 3]) -> pub [Field; 2] {
keccak256(data)
}
// // Field should fit in 128 bits
// // ACIR only supports fields of up to 126 bits!
// // Same with internal fields for unconstrained functions, apprently.
// #[aztec(public-vm)]
// fn setOpcodeSmallField() -> pub Field {
// 200 as Field
// }

#[aztec(public-vm)]
fn poseidon_hash(data: [Field; 3]) -> pub Field {
poseidon(data)
}
// // /************************************************************************
// // * Hashing functions
// // ************************************************************************/
// #[aztec(public-vm)]
// fn keccak_hash(data: [Field; 3]) -> pub [Field; 2] {
// keccak256(data)
// }

#[aztec(public-vm)]
fn sha256_hash(data: [Field; 3]) -> pub [Field; 2] {
sha256(data)
}
// #[aztec(public-vm)]
// fn poseidon_hash(data: [Field; 3]) -> pub Field {
// poseidon(data)
// }

#[aztec(public-vm)]
fn pedersen_hash(data: [Field; 3]) -> pub Field {
dep::std::hash::pedersen_hash(data)
}
// #[aztec(public-vm)]
// fn sha256_hash(data: [Field; 3]) -> pub [Field; 2] {
// sha256(data)
// }

// /************************************************************************
// * AvmContext functions
// ************************************************************************/
#[aztec(public-vm)]
fn getAddress() -> pub AztecAddress {
context.address()
}
// #[aztec(public-vm)]
// fn pedersen_hash(data: [Field; 3]) -> pub Field {
// dep::std::hash::pedersen_hash(data)
// }

#[aztec(public-vm)]
fn getStorageAddress() -> pub AztecAddress {
context.storage_address()
}
// // /************************************************************************
// // * AvmContext functions
// // ************************************************************************/
// #[aztec(public-vm)]
// fn getAddress() -> pub AztecAddress {
// context.address()
// }

#[aztec(public-vm)]
fn getSender() -> pub AztecAddress {
context.sender()
}
// #[aztec(public-vm)]
// fn getStorageAddress() -> pub AztecAddress {
// context.storage_address()
// }

#[aztec(public-vm)]
fn getOrigin() -> pub AztecAddress {
context.origin()
}
// #[aztec(public-vm)]
// fn getSender() -> pub AztecAddress {
// context.sender()
// }

#[aztec(public-vm)]
fn getPortal() -> pub EthAddress {
context.portal()
}
// #[aztec(public-vm)]
// fn getOrigin() -> pub AztecAddress {
// context.origin()
// }

#[aztec(public-vm)]
fn getFeePerL1Gas() -> pub Field {
context.fee_per_l1_gas()
}
// #[aztec(public-vm)]
// fn getPortal() -> pub EthAddress {
// context.portal()
// }

#[aztec(public-vm)]
fn getFeePerL2Gas() -> pub Field {
context.fee_per_l2_gas()
}
// #[aztec(public-vm)]
// fn getFeePerL1Gas() -> pub Field {
// context.fee_per_l1_gas()
// }

#[aztec(public-vm)]
fn getFeePerDaGas() -> pub Field {
context.fee_per_da_gas()
}
// #[aztec(public-vm)]
// fn getFeePerL2Gas() -> pub Field {
// context.fee_per_l2_gas()
// }

#[aztec(public-vm)]
fn getChainId() -> pub Field {
context.chain_id()
}
// #[aztec(public-vm)]
// fn getFeePerDaGas() -> pub Field {
// context.fee_per_da_gas()
// }

#[aztec(public-vm)]
fn getVersion() -> pub Field {
context.version()
}
// #[aztec(public-vm)]
// fn getChainId() -> pub Field {
// context.chain_id()
// }

#[aztec(public-vm)]
fn getBlockNumber() -> pub Field {
context.block_number()
}
// #[aztec(public-vm)]
// fn getVersion() -> pub Field {
// context.version()
// }

#[aztec(public-vm)]
fn getTimestamp() -> pub Field {
context.timestamp()
}
// #[aztec(public-vm)]
// fn getBlockNumber() -> pub Field {
// context.block_number()
// }

// #[aztec(public-vm)]
// fn getContractCallDepth() -> pub Field {
// context.contract_call_depth()
// fn getTimestamp() -> pub Field {
// context.timestamp()
// }

// // #[aztec(public-vm)]
// // fn getContractCallDepth() -> pub Field {
// // context.contract_call_depth()
// // }

// Function required for all contracts
unconstrained fn compute_note_hash_and_nullifier(
_contract_address: AztecAddress,
Expand Down
8 changes: 7 additions & 1 deletion noir/aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,14 @@ fn transform_function(
/// Transform a function to work with AVM bytecode
fn transform_vm_function(
func: &mut NoirFunction,
_storage_defined: bool,
storage_defined: bool,
) -> Result<(), AztecMacroError> {
// Create access to storage
if storage_defined {
let storage = abstract_storage("AvmContext", true);
func.def.body.0.insert(0, storage);
}

// Push Avm context creation to the beginning of the function
let create_context = create_avm_context()?;
func.def.body.0.insert(0, create_context);
Expand Down
Loading

0 comments on commit 58da881

Please sign in to comment.