Skip to content

Commit 6b18887

Browse files
author
sklppy88
committed
init
1 parent 2ebe361 commit 6b18887

6 files changed

Lines changed: 88 additions & 49 deletions

File tree

noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ where
2929
contract_address,
3030
ovsk_app,
3131
ovpk,
32-
ivpk,
3332
recipient,
3433
plaintext,
3534
false,

noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ where
4040
contract_address,
4141
ovsk_app,
4242
ovpk,
43-
ivpk,
4443
recipient,
4544
plaintext,
4645
true,

noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use crate::{
1616
keys::point_to_symmetric_key::point_to_symmetric_key, oracle::random::random,
1717
utils::point::point_to_bytes,
1818
};
19+
use protocol_types::public_keys::AddressPoint;
1920

2021
fn compute_private_log_payload<let P: u32, let M: u32>(
2122
contract_address: AztecAddress,
2223
ovsk_app: Field,
2324
ovpk: OvpkM,
24-
ivpk: IvpkM,
2525
recipient: AztecAddress,
2626
plaintext: [u8; P],
2727
include_public_values_prefix: bool,
@@ -30,17 +30,13 @@ fn compute_private_log_payload<let P: u32, let M: u32>(
3030

3131
let header = EncryptedLogHeader::new(contract_address);
3232

33-
let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, recipient);
33+
let incoming_header_ciphertext: [u8; 48] =
34+
header.compute_ciphertext(eph_sk, recipient.to_address_point());
3435
let outgoing_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ovpk);
3536
let incoming_body_ciphertext =
36-
compute_incoming_body_ciphertext(plaintext, eph_sk, IvpkM { inner: recipient.to_point() });
37-
let outgoing_body_ciphertext: [u8; 144] = compute_outgoing_body_ciphertext(
38-
recipient,
39-
IvpkM { inner: recipient.to_point() },
40-
fr_to_fq(ovsk_app),
41-
eph_sk,
42-
eph_pk,
43-
);
37+
compute_incoming_body_ciphertext(plaintext, eph_sk, recipient.to_address_point());
38+
let outgoing_body_ciphertext: [u8; 144] =
39+
compute_outgoing_body_ciphertext(recipient, fr_to_fq(ovsk_app), eph_sk, eph_pk);
4440

4541
// If we include the prefix for number of public values, we need to add 1 byte to the offset
4642
let mut offset = if include_public_values_prefix { 1 } else { 0 };
@@ -113,9 +109,9 @@ fn generate_ephemeral_key_pair() -> (Scalar, Point) {
113109
pub fn compute_incoming_body_ciphertext<let P: u32>(
114110
plaintext: [u8; P],
115111
eph_sk: Scalar,
116-
ivpk: IvpkM,
112+
address_point: AddressPoint,
117113
) -> [u8] {
118-
let full_key = point_to_symmetric_key(eph_sk, ivpk.to_point());
114+
let full_key = point_to_symmetric_key(eph_sk, address_point.to_point());
119115
let mut sym_key = [0; 16];
120116
let mut iv = [0; 16];
121117

@@ -130,7 +126,6 @@ pub fn compute_incoming_body_ciphertext<let P: u32>(
130126
/// be able to derive the key with which the incoming log can be decrypted.
131127
pub fn compute_outgoing_body_ciphertext(
132128
recipient: AztecAddress,
133-
recipient_ivpk: IvpkM,
134129
ovsk_app: Scalar,
135130
eph_sk: Scalar,
136131
eph_pk: Point,
@@ -143,7 +138,7 @@ pub fn compute_outgoing_body_ciphertext(
143138
let serialized_eph_sk_low: [u8; 32] = eph_sk.lo.to_be_bytes();
144139

145140
let address_bytes: [u8; 32] = recipient.to_field().to_be_bytes();
146-
let serialized_recipient_ivpk = point_to_bytes(recipient_ivpk.to_point());
141+
let serialized_recipient_ivpk = point_to_bytes(recipient.to_address_point().to_point());
147142

148143
for i in 0..32 {
149144
buffer[i] = serialized_eph_sk_high[i];
@@ -184,6 +179,7 @@ mod test {
184179
};
185180
use std::embedded_curve_ops::fixed_base_scalar_mul as derive_public_key;
186181
use std::test::OracleMock;
182+
use protocol_types::public_keys::AddressPoint;
187183

188184
#[test]
189185
unconstrained fn test_encrypted_log_matches_typescript() {
@@ -228,13 +224,12 @@ mod test {
228224
contract_address,
229225
ovsk_app,
230226
ovpk_m,
231-
ivpk_m,
232227
recipient,
233228
plaintext,
234229
false,
235230
);
236231

237-
// The following value was generated by `tagged_log.test.ts`
232+
// The following value was generated by `encrypted_log_payload.test.ts`
238233
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
239234
let encrypted_log_from_typescript = [
240235
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -271,7 +266,7 @@ mod test {
271266
lo: 0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd,
272267
hi: 0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
273268
};
274-
let ivpk = IvpkM {
269+
let address_point = AddressPoint {
275270
inner: Point {
276271
x: 0x2688431c705a5ff3e6c6f2573c9e3ba1c1026d2251d0dbbf2d810aa53fd1d186,
277272
y: 0x1e96887b117afca01c00468264f4f80b5bb16d94c1808a448595f115556e5c8e,
@@ -289,7 +284,7 @@ mod test {
289284

290285
// `compute_incoming_body_ciphertext(...)` function then derives symmetric key from `eph_sk` and `ivpk` and encrypts
291286
// the note plaintext using AES-128.
292-
let ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, ivpk);
287+
let ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, address_point);
293288

294289
// The following value was generated by `encrypted_note_log_incoming_body.test.ts`.
295290
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
@@ -319,39 +314,31 @@ mod test {
319314
lo: 0x00000000000000000000000000000000d0d302ee245dfaf2807e604eec4715fe,
320315
hi: 0x000000000000000000000000000000000f096b423017226a18461115fa8d34bb,
321316
};
322-
let recipient_ivsk = Scalar {
323-
lo: 0x000000000000000000000000000000004828f8f95676ebb481df163f87fd4022,
324-
hi: 0x000000000000000000000000000000000f4d97c25d578f9348251a71ca17ae31,
325-
};
317+
326318
let sender_ovsk_app = Scalar {
327319
lo: 0x0000000000000000000000000000000074d2e28c6bc5176ac02cf7c7d36a444e,
328320
hi: 0x00000000000000000000000000000000089c6887cb1446d86c64e81afc78048b,
329321
};
330322

331323
let eph_pk = derive_public_key(eph_sk);
332-
let recipient_ivpk = IvpkM { inner: derive_public_key(recipient_ivsk) };
333-
334-
let recipient = AztecAddress::from_field(0xdeadbeef);
335-
336-
let ciphertext = compute_outgoing_body_ciphertext(
337-
recipient,
338-
recipient_ivpk,
339-
sender_ovsk_app,
340-
eph_sk,
341-
eph_pk,
324+
let recipient = AztecAddress::from_field(
325+
0x25afb798ea6d0b8c1618e50fdeafa463059415013d3b7c75d46abf5e242be70c,
342326
);
343327

344-
// The following value was generated by `encrypted_log_outgoing_body.test.ts`
328+
let ciphertext =
329+
compute_outgoing_body_ciphertext(recipient, sender_ovsk_app, eph_sk, eph_pk);
330+
331+
// The following value was generated by `encrypted_log_payload.test.ts`
345332
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
346333
let outgoing_body_ciphertext_from_typescript = [
347334
127, 182, 227, 75, 192, 197, 54, 47, 168, 134, 233, 148, 251, 46, 86, 12, 73, 50, 238,
348335
50, 31, 174, 27, 202, 110, 77, 161, 197, 244, 124, 17, 100, 143, 150, 232, 14, 156, 248,
349336
43, 177, 16, 82, 244, 103, 88, 74, 84, 200, 15, 65, 187, 14, 163, 60, 91, 22, 104, 31,
350-
211, 190, 124, 121, 79, 92, 239, 65, 185, 106, 51, 178, 168, 137, 84, 43, 79, 158, 151,
351-
152, 83, 42, 170, 13, 106, 209, 254, 74, 39, 145, 73, 215, 17, 234, 196, 89, 30, 58,
352-
120, 127, 88, 69, 121, 61, 18, 206, 89, 118, 243, 238, 177, 71, 73, 47, 147, 4, 155, 25,
353-
173, 248, 206, 52, 17, 180, 122, 186, 106, 191, 252, 102, 197, 91, 16, 39, 94, 91, 224,
354-
30, 168, 177, 26, 144, 5, 124, 128, 6,
337+
211, 190, 124, 121, 79, 92, 238, 182, 194, 225, 34, 71, 67, 116, 27, 231, 68, 161, 147,
338+
94, 53, 195, 83, 237, 172, 52, 173, 229, 26, 234, 107, 43, 82, 68, 16, 105, 37, 125,
339+
117, 86, 133, 50, 21, 92, 74, 229, 105, 141, 83, 229, 255, 251, 21, 61, 234, 61, 168,
340+
221, 106, 231, 8, 73, 208, 60, 251, 46, 251, 228, 148, 144, 187, 195, 38, 18, 223, 153,
341+
8, 121, 178, 84, 237, 148, 254, 219, 59, 62,
355342
];
356343

357344
assert_eq(outgoing_body_ciphertext_from_typescript, ciphertext);

noir-projects/noir-protocol-circuits/crates/types/src/address/aztec_address.nr

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::{
2323
ec::{pow, sqrt},
2424
embedded_curve_ops::{EmbeddedCurveScalar, fixed_base_scalar_mul as derive_public_key},
2525
};
26+
use crate::public_keys::AddressPoint;
2627

2728
// Aztec address
2829
pub struct AztecAddress {
@@ -65,8 +66,12 @@ impl Deserialize<AZTEC_ADDRESS_LENGTH> for AztecAddress {
6566
}
6667
}
6768

68-
impl ToPoint for AztecAddress {
69-
fn to_point(self) -> Point {
69+
impl AztecAddress {
70+
pub fn zero() -> Self {
71+
Self { inner: 0 }
72+
}
73+
74+
pub fn to_address_point(self) -> AddressPoint {
7075
// We compute the address point by taking our address, setting it to x, and then solving for y in the
7176
// equation which defines our bn curve:
7277
// y^2 = x^3 - 17; x = address
@@ -83,13 +88,7 @@ impl ToPoint for AztecAddress {
8388
y = (MAX_FIELD_VALUE + 1) - y;
8489
}
8590

86-
Point { x: self.inner, y, is_infinite: false }
87-
}
88-
}
89-
90-
impl AztecAddress {
91-
pub fn zero() -> Self {
92-
Self { inner: 0 }
91+
AddressPoint { inner: Point { x: self.inner, y, is_infinite: false } }
9392
}
9493

9594
pub fn compute(public_keys: PublicKeys, partial_address: PartialAddress) -> AztecAddress {

noir-projects/noir-protocol-circuits/crates/types/src/public_keys.nr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ impl Deserialize<PUBLIC_KEYS_LENGTH> for PublicKeys {
195195
}
196196
}
197197

198+
pub struct AddressPoint {
199+
inner: Point,
200+
}
201+
202+
impl ToPoint for AddressPoint {
203+
fn to_point(self) -> Point {
204+
self.inner
205+
}
206+
}
207+
198208
#[test]
199209
unconstrained fn compute_public_keys_hash() {
200210
let keys = PublicKeys {

yarn-project/circuit-types/src/logs/l1_payload/encrypted_log_payload.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import {
1010
} from '@aztec/circuits.js';
1111
import { randomBytes } from '@aztec/foundation/crypto';
1212
import { Fr, GrumpkinScalar } from '@aztec/foundation/fields';
13+
import { serializeToBuffer } from '@aztec/foundation/serialize';
1314
import { updateInlineTestData } from '@aztec/foundation/testing';
1415

1516
import { EncryptedLogPayload } from './encrypted_log_payload.js';
17+
import { encrypt } from './encryption_util.js';
18+
import { derivePoseidonAESSecret } from './shared_secret_derivation.js';
1619

1720
// placeholder value until tagging is implemented
1821
const PLACEHOLDER_TAG = new Fr(33);
@@ -59,6 +62,48 @@ describe('EncryptedLogPayload', () => {
5962
});
6063
});
6164

65+
it('outgoing ciphertest matches Noir', () => {
66+
const ephSk = GrumpkinScalar.fromHighLow(
67+
new Fr(0x000000000000000000000000000000000f096b423017226a18461115fa8d34bbn),
68+
new Fr(0x00000000000000000000000000000000d0d302ee245dfaf2807e604eec4715fen),
69+
);
70+
71+
const senderOvskApp = GrumpkinScalar.fromHighLow(
72+
new Fr(0x00000000000000000000000000000000089c6887cb1446d86c64e81afc78048bn),
73+
new Fr(0x0000000000000000000000000000000074d2e28c6bc5176ac02cf7c7d36a444en),
74+
);
75+
76+
const ephPk = derivePublicKeyFromSecretKey(ephSk);
77+
78+
const recipient = AztecAddress.fromBigInt(0x25afb798ea6d0b8c1618e50fdeafa463059415013d3b7c75d46abf5e242be70cn);
79+
80+
const outgoingBodyPlaintext = serializeToBuffer(
81+
ephSk.hi,
82+
ephSk.lo,
83+
recipient,
84+
computePoint(recipient).toCompressedBuffer(),
85+
);
86+
const outgoingBodyCiphertext = encrypt(
87+
outgoingBodyPlaintext,
88+
senderOvskApp,
89+
ephPk,
90+
derivePoseidonAESSecret,
91+
).toString('hex');
92+
93+
expect(outgoingBodyCiphertext).toMatchInlineSnapshot(
94+
`"7fb6e34bc0c5362fa886e994fb2e560c4932ee321fae1bca6e4da1c5f47c11648f96e80e9cf82bb11052f467584a54c80f41bb0ea33c5b16681fd3be7c794f5ceeb6c2e1224743741be744a1935e35c353edac34ade51aea6b2b52441069257d75568532155c4ae5698d53e5fffb153dea3da8dd6ae70849d03cfb2efbe49490bbc32612df990879b254ed94fedb3b3e"`,
95+
);
96+
97+
const byteArrayString = `[${outgoingBodyCiphertext.match(/.{1,2}/g)!.map(byte => parseInt(byte, 16))}]`;
98+
99+
// Run with AZTEC_GENERATE_TEST_DATA=1 to update noir test data
100+
updateInlineTestData(
101+
'noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr',
102+
'outgoing_body_ciphertext_from_typescript',
103+
byteArrayString,
104+
);
105+
});
106+
62107
it('encrypted tagged log matches Noir', () => {
63108
// All the values in this test were arbitrarily set and copied over to `payload.nr`
64109
const contract = AztecAddress.fromString('0x10f48cd9eff7ae5b209c557c70de2e657ee79166868676b787e9417e19260e04');

0 commit comments

Comments
 (0)