Skip to content

Commit

Permalink
addressing feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 24, 2024
1 parent c8a029c commit 9585935
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 79 deletions.
45 changes: 36 additions & 9 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ pub fn compute_encrypted_log<let P: u32, let M: u32>(

let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, recipient);
let outgoing_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ovpk);
let incoming_body_ciphertext = compute_incoming_body_ciphertext(plaintext, eph_sk, IvpkM { inner: recipient.to_point() });
let incoming_body_ciphertext =
compute_incoming_body_ciphertext(plaintext, eph_sk, IvpkM { inner: recipient.to_point() });
let outgoing_body_ciphertext: [u8; 144] = compute_outgoing_body_ciphertext(
recipient,
IvpkM { inner: recipient.to_point() },
fr_to_fq(ovsk_app),
eph_sk,
eph_pk
eph_pk,
);

let mut encrypted_bytes: [u8; M] = [0; M];
Expand Down Expand Up @@ -166,22 +167,24 @@ mod test {
#[test]
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 contract_address = AztecAddress::from_field(
0x10f48cd9eff7ae5b209c557c70de2e657ee79166868676b787e9417e19260e04,
);
let ovsk_app = 0x191ac5e29bbc8f80f29ed06b75eaf30c036ed7952d844833860c527077c8c3b4;
let ovpk_m = OvpkM {
inner: Point {
x: 0x07f696b8b233de2c1935e43c793399586f532da5ff7c0356636a75acb862e964,
y: 0x156e8a3e42bfca3663936ba98c7fd26386a14657c23b5f5146f1a94b6c465154,
is_infinite: false
}
is_infinite: false,
},
};

let ivpk_m = IvpkM {
inner: Point {
x: 0x18dd22d6a4032eefe3a7a55703f583396596235f7c186e450c92981186ee7404,
y: 0x2e49e00996565114016a1a478309842ecbaf930fb716c3f498e7e10370631d75,
is_infinite: false
}
is_infinite: false,
},
};

let plaintext = [
Expand All @@ -196,7 +199,9 @@ mod test {
let eph_sk = 0x1358d15019d4639393d62b97e1588c095957ce74a1c32d6ec7d62fe6705d9538;
let _ = OracleMock::mock("getRandomField").returns(eph_sk);

let recipient = AztecAddress::from_field(0x25afb798ea6d0b8c1618e50fdeafa463059415013d3b7c75d46abf5e242be70c);
let recipient = AztecAddress::from_field(
0x25afb798ea6d0b8c1618e50fdeafa463059415013d3b7c75d46abf5e242be70c,
);

let log: [u8; 448] = compute_encrypted_log(
contract_address,
Expand All @@ -210,7 +215,29 @@ 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_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, 5, 119, 121, 10, 234, 188, 194, 216, 30, 200, 208, 201, 158, 127, 93, 43, 242, 241, 69, 32, 37, 220, 119, 122, 23, 132, 4, 248, 81, 217, 61, 232, 24, 146, 63, 133, 24, 120, 113, 217, 155, 223, 149, 214, 149, 239, 240, 169, 224, 155, 161, 81, 83, 252, 155, 77, 34, 75, 110, 30, 113, 223, 189, 202, 171, 6, 192, 157, 91, 60, 116, 155, 254, 190, 28, 4, 7, 236, 205, 4, 245, 27, 187, 89, 20, 38, 128, 200, 160, 145, 185, 127, 198, 203, 207, 97, 246, 194, 175, 155, 142, 188, 143, 120, 83, 122, 178, 63, 208, 197, 232, 24, 228, 212, 45, 69, 157, 38, 90, 219, 119, 194, 239, 130, 155, 246, 143, 135, 242, 196, 123, 71, 139, 181, 122, 231, 228, 26, 7, 100, 63, 101, 195, 83, 8, 61, 85, 123, 148, 227, 29, 164, 162, 161, 49, 39, 73, 141, 46, 179, 240, 52, 109, 165, 238, 210, 233, 188, 36, 90, 175, 2, 42, 149, 78, 208, 176, 145, 50, 180, 152, 245, 55, 112, 40, 153, 180, 78, 54, 102, 119, 98, 56, 235, 246, 51, 179, 86, 45, 127, 18, 77, 187, 168, 41, 24, 232, 113, 149, 138, 148, 33, 143, 215, 150, 188, 105, 131, 254, 236, 199, 206, 56, 44, 130, 134, 29, 99, 254, 69, 153, 146, 68, 234, 148, 148, 178, 38, 221, 182, 148, 178, 100, 13, 206, 0, 91, 71, 58, 207, 26, 227, 190, 21, 143, 85, 138, 209, 202, 34, 142, 159, 121, 61, 9, 57, 2, 48, 162, 89, 126, 14, 83, 173, 40, 247, 170, 154, 112, 12, 204, 48, 38, 7, 173, 108, 38, 234, 20, 16, 115, 91, 106, 140, 121, 63, 99, 23, 247, 0, 148, 9, 163, 145, 43, 21, 238, 47, 40, 204, 241, 124, 246, 201, 75, 114, 3, 1, 229, 197, 130, 109, 227, 158, 133, 188, 125, 179, 220, 51, 170, 121, 175, 202, 243, 37, 103, 13, 27, 53, 157, 8, 177, 11, 208, 120, 64, 211, 148, 201, 240, 56
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, 5, 119,
121, 10, 234, 188, 194, 216, 30, 200, 208, 201, 158, 127, 93, 43, 242, 241, 69, 32, 37,
220, 119, 122, 23, 132, 4, 248, 81, 217, 61, 232, 24, 146, 63, 133, 24, 120, 113, 217,
155, 223, 149, 214, 149, 239, 240, 169, 224, 155, 161, 81, 83, 252, 155, 77, 34, 75,
110, 30, 113, 223, 189, 202, 171, 6, 192, 157, 91, 60, 116, 155, 254, 190, 28, 4, 7,
236, 205, 4, 245, 27, 187, 89, 20, 38, 128, 200, 160, 145, 185, 127, 198, 203, 207, 97,
246, 194, 175, 155, 142, 188, 143, 120, 83, 122, 178, 63, 208, 197, 232, 24, 228, 212,
45, 69, 157, 38, 90, 219, 119, 194, 239, 130, 155, 246, 143, 135, 242, 196, 123, 71,
139, 181, 122, 231, 228, 26, 7, 100, 63, 101, 195, 83, 8, 61, 85, 123, 148, 227, 29,
164, 162, 161, 49, 39, 73, 141, 46, 179, 240, 52, 109, 165, 238, 210, 233, 188, 36, 90,
175, 2, 42, 149, 78, 208, 176, 145, 50, 180, 152, 245, 55, 112, 40, 153, 180, 78, 54,
102, 119, 98, 56, 235, 246, 51, 179, 86, 45, 127, 18, 77, 187, 168, 41, 24, 232, 113,
149, 138, 148, 33, 143, 215, 150, 188, 105, 131, 254, 236, 199, 206, 56, 44, 130, 134,
29, 99, 254, 69, 153, 146, 68, 234, 148, 148, 178, 38, 221, 182, 148, 178, 100, 13, 206,
0, 91, 71, 58, 207, 26, 227, 190, 21, 143, 85, 138, 209, 202, 34, 142, 159, 121, 61, 9,
57, 2, 48, 162, 89, 126, 14, 83, 173, 40, 247, 170, 154, 112, 12, 204, 48, 38, 7, 173,
108, 38, 234, 20, 16, 115, 91, 106, 140, 121, 63, 99, 23, 247, 0, 148, 9, 163, 145, 43,
21, 238, 47, 40, 204, 241, 124, 246, 201, 75, 114, 3, 1, 229, 197, 130, 109, 227, 158,
133, 188, 125, 179, 220, 51, 170, 121, 175, 202, 243, 37, 103, 13, 27, 53, 157, 8, 177,
11, 208, 120, 64, 211, 148, 201, 240, 56,
];
assert_eq(encrypted_log_from_typescript, log);
}
Expand Down
4 changes: 3 additions & 1 deletion noir-projects/aztec-nr/aztec/src/keys/getters/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub fn get_public_keys(account: AztecAddress) -> PublicKeys {
let (hinted_canonical_public_keys, partial_address) =
unsafe { get_public_keys_and_partial_address(account) };
assert_eq(
account, AztecAddress::compute(hinted_canonical_public_keys, partial_address), "Invalid public keys hint for address"
account,
AztecAddress::compute(hinted_canonical_public_keys, partial_address),
"Invalid public keys hint for address",
);

hinted_canonical_public_keys
Expand Down
4 changes: 3 additions & 1 deletion noir-projects/aztec-nr/aztec/src/keys/getters/test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ unconstrained fn test_get_public_keys_unknown() {
bad_response[i] = original_keys[i];
}

let _ = OracleMock::mock("getPublicKeysAndPartialAddress").returns(bad_response.serialize()).times(1);
let _ = OracleMock::mock("getPublicKeysAndPartialAddress")
.returns(bad_response.serialize())
.times(1);
let _ = get_public_keys(account.address);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::tests::private_call_data_validator_builder::PrivateCallDataValidatorBuilder;
use dep::types::address::AztecAddress;
use std::embedded_curve_ops::{EmbeddedCurvePoint, EmbeddedCurveScalar, fixed_base_scalar_mul as derive_public_key};
use std::embedded_curve_ops::{
EmbeddedCurvePoint, EmbeddedCurveScalar, fixed_base_scalar_mul as derive_public_key,
};

impl PrivateCallDataValidatorBuilder {
pub fn new_with_regular_contract() -> Self {
Expand Down Expand Up @@ -72,7 +74,8 @@ fn validate_contract_address_incorrect_partial_address_preimage_fails() {
fn validate_contract_address_incorrect_address_preimage_fails() {
let mut builder = PrivateCallDataValidatorBuilder::new_with_regular_contract();

builder.private_call.public_keys.ivpk_m.inner = derive_public_key(EmbeddedCurveScalar::from_field(69));
builder.private_call.public_keys.ivpk_m.inner =
derive_public_key(EmbeddedCurveScalar::from_field(69));

builder.validate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ use crate::{
abis::function_selector::FunctionSelector,
public_keys::{ToPoint, PublicKeys, NpkM, IvpkM, OvpkM, TpkM},
address::{
partial_address::PartialAddress, public_keys_hash::PublicKeysHash,
salted_initialization_hash::SaltedInitializationHash
},
partial_address::PartialAddress, public_keys_hash::PublicKeysHash,
salted_initialization_hash::SaltedInitializationHash,
},
constants::{
AZTEC_ADDRESS_LENGTH, FUNCTION_TREE_HEIGHT, GENERATOR_INDEX__PUBLIC_KEYS_HASH,
GENERATOR_INDEX__CONTRACT_ADDRESS_V1
},
contract_class_id::ContractClassId,
AZTEC_ADDRESS_LENGTH, FUNCTION_TREE_HEIGHT, GENERATOR_INDEX__PUBLIC_KEYS_HASH,
GENERATOR_INDEX__CONTRACT_ADDRESS_V1,
}, contract_class_id::ContractClassId,
hash::{poseidon2_hash_with_separator, private_functions_root_from_siblings},
merkle_tree::membership::MembershipWitness,
traits::{Empty, FromField, ToField, Serialize, Deserialize}, utils,
};

global BN254_FR_MODULUS_DIV_2: Field = 10944121435919637611123202872628637544274182200208017171849102093287904247808;
global BN254_FR_MODULUS_DIV_2: Field =
10944121435919637611123202872628637544274182200208017171849102093287904247808;

// We do below because `use crate::point::Point;` does not work
use dep::std::embedded_curve_ops::EmbeddedCurvePoint as Point;

use std::{
ec::{sqrt, pow},
embedded_curve_ops::{fixed_base_scalar_mul as derive_public_key, EmbeddedCurveScalar}
embedded_curve_ops::{fixed_base_scalar_mul as derive_public_key, EmbeddedCurveScalar},
};

// Aztec address
Expand Down Expand Up @@ -77,7 +77,6 @@ impl ToPoint for AztecAddress {
// Do we throw here or can we soft continue and this is fine ? Test this with points not on curve to see what happens.
// let y_is_square = is_square(y_squared);
// assert(y_is_square);

let mut y = sqrt(y_squared);

// If we get a negative y coordinate, we pin it to the positive one by subtracting it from the Field modulus
Expand All @@ -94,12 +93,13 @@ impl AztecAddress {
Self { inner: 0 }
}

pub fn compute_preaddress(pub_keys_hash: PublicKeysHash, partial_address: PartialAddress) -> AztecAddress {
AztecAddress::from_field(
poseidon2_hash_with_separator(
[pub_keys_hash.to_field(), partial_address.to_field()],
GENERATOR_INDEX__CONTRACT_ADDRESS_V1
)
pub fn compute_preaddress(
pub_keys_hash: PublicKeysHash,
partial_address: PartialAddress,
) -> Field {
poseidon2_hash_with_separator(
[pub_keys_hash.to_field(), partial_address.to_field()],
GENERATOR_INDEX__CONTRACT_ADDRESS_V1,
)
}

Expand All @@ -108,10 +108,12 @@ impl AztecAddress {

let pre_address = poseidon2_hash_with_separator(
[public_keys_hash.to_field(), partial_address.to_field()],
GENERATOR_INDEX__CONTRACT_ADDRESS_V1
GENERATOR_INDEX__CONTRACT_ADDRESS_V1,
);

let address_point = derive_public_key(EmbeddedCurveScalar::from_field(pre_address)).add(public_keys.ivpk_m.to_point());
let address_point = derive_public_key(EmbeddedCurveScalar::from_field(pre_address)).add(
public_keys.ivpk_m.to_point(),
);
AztecAddress::from_field(address_point.x)
}

Expand All @@ -122,7 +124,7 @@ impl AztecAddress {
contract_class_artifact_hash: Field,
contract_class_public_bytecode_commitment: Field,
salted_initialization_hash: SaltedInitializationHash,
public_keys: PublicKeys
public_keys: PublicKeys,
) -> Self {
let private_functions_root = private_functions_root_from_siblings(
function_selector,
Expand Down Expand Up @@ -167,36 +169,39 @@ fn compute_address_from_partial_and_pub_keys() {
inner: Point {
x: 0x22f7fcddfa3ce3e8f0cc8e82d7b94cdd740afa3e77f8e4a63ea78a239432dcab,
y: 0x0471657de2b6216ade6c506d28fbc22ba8b8ed95c871ad9f3e3984e90d9723a7,
is_infinite: false
}
is_infinite: false,
},
},
ivpk_m: IvpkM {
inner: Point {
x: 0x111223493147f6785514b1c195bb37a2589f22a6596d30bb2bb145fdc9ca8f1e,
y: 0x273bbffd678edce8fe30e0deafc4f66d58357c06fd4a820285294b9746c3be95,
is_infinite: false
}
is_infinite: false,
},
},
ovpk_m: OvpkM {
inner: Point {
x: 0x09115c96e962322ffed6522f57194627136b8d03ac7469109707f5e44190c484,
y: 0x0c49773308a13d740a7f0d4f0e6163b02c5a408b6f965856b6a491002d073d5b,
is_infinite: false
}
is_infinite: false,
},
},
tpk_m: TpkM {
inner: Point {
x: 0x00d3d81beb009873eb7116327cf47c612d5758ef083d4fda78e9b63980b2a762,
y: 0x2f567d22d2b02fe1f4ad42db9d58a36afd1983e7e2909d1cab61cafedad6193a,
is_infinite: false
}
}
is_infinite: false,
},
},
};

let partial_address = PartialAddress::from_field(0x0a7c585381b10f4666044266a02405bf6e01fa564c8517d4ad5823493abd31de);
let partial_address = PartialAddress::from_field(
0x0a7c585381b10f4666044266a02405bf6e01fa564c8517d4ad5823493abd31de,
);

let address = AztecAddress::compute(public_keys, partial_address);
let expected_computed_address_from_partial_and_pubkey = 0x24e4646f58b9fbe7d38e317db8d5636c423fbbdfbe119fc190fe9c64747e0c62;
let expected_computed_address_from_partial_and_pubkey =
0x24e4646f58b9fbe7d38e317db8d5636c423fbbdfbe119fc190fe9c64747e0c62;
assert(address.to_field() == expected_computed_address_from_partial_and_pubkey);
}

Expand All @@ -205,9 +210,10 @@ fn compute_preaddress_from_partial_and_pub_keys_hash() {
let pub_keys_hash = PublicKeysHash::from_field(1);
let partial_address = PartialAddress::from_field(2);

let address = AztecAddress::compute_preaddress(pub_keys_hash, partial_address);
let expected_computed_preaddress_from_partial_and_pubkey = 0x23ce9be3fa3c846b0f9245cc796902e731d04f086e8a42473bb29e405fc98075;
assert(address.to_field() == expected_computed_preaddress_from_partial_and_pubkey);
let preaddress = AztecAddress::compute_preaddress(pub_keys_hash, partial_address);
let expected_computed_preaddress_from_partial_and_pubkey =
0x23ce9be3fa3c846b0f9245cc796902e731d04f086e8a42473bb29e405fc98075;
assert(preaddress == expected_computed_preaddress_from_partial_and_pubkey);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
derivePublicKeyFromSecretKey,
} from '@aztec/circuits.js';
import { randomBytes } from '@aztec/foundation/crypto';
import { Fr, GrumpkinScalar, Point } from '@aztec/foundation/fields';
import { Fr, GrumpkinScalar } from '@aztec/foundation/fields';
import { updateInlineTestData } from '@aztec/foundation/testing';

import { EncryptedLogPayload } from './encrypted_log_payload.js';
Expand Down Expand Up @@ -41,12 +41,7 @@ describe('EncryptedLogPayload', () => {

const ephSk = GrumpkinScalar.random();

encrypted = original.encrypt(
ephSk,
completeAddress.address,
computePoint(completeAddress.address),
ovKeys,
);
encrypted = original.encrypt(ephSk, completeAddress.address, computePoint(completeAddress.address), ovKeys);
});

it('decrypt a log as incoming', () => {
Expand Down Expand Up @@ -83,12 +78,7 @@ describe('EncryptedLogPayload', () => {
);

const encrypted = log
.encrypt(
ephSk,
recipientCompleteAddress.address,
computePoint(recipientCompleteAddress.address),
ovKeys,
)
.encrypt(ephSk, recipientCompleteAddress.address, computePoint(recipientCompleteAddress.address), ovKeys)
.toString('hex');
expect(encrypted).toMatchInlineSnapshot(
`"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008d460c0e434d846ec1ea286e4090eb56376ff27bddc1aacae1d856549f701fa70577790aeabcc2d81ec8d0c99e7f5d2bf2f1452025dc777a178404f851d93de818923f85187871d99bdf95d695eff0a9e09ba15153fc9b4d224b6e1e71dfbdcaab06c09d5b3c749bfebe1c0407eccd04f51bbb59142680c8a091b97fc6cbcf61f6c2af9b8ebc8f78537ab23fd0c5e818e4d42d459d265adb77c2ef829bf68f87f2c47b478bb57ae7e41a07643f65c353083d557b94e31da4a2a13127498d2eb3f0346da5eed2e9bc245aaf022a954ed0b09132b498f537702899b44e3666776238ebf633b3562d7f124dbba82918e871958a94218fd796bc6983feecc7ce382c82861d63fe45999244ea9494b226ddb694b2640dce005b473acf1ae3be158f558ad1ca228e9f793d09390230a2597e0e53ad28f7aa9a700ccc302607ad6c26ea1410735b6a8c793f6317f7009409a3912b15ee2f28ccf17cf6c94b720301e5c5826de39e85bc7db3dc33aa79afcaf325670d1b359d08b10bd07840d394c9f038"`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ContractAddress computeContractAddressFromInstance 1`] = `"0x0ea56faa48431d99cc2d073463d1b718c11174bb551a3d1d6f296b0096089dbb"`;
exports[`ContractAddress computeContractAddressFromInstance 1`] = `"0x226f73921336a2893fd3748d317de107ab1cdd8fa7d9ddc235e8a189236cd265"`;

exports[`ContractAddress computeInitializationHash 1`] = `Fr<0x153329c6098512a53cb449057bb5adade93e360575b71192b7b304ace35b2b6b>`;

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/keys/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function computeAddressSecret(preaddress: Fr, ivsk: Fq) {

// If our secret computes a point with a negative y-coordinate, we then negate the secret to produce the secret
// that can decrypt payloads encrypted with the point having a positive y-coordinate.
if (!addressPointCandidate.y.lt(new Fr((Fr.MODULUS - 1n) / 2n))) {
if (!(addressPointCandidate.y.toBigInt() <= (Fr.MODULUS - 1n) / 2n)) {
return new Fq(Fq.MODULUS - addressSecretCandidate.toBigInt());
}

Expand Down
Loading

0 comments on commit 9585935

Please sign in to comment.