Skip to content

Commit

Permalink
refactor: 1 struct per file (#4693)
Browse files Browse the repository at this point in the history
Fixes #4410
  • Loading branch information
benesjan committed Feb 21, 2024
1 parent 3675e1d commit 19d2bbe
Show file tree
Hide file tree
Showing 59 changed files with 1,033 additions and 839 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dep::protocol_types::{
abis::{new_contract_data::NewContractData as ContractLeafPreimage},
address::{AztecAddress, EthAddress}, contract_class::ContractClassId, grumpkin_point::GrumpkinPoint
address::{AztecAddress, EthAddress}, contract_class_id::ContractClassId,
grumpkin_point::GrumpkinPoint
};
use dep::std::merkle::compute_merkle_root;

Expand Down
42 changes: 0 additions & 42 deletions noir-projects/aztec-nr/compressed-string/src/compressed_string.nr
Original file line number Diff line number Diff line change
@@ -1,46 +1,4 @@
use dep::aztec::protocol_types::{utils::field::field_from_bytes, traits::{Serialize, Deserialize}};
use dep::std;

// A Fixedsize Compressed String.
// Essentially a special version of Compressed String for practical use.
struct FieldCompressedString{
value: Field
}

impl Serialize<1> for FieldCompressedString {
fn serialize(self) -> [Field; 1] {
[self.value]
}
}

impl Deserialize<1> for FieldCompressedString {
fn deserialize(input: [Field; 1]) -> Self {
Self { value: input[0] }
}
}

impl FieldCompressedString {
pub fn is_eq(self, other: FieldCompressedString) -> bool {
self.value == other.value
}

pub fn from_field(input_field: Field) -> Self {
Self { value: input_field }
}

pub fn from_string(input_string: str<31>) -> Self {
Self { value: field_from_bytes(input_string.as_bytes(), true) }
}

pub fn to_bytes(self) -> [u8; 31] {
let mut result = [0; 31];
let bytes = self.value.to_be_bytes(31);
for i in 0..31 {
result[i] = bytes[i];
}
result
}
}

// The general Compressed String.
// Compresses M bytes into N fields.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use dep::aztec::protocol_types::{utils::field::field_from_bytes, traits::{Serialize, Deserialize}};

// A Fixedsize Compressed String.
// Essentially a special version of Compressed String for practical use.
struct FieldCompressedString{
value: Field
}

impl Serialize<1> for FieldCompressedString {
fn serialize(self) -> [Field; 1] {
[self.value]
}
}

impl Deserialize<1> for FieldCompressedString {
fn deserialize(input: [Field; 1]) -> Self {
Self { value: input[0] }
}
}

impl FieldCompressedString {
pub fn is_eq(self, other: FieldCompressedString) -> bool {
self.value == other.value
}

pub fn from_field(input_field: Field) -> Self {
Self { value: input_field }
}

pub fn from_string(input_string: str<31>) -> Self {
Self { value: field_from_bytes(input_string.as_bytes(), true) }
}

pub fn to_bytes(self) -> [u8; 31] {
let mut result = [0; 31];
let bytes = self.value.to_be_bytes(31);
for i in 0..31 {
result[i] = bytes[i];
}
result
}
}
5 changes: 3 additions & 2 deletions noir-projects/aztec-nr/compressed-string/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod compressed_string;
mod field_compressed_string;

use crate::compressed_string::{CompressedString};
use crate::compressed_string::FieldCompressedString;
use crate::compressed_string::CompressedString;
use crate::field_compressed_string::FieldCompressedString;
4 changes: 3 additions & 1 deletion noir-projects/aztec-nr/easy-private-state/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
mod easy_private_state;
mod easy_private_uint;

use crate::easy_private_uint::EasyPrivateUint;
20 changes: 20 additions & 0 deletions noir-projects/aztec-nr/slow-updates-tree/src/leaf.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use dep::aztec::protocol_types::traits::{Serialize, Deserialize};

// A leaf in the tree.
struct Leaf {
next_change: Field,
before: Field,
after: Field,
}

impl Serialize<3> for Leaf {
fn serialize(leaf: Leaf) -> [Field; 3] {
[leaf.next_change, leaf.before, leaf.after]
}
}

impl Deserialize<3> for Leaf {
fn deserialize(serialized: [Field; 3]) -> Leaf {
Leaf { next_change: serialized[0], before: serialized[1], after: serialized[2] }
}
}
7 changes: 7 additions & 0 deletions noir-projects/aztec-nr/slow-updates-tree/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
mod leaf;
mod slow_map;
mod slow_update_proof;

use crate::leaf::Leaf;
use crate::slow_map::SlowMap;
use crate::slow_update_proof::{deserialize_slow_update_proof, SlowUpdateProof};
use dep::std::merkle::compute_merkle_root;
93 changes: 9 additions & 84 deletions noir-projects/aztec-nr/slow-updates-tree/src/slow_map.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use dep::aztec::context::{PrivateContext, PublicContext, Context};
use dep::aztec::oracle::storage::{storage_read, storage_write};
use dep::aztec::protocol_types::traits::{Serialize, Deserialize};
use crate::{
leaf::Leaf,
slow_update_proof::SlowUpdateProof,
};
use dep::aztec::{
context::Context,
oracle::storage::{storage_read, storage_write},
protocol_types::traits::{Serialize, Deserialize},
};
use dep::std::hash::pedersen_hash;
use dep::std::merkle::compute_merkle_root;
use dep::std::option::Option;

// The epoch length is just a random number for now.
global EPOCH_LENGTH: u120 = 100;
Expand All @@ -12,86 +17,6 @@ fn compute_next_change(time: Field) -> Field {
((time as u120 / EPOCH_LENGTH + 1) * EPOCH_LENGTH) as Field
}

// A leaf in the tree.
struct Leaf {
next_change: Field,
before: Field,
after: Field,
}

impl Serialize<3> for Leaf {
fn serialize(leaf: Leaf) -> [Field; 3] {
[leaf.next_change, leaf.before, leaf.after]
}
}

impl Deserialize<3> for Leaf {
fn deserialize(serialized: [Field; 3]) -> Leaf {
Leaf { next_change: serialized[0], before: serialized[1], after: serialized[2] }
}
}

// Subset of the MembershipProof that is needed for the slow update.
struct SlowUpdateInner<N> {
value: Field, // Value only really used for the private flow though :thinking:
sibling_path: [Field; N],
}

// docs:start:slow_update_proof
// The slow update proof. Containing two merkle paths
// One for the before and one for the after trees.
// M = 2 * N + 4
struct SlowUpdateProof<N, M> {
index: Field,
new_value: Field,
before: SlowUpdateInner<N>,
after: SlowUpdateInner<N>,
}
// docs:end:slow_update_proof

pub fn deserialize_slow_update_proof<N, M>(serialized: [Field; M]) -> SlowUpdateProof<N, M> {
SlowUpdateProof::deserialize(serialized)
}

impl<N, M> SlowUpdateProof<N, M> {
pub fn serialize(self: Self) -> [Field; M] {
let mut serialized = [0; M];
serialized[0] = self.index;
serialized[1] = self.new_value;
serialized[2] = self.before.value;
serialized[3 + N] = self.after.value;

for i in 0..N {
serialized[3 + i] = self.before.sibling_path[i];
serialized[4 + N + i] = self.after.sibling_path[i];
}
serialized
}

pub fn deserialize(serialized: [Field; M]) -> Self {
let mut before_sibling_path = [0; N];
let mut after_sibling_path = [0; N];

for i in 0..N {
before_sibling_path[i] = serialized[3 + i];
after_sibling_path[i] = serialized[4 + N + i];
}

Self {
index: serialized[0],
new_value: serialized[1],
before: SlowUpdateInner {
value: serialized[2],
sibling_path: before_sibling_path,
},
after: SlowUpdateInner {
value: serialized[3 + N],
sibling_path: after_sibling_path,
},
}
}
}

// The simple slow map which stores a sparse tree
struct SlowMap<N,M> {
context: Context,
Expand Down
52 changes: 52 additions & 0 deletions noir-projects/aztec-nr/slow-updates-tree/src/slow_update_proof.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Subset of the MembershipProof that is needed for the slow update.
struct SlowUpdateInner<N> {
value: Field, // Value only really used for the private flow though :thinking:
sibling_path: [Field; N],
}

// The slow update proof. Containing two merkle paths
// One for the before and one for the after trees.
// M = 2 * N + 4
struct SlowUpdateProof<N, M> {
index: Field,
new_value: Field,
before: SlowUpdateInner<N>,
after: SlowUpdateInner<N>,
}

pub fn deserialize_slow_update_proof<N, M>(serialized: [Field; M]) -> SlowUpdateProof<N, M> {
SlowUpdateProof::deserialize(serialized)
}

impl<N, M> SlowUpdateProof<N, M> {
pub fn serialize(self: Self) -> [Field; M] {
let mut serialized = [0; M];
serialized[0] = self.index;
serialized[1] = self.new_value;
serialized[2] = self.before.value;
serialized[3 + N] = self.after.value;

for i in 0..N {
serialized[3 + i] = self.before.sibling_path[i];
serialized[4 + N + i] = self.after.sibling_path[i];
}
serialized
}

pub fn deserialize(serialized: [Field; M]) -> Self {
let mut before_sibling_path = [0; N];
let mut after_sibling_path = [0; N];

for i in 0..N {
before_sibling_path[i] = serialized[3 + i];
after_sibling_path[i] = serialized[4 + N + i];
}

Self {
index: serialized[0],
new_value: serialized[1],
before: SlowUpdateInner { value: serialized[2], sibling_path: before_sibling_path },
after: SlowUpdateInner { value: serialized[3 + N], sibling_path: after_sibling_path }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dep::aztec::protocol_types::{
contract_class::ContractClassId,
contract_class_id::ContractClassId,
constants::{MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS, REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE},
traits::{Serialize}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::aztec::protocol_types;
use dep::aztec::protocol_types::{
contract_class::ContractClassId, abis::function_selector::FunctionSelector,
contract_class_id::ContractClassId, abis::function_selector::FunctionSelector,
constants::{
FUNCTION_TREE_HEIGHT, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT,
MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::aztec::protocol_types;
use dep::aztec::protocol_types::{
contract_class::ContractClassId, abis::function_selector::FunctionSelector,
contract_class_id::ContractClassId, abis::function_selector::FunctionSelector,
constants::{
ARTIFACT_FUNCTION_TREE_MAX_HEIGHT, MAX_PACKED_BYTECODE_SIZE_PER_UNCONSTRAINED_FUNCTION_IN_FIELDS,
REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod events;
contract ContractClassRegisterer {
use dep::std::option::Option;
use dep::aztec::protocol_types::{
address::{AztecAddress, EthAddress}, contract_class::ContractClassId,
address::{AztecAddress, EthAddress}, contract_class_id::ContractClassId,
constants::{
ARTIFACT_FUNCTION_TREE_MAX_HEIGHT, FUNCTION_TREE_HEIGHT,
MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS, REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dep::aztec::protocol_types::{
contract_class::ContractClassId,
contract_class_id::ContractClassId,
address::{AztecAddress, EthAddress, PublicKeysHash, PartialAddress},
constants::{DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE}, traits::{Serialize}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ contract ContractInstanceDeployer {
use dep::std::option::Option;
use dep::aztec::protocol_types::{
address::{AztecAddress, EthAddress, PublicKeysHash, PartialAddress},
contract_class::ContractClassId, constants::{DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE},
traits::{Serialize}
contract_class_id::ContractClassId,
constants::{DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE}, traits::{Serialize}
};

use dep::aztec::log::{emit_unencrypted_log, emit_unencrypted_log_from_private};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract Counter {
VALUE_NOTE_LEN,
},
};
use dep::easy_private_state::easy_private_state::EasyPrivateUint;
use dep::easy_private_state::EasyPrivateUint;
// docs:end:imports

// docs:start:storage_struct
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// docs:start:easy_private_token_contract
contract EasyPrivateToken {
use dep::aztec::protocol_types::address::AztecAddress;
use dep::std::option::Option;
use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
note::{
note_header::NoteHeader,
utils as note_utils,
Expand All @@ -14,7 +12,7 @@ contract EasyPrivateToken {
balance_utils,
value_note::ValueNote,
};
use dep::easy_private_state::easy_private_state::EasyPrivateUint;
use dep::easy_private_state::EasyPrivateUint;

struct Storage {
balances: Map<AztecAddress, EasyPrivateUint>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
contract InclusionProofs {
use dep::aztec::protocol_types::{
abis::function_selector::FunctionSelector, address::{AztecAddress, EthAddress},
grumpkin_point::GrumpkinPoint, contract_class::ContractClassId
grumpkin_point::GrumpkinPoint, contract_class_id::ContractClassId
};
use dep::aztec::{
state_vars::{map::Map, set::Set, public_state::PublicState}, context::Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract SlowTree {
state_vars::{map::Map, public_state::PublicState, set::Set},
protocol_types::type_serialization::FIELD_SERIALIZED_LEN
};
use dep::slow_updates_tree::slow_map::{SlowMap, Leaf, SlowUpdateProof, compute_merkle_root, deserialize_slow_update_proof};
use dep::slow_updates_tree::{SlowMap, Leaf, SlowUpdateProof, compute_merkle_root, deserialize_slow_update_proof};

// docs:start:import_pop_capsule
use crate::capsule::pop_capsule;
Expand Down
Loading

0 comments on commit 19d2bbe

Please sign in to comment.