Skip to content

Commit

Permalink
chore: remove aes slice (#6550)
Browse files Browse the repository at this point in the history
Closes: #6547

Also generalises EncryptedLogBody so it can be used with things that
aren't notes

---------

Co-authored-by: LHerskind <[email protected]>
  • Loading branch information
Thunkar and LHerskind authored May 22, 2024
1 parent da39911 commit f44d567
Show file tree
Hide file tree
Showing 40 changed files with 305 additions and 197 deletions.
4 changes: 3 additions & 1 deletion noir-projects/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use dep::aztec::{
};

global ADDRESS_NOTE_LEN: Field = 3;
// ADDRESS_NOTE_LEN * 32 + 32(storage_slot as bytes) + 32(note_type_id as bytes)
global ADDRESS_NOTE_BYTES_LEN: Field = 3 * 32 + 64;

// docs:start:address_note_def
// Stores an address
Expand All @@ -19,7 +21,7 @@ struct AddressNote {
randomness: Field,
}

impl NoteInterface<ADDRESS_NOTE_LEN> for AddressNote {
impl NoteInterface<ADDRESS_NOTE_LEN, ADDRESS_NOTE_BYTES_LEN> for AddressNote {

fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,14 @@ impl PrivateContext {
self.encrypted_logs_hashes.push(side_effect);
}

pub fn encrypt_and_emit_note<Note, N, M>(
pub fn encrypt_and_emit_note<Note, N, NB, M>(
&mut self,
contract_address: AztecAddress,
storage_slot: Field,
note_type_id: Field,
ivpk_m: GrumpkinPoint,
note: Note
) where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
let note_hash: Field = compute_note_hash_for_insertion(note);
let note_exists_index = find_index(
self.new_note_hashes.storage,
Expand Down
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/header.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::protocol_types::{address::AztecAddress, grumpkin_private_key::GrumpkinP

use crate::keys::point_to_symmetric_key::point_to_symmetric_key;

use dep::std::aes128::aes128_encrypt_slice;
use dep::std::aes128::aes128_encrypt;

struct EncryptedLogHeader {
address: AztecAddress,
Expand All @@ -24,8 +24,8 @@ impl EncryptedLogHeader {
iv[i] = full_key[i + 16];
}

let input: [u8] = self.address.to_field().to_be_bytes(32);
aes128_encrypt_slice(input, iv, sym_key).as_array()
let input: [u8; 32] = self.address.to_field().to_be_bytes(32).as_array();
aes128_encrypt(input, iv, sym_key).as_array()
}
}

Expand Down
83 changes: 38 additions & 45 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr
Original file line number Diff line number Diff line change
@@ -1,51 +1,20 @@
use crate::note::{note_interface::NoteInterface};
use dep::protocol_types::{grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint};

use dep::std::aes128::aes128_encrypt_slice;
use dep::std::aes128::aes128_encrypt;
use crate::keys::point_to_symmetric_key::point_to_symmetric_key;

struct EncryptedLogIncomingBody<Note> {
storage_slot: Field,
note_type_id: Field,
note: Note,
struct EncryptedLogIncomingBody<M> {
plaintext: [u8; M]
}

impl<Note> EncryptedLogIncomingBody<Note> {
pub fn new<N>(
storage_slot: Field,
note_type_id: Field,
note: Note
) -> Self where Note: NoteInterface<N> {
Self { storage_slot, note_type_id, note }
impl<M> EncryptedLogIncomingBody<M> {
pub fn from_note<T, N>(note: T, storage_slot: Field) -> Self where T: NoteInterface<N, M> {
let mut plaintext = note.to_be_bytes(storage_slot);
EncryptedLogIncomingBody { plaintext }
}

pub fn compute_ciphertext<N>(
self,
eph_sk: GrumpkinPrivateKey,
ivpk_app: GrumpkinPoint
) -> [u8] where Note: NoteInterface<N> {
let serialized_note: [Field; N] = self.note.serialize_content();

let mut buffer_slice: [u8] = &[];

let storage_slot_bytes = self.storage_slot.to_be_bytes(32);
let note_type_id_bytes = self.note_type_id.to_be_bytes(32);

for i in 0..32 {
buffer_slice = buffer_slice.push_back(storage_slot_bytes[i]);
}

for i in 0..32 {
buffer_slice = buffer_slice.push_back(note_type_id_bytes[i]);
}

for i in 0..serialized_note.len() {
let bytes = serialized_note[i].to_be_bytes(32);
for j in 0..32 {
buffer_slice = buffer_slice.push_back(bytes[j]);
}
}

pub fn compute_ciphertext(self, eph_sk: GrumpkinPrivateKey, ivpk_app: GrumpkinPoint) -> [u8] {
let full_key = point_to_symmetric_key(eph_sk, ivpk_app);
let mut sym_key = [0; 16];
let mut iv = [0; 16];
Expand All @@ -54,7 +23,7 @@ impl<Note> EncryptedLogIncomingBody<Note> {
sym_key[i] = full_key[i];
iv[i] = full_key[i + 16];
}
aes128_encrypt_slice(buffer_slice, iv, sym_key)
aes128_encrypt(self.plaintext, iv, sym_key)
}
}

Expand All @@ -78,11 +47,12 @@ mod test {
}

global ADDRESS_NOTE_LEN: Field = 3;
global ADDRESS_NOTE_BYTES_LEN = 32 * 3 + 64;

impl NoteInterface<ADDRESS_NOTE_LEN> for AddressNote {
impl NoteInterface<ADDRESS_NOTE_LEN, ADDRESS_NOTE_BYTES_LEN> for AddressNote {
fn compute_note_content_hash(self) -> Field {1}

fn get_note_type_id() -> Field {2}
fn get_note_type_id() -> Field {1}

fn get_header(self) -> NoteHeader { self.header}

Expand All @@ -99,6 +69,28 @@ mod test {
fn deserialize_content(fields: [Field; ADDRESS_NOTE_LEN]) -> Self {
AddressNote { address: AztecAddress::from_field(fields[0]), owner: AztecAddress::from_field(fields[1]), randomness: fields[2], header: NoteHeader::empty() }
}

fn to_be_bytes(self, storage_slot: Field) -> [u8; ADDRESS_NOTE_BYTES_LEN] {
let serialized_note = self.serialize_content();

let mut buffer: [u8; ADDRESS_NOTE_BYTES_LEN] = [0; ADDRESS_NOTE_BYTES_LEN];

let storage_slot_bytes = storage_slot.to_be_bytes(32);
let note_type_id_bytes = AddressNote::get_note_type_id().to_be_bytes(32);

for i in 0..32 {
buffer[i] = storage_slot_bytes[i];
buffer[32 + i] = note_type_id_bytes[i];
}

for i in 0..serialized_note.len() {
let bytes = serialized_note[i].to_be_bytes(32);
for j in 0..32 {
buffer[64 + i * 32 + j] = bytes[j];
}
}
buffer
}
}

impl AddressNote {
Expand All @@ -115,9 +107,7 @@ mod test {
3
);

let note_type_id = 1;
let storage_slot = 2;
let body = EncryptedLogIncomingBody::new(storage_slot, note_type_id, note);

let eph_sk = GrumpkinPrivateKey::new(
0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
Expand All @@ -128,15 +118,18 @@ mod test {
0x1e96887b117afca01c00468264f4f80b5bb16d94c1808a448595f115556e5c8e
);

let body = EncryptedLogIncomingBody::from_note(note, storage_slot);

let ciphertext = body.compute_ciphertext(eph_sk, ivpk_app);

let expected_body_ciphertext = [
131, 119, 105, 129, 244, 32, 151, 205, 12, 99, 93, 62, 10, 180, 72, 21, 47, 232, 95, 17, 240, 230, 80, 129, 174, 158, 23, 76, 114, 185, 43, 18, 254, 148, 147, 230, 66, 216, 167, 62, 180, 213, 238, 33, 108, 29, 84, 139, 99, 206, 212, 253, 92, 116, 137, 31, 0, 104, 45, 91, 250, 109, 141, 114, 189, 53, 35, 60, 108, 156, 170, 206, 150, 114, 150, 187, 198, 13, 62, 153, 133, 13, 169, 167, 242, 221, 40, 168, 186, 203, 104, 82, 47, 238, 142, 179, 90, 37, 9, 70, 245, 176, 122, 247, 42, 87, 75, 7, 20, 89, 166, 123, 14, 26, 230, 156, 49, 94, 0, 94, 72, 58, 171, 239, 115, 174, 155, 7, 151, 17, 60, 206, 193, 134, 70, 87, 215, 88, 21, 194, 63, 26, 106, 105, 124, 213, 252, 152, 192, 71, 115, 13, 181, 5, 169, 15, 170, 196, 174, 228, 170, 192, 91, 76, 110, 220, 89, 47, 248, 144, 189, 251, 167, 149, 248, 226
];

assert_eq(expected_body_ciphertext.len(), ciphertext.len());

for i in 0..expected_body_ciphertext.len() {
assert_eq(ciphertext[i], expected_body_ciphertext[i]);
}
assert_eq(expected_body_ciphertext.len(), ciphertext.len());
}
}
11 changes: 7 additions & 4 deletions noir-projects/aztec-nr/aztec/src/history/note_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
oracle::get_membership_witness::get_note_hash_membership_witness
};

pub fn _note_inclusion<Note, N>(note: Note, header: Header) where Note: NoteInterface<N> {
pub fn _note_inclusion<Note, N, M>(note: Note, header: Header) where Note: NoteInterface<N, M> {
// 1) Compute note_hash
let note_hash = compute_note_hash_for_consumption(note);

Expand All @@ -20,15 +20,18 @@ pub fn _note_inclusion<Note, N>(note: Note, header: Header) where Note: NoteInte
);
}

pub fn prove_note_inclusion<Note, N>(note: Note, context: PrivateContext) where Note: NoteInterface<N> {
pub fn prove_note_inclusion<Note, N, M>(
note: Note,
context: PrivateContext
) where Note: NoteInterface<N, M> {
_note_inclusion(note, context.historical_header);
}

pub fn prove_note_inclusion_at<Note, N>(
pub fn prove_note_inclusion_at<Note, N, M>(
note: Note,
block_number: u32, // The block at which we'll prove that the note exists
context: PrivateContext
) where Note: NoteInterface<N> {
) where Note: NoteInterface<N, M> {
let header = context.get_header_at(block_number);

_note_inclusion(note, header);
Expand Down
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/aztec/src/history/note_validity.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use crate::{
note::{utils::compute_siloed_nullifier, note_interface::NoteInterface}
};

pub fn prove_note_validity<Note, N>(note: Note, context: &mut PrivateContext) where Note: NoteInterface<N> {
pub fn prove_note_validity<Note, N, M>(note: Note, context: &mut PrivateContext) where Note: NoteInterface<N, M> {
prove_note_inclusion(note, *context);
prove_note_not_nullified(note, context);
}

// A helper function that proves that a note is valid at the given block number
pub fn prove_note_validity_at<Note, N>(
pub fn prove_note_validity_at<Note, N, M>(
note: Note,
block_number: u32,
context: &mut PrivateContext
) where Note: NoteInterface<N> {
) where Note: NoteInterface<N, M> {
// We are calling the internal functions here because we want to avoid calling get_header_at twice
let header = context.get_header_at(block_number);
_note_inclusion(note, header);
Expand Down
10 changes: 3 additions & 7 deletions noir-projects/aztec-nr/aztec/src/history/nullifier_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,16 @@ pub fn prove_nullifier_inclusion_at(
_nullifier_inclusion(nullifier, header);
}

pub fn prove_note_is_nullified<Note, N>(
pub fn prove_note_is_nullified<Note, N, M>(
note: Note,
context: &mut PrivateContext
) where Note: NoteInterface<N> {
) where Note: NoteInterface<N, M> {
let nullifier = compute_siloed_nullifier(note, context);

_nullifier_inclusion(nullifier, context.historical_header);
}

pub fn prove_note_is_nullified_at<Note, N>(
note: Note,
block_number: u32,
context: &mut PrivateContext
) where Note: NoteInterface<N> {
pub fn prove_note_is_nullified_at<Note, N, M>(note: Note, block_number: u32, context: &mut PrivateContext) where Note: NoteInterface<N, M> {
let nullifier = compute_siloed_nullifier(note, context);
let header = context.get_header_at(block_number);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,17 @@ pub fn prove_nullifier_not_included_at(nullifier: Field, block_number: u32, cont
_nullifier_non_inclusion(nullifier, header);
}

pub fn prove_note_not_nullified<Note, N>(
note: Note,
context: &mut PrivateContext
) where Note: NoteInterface<N> {
pub fn prove_note_not_nullified<Note, N, M>(note: Note, context: &mut PrivateContext) where Note: NoteInterface<N, M> {
let nullifier = compute_siloed_nullifier(note, context);

_nullifier_non_inclusion(nullifier, context.historical_header);
}

pub fn prove_note_not_nullified_at<Note, N>(
pub fn prove_note_not_nullified_at<Note, N, M>(
note: Note,
block_number: u32, // The block at which we'll prove that the note was not nullified
context: &mut PrivateContext
) where Note: NoteInterface<N> {
) where Note: NoteInterface<N, M> {
let nullifier = compute_siloed_nullifier(note, context);
let header = context.get_header_at(block_number);

Expand Down
13 changes: 8 additions & 5 deletions noir-projects/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use crate::note::{
};
use crate::oracle::notes::{notify_created_note, notify_nullified_note};

pub fn create_note<Note, N>(
pub fn create_note<Note, N, M>(
context: &mut PrivateContext,
storage_slot: Field,
note: &mut Note,
broadcast: bool,
ivpk_m: GrumpkinPoint
) where Note: NoteInterface<N> {
) where Note: NoteInterface<N, M> {
let contract_address = (*context).this_address();

let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
Expand Down Expand Up @@ -42,11 +42,11 @@ pub fn create_note<Note, N>(
}
}

pub fn create_note_hash_from_public<Note, N>(
pub fn create_note_hash_from_public<Note, N, M>(
context: &mut PublicContext,
storage_slot: Field,
note: &mut Note
) where Note: NoteInterface<N> {
) where Note: NoteInterface<N, M> {
let contract_address = (*context).this_address();

let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
Expand All @@ -57,7 +57,10 @@ pub fn create_note_hash_from_public<Note, N>(
context.push_new_note_hash(inner_note_hash);
}

pub fn destroy_note<Note, N>(context: &mut PrivateContext, note: Note) where Note: NoteInterface<N> {
pub fn destroy_note<Note, N, M>(
context: &mut PrivateContext,
note: Note
) where Note: NoteInterface<N, M> {
let mut nullifier = 0;
let mut consumed_note_hash: Field = 0;
nullifier = note.compute_nullifier(context);
Expand Down
Loading

0 comments on commit f44d567

Please sign in to comment.