Skip to content

Commit

Permalink
feat: typing specific pub keys (#8099)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Aug 22, 2024
1 parent f724b07 commit 638793a
Show file tree
Hide file tree
Showing 26 changed files with 328 additions and 221 deletions.
15 changes: 9 additions & 6 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
contract BoxReact {
use dep::aztec::prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point};
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys;
use dep::aztec::{
keys::public_keys::{IvpkM, OvpkM},
prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point},
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys
};
use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN};

#[aztec(storage)]
Expand All @@ -14,8 +17,8 @@ contract BoxReact {
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: Point,
owner_ivpk_m: Point
owner_ovpk_m: OvpkM,
owner_ivpk_m: IvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
Expand All @@ -27,8 +30,8 @@ contract BoxReact {
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: Point,
owner_ivpk_m: Point
owner_ovpk_m: OvpkM,
owner_ivpk_m: IvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
Expand Down
4 changes: 2 additions & 2 deletions boxes/boxes/react/src/hooks/useContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export function useContract() {
Fr.random(),
wallet.getCompleteAddress().address,
masterNullifierPublicKey.hash(),
masterOutgoingViewingPublicKey.toNoirStruct(),
masterIncomingViewingPublicKey.toNoirStruct(),
masterOutgoingViewingPublicKey.toWrappedNoirStruct(),
masterIncomingViewingPublicKey.toWrappedNoirStruct(),
).send({
contractAddressSalt: salt,
});
Expand Down
4 changes: 2 additions & 2 deletions boxes/boxes/react/src/hooks/useNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export function useNumber({ contract }: { contract: Contract }) {
value,
deployerWallet.getCompleteAddress().address,
masterNullifierPublicKey.hash(),
masterOutgoingViewingPublicKey.toNoirStruct(),
masterIncomingViewingPublicKey.toNoirStruct(),
masterOutgoingViewingPublicKey.toWrappedNoirStruct(),
masterIncomingViewingPublicKey.toWrappedNoirStruct(),
)
.send()
.wait(),
Expand Down
8 changes: 4 additions & 4 deletions boxes/boxes/react/tests/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe('BoxReact Contract Tests', () => {
Fr.random(),
accountCompleteAddress.address,
masterNullifierPublicKey.hash(),
masterOutgoingViewingPublicKey.toNoirStruct(),
masterIncomingViewingPublicKey.toNoirStruct(),
masterOutgoingViewingPublicKey.toWrappedNoirStruct(),
masterIncomingViewingPublicKey.toWrappedNoirStruct(),
)
.send({ contractAddressSalt: salt })
.deployed();
Expand All @@ -39,8 +39,8 @@ describe('BoxReact Contract Tests', () => {
numberToSet,
accountCompleteAddress.address,
masterNullifierPublicKey.hash(),
masterOutgoingViewingPublicKey.toNoirStruct(),
masterIncomingViewingPublicKey.toNoirStruct(),
masterOutgoingViewingPublicKey.toWrappedNoirStruct(),
masterIncomingViewingPublicKey.toWrappedNoirStruct(),
)
.send()
.wait();
Expand Down
15 changes: 9 additions & 6 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
contract Vanilla {
use dep::aztec::prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point};
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys;
use dep::aztec::{
keys::public_keys::{IvpkM, OvpkM},
prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point},
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys
};
use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN};

#[aztec(storage)]
Expand All @@ -14,8 +17,8 @@ contract Vanilla {
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: Point,
owner_ivpk_m: Point
owner_ovpk_m: OvpkM,
owner_ivpk_m: IvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
Expand All @@ -27,8 +30,8 @@ contract Vanilla {
number: Field,
owner: AztecAddress,
owner_npk_m_hash: Field,
owner_ovpk_m: Point,
owner_ivpk_m: Point
owner_ovpk_m: OvpkM,
owner_ivpk_m: IvpkM
) {
let numbers = storage.numbers;
let mut new_number = ValueNote::new(number, owner_npk_m_hash);
Expand Down
8 changes: 4 additions & 4 deletions boxes/boxes/vanilla/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ document.querySelector('#deploy').addEventListener('click', async ({ target }: a
Fr.random(),
wallet.getCompleteAddress().address,
masterNullifierPublicKey.hash(),
masterOutgoingViewingPublicKey.toNoirStruct(),
masterIncomingViewingPublicKey.toNoirStruct(),
masterOutgoingViewingPublicKey.toWrappedNoirStruct(),
masterIncomingViewingPublicKey.toWrappedNoirStruct(),
)
.send({ contractAddressSalt: Fr.random() })
.deployed();
Expand All @@ -51,8 +51,8 @@ document.querySelector('#set').addEventListener('submit', async (e: Event) => {
parseInt(value),
owner,
masterNullifierPublicKey.hash(),
masterOutgoingViewingPublicKey.toNoirStruct(),
masterIncomingViewingPublicKey.toNoirStruct(),
masterOutgoingViewingPublicKey.toWrappedNoirStruct(),
masterIncomingViewingPublicKey.toWrappedNoirStruct(),
)
.send()
.wait();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::{
context::PrivateContext, event::event_interface::EventInterface,
encrypted_logs::payload::compute_encrypted_event_log, keys::getters::get_current_public_keys,
encrypted_logs::payload::compute_encrypted_event_log,
keys::{getters::get_current_public_keys, public_keys::{OvpkM, IvpkM}},
oracle::logs_traits::LensForEncryptedEvent, oracle::unsafe_rand::unsafe_rand
};
use dep::protocol_types::{address::AztecAddress, point::Point, hash::sha256_to_field};
use dep::protocol_types::{address::AztecAddress, hash::sha256_to_field};

unconstrained fn compute_unconstrained<Event, let NB: u32, let MB: u32, let OB: u32>(
contract_address: AztecAddress,
randomness: Field,
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
Expand All @@ -29,8 +30,8 @@ fn compute<Event, let NB: u32, let MB: u32, let OB: u32>(
contract_address: AztecAddress,
randomness: Field,
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
Expand All @@ -51,10 +52,10 @@ fn emit_with_keys<Event, let NB: u32, let MB: u32, let OB: u32>(
context: &mut PrivateContext,
randomness: Field,
event: Event,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
iv: AztecAddress,
inner_compute: fn(AztecAddress, Field, Field, Point, Point, AztecAddress, Event) -> ([u8; OB], Field)
inner_compute: fn(AztecAddress, Field, Field, OvpkM, IvpkM, AztecAddress, Event) -> ([u8; OB], Field)
) where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
let contract_address: AztecAddress = context.this_address();
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());
Expand Down Expand Up @@ -116,10 +117,10 @@ pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let NB: u32

pub fn encode_and_encrypt_event_with_keys<Event, let NB: u32, let MB: u32, let OB: u32>(
context: &mut PrivateContext,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Point, Point, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
Expand All @@ -128,10 +129,10 @@ pub fn encode_and_encrypt_event_with_keys<Event, let NB: u32, let MB: u32, let O

pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, let NB: u32, let MB: u32, let OB: u32>(
context: &mut PrivateContext,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Point, Point, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
Expand All @@ -141,10 +142,10 @@ pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, let NB: u32, let
pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, let NB: u32, let MB: u32, let OB: u32>(
context: &mut PrivateContext,
randomness: Field,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, Point, Point, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
Expand All @@ -153,10 +154,10 @@ pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, let NB: u32, le
pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained<Event, let NB: u32, let MB: u32, let OB: u32>(
context: &mut PrivateContext,
randomness: Field,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, Point, Point, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
context::PrivateContext, note::{note_emission::NoteEmission, note_interface::NoteInterface},
keys::getters::get_current_public_keys, encrypted_logs::payload::compute_encrypted_note_log,
oracle::logs_traits::LensForEncryptedLog
keys::{getters::get_current_public_keys, public_keys::{OvpkM, IvpkM}},
encrypted_logs::payload::compute_encrypted_note_log, oracle::logs_traits::LensForEncryptedLog
};
use dep::protocol_types::{
hash::sha256_to_field, address::AztecAddress, point::Point, abis::note_hash::NoteHash,
Expand All @@ -12,8 +12,8 @@ unconstrained fn compute_unconstrained<Note, let N: u32, let NB: u32, let M: u32
contract_address: AztecAddress,
storage_slot: Field,
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
note: Note
) -> ([u8; M], Field) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
Expand All @@ -32,8 +32,8 @@ fn compute<Note, let N: u32, let NB: u32, let M: u32>(
contract_address: AztecAddress,
storage_slot: Field,
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
note: Note
) -> ([u8; M], Field) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
Expand All @@ -53,10 +53,10 @@ fn compute<Note, let N: u32, let NB: u32, let M: u32>(
fn emit_with_keys<Note, let N: u32, let NB: u32, let M: u32>(
context: &mut PrivateContext,
note: Note,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
inner_compute: fn(AztecAddress, Field, Field, Point, Point, AztecAddress, Note) -> ([u8; M], Field)
inner_compute: fn(AztecAddress, Field, Field, OvpkM, IvpkM, AztecAddress, Note) -> ([u8; M], Field)
) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
let note_header = note.get_header();
let note_hash_counter = note_header.note_hash_counter;
Expand Down Expand Up @@ -107,21 +107,21 @@ pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32, let NB: u32, let

pub fn encode_and_encrypt_note_with_keys<Note, let N: u32, let NB: u32, let M: u32>(
context: &mut PrivateContext,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Point, Point, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
emit_with_keys(context, e.note, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_note_with_keys_unconstrained<Note, let N: u32, let NB: u32, let M: u32>(
context: &mut PrivateContext,
ovpk: Point,
ivpk: Point,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Point, Point, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
emit_with_keys(context, e.note, ovpk, ivpk, recipient, compute_unconstrained);
}
Expand Down
16 changes: 9 additions & 7 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/header.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::protocol_types::{address::AztecAddress, scalar::Scalar, point::Point};

use crate::keys::point_to_symmetric_key::point_to_symmetric_key;
use crate::keys::{point_to_symmetric_key::point_to_symmetric_key, public_keys::ToPoint};

use std::aes128::aes128_encrypt;

Expand All @@ -13,8 +13,8 @@ impl EncryptedLogHeader {
EncryptedLogHeader { address }
}

fn compute_ciphertext(self, secret: Scalar, point: Point) -> [u8; 48] {
let full_key = point_to_symmetric_key(secret, point);
fn compute_ciphertext<T>(self, secret: Scalar, pk: T) -> [u8; 48] where T: ToPoint {
let full_key = point_to_symmetric_key(secret, pk.to_point());
let mut sym_key = [0; 16];
let mut iv = [0; 16];

Expand All @@ -36,10 +36,12 @@ fn test_encrypted_log_header_matches_noir() {
lo: 0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd,
hi: 0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06
};
let point = Point {
x: 0x2688431c705a5ff3e6c6f2573c9e3ba1c1026d2251d0dbbf2d810aa53fd1d186,
y: 0x1e96887b117afca01c00468264f4f80b5bb16d94c1808a448595f115556e5c8e,
is_infinite: false
let point = crate::keys::public_keys::IvpkM {
inner: Point {
x: 0x2688431c705a5ff3e6c6f2573c9e3ba1c1026d2251d0dbbf2d810aa53fd1d186,
y: 0x1e96887b117afca01c00468264f4f80b5bb16d94c1808a448595f115556e5c8e,
is_infinite: false
}
};

let ciphertext = header.compute_ciphertext(secret, point);
Expand Down
Loading

0 comments on commit 638793a

Please sign in to comment.