Skip to content

Commit

Permalink
refactor: untangled TS encryption functionality (#8827)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Sep 30, 2024
1 parent fad3d6e commit 048a848
Show file tree
Hide file tree
Showing 36 changed files with 608 additions and 1,874 deletions.
22 changes: 12 additions & 10 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ fn generate_ephemeral_key_pair() -> (Scalar, Point) {
// @todo Need to draw randomness from the full domain of Fq not only Fr
// We use the unsafe version of `fr_to_fq` because multi_scalar_mul (called by derive_public_key) will constrain
// the scalars.
let eph_sk = fr_to_fq_unsafe(unsafe_rand());
let randomness = unsafe {
unsafe_rand()
};
let eph_sk = fr_to_fq_unsafe(randomness);
let eph_pk = derive_public_key(eph_sk);

(eph_sk, eph_pk)
Expand Down Expand Up @@ -141,17 +144,16 @@ pub fn compute_outgoing_body_ciphertext(
mod test {
use crate::{
encrypted_logs::payload::{compute_encrypted_log, compute_incoming_body_ciphertext, compute_outgoing_body_ciphertext},
keys::public_keys::{OvpkM, IvpkM}, test::mocks::mock_note::MockNoteBuilder
keys::public_keys::{OvpkM, IvpkM}
};
use std::embedded_curve_ops::fixed_base_scalar_mul as derive_public_key;
use dep::protocol_types::{address::AztecAddress, point::Point, scalar::Scalar};
use std::test::OracleMock;

#[test]
unconstrained fn test_encrypted_note_log_matches_typescript() {
unconstrained fn test_encrypted_log_matches_typescript() {
// All the values in this test were copied over from `tagged_log.test.ts`
let contract_address = AztecAddress::from_field(0x10f48cd9eff7ae5b209c557c70de2e657ee79166868676b787e9417e19260e04);
let storage_slot = 0x0fe46be583b71f4ab5b70c2657ff1d05cccf1d292a9369628d1a194f944e6599;
let ovsk_app = 0x03a6513d6def49f41d20373d2cec894c23e7492794b08fc50c0e8a1bd2512612;
let ovpk_m = OvpkM {
inner: Point {
Expand All @@ -168,9 +170,9 @@ mod test {
}
};

let note_value = 0x301640ceea758391b2e161c92c0513f129020f4125256afdae2646ce31099f5c;
let note = MockNoteBuilder::new(note_value).contract_address(contract_address).storage_slot(storage_slot).build();
let plaintext = note.to_be_bytes(storage_slot);
let plaintext = [
0, 0, 0, 1, 48, 22, 64, 206, 234, 117, 131, 145, 178, 225, 97, 201, 44, 5, 19, 241, 41, 2, 15, 65, 37, 37, 106, 253, 174, 38, 70, 206, 49, 9, 159, 92, 16, 244, 140, 217, 239, 247, 174, 91, 32, 156, 85, 124, 112, 222, 46, 101, 126, 231, 145, 102, 134, 134, 118, 183, 135, 233, 65, 126, 25, 38, 14, 4, 15, 228, 107, 229, 131, 183, 31, 74, 181, 183, 12, 38, 87, 255, 29, 5, 204, 207, 29, 41, 42, 147, 105, 98, 141, 26, 25, 79, 148, 78, 101, 153, 0, 0, 16, 39
];

let eph_sk = 0x1358d15019d4639393d62b97e1588c095957ce74a1c32d6ec7d62fe6705d9538;
let _ = OracleMock::mock("getRandomField").returns(eph_sk);
Expand All @@ -181,10 +183,10 @@ mod test {

// The following value was generated by `tagged_log.test.ts`
// --> Run the test with AZTEC_GENERATE_TEST_DATA=1 flag to update test data.
let encrypted_note_log_from_typescript = [
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, 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, 0, 0, 0, 0, 0, 0, 141, 70, 12, 14, 67, 77, 132, 110, 193, 234, 40, 110, 64, 144, 235, 86, 55, 111, 242, 123, 221, 193, 170, 202, 225, 216, 86, 84, 159, 112, 31, 167, 126, 79, 51, 186, 47, 71, 253, 172, 99, 112, 241, 59, 197, 241, 107, 186, 232, 87, 187, 230, 171, 62, 228, 234, 42, 51, 145, 146, 238, 242, 42, 71, 206, 13, 244, 66, 111, 195, 20, 203, 98, 148, 204, 242, 145, 183, 156, 29, 141, 54, 44, 220, 194, 35, 229, 16, 32, 204, 211, 49, 142, 112, 82, 202, 116, 241, 254, 146, 42, 217, 20, 189, 70, 228, 182, 171, 205, 104, 27, 99, 171, 28, 91, 244, 21, 30, 130, 240, 5, 72, 174, 124, 97, 197, 157, 248, 193, 23, 193, 76, 46, 141, 144, 70, 211, 45, 67, 167, 218, 129, 140, 104, 190, 41, 110, 249, 209, 68, 106, 135, 164, 80, 235, 63, 101, 80, 32, 13, 38, 99, 145, 91, 11, 173, 151, 231, 247, 65, 153, 117, 229, 167, 64, 239, 182, 126, 235, 83, 4, 169, 8, 8, 160, 4, 235, 252, 21, 96, 84, 161, 69, 145, 145, 215, 254, 161, 117, 246, 198, 65, 89, 179, 194, 90, 19, 121, 12, 202, 114, 80, 195, 14, 60, 128, 105, 142, 100, 86, 90, 108, 157, 219, 22, 172, 20, 121, 195, 25, 159, 236, 2, 70, 75, 42, 37, 34, 2, 17, 149, 20, 176, 32, 18, 204, 56, 117, 121, 34, 15, 3, 88, 123, 64, 68, 74, 233, 63, 59, 131, 222, 194, 192, 167, 110, 217, 10, 128, 73, 129, 172, 205, 103, 212, 60, 151, 141, 10, 151, 222, 151, 180, 43, 91, 148, 201, 110, 165, 10, 238, 32, 134, 235, 99, 216, 200, 182, 31, 22, 156, 18, 209, 222, 172, 239, 193, 212, 86, 99, 62, 70, 182, 45, 175, 241, 91, 202, 179, 225, 236, 95, 71, 66, 151, 225, 203, 53, 216, 85, 102, 130, 6, 8, 25, 180, 86, 58, 140, 198, 105, 102, 177, 42, 94, 115, 247, 145, 147, 24, 231, 39, 73, 27, 10, 219, 130, 115, 188, 74, 114, 5, 177, 199, 83, 183, 106, 87, 204, 238, 231, 72, 45, 240, 39, 174, 25, 98, 53, 187, 156, 159, 244, 38
let encrypted_log_from_typescript = [
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, 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, 0, 0, 0, 0, 0, 0, 141, 70, 12, 14, 67, 77, 132, 110, 193, 234, 40, 110, 64, 144, 235, 86, 55, 111, 242, 123, 221, 193, 170, 202, 225, 216, 86, 84, 159, 112, 31, 167, 126, 79, 51, 186, 47, 71, 253, 172, 99, 112, 241, 59, 197, 241, 107, 186, 232, 87, 187, 230, 171, 62, 228, 234, 42, 51, 145, 146, 238, 242, 42, 71, 206, 13, 244, 66, 111, 195, 20, 203, 98, 148, 204, 242, 145, 183, 156, 29, 141, 54, 44, 220, 194, 35, 229, 16, 32, 204, 211, 49, 142, 112, 82, 202, 116, 241, 254, 146, 42, 217, 20, 189, 70, 228, 182, 171, 205, 104, 27, 99, 171, 28, 91, 244, 21, 30, 130, 240, 5, 72, 174, 124, 97, 197, 157, 248, 193, 23, 193, 76, 46, 141, 144, 70, 211, 45, 67, 167, 218, 129, 140, 104, 190, 41, 110, 249, 209, 68, 106, 135, 164, 80, 235, 63, 101, 80, 32, 13, 38, 99, 145, 91, 11, 173, 151, 231, 247, 65, 153, 117, 229, 167, 64, 239, 182, 126, 235, 83, 4, 169, 8, 8, 160, 4, 235, 252, 21, 96, 84, 161, 69, 145, 145, 215, 254, 161, 117, 246, 198, 65, 89, 179, 194, 90, 19, 121, 12, 202, 114, 80, 195, 14, 60, 128, 105, 142, 100, 86, 90, 108, 157, 219, 22, 172, 20, 121, 195, 25, 159, 236, 2, 70, 75, 42, 37, 34, 2, 17, 149, 20, 176, 32, 18, 204, 56, 117, 121, 34, 15, 3, 88, 123, 64, 68, 74, 233, 63, 59, 131, 222, 194, 192, 167, 110, 217, 10, 128, 73, 129, 172, 61, 43, 12, 98, 165, 203, 191, 154, 161, 150, 4, 239, 95, 48, 60, 159, 33, 222, 142, 102, 73, 193, 236, 145, 197, 160, 216, 254, 113, 243, 25, 244, 251, 192, 222, 35, 7, 114, 101, 35, 152, 151, 112, 24, 32, 94, 138, 71, 160, 91, 68, 131, 217, 117, 140, 19, 147, 37, 197, 192, 21, 43, 172, 239, 239, 205, 15, 110, 76, 26, 211, 42, 117, 4, 15, 135, 145, 247, 37, 73, 84, 164, 149, 250, 35, 0, 205, 105, 178, 143, 104, 98, 100, 250, 193, 154, 136, 175, 177, 109, 225, 207, 252, 147, 250, 250, 189, 117, 147, 101, 230, 132
];
assert_eq(encrypted_note_log_from_typescript, log);
assert_eq(encrypted_log_from_typescript, log);
}

#[test]
Expand Down
47 changes: 2 additions & 45 deletions noir-projects/noir-contracts/contracts/test_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ contract Test {
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note;
use dep::aztec::encrypted_logs::encrypted_event_emission::encode_and_encrypt_event_with_randomness_unconstrained;

use dep::aztec::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::Serialize, point::Point, scalar::Scalar};

use dep::aztec::encrypted_logs::header::EncryptedLogHeader;
use dep::aztec::encrypted_logs::payload::{compute_incoming_body_ciphertext, compute_outgoing_body_ciphertext};
use dep::aztec::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::Serialize, point::Point};

use dep::aztec::note::constants::MAX_NOTES_PER_PAGE;
use dep::aztec::keys::getters::get_public_keys;
Expand All @@ -34,9 +31,8 @@ contract Test {
use dep::token_portal_content_hash_lib::{get_mint_private_content_hash, get_mint_public_content_hash};
use dep::value_note::value_note::ValueNote;
// TODO investigate why the macros require EmbeddedCurvePoint and EmbeddedCurveScalar
use std::embedded_curve_ops::{EmbeddedCurveScalar, EmbeddedCurvePoint, fixed_base_scalar_mul as derive_public_key};
use std::embedded_curve_ops::{EmbeddedCurveScalar, EmbeddedCurvePoint};
use std::meta::derive;
use std::aes128::aes128_encrypt;

use crate::test_note::TestNote;

Expand Down Expand Up @@ -400,45 +396,6 @@ contract Test {
assert(context.version() == version, "Invalid version");
}

#[private]
fn encrypt(input: [u8; 64], iv: [u8; 16], key: [u8; 16]) -> [u8; 80] {
aes128_encrypt(input, iv, key).as_array()
}

#[private]
fn encrypt_with_padding(input: [u8; 65], iv: [u8; 16], key: [u8; 16]) -> [u8; 80] {
aes128_encrypt(input, iv, key).as_array()
}

#[private]
fn compute_note_header_ciphertext(secret: Scalar, ivpk: IvpkM) -> [u8; 48] {
EncryptedLogHeader::new(context.this_address()).compute_ciphertext(secret, ivpk)
}

// 64 bytes + 32 * #fields + 16 = 112 bytes
#[private]
fn compute_incoming_log_body_ciphertext(
secret: Scalar,
ivpk: IvpkM,
storage_slot: Field,
value: Field
) -> [u8; 112] {
let note = TestNote::new(value);
let plaintext = note.to_be_bytes(storage_slot);
compute_incoming_body_ciphertext(plaintext, secret, ivpk).as_array()
}

#[private]
fn compute_outgoing_log_body_ciphertext(
eph_sk: Scalar,
recipient: AztecAddress,
recipient_ivpk: IvpkM,
ovsk_app: Scalar
) -> [u8; 144] {
let eph_pk = derive_public_key(eph_sk);
compute_outgoing_body_ciphertext(recipient, recipient_ivpk, ovsk_app, eph_sk, eph_pk)
}

#[public]
fn assert_public_global_vars(
chain_id: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use dep::aztec::macros::aztec;
contract TestLog {
use std::meta::derive;
use dep::aztec::prelude::PrivateSet;
use dep::aztec::protocol_types::{scalar::Scalar, address::AztecAddress, traits::Serialize};
use dep::aztec::keys::{getters::get_public_keys, public_keys::IvpkM};
use dep::aztec::protocol_types::{address::AztecAddress, traits::Serialize};
use dep::aztec::keys::getters::get_public_keys;
use dep::value_note::value_note::ValueNote;
use dep::aztec::encrypted_logs::payload::compute_incoming_body_ciphertext;
use dep::aztec::encrypted_logs::encrypted_event_emission::encode_and_encrypt_event_with_randomness;
use dep::aztec::unencrypted_logs::unencrypted_event_emission::encode_event;
use dep::aztec::macros::{storage::storage, events::event, functions::{private, public}};
Expand Down Expand Up @@ -36,19 +35,6 @@ contract TestLog {
// EXAMPLE_EVENT_0_BYTES_LEN + 16
global EXAMPLE_EVENT_0_CIPHERTEXT_BYTES_LEN = 144;

#[private]
fn compute_incoming_log_body_ciphertext(
secret: Scalar,
ivpk: IvpkM,
randomness: Field,
event_type_id: Field,
preimage: [Field; 2]
) -> [u8; EXAMPLE_EVENT_0_CIPHERTEXT_BYTES_LEN] {
let event = ExampleEvent0 { value0: preimage[0], value1: preimage[1] };
let plaintext = event.private_to_be_bytes(randomness);
compute_incoming_body_ciphertext(plaintext, secret, ivpk).as_array()
}

#[private]
fn emit_encrypted_events(other: AztecAddress, randomness: [Field; 2], preimages: [Field; 4]) {
let event0 = ExampleEvent0 { value0: preimages[0], value1: preimages[1] };
Expand Down
5 changes: 1 addition & 4 deletions yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ export {
Comparator,
CompleteAddress,
EncryptedL2BlockL2Logs,
EncryptedLogHeader,
EncryptedNoteLogIncomingBody,
EncryptedLogOutgoingBody,
EventType,
ExtendedNote,
UniqueNote,
Expand Down Expand Up @@ -134,7 +131,7 @@ export {
merkleTreeIds,
mockTx,
mockEpochProofQuote,
TaggedLog,
EncryptedLogPayload,
L1NotePayload,
L1EventPayload,
EpochProofQuote,
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 048a848

Please sign in to comment.