Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Jul 15, 2024
1 parent 699fb79 commit e69efd2
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 75 deletions.
4 changes: 2 additions & 2 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract BoxReact {
) {
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_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m));
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner));
}

#[aztec(private)]
Expand All @@ -33,7 +33,7 @@ contract BoxReact {
) {
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_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m));
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner));
}

unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote {
Expand Down
4 changes: 2 additions & 2 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract Vanilla {
) {
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_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m));
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner));
}

#[aztec(private)]
Expand All @@ -33,7 +33,7 @@ contract Vanilla {
) {
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_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m));
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner));
}

unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ unconstrained fn compute_unconstrained<Event, NB, MB, OB>(
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
compute(contract_address, randomness, ovsk_app, ovpk, ivpk, event)
compute(
contract_address,
randomness,
ovsk_app,
ovpk,
ivpk,
recipient,
event
)
}

fn compute<Event, NB, MB, OB>(
Expand All @@ -22,9 +31,18 @@ fn compute<Event, NB, MB, OB>(
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
let encrypted_log: [u8; OB] = compute_encrypted_event_log(contract_address, randomness, ovsk_app, ovpk, ivpk, event);
let encrypted_log: [u8; OB] = compute_encrypted_event_log(
contract_address,
randomness,
ovsk_app,
ovpk,
ivpk,
recipient,
event
);
let log_hash = sha256_to_field(encrypted_log);
(encrypted_log, log_hash)
}
Expand All @@ -35,11 +53,12 @@ fn emit_with_keys<Event, NB, MB, OB>(
event: Event,
ovpk: Point,
ivpk: Point,
inner_compute: fn(AztecAddress, Field, Field, Point, Point, Event) -> ([u8; OB], Field)
iv: AztecAddress,
inner_compute: fn(AztecAddress, Field, Field, Point, Point, 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());
let (encrypted_log, log_hash) = inner_compute(contract_address, randomness, ovsk_app, ovpk, ivpk, event);
let (encrypted_log, log_hash) = inner_compute(contract_address, randomness, ovsk_app, ovpk, ivpk, iv, event);
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}

Expand All @@ -53,7 +72,7 @@ pub fn encode_and_encrypt_event<Event, NB, MB, OB>(
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, compute);
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute);
}
}

Expand All @@ -67,7 +86,7 @@ pub fn encode_and_encrypt_event_unconstrained<Event, NB, MB, OB>(
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, compute_unconstrained);
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute_unconstrained);
}
}

Expand All @@ -81,7 +100,7 @@ pub fn encode_and_encrypt_event_with_randomness<Event, NB, MB, OB>(
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
emit_with_keys(context, randomness, e, ovpk, ivpk, compute);
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute);
}
}

Expand All @@ -95,50 +114,54 @@ pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, NB, MB, OB>
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
emit_with_keys(context, randomness, e, ovpk, ivpk, compute_unconstrained);
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute_unconstrained);
}
}

pub fn encode_and_encrypt_event_with_keys<Event, NB, MB, OB>(
context: &mut PrivateContext,
ovpk: Point,
ivpk: Point
) -> fn[(&mut PrivateContext, Point, Point)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
ivpk: Point,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Point, Point, 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, compute);
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, NB, MB, OB>(
context: &mut PrivateContext,
ovpk: Point,
ivpk: Point
) -> fn[(&mut PrivateContext, Point, Point)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
ivpk: Point,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Point, Point, 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, compute_unconstrained);
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
}

pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, NB, MB, OB>(
context: &mut PrivateContext,
randomness: Field,
ovpk: Point,
ivpk: Point
) -> fn[(&mut PrivateContext, Field, Point, Point)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
ivpk: Point,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, Point, Point, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, compute);
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained<Event, NB, MB, OB>(
context: &mut PrivateContext,
randomness: Field,
ovpk: Point,
ivpk: Point
) -> fn[(&mut PrivateContext, Field, Point, Point)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
ivpk: Point,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, Point, Point, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, compute_unconstrained);
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ unconstrained fn compute_unconstrained<Note, N, NB, M>(
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
recipient: AztecAddress,
note: Note
) -> ([u8; M], Field) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
compute(contract_address, storage_slot, ovsk_app, ovpk, ivpk, note)
compute(
contract_address,
storage_slot,
ovsk_app,
ovpk,
ivpk,
recipient,
note
)
}

fn compute<Note, N, NB, M>(
Expand All @@ -24,9 +33,18 @@ fn compute<Note, N, NB, M>(
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
recipient: AztecAddress,
note: Note
) -> ([u8; M], Field) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
let encrypted_log: [u8; M] = compute_encrypted_note_log(contract_address, storage_slot, ovsk_app, ovpk, ivpk, note);
let encrypted_log: [u8; M] = compute_encrypted_note_log(
contract_address,
storage_slot,
ovsk_app,
ovpk,
ivpk,
recipient,
note
);
let log_hash = sha256_to_field(encrypted_log);
(encrypted_log, log_hash)
}
Expand All @@ -36,7 +54,8 @@ fn emit_with_keys<Note, N, NB, M>(
note: Note,
ovpk: Point,
ivpk: Point,
inner_compute: fn(AztecAddress, Field, Field, Point, Point, Note) -> ([u8; M], Field)
recipient: AztecAddress,
inner_compute: fn(AztecAddress, Field, Field, Point, Point, 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 All @@ -53,7 +72,15 @@ fn emit_with_keys<Note, N, NB, M>(
let contract_address: AztecAddress = context.this_address();
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());

let (encrypted_log, log_hash) = inner_compute(contract_address, storage_slot, ovsk_app, ovpk, ivpk, note);
let (encrypted_log, log_hash) = inner_compute(
contract_address,
storage_slot,
ovsk_app,
ovpk,
ivpk,
recipient,
note
);

context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash);
}
Expand All @@ -67,7 +94,7 @@ pub fn encode_and_encrypt_note<Note, N, NB, M>(
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
emit_with_keys(context, e.note, ovpk, ivpk, compute);
emit_with_keys(context, e.note, ovpk, ivpk, iv, compute);
}
}

Expand All @@ -80,26 +107,28 @@ pub fn encode_and_encrypt_note_unconstrained<Note, N, NB, M>(
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
emit_with_keys(context, e.note, ovpk, ivpk, compute_unconstrained);
emit_with_keys(context, e.note, ovpk, ivpk, iv, compute_unconstrained);
}
}

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

pub fn encode_and_encrypt_note_with_keys_unconstrained<Note, N, NB, M>(
context: &mut PrivateContext,
ovpk: Point,
ivpk: Point
) -> fn[(&mut PrivateContext, Point, Point)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
ivpk: Point,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Point, Point, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
emit_with_keys(context, e.note, ovpk, ivpk, compute_unconstrained);
emit_with_keys(context, e.note, ovpk, ivpk, recipient, compute_unconstrained);
}
}
8 changes: 2 additions & 6 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ pub fn compute_encrypted_event_log<Event, NB, MB, OB>(
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
recipient: AztecAddress,
event: Event
) -> [u8; OB] where Event: EventInterface<NB, MB> {
// @todo Need to draw randomness from the full domain of Fq not only Fr
let eph_sk: Scalar = fr_to_fq(unsafe_rand());
let eph_pk = derive_public_key(eph_sk);

// TODO: (#7177) This value needs to be populated!
let recipient = AztecAddress::from_field(0);

let ivpk_app = compute_ivpk_app(ivpk, contract_address);

let header = EncryptedLogHeader::new(contract_address);
Expand Down Expand Up @@ -78,15 +76,13 @@ pub fn compute_encrypted_note_log<Note, N, NB, M>(
ovsk_app: Field,
ovpk: Point,
ivpk: Point,
recipient: AztecAddress,
note: Note
) -> [u8; M] where Note: NoteInterface<N, NB> {
// @todo Need to draw randomness from the full domain of Fq not only Fr
let eph_sk: Scalar = fr_to_fq(unsafe_rand());
let eph_pk = derive_public_key(eph_sk);

// TODO: (#7177) This value needs to be populated!
let recipient = AztecAddress::from_field(0);

let ivpk_app = compute_ivpk_app(ivpk, contract_address);

let header = EncryptedLogHeader::new(contract_address);
Expand Down
6 changes: 6 additions & 0 deletions noir-projects/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ unconstrained fn compute_encrypted_note_log_oracle<N, M>(
_ovsk_app: Field,
_ovpk_m: Point,
_ivpk_m: Point,
_recipient: AztecAddress,
_preimage: [Field; N]
) -> [u8; M] {}

Expand All @@ -48,6 +49,7 @@ unconstrained pub fn compute_encrypted_note_log<N, M>(
ovsk_app: Field,
ovpk_m: Point,
ivpk_m: Point,
recipient: AztecAddress,
preimage: [Field; N]
) -> [u8; M] {
compute_encrypted_note_log_oracle(
Expand All @@ -57,6 +59,7 @@ unconstrained pub fn compute_encrypted_note_log<N, M>(
ovsk_app,
ovpk_m,
ivpk_m,
recipient,
preimage
)
}
Expand All @@ -70,6 +73,7 @@ unconstrained fn compute_encrypted_event_log_oracle<N, M>(
_ovsk_app: Field,
_ovpk_m: Point,
_ivpk_m: Point,
_recipient: AztecAddress,
_preimage: [Field; N]
) -> [u8; M] {}

Expand All @@ -80,6 +84,7 @@ unconstrained pub fn compute_encrypted_event_log<N, M>(
ovsk_app: Field,
ovpk_m: Point,
ivpk_m: Point,
recipient: AztecAddress,
preimage: [Field; N]
) -> [u8; M] {
compute_encrypted_event_log_oracle(
Expand All @@ -89,6 +94,7 @@ unconstrained pub fn compute_encrypted_event_log<N, M>(
ovsk_app,
ovpk_m,
ivpk_m,
recipient,
preimage
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ impl Deck<&mut PrivateContext> {
let mut inserted_cards = &[];
for card in cards {
let mut card_note = CardNote::from_card(card, owner_npk_m_hash);
self.set.insert(&mut card_note.note).emit(encode_and_encrypt_note_with_keys(self.set.context, msg_sender_ovpk_m, owner_ivpk_m));
self.set.insert(&mut card_note.note).emit(
encode_and_encrypt_note_with_keys(self.set.context, msg_sender_ovpk_m, owner_ivpk_m, owner)
);
inserted_cards = inserted_cards.push_back(card_note);
}

Expand Down
Loading

0 comments on commit e69efd2

Please sign in to comment.