Skip to content

Commit

Permalink
chore: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jan 9, 2024
1 parent ff5ea6d commit 9419450
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 70 deletions.
19 changes: 11 additions & 8 deletions yarn-project/aztec-nr/compressed-string/src/compressed_string.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use dep::std;

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

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

Expand Down Expand Up @@ -39,19 +39,22 @@ impl FixedsizeCompressedString{
}
}

fn deserialize(fields: [Field; 1]) -> FixedsizeCompressedString {
FixedsizeCompressedString { value: fields[0] }
fn deserialize(fields: [Field; 1]) -> FieldCompressedString {
FieldCompressedString { value: fields[0] }
}

fn serialize(value: FixedsizeCompressedString) -> [Field; 1] {
fn serialize(value: FieldCompressedString) -> [Field; 1] {
value.serialize()
}
// Need to figure this out afterwards.
global FixedsizeCompressedStringSerializationMethods = TypeSerializationInterface {
global FieldCompressedStringSerializationMethods = TypeSerializationInterface {
deserialize,
serialize,
};

// The general Compressed String.
// Compresses M bytes into N fields.
// Can be used for longer strings that don't fit in a single field.
// Each field can store 31 characters, so N should be M/31 rounded up.
struct CompressedString<N, M> {
value: [Field; N]
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/compressed-string/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod compressed_string;

use crate::compressed_string::{CompressedString};
use crate::compressed_string::{FixedsizeCompressedString, FixedsizeCompressedStringSerializationMethods};
use crate::compressed_string::{FieldCompressedString, FieldCompressedStringSerializationMethods};
2 changes: 1 addition & 1 deletion yarn-project/foundation/src/abi/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('abi/encoder', () => {
};

const str = 'abc';
// As bigints padded with 0 for length 4
// As bigints padded with 0 for length 4. ("a" = 97, "b" = 98, "c" = 99, 0)
const expected = [new Fr(97), new Fr(98), new Fr(99), new Fr(0)];
expect(encodeArguments(abi, [str])).toEqual(expected);
});
Expand Down
55 changes: 6 additions & 49 deletions yarn-project/noir-contracts/contracts/reader_contract/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
contract Reader {
use dep::std::option::Option;
use dep::protocol_types::{
address::{
AztecAddress,
EthAddress,
},
address::AztecAddress,
abis::function_selector::FunctionSelector,
};
use dep::protocol_types;
use dep::aztec::log::emit_unencrypted_log;

use dep::compressed_string::{FixedsizeCompressedString, FixedsizeCompressedStringSerializationMethods};

use dep::aztec::{
context::Context,
abi,
abi::PrivateContextInputs,
hash::pedersen_hash,
context::PrivateContext,
note::{
note_header::NoteHeader,
utils as note_utils,
},
oracle::{
get_public_key::get_public_key as get_public_key_oracle,
context::get_portal_address,
rand::rand
},
state_vars::immutable_singleton::ImmutableSingleton,
log::emit_unencrypted_log_from_private,
types::vec::BoundedVec,
};

struct Storage {}

impl Storage {
fn init(_context: Context) -> Self {
Storage {}
}
}
use dep::compressed_string::{FieldCompressedString, FieldCompressedStringSerializationMethods};

#[aztec(private)]

Expand All @@ -48,17 +14,17 @@ contract Reader {
fn check_name(who: AztecAddress, what: str<31>) {
let selector = FunctionSelector::from_signature("public_get_name()");
let ret = context.call_public_function_no_args(who, selector);
let name = FixedsizeCompressedString::from_field(ret[0]);
let _what = FixedsizeCompressedString::from_string(what);
let name = FieldCompressedString::from_field(ret[0]);
let _what = FieldCompressedString::from_string(what);
assert(name.is_eq(_what));
}

#[aztec(public)]
fn check_symbol(who: AztecAddress, what: str<31>) {
let selector = FunctionSelector::from_signature("public_get_symbol()");
let ret = context.call_public_function_no_args(who, selector);
let symbol = FixedsizeCompressedString::from_field(ret[0]);
let _what = FixedsizeCompressedString::from_string(what);
let symbol = FieldCompressedString::from_field(ret[0]);
let _what = FieldCompressedString::from_string(what);
assert(symbol.is_eq(_what));
}

Expand All @@ -68,13 +34,4 @@ contract Reader {
let ret = context.call_public_function_no_args(who, selector);
assert(ret[0] as u8 == what);
}

unconstrained fn compute_note_hash_and_nullifier(
_contract_address: AztecAddress,
_nonce: Field,
_storage_slot: Field,
_serialized_note: [Field; 1]
) -> pub [Field; 4] {
[0x0d, 0x0e, 0x0a, 0x0d]
}
}
22 changes: 11 additions & 11 deletions yarn-project/noir-contracts/contracts/token_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract Token {
use dep::std::option::Option;

use dep::safe_math::SafeU120;
use dep::compressed_string::{FixedsizeCompressedString, FixedsizeCompressedStringSerializationMethods};
use dep::compressed_string::{FieldCompressedString, FieldCompressedStringSerializationMethods};

use dep::aztec::{
note::{
Expand Down Expand Up @@ -70,8 +70,8 @@ contract Token {
pending_shields: Set<TransparentNote, TRANSPARENT_NOTE_LEN>,
// docs:end:storage_pending_shields
public_balances: Map<PublicState<SafeU120, SAFE_U120_SERIALIZED_LEN>>,
symbol: PublicState<FixedsizeCompressedString, 1>,
name: PublicState<FixedsizeCompressedString, 1>,
symbol: PublicState<FieldCompressedString, 1>,
name: PublicState<FieldCompressedString, 1>,
decimals: PublicState<u8, U8_SERIALIZED_LEN>,
}
// docs:end:storage_struct
Expand Down Expand Up @@ -125,12 +125,12 @@ contract Token {
symbol: PublicState::new(
context,
7,
FixedsizeCompressedStringSerializationMethods,
FieldCompressedStringSerializationMethods,
),
name: PublicState::new(
context,
8,
FixedsizeCompressedStringSerializationMethods,
FieldCompressedStringSerializationMethods,
),
decimals: PublicState::new(
context,
Expand All @@ -146,8 +146,8 @@ contract Token {
#[aztec(private)]
fn constructor(admin: AztecAddress, name: str<31>, symbol: str<31>, decimals: u8) {
let selector = FunctionSelector::from_signature("_initialize((Field),(Field),(Field),u8)");
let name_s = FixedsizeCompressedString::from_string(name);
let symbol_s = FixedsizeCompressedString::from_string(symbol);
let name_s = FieldCompressedString::from_string(name);
let symbol_s = FieldCompressedString::from_string(symbol);
context.call_public_function(
context.this_address(),
selector,
Expand All @@ -167,7 +167,7 @@ contract Token {
// docs:end:set_admin

#[aztec(public)]
fn public_get_name() -> pub FixedsizeCompressedString {
fn public_get_name() -> pub FieldCompressedString {
storage.name.read()
}

Expand All @@ -176,7 +176,7 @@ contract Token {
}

#[aztec(public)]
fn public_get_symbol() -> pub FixedsizeCompressedString {
fn public_get_symbol() -> pub FieldCompressedString {
storage.symbol.read()
}

Expand Down Expand Up @@ -367,8 +367,8 @@ contract Token {
#[aztec(public)]
internal fn _initialize(
new_admin: AztecAddress,
name: FixedsizeCompressedString,
symbol: FixedsizeCompressedString,
name: FieldCompressedString,
symbol: FieldCompressedString,
decimals: u8
) {
assert(!new_admin.is_zero(), "invalid admin");
Expand Down

0 comments on commit 9419450

Please sign in to comment.