-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathencrypted_event_emission.nr
164 lines (153 loc) · 6.26 KB
/
encrypted_event_emission.nr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::{
context::PrivateContext, event::event_interface::EventInterface,
encrypted_logs::payload::compute_encrypted_event_log,
keys::{getters::get_public_keys, public_keys::{OvpkM, IvpkM}},
oracle::logs_traits::LensForEncryptedEvent, 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>(
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> {
compute(
contract_address,
randomness,
ovsk_app,
ovpk,
ivpk,
recipient,
event
)
}
fn compute<Event, let N: u32, let OB: 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> {
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)
}
fn emit_with_keys<Event, let N: u32, let OB: 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> {
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>(
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> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute);
}
}
pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32, let OB: 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> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute_unconstrained);
}
}
pub fn encode_and_encrypt_event_with_randomness<Event, let N: u32, let OB: 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> {
| 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>(
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> {
| 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>(
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> {
| 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>(
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> {
| 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>(
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> {
| 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>(
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> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
}