Skip to content

Commit 2a13cfd

Browse files
author
sklppy88
committed
init
1 parent 300de7f commit 2a13cfd

File tree

7 files changed

+99
-64
lines changed

7 files changed

+99
-64
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ where
2222
let contract_address: AztecAddress = context.this_address();
2323
let plaintext = event.private_to_be_bytes(randomness);
2424
let encrypted_log: [u8; 416 + N * 32] =
25-
compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
25+
compute_encrypted_log(contract_address, ovsk_app, ovpk, recipient, plaintext);
2626
let log_hash = sha256_to_field(encrypted_log);
2727
(encrypted_log, log_hash)
2828
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ where
3030

3131
let plaintext = note.to_be_bytes(storage_slot);
3232
let encrypted_log: [u8; 416 + N * 32] =
33-
compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
33+
compute_encrypted_log(contract_address, ovsk_app, ovpk, recipient, plaintext);
3434
let log_hash = sha256_to_field(encrypted_log);
3535

3636
(note_hash_counter, encrypted_log, log_hash)
@@ -122,7 +122,6 @@ pub fn encrypt_and_emit_partial_log<let M: u32>(
122122
context.this_address(),
123123
ovsk_app,
124124
recipient_keys.ovpk_m,
125-
recipient_keys.ivpk_m,
126125
recipient,
127126
log_plaintext,
128127
);

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

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,26 @@ use crate::{
1212
encrypted_logs::header::EncryptedLogHeader,
1313
keys::point_to_symmetric_key::point_to_symmetric_key,
1414
};
15+
use protocol_types::public_keys::AddressPoint;
1516

1617
pub fn compute_encrypted_log<let P: u32, let M: u32>(
1718
contract_address: AztecAddress,
1819
ovsk_app: Field,
1920
ovpk: OvpkM,
20-
ivpk: IvpkM,
2121
recipient: AztecAddress,
2222
plaintext: [u8; P],
2323
) -> [u8; M] {
2424
let (eph_sk, eph_pk) = generate_ephemeral_key_pair();
2525

2626
let header = EncryptedLogHeader::new(contract_address);
2727

28-
let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, recipient);
28+
let incoming_header_ciphertext: [u8; 48] =
29+
header.compute_ciphertext(eph_sk, recipient.to_address_point());
2930
let outgoing_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ovpk);
3031
let incoming_body_ciphertext =
31-
compute_incoming_body_ciphertext(plaintext, eph_sk, IvpkM { inner: recipient.to_point() });
32-
let outgoing_body_ciphertext: [u8; 144] = compute_outgoing_body_ciphertext(
33-
recipient,
34-
IvpkM { inner: recipient.to_point() },
35-
fr_to_fq(ovsk_app),
36-
eph_sk,
37-
eph_pk,
38-
);
32+
compute_incoming_body_ciphertext(plaintext, eph_sk, recipient.to_address_point());
33+
let outgoing_body_ciphertext: [u8; 144] =
34+
compute_outgoing_body_ciphertext(recipient, fr_to_fq(ovsk_app), eph_sk, eph_pk);
3935

4036
let mut encrypted_bytes: [u8; M] = [0; M];
4137
// @todo We ignore the tags for now
@@ -96,9 +92,9 @@ fn generate_ephemeral_key_pair() -> (Scalar, Point) {
9692
pub fn compute_incoming_body_ciphertext<let P: u32>(
9793
plaintext: [u8; P],
9894
eph_sk: Scalar,
99-
ivpk: IvpkM,
95+
address_point: AddressPoint,
10096
) -> [u8] {
101-
let full_key = point_to_symmetric_key(eph_sk, ivpk.to_point());
97+
let full_key = point_to_symmetric_key(eph_sk, address_point.to_point());
10298
let mut sym_key = [0; 16];
10399
let mut iv = [0; 16];
104100

@@ -113,7 +109,6 @@ pub fn compute_incoming_body_ciphertext<let P: u32>(
113109
/// be able to derive the key with which the incoming log can be decrypted.
114110
pub fn compute_outgoing_body_ciphertext(
115111
recipient: AztecAddress,
116-
recipient_ivpk: IvpkM,
117112
ovsk_app: Scalar,
118113
eph_sk: Scalar,
119114
eph_pk: Point,
@@ -126,7 +121,7 @@ pub fn compute_outgoing_body_ciphertext(
126121
let serialized_eph_sk_low: [u8; 32] = eph_sk.lo.to_be_bytes();
127122

128123
let address_bytes: [u8; 32] = recipient.to_field().to_be_bytes();
129-
let serialized_recipient_ivpk = point_to_bytes(recipient_ivpk.to_point());
124+
let serialized_recipient_ivpk = point_to_bytes(recipient.to_address_point().to_point());
130125

131126
for i in 0..32 {
132127
buffer[i] = serialized_eph_sk_high[i];
@@ -163,6 +158,7 @@ mod test {
163158
address::AztecAddress, public_keys::{OvpkM, IvpkM}, point::Point, scalar::Scalar,
164159
};
165160
use std::test::OracleMock;
161+
use protocol_types::public_keys::AddressPoint;
166162

167163
#[test]
168164
unconstrained fn test_encrypted_log_matches_typescript() {
@@ -203,16 +199,10 @@ mod test {
203199
0x25afb798ea6d0b8c1618e50fdeafa463059415013d3b7c75d46abf5e242be70c,
204200
);
205201

206-
let log: [u8; 448] = compute_encrypted_log(
207-
contract_address,
208-
ovsk_app,
209-
ovpk_m,
210-
ivpk_m,
211-
recipient,
212-
plaintext,
213-
);
202+
let log: [u8; 448] =
203+
compute_encrypted_log(contract_address, ovsk_app, ovpk_m, recipient, plaintext);
214204

215-
// The following value was generated by `tagged_log.test.ts`
205+
// The following value was generated by `encrypted_log_payload.test.ts`
216206
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
217207
let encrypted_log_from_typescript = [
218208
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,
@@ -249,7 +239,7 @@ mod test {
249239
lo: 0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd,
250240
hi: 0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
251241
};
252-
let ivpk = IvpkM {
242+
let address_point = AddressPoint {
253243
inner: Point {
254244
x: 0x2688431c705a5ff3e6c6f2573c9e3ba1c1026d2251d0dbbf2d810aa53fd1d186,
255245
y: 0x1e96887b117afca01c00468264f4f80b5bb16d94c1808a448595f115556e5c8e,
@@ -267,7 +257,7 @@ mod test {
267257

268258
// `compute_incoming_body_ciphertext(...)` function then derives symmetric key from `eph_sk` and `ivpk` and encrypts
269259
// the note plaintext using AES-128.
270-
let ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, ivpk);
260+
let ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, address_point);
271261

272262
// The following value was generated by `encrypted_note_log_incoming_body.test.ts`.
273263
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
@@ -297,39 +287,31 @@ mod test {
297287
lo: 0x00000000000000000000000000000000d0d302ee245dfaf2807e604eec4715fe,
298288
hi: 0x000000000000000000000000000000000f096b423017226a18461115fa8d34bb,
299289
};
300-
let recipient_ivsk = Scalar {
301-
lo: 0x000000000000000000000000000000004828f8f95676ebb481df163f87fd4022,
302-
hi: 0x000000000000000000000000000000000f4d97c25d578f9348251a71ca17ae31,
303-
};
290+
304291
let sender_ovsk_app = Scalar {
305292
lo: 0x0000000000000000000000000000000074d2e28c6bc5176ac02cf7c7d36a444e,
306293
hi: 0x00000000000000000000000000000000089c6887cb1446d86c64e81afc78048b,
307294
};
308295

309296
let eph_pk = derive_public_key(eph_sk);
310-
let recipient_ivpk = IvpkM { inner: derive_public_key(recipient_ivsk) };
311-
312-
let recipient = AztecAddress::from_field(0xdeadbeef);
313-
314-
let ciphertext = compute_outgoing_body_ciphertext(
315-
recipient,
316-
recipient_ivpk,
317-
sender_ovsk_app,
318-
eph_sk,
319-
eph_pk,
297+
let recipient = AztecAddress::from_field(
298+
0x25afb798ea6d0b8c1618e50fdeafa463059415013d3b7c75d46abf5e242be70c,
320299
);
321300

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

335317
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
@@ -24,6 +24,7 @@ use std::{
2424
ec::{sqrt, pow},
2525
embedded_curve_ops::{fixed_base_scalar_mul as derive_public_key, EmbeddedCurveScalar},
2626
};
27+
use crate::public_keys::AddressPoint;
2728

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

69-
impl ToPoint for AztecAddress {
70-
fn to_point(self) -> Point {
70+
impl AztecAddress {
71+
pub fn zero() -> Self {
72+
Self { inner: 0 }
73+
}
74+
75+
pub fn to_address_point(self) -> AddressPoint {
7176
// Calculate y^2 = x^3 - 17
7277
let y_squared = pow(self.inner, 3) - 17;
7378

@@ -84,13 +89,7 @@ impl ToPoint for AztecAddress {
8489
y = (BN254_FR_MODULUS_DIV_2 + BN254_FR_MODULUS_DIV_2 + 1) - y;
8590
}
8691

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

9695
pub fn compute_preaddress(

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
@@ -193,6 +193,16 @@ impl Deserialize<PUBLIC_KEYS_LENGTH> for PublicKeys {
193193
}
194194
}
195195

196+
pub struct AddressPoint {
197+
inner: Point,
198+
}
199+
200+
impl ToPoint for AddressPoint {
201+
fn to_point(self) -> Point {
202+
self.inner
203+
}
204+
}
205+
196206
#[test]
197207
unconstrained fn compute_public_keys_hash() {
198208
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');

yarn-project/protocol-contracts/src/protocol_contract_data.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ export const ProtocolContractAddress: Record<ProtocolContractName, AztecAddress>
5050
};
5151

5252
export const ProtocolContractLeaf = {
53-
AuthRegistry: Fr.fromString('0x087d102766af335cf7654eb5d946dccf114d0eb1e86dc788cbc10d87e1c84fd0'),
54-
ContractInstanceDeployer: Fr.fromString('0x10141a7093a1050d38fc0467c6c81ca7beb158ef47095145ce4edd52be7ef661'),
55-
ContractClassRegisterer: Fr.fromString('0x074de3b4ee658fd695a1be6c4f9f10fe3042b948955436e0e20c31c7027cfaef'),
56-
MultiCallEntrypoint: Fr.fromString('0x19bc0ab26e84d8b2551cb3c83000a5da0f6958056fcaa91f63c14c35e266ef0d'),
57-
FeeJuice: Fr.fromString('0x033a034d778d077970ae6108253a4971e37af6bd163d80b3be184c5838045eda'),
58-
Router: Fr.fromString('0x24eed1db8f33a2db1611412c16157a28a57c7bef4794844c4ef228d538438ac8'),
53+
AuthRegistry: Fr.fromString('0x25287d474d84b6ecb5d4f5e90b8efe5969a35ddb9d605077e5df17dac0f2aa58'),
54+
ContractInstanceDeployer: Fr.fromString('0x1b92794557c9c6694ede81d4ea8909f786fe37ec51018d673f9ed6d8df09fcb2'),
55+
ContractClassRegisterer: Fr.fromString('0x034af167c41eeb46cb695f9fc56824b3339b23aa670ebfef7bf5d3c8dd5c13d4'),
56+
MultiCallEntrypoint: Fr.fromString('0x009d1fc8ca80534c2de293ce6eedc71cc145e0562fd1af0826c734c77b1543a5'),
57+
FeeJuice: Fr.fromString('0x07c4d7db5027bcdb7b71a60186a5e137d22bd8412d11fee0676d070c68d0f7ee'),
58+
Router: Fr.fromString('0x12df324157fddc5be72e52e527416e3f7c70240deab1c953613d904232e1eb78'),
5959
};
6060

6161
export const protocolContractTreeRoot = Fr.fromString(
62-
'0x1299197d756a2e86830d877cb2c5c71ab20aa4f97726fc7ccd2d468ce20d81a6',
62+
'0x04180f14bbf7d65ec020746102a118a58c4aa4016cdd85a41614fc4d972607cf',
6363
);

0 commit comments

Comments
 (0)