Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Sep 26, 2024
1 parent e219074 commit 9331234
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ use crate::{
context::PrivateContext, event::event_interface::EventInterface,
encrypted_logs::payload::compute_encrypted_log,
keys::{getters::get_public_keys, public_keys::{OvpkM, IvpkM}},
oracle::logs_traits::LensForEncryptedEvent, oracle::unsafe_rand::unsafe_rand
oracle::unsafe_rand::unsafe_rand
};
use dep::protocol_types::{address::AztecAddress, hash::sha256_to_field};

unconstrained fn compute_unconstrained<Event, let N: u32, let OB: u32>(
unconstrained fn compute_unconstrained<Event, let N: u32>(
contract_address: AztecAddress,
randomness: Field,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> ([u8; 416 + N], Field) where Event: EventInterface<N> {
compute(
contract_address,
randomness,
Expand All @@ -26,41 +26,41 @@ unconstrained fn compute_unconstrained<Event, let N: u32, let OB: u32>(
)
}

fn compute<Event, let N: u32, let OB: u32>(
fn compute<Event, let N: u32>(
contract_address: AztecAddress,
randomness: Field,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> ([u8; 416 + N], Field) where Event: EventInterface<N> {
let plaintext = event.private_to_be_bytes(randomness);
let encrypted_log: [u8; OB] = compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
let encrypted_log: [u8; 416 + N] = compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
let log_hash = sha256_to_field(encrypted_log);
(encrypted_log, log_hash)
}

fn emit_with_keys<Event, let N: u32, let OB: u32>(
fn emit_with_keys<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
event: Event,
ovpk: OvpkM,
ivpk: IvpkM,
iv: AztecAddress,
inner_compute: fn(AztecAddress, Field, Field, OvpkM, IvpkM, AztecAddress, Event) -> ([u8; OB], Field)
) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
inner_compute: fn(AztecAddress, Field, Field, OvpkM, IvpkM, AztecAddress, Event) -> ([u8; 416 + N], Field)
) where Event: EventInterface<N> {
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, iv, event);
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}

pub fn encode_and_encrypt_event<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event<Event, let N: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
Expand All @@ -69,11 +69,11 @@ pub fn encode_and_encrypt_event<Event, let N: u32, let OB: u32>(
}
}

pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
Expand All @@ -82,75 +82,75 @@ pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32, let OB: u32>(
}
}

pub fn encode_and_encrypt_event_with_randomness<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_randomness<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute);
}
}

pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute_unconstrained);
}
}

pub fn encode_and_encrypt_event_with_keys<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys<Event, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
}

pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_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) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
Expand Down
37 changes: 0 additions & 37 deletions noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
use dep::protocol_types::address::AztecAddress;

trait LensForEncryptedEvent<let N: u32, let M: u32> {
// N = event preimage input in bytes
// M = encryption output len in bytes (= 416 + M)
fn output(self: [u8; N]) -> [u8; M];
}

impl LensForEncryptedEvent<96, 448> for [u8; 96] {
fn output(self) -> [u8; 448] {
[self[0] as u8; 448]
}
}
impl LensForEncryptedEvent<128, 480> for [u8; 128] {
fn output(self) -> [u8; 480] {
[self[0] as u8; 480]
}
}
impl LensForEncryptedEvent<160, 512> for [u8; 160] {
fn output(self) -> [u8; 512] {
[self[0] as u8; 512]
}
}
impl LensForEncryptedEvent<192, 544> for [u8; 192] {
fn output(self) -> [u8; 544] {
[self[0] as u8; 544]
}
}
impl LensForEncryptedEvent<224, 576> for [u8; 224] {
fn output(self) -> [u8; 576] {
[self[0] as u8; 576]
}
}
impl LensForEncryptedEvent<256, 608> for [u8; 256] {
fn output(self) -> [u8; 608] {
[self[0] as u8; 608]
}
}

// This trait defines the length of the inputs in bytes to
// the unencrypted log hash fn, where the log can be any type T
// as long as the ACVM can convert to fields.
Expand Down

0 comments on commit 9331234

Please sign in to comment.