Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement serdes for u64 #4990 #5411

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,23 @@ contract Crowdfunding {
// Crowdfunding campaign operator
operator: SharedImmutable<AztecAddress>,
// End of the crowdfunding campaign after which no more donations are accepted
// TODO(#4990): Make deadline a u64 once the neccessary traits are implemented
deadline: PublicImmutable<Field>,
deadline: PublicImmutable<u64>,
// Notes emitted to donors when they donate (later on used to claim rewards in the Claim contract)
claim_notes: PrivateSet<ValueNote>,
}

#[aztec(public)]
#[aztec(initializer)]
fn constructor(donation_token: AztecAddress, operator: AztecAddress, deadline: u64) {
// TODO(#4990): Make deadline a u64 once the neccessary traits are implemented
storage.donation_token.initialize(donation_token);
storage.operator.initialize(operator);
storage.deadline.initialize(deadline as Field);
storage.deadline.initialize(deadline);
}

#[aztec(public)]
#[aztec(internal)]
fn _check_deadline() {
// TODO(#4990): Remove the cast here once u64 is used directly
let deadline = storage.deadline.read() as u64;
let deadline = storage.deadline.read();
assert(context.timestamp() as u64 < deadline, "Deadline has passed");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use crate::traits::{Serialize, Deserialize};

global BOOL_SERIALIZED_LEN: Field = 1;
global U32_SERIALIZED_LEN: Field = 1;
global U8_SERIALIZED_LEN: Field = 1;
global U32_SERIALIZED_LEN: Field = 1;
global U64_SERIALIZED_LEN: Field = 1;
global U128_SERIALIZED_LEN: Field = 1;
global FIELD_SERIALIZED_LEN: Field = 1;

impl Serialize<U32_SERIALIZED_LEN> for u32 {
fn serialize(self) -> [Field; U32_SERIALIZED_LEN] {
impl Serialize<BOOL_SERIALIZED_LEN> for bool {
fn serialize(self) -> [Field; BOOL_SERIALIZED_LEN] {
[self as Field]
}
}

impl Deserialize<U32_SERIALIZED_LEN> for u32 {
fn deserialize(fields: [Field; U32_SERIALIZED_LEN]) -> Self {
fields[0] as u32
impl Deserialize<BOOL_SERIALIZED_LEN> for bool {
fn deserialize(fields: [Field; BOOL_SERIALIZED_LEN]) -> bool {
fields[0] as bool
}
}

Expand All @@ -29,39 +31,51 @@ impl Deserialize<U8_SERIALIZED_LEN> for u8 {
}
}

impl Serialize<FIELD_SERIALIZED_LEN> for Field {
impl Serialize<U32_SERIALIZED_LEN> for u32 {
fn serialize(self) -> [Field; U32_SERIALIZED_LEN] {
[self]
[self as Field]
}
}

impl Deserialize<FIELD_SERIALIZED_LEN> for Field {
fn deserialize(fields: [Field; FIELD_SERIALIZED_LEN]) -> Self {
fields[0]
impl Deserialize<U32_SERIALIZED_LEN> for u32 {
fn deserialize(fields: [Field; U32_SERIALIZED_LEN]) -> Self {
fields[0] as u32
}
}

impl Serialize<BOOL_SERIALIZED_LEN> for bool {
fn serialize(self) -> [Field; BOOL_SERIALIZED_LEN] {
impl Serialize<U64_SERIALIZED_LEN> for u64 {
fn serialize(self) -> [Field; U64_SERIALIZED_LEN] {
[self as Field]
}
}

impl Deserialize<BOOL_SERIALIZED_LEN> for bool {
fn deserialize(fields: [Field; BOOL_SERIALIZED_LEN]) -> bool {
fields[0] as bool
impl Deserialize<U64_SERIALIZED_LEN> for u64 {
fn deserialize(fields: [Field; U64_SERIALIZED_LEN]) -> Self {
fields[0] as u64
}
}

impl Serialize<1> for U128 {
impl Serialize<U128_SERIALIZED_LEN> for U128 {
fn serialize(self) -> [Field; 1] {
[self.to_integer()]
}

}

impl Deserialize<1> for U128 {
fn deserialize(fields: [Field; 1]) -> Self {
impl Deserialize<U128_SERIALIZED_LEN> for U128 {
fn deserialize(fields: [Field; U128_SERIALIZED_LEN]) -> Self {
U128::from_integer(fields[0])
}
}

impl Serialize<FIELD_SERIALIZED_LEN> for Field {
fn serialize(self) -> [Field; U32_SERIALIZED_LEN] {
[self]
}
}

impl Deserialize<FIELD_SERIALIZED_LEN> for Field {
fn deserialize(fields: [Field; FIELD_SERIALIZED_LEN]) -> Self {
fields[0]
}
}
Loading