Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 27, 2024
1 parent 9e3e536 commit ac31baa
Show file tree
Hide file tree
Showing 25 changed files with 34 additions and 110 deletions.
12 changes: 5 additions & 7 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::macros::aztec;
#[aztec]
contract BoxReact {
use dep::aztec::{
protocol_types::public_keys::{IvpkM, OvpkM},
protocol_types::public_keys::OvpkM,
prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point},
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
macros::{storage::storage, functions::{private, public, initializer}}
Expand All @@ -21,25 +21,23 @@ contract BoxReact {
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: OvpkM,
owner_ivpk_m: IvpkM
owner_ovpk_m: OvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner));
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
}

#[private]
fn setNumber(
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: OvpkM,
owner_ivpk_m: IvpkM
owner_ovpk_m: OvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner));
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
}

unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote {
Expand Down
12 changes: 5 additions & 7 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::macros::aztec;
#[aztec]
contract Vanilla {
use dep::aztec::{
protocol_types::public_keys::{IvpkM, OvpkM},
protocol_types::public_keys::OvpkM,
prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point},
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
macros::{storage::storage, functions::{private, public, initializer}}
Expand All @@ -21,25 +21,23 @@ contract Vanilla {
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: OvpkM,
owner_ivpk_m: IvpkM
owner_ovpk_m: OvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner));
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
}

#[private]
fn setNumber(
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: OvpkM,
owner_ivpk_m: IvpkM
owner_ovpk_m: OvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner));
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
}

unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use dep::protocol_types::{
address::AztecAddress,
hash::sha256_to_field,
public_keys::{IvpkM, OvpkM},
public_keys::OvpkM,
};

/// Computes private event log payload and a log hash
Expand All @@ -15,7 +15,6 @@ fn compute_payload_and_hash<Event, let N: u32>(
randomness: Field,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> ([u8; 416 + N * 32], Field)
where
Expand All @@ -42,22 +41,20 @@ unconstrained fn compute_payload_and_hash_unconstrained<Event, let N: u32>(
event: Event,
randomness: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> ([u8; 416 + N * 32], Field)
where
Event: EventInterface<N>,
{
let ovsk_app = get_ovsk_app(ovpk.hash());
compute_payload_and_hash(context, event, randomness, ovsk_app, ovpk, ivpk, recipient)
compute_payload_and_hash(context, event, randomness, ovsk_app, ovpk, recipient)
}

pub fn encode_and_encrypt_event<Event, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> ()
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress)](Event) -> ()
where
Event: EventInterface<N>,
{
Expand All @@ -69,17 +66,16 @@ where
let randomness = unsafe { random() };
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());
let (encrypted_log, log_hash) =
compute_payload_and_hash(*context, e, randomness, ovsk_app, ovpk, ivpk, recipient);
compute_payload_and_hash(*context, e, randomness, ovsk_app, ovpk, recipient);
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}
}

pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> ()
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress)](Event) -> ()
where
Event: EventInterface<N>,
{
Expand All @@ -90,7 +86,7 @@ where
// value generation.
let randomness = unsafe { random() };
let (encrypted_log, log_hash) = unsafe {
compute_payload_and_hash_unconstrained(*context, e, randomness, ovpk, ivpk, recipient)
compute_payload_and_hash_unconstrained(*context, e, randomness, ovpk, recipient)
};
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}
Expand All @@ -103,16 +99,15 @@ pub fn encode_and_encrypt_event_with_randomness<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, Field, IvpkM, AztecAddress)](Event) -> ()
) -> fn[(&mut PrivateContext, OvpkM, Field, AztecAddress)](Event) -> ()
where
Event: EventInterface<N>,
{
|e: Event| {
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());
let (encrypted_log, log_hash) =
compute_payload_and_hash(*context, e, randomness, ovsk_app, ovpk, ivpk, recipient);
compute_payload_and_hash(*context, e, randomness, ovsk_app, ovpk, recipient);
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}
}
Expand All @@ -121,9 +116,8 @@ pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let N: u32>
context: &mut PrivateContext,
randomness: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> ()
) -> fn[(&mut PrivateContext, Field, OvpkM, AztecAddress)](Event) -> ()
where
Event: EventInterface<N>,
{
Expand All @@ -143,7 +137,7 @@ where
// return the log from this function to the app, otherwise it could try to do stuff with it and then that might
// be wrong.
let (encrypted_log, log_hash) = unsafe {
compute_payload_and_hash_unconstrained(*context, e, randomness, ovpk, ivpk, recipient)
compute_payload_and_hash_unconstrained(*context, e, randomness, ovpk, recipient)
};
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dep::protocol_types::{
abis::note_hash::NoteHash,
address::AztecAddress,
hash::sha256_to_field,
public_keys::{IvpkM, OvpkM, PublicKeys},
public_keys::{OvpkM, PublicKeys},
};

/// Computes private note log payload and a log hash
Expand All @@ -17,7 +17,6 @@ fn compute_payload_and_hash<Note, let N: u32>(
note: Note,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> (u32, [u8; 417 + N * 32], Field)
where
Expand Down Expand Up @@ -47,14 +46,13 @@ unconstrained fn compute_payload_and_hash_unconstrained<Note, let N: u32>(
context: PrivateContext,
note: Note,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> (u32, [u8; 417 + N * 32], Field)
where
Note: NoteInterface<N>,
{
let ovsk_app = get_ovsk_app(ovpk.hash());
compute_payload_and_hash(context, note, ovsk_app, ovpk, ivpk, recipient)
compute_payload_and_hash(context, note, ovsk_app, ovpk, recipient)
}

// This function seems to be affected by the following Noir bug:
Expand All @@ -63,27 +61,25 @@ where
pub fn encode_and_encrypt_note<Note, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> ()
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress)](NoteEmission<Note>) -> ()
where
Note: NoteInterface<N>,
{
|e: NoteEmission<Note>| {
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());

let (note_hash_counter, encrypted_log, log_hash) =
compute_payload_and_hash(*context, e.note, ovsk_app, ovpk, ivpk, recipient);
compute_payload_and_hash(*context, e.note, ovsk_app, ovpk, recipient);
context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash);
}
}

pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> ()
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress)](NoteEmission<Note>) -> ()
where
Note: NoteInterface<N>,
{
Expand All @@ -108,7 +104,7 @@ where
// whatever), or cause for the log to not be deleted when it should have (which is also fine - it'll be a log
// for a note that doesn't exist).
let (note_hash_counter, encrypted_log, log_hash) = unsafe {
compute_payload_and_hash_unconstrained(*context, e.note, ovpk, ivpk, recipient)
compute_payload_and_hash_unconstrained(*context, e.note, ovpk, recipient)
};
context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash);
}
Expand Down
20 changes: 6 additions & 14 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::protocol_types::{
constants::GENERATOR_INDEX__SYMMETRIC_KEY,
hash::poseidon2_hash_with_separator,
point::Point,
public_keys::{IvpkM, OvpkM},
public_keys::OvpkM,
scalar::Scalar,
};
use std::{
Expand Down Expand Up @@ -122,7 +122,7 @@ pub fn compute_incoming_body_ciphertext<let P: u32>(
aes128_encrypt(plaintext, iv, sym_key)
}

/// Encrypts ephemeral secret key and recipient's ivpk --> with this information the recipient of outgoing will
/// Encrypts ephemeral secret key and recipient's address point --> with this information the recipient of outgoing will
/// be able to derive the key with which the incoming log can be decrypted.
pub fn compute_outgoing_body_ciphertext(
recipient: AztecAddress,
Expand All @@ -138,15 +138,15 @@ pub fn compute_outgoing_body_ciphertext(
let serialized_eph_sk_low: [u8; 32] = eph_sk.lo.to_be_bytes();

let address_bytes: [u8; 32] = recipient.to_field().to_be_bytes();
let serialized_recipient_ivpk = point_to_bytes(recipient.to_address_point().to_point());
let serialized_recipient_address_point = point_to_bytes(recipient.to_address_point().to_point());

for i in 0..32 {
buffer[i] = serialized_eph_sk_high[i];
buffer[i + 32] = serialized_eph_sk_low[i];
buffer[i + 64] = address_bytes[i];
}
for i in 0..32 {
buffer[i + 96] = serialized_recipient_ivpk[i];
buffer[i + 96] = serialized_recipient_address_point[i];
}

// We compute the symmetric key using poseidon.
Expand Down Expand Up @@ -174,7 +174,7 @@ mod test {
use dep::protocol_types::{
address::AztecAddress,
point::Point,
public_keys::{IvpkM, OvpkM},
public_keys::OvpkM,
scalar::Scalar,
};
use protocol_types::public_keys::AddressPoint;
Expand All @@ -196,14 +196,6 @@ mod test {
},
};

let ivpk_m = IvpkM {
inner: Point {
x: 0x18dd22d6a4032eefe3a7a55703f583396596235f7c186e450c92981186ee7404,
y: 0x2e49e00996565114016a1a478309842ecbaf930fb716c3f498e7e10370631d75,
is_infinite: false,
},
};

let plaintext = [
0, 0, 0, 1, 48, 22, 64, 206, 234, 117, 131, 145, 178, 225, 97, 201, 44, 5, 19, 241, 41,
2, 15, 65, 37, 37, 106, 253, 174, 38, 70, 206, 49, 9, 159, 92, 16, 244, 140, 217, 239,
Expand Down Expand Up @@ -282,7 +274,7 @@ mod test {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
];

// `compute_incoming_body_ciphertext(...)` function then derives symmetric key from `eph_sk` and `ivpk` and encrypts
// `compute_incoming_body_ciphertext(...)` function then derives symmetric key from `eph_sk` and `address_point` and encrypts
// the note plaintext using AES-128.
let ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, address_point);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ impl EasyPrivateUint<&mut PrivateContext> {
self.set.insert(&mut addend_note).emit(encode_and_encrypt_note(
self.context,
outgoing_viewer_keys.ovpk_m,
owner_keys.ivpk_m,
owner,
));
// docs:end:insert
Expand Down Expand Up @@ -65,7 +64,6 @@ impl EasyPrivateUint<&mut PrivateContext> {
self.set.insert(&mut result_note).emit(encode_and_encrypt_note(
self.context,
outgoing_viewer_keys.ovpk_m,
owner_keys.ivpk_m,
owner,
));
}
Expand Down
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub fn increment(
balance.insert(&mut note).emit(encode_and_encrypt_note(
balance.context,
outgoing_viewer_ovpk_m,
recipient_keys.ivpk_m,
recipient,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ contract AppSubscription {
storage.subscriptions.at(user_address).replace(&mut note).emit(encode_and_encrypt_note(
&mut context,
keys.ovpk_m,
keys.ivpk_m,
user_address,
));

Expand Down Expand Up @@ -121,7 +120,6 @@ contract AppSubscription {
encode_and_encrypt_note(
&mut context,
msg_sender_ovpk_m,
subscriber_keys.ivpk_m,
subscriber,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ impl Deck<&mut PrivateContext> {
pub fn add_cards<let N: u32>(&mut self, cards: [Card; N], owner: AztecAddress) -> [CardNote] {
let owner_keys = get_public_keys(owner);

let owner_ivpk_m = owner_keys.ivpk_m;
let owner_npk_m_hash = owner_keys.npk_m.hash();
let msg_sender_ovpk_m = get_public_keys(self.set.context.msg_sender()).ovpk_m;

Expand All @@ -120,7 +119,6 @@ impl Deck<&mut PrivateContext> {
self.set.insert(&mut card_note.note).emit(encode_and_encrypt_note(
self.set.context,
msg_sender_ovpk_m,
owner_ivpk_m,
owner,
));
inserted_cards = inserted_cards.push_back(card_note);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ contract Child {
storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(
&mut context,
owner_keys.ovpk_m,
owner_keys.ivpk_m,
owner,
));
new_value
Expand Down
Loading

0 comments on commit ac31baa

Please sign in to comment.