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 39606b0
Show file tree
Hide file tree
Showing 25 changed files with 52 additions and 199 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 @@ -2,11 +2,7 @@ use crate::{
context::PrivateContext, encrypted_logs::payload::compute_private_log_payload,
event::event_interface::EventInterface, keys::getters::get_ovsk_app, oracle::random::random,
};
use dep::protocol_types::{
address::AztecAddress,
hash::sha256_to_field,
public_keys::{IvpkM, OvpkM},
};
use dep::protocol_types::{address::AztecAddress, hash::sha256_to_field, public_keys::OvpkM};

/// Computes private event log payload and a log hash
fn compute_payload_and_hash<Event, let N: u32>(
Expand All @@ -15,7 +11,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 +37,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 +62,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 +82,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 +95,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 +112,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 +133,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 @@ -107,9 +103,8 @@ where
// for the log to be deleted when it shouldn't have (which is fine - they can already make the content be
// 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)
};
let (note_hash_counter, encrypted_log, log_hash) =
unsafe { compute_payload_and_hash_unconstrained(*context, e.note, ovpk, recipient) };
context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash);
}
}
30 changes: 8 additions & 22 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use dep::protocol_types::{
address::AztecAddress,
constants::GENERATOR_INDEX__SYMMETRIC_KEY,
hash::poseidon2_hash_with_separator,
point::Point,
public_keys::{IvpkM, OvpkM},
scalar::Scalar,
address::AztecAddress, constants::GENERATOR_INDEX__SYMMETRIC_KEY,
hash::poseidon2_hash_with_separator, point::Point, public_keys::OvpkM, scalar::Scalar,
};
use std::{
aes128::aes128_encrypt, embedded_curve_ops::fixed_base_scalar_mul as derive_public_key,
Expand Down Expand Up @@ -122,7 +118,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 +134,16 @@ 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 All @@ -172,10 +169,7 @@ mod test {
compute_private_log_payload,
};
use dep::protocol_types::{
address::AztecAddress,
point::Point,
public_keys::{IvpkM, OvpkM},
scalar::Scalar,
address::AztecAddress, point::Point, public_keys::OvpkM, scalar::Scalar,
};
use protocol_types::public_keys::AddressPoint;
use std::embedded_curve_ops::fixed_base_scalar_mul as derive_public_key;
Expand All @@ -196,14 +190,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 +268,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 @@ -118,12 +117,7 @@ contract AppSubscription {
let mut subscription_note =
SubscriptionNote::new(subscriber_keys.npk_m.hash(), expiry_block_number, tx_count);
storage.subscriptions.at(subscriber).initialize_or_replace(&mut subscription_note).emit(
encode_and_encrypt_note(
&mut context,
msg_sender_ovpk_m,
subscriber_keys.ivpk_m,
subscriber,
),
encode_and_encrypt_note(&mut context, msg_sender_ovpk_m, subscriber),
);
}

Expand Down
Loading

0 comments on commit 39606b0

Please sign in to comment.