Skip to content

Provide an implementation of a note with a single field from aztec.nr #2948

@spalladino

Description

@spalladino

Storing a single field value in a private note in a singleton requires the user to write the following boilerplate code. Until we have something to autogenerate this code (noir macros or code generators), we should provide an implementation for it from aztec.nr, similar to how we do with the value note.

use dep::aztec::note::{
    note_header::NoteHeader,
    note_interface::NoteInterface,
    utils::compute_note_hash_for_read_or_nullify,
};
use dep::aztec::oracle::{
    get_secret_key::get_secret_key,
    get_public_key::get_public_key,
};
use dep::aztec::types::address::AztecAddress;

struct FieldNote {
    value: Field,
    header: NoteHeader,
}

global FIELD_NOTE_LEN: Field = 1;

impl FieldNote {
    fn new(_value: Field) -> Self {
        FieldNote {
            value: _value,
            header: NoteHeader::empty(),
        }
    }

    fn serialize(self) -> [Field; FIELD_NOTE_LEN] {
        [self.value]
    }

    fn deserialize(preimage: [Field; FIELD_NOTE_LEN]) -> Self {
        FieldNote {
            value: preimage[0],
            header: NoteHeader::empty(),
        }
    }

    fn compute_note_hash(self) -> Field {
        dep::std::hash::pedersen([
            self.value
        ])[0]
    }

    fn compute_nullifier(self) -> Field {
         0
    }

    fn set_header(&mut self, header: NoteHeader) {
        self.header = header;
    }
}

fn deserialize(preimage: [Field; FIELD_NOTE_LEN]) -> FieldNote {
    FieldNote::deserialize(preimage)
}

fn serialize(note: FieldNote) -> [Field; FIELD_NOTE_LEN] {
    note.serialize()
}

fn compute_note_hash(note: FieldNote) -> Field {
    note.compute_note_hash()
}

fn compute_nullifier(note: FieldNote) -> Field {
    note.compute_nullifier()
}

fn get_header(note: FieldNote) -> NoteHeader {
    note.header
}

fn set_header(note: &mut FieldNote, header: NoteHeader) {
    note.set_header(header)
}

global FieldNoteMethods = NoteInterface {
    deserialize,
    serialize,
    compute_note_hash,
    compute_nullifier,
    get_header,
    set_header,
};

Metadata

Metadata

Assignees

Labels

C-aztec.nrComponent: Aztec smart contract frameworkT-feedbackType: recording user feedback

Type

No type
No fields configured for issues without a type.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions