Skip to content

Commit

Permalink
feat: implement serdes for u64 #4990 (#5411)
Browse files Browse the repository at this point in the history
Implements it in crowdfunding.nr, does some cleanup as well

Resolves #4990.
  • Loading branch information
sklppy88 authored Mar 26, 2024
1 parent 85f14c5 commit 5a6bcef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
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]
}
}

0 comments on commit 5a6bcef

Please sign in to comment.