Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 23, 2024
1 parent e42e219 commit 41bb3f1
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use crate::{
};

use dep::std::embedded_curve_ops::EmbeddedCurvePoint as Point;
use dep::std::embedded_curve_ops::fixed_base_scalar_mul as derive_public_key;
use std::embedded_curve_ops::EmbeddedCurveScalar;
use std::default::Default;

pub global PUBLIC_KEYS_LENGTH: u32 = 12;

Expand Down Expand Up @@ -107,6 +110,49 @@ impl Empty for PublicKeys {
}
}

impl Default for PublicKeys {
// This information is duplicated in circuits.js/src/types/public_keys.ts
// We use this because empty will produce a point not on the curve.
// This is:
// "az_null_npk"
// "az_null_ivpk"
// "az_null_ovpk"
// "az_null_tpk"
// as bytes, hashed to curve using grumpkin::g1::affine_element::hash_to_curve(<X>, 0);
fn default() -> Self {
PublicKeys {
npk_m: NpkM {
inner: Point {
x: 0x01498945581e0eb9f8427ad6021184c700ef091d570892c437d12c7d90364bbd,
y: 0x170ae506787c5c43d6ca9255d571c10fa9ffa9d141666e290c347c5c9ab7e344,
is_infinite: false
}
},
ivpk_m: IvpkM {
inner: Point {
x: 0x00c044b05b6ca83b9c2dbae79cc1135155956a64e136819136e9947fe5e5866c,
y: 0x1c1f0ca244c7cd46b682552bff8ae77dea40b966a71de076ec3b7678f2bdb151,
is_infinite: false
}
},
ovpk_m: OvpkM {
inner: Point {
x: 0x1b00316144359e9a3ec8e49c1cdb7eeb0cedd190dfd9dc90eea5115aa779e287,
y: 0x080ffc74d7a8b0bccb88ac11f45874172f3847eb8b92654aaa58a3d2b8dc7833,
is_infinite: false
}
},
tpk_m: TpkM {
inner: Point {
x: 0x019c111f36ad3fc1d9b7a7a14344314d2864b94f030594cd67f753ef774a1efb,
y: 0x2039907fe37f08d10739255141bb066c506a12f7d1e8dfec21abc58494705b6f,
is_infinite: false
}
}
}
}
}

impl Eq for PublicKeys {
fn eq(self, other: PublicKeys) -> bool {
(self.npk_m.inner == other.npk_m.inner)
Expand Down Expand Up @@ -174,13 +220,13 @@ unconstrained fn compute_public_keys_hash() {
}

#[test]
unconstrained fn compute_empty_hash() {
let keys = PublicKeys::empty();
unconstrained fn compute_default_hash() {
let keys = PublicKeys::default();

let actual = keys.hash();
let test_data_empty_hash = 0x0000000000000000000000000000000000000000000000000000000000000000;
let test_data_default_hash = 0x2ed36bfe5497fb6aa79ff85ed6657da92c17d50ac1ab5960e79a78c33687156f;

assert(actual.to_field() == test_data_empty_hash);
assert(actual.to_field() == test_data_default_hash);
}

#[test]
Expand All @@ -204,3 +250,25 @@ unconstrained fn test_public_keys_serialization() {
assert_eq(keys.tpk_m.inner.x, deserialized.tpk_m.inner.x);
assert_eq(keys.tpk_m.inner.y, deserialized.tpk_m.inner.y);
}

#[test]
unconstrained fn hash_to_cdurve() {
let keys = PublicKeys {
npk_m: NpkM { inner: Point { x: 1, y: 2, is_infinite: false } },
ivpk_m: IvpkM { inner: Point { x: 3, y: 4, is_infinite: false } },
ovpk_m: OvpkM { inner: Point { x: 5, y: 6, is_infinite: false } },
tpk_m: TpkM { inner: Point { x: 7, y: 8, is_infinite: false } }
};

let serialized = keys.serialize();
let deserialized = PublicKeys::deserialize(serialized);

assert_eq(keys.npk_m.inner.x, deserialized.npk_m.inner.x);
assert_eq(keys.npk_m.inner.y, deserialized.npk_m.inner.y);
assert_eq(keys.ivpk_m.inner.x, deserialized.ivpk_m.inner.x);
assert_eq(keys.ivpk_m.inner.y, deserialized.ivpk_m.inner.y);
assert_eq(keys.ovpk_m.inner.x, deserialized.ovpk_m.inner.x);
assert_eq(keys.ovpk_m.inner.y, deserialized.ovpk_m.inner.y);
assert_eq(keys.tpk_m.inner.x, deserialized.tpk_m.inner.x);
assert_eq(keys.tpk_m.inner.y, deserialized.tpk_m.inner.y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ impl Empty for FixtureBuilder {
returns_hash: 0,
function_leaf_membership_witness: MembershipWitness::empty(),
salted_initialization_hash: SaltedInitializationHash::from_field(0),
public_keys: PublicKeys::empty(),
public_keys: PublicKeys::default(),
contract_class_artifact_hash: 0,
contract_class_public_bytecode_commitment: 0,
acir_hash: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ global default_contract = ContractData {
address: AztecAddress { inner: 0x0e66d7cd9692428c550b93c9ef5f49ca9f02c03e98cb3c922d8c773f78f79fed },
partial_address: PartialAddress { inner: 0x0cf203c94c91bed28440b00ecd888d88cce1f86ddf2aa8d33acbb9b6fc06d382 },
contract_class_id: ContractClassId { inner: 0x28e91aaf764bc6083e2796ff884079ad895d4b948d6ce8f37f01b29d0bc95a21 },
public_keys: PublicKeys::empty(),
public_keys: PublicKeys::default(),
salted_initialization_hash: SaltedInitializationHash { inner: 0x13a939daa511233e5446905ed2cadbee14948fa75df183b53b5c14b612bffe88 },
deployer: AztecAddress { inner: 0x0000000000000000000000000000000000000000000000000000000000000000 }
};
Expand All @@ -40,7 +40,7 @@ global parent_contract = ContractData {
address: AztecAddress { inner: 0x24415b2e716d6c7099580ab8e383fd5b16dc9fb441aa308571d8e24a2257da24 },
partial_address: PartialAddress { inner: 0x245df9f519d616473880260dd64b19a838081bb44dc17cd6ea5d870a63d2bf57 },
contract_class_id: ContractClassId { inner: 0x00236b0dc6c537d5106543053c5b85c4cbe95b0474f8238b094bae63f1cbcfee },
public_keys: PublicKeys::empty(),
public_keys: PublicKeys::default(),
salted_initialization_hash: SaltedInitializationHash { inner: 0x24bd6ac7a182e2cf25e437c72f53544ef81dfd97d9afee23abb07a638e7be749 },
deployer: AztecAddress { inner: 0x0000000000000000000000000000000000000000000000000000000000000000 }
};
Expand All @@ -50,7 +50,8 @@ pub fn get_protocol_contract(index: u32) -> ContractData {
let artifact_hash = 576576 + seed;
let salted_initialization_hash = SaltedInitializationHash { inner: 281972 + seed };
let public_bytecode_commitment = 38383 + seed;
let public_keys = PublicKeys::empty();
// Empty public keys here will throw an error when doing ec ops
let public_keys = PublicKeys::default();

let function = get_protocol_contract_function(index);
let private_functions_root = private_functions_root_from_siblings(
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Contract extends ContractBase {
*/
public static deploy(wallet: Wallet, artifact: ContractArtifact, args: any[], constructorName?: string) {
const postDeployCtor = (address: AztecAddress, wallet: Wallet) => Contract.at(address, artifact, wallet);
return new DeployMethod(PublicKeys.empty(), wallet, artifact, postDeployCtor, args, constructorName);
return new DeployMethod(PublicKeys.default(), wallet, artifact, postDeployCtor, args, constructorName);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/deployment/contract_deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ContractDeployer {
public deploy(...args: any[]) {
const postDeployCtor = (address: AztecAddress, wallet: Wallet) => Contract.at(address, this.artifact, wallet);
return new DeployMethod(
this.publicKeys ?? PublicKeys.empty(),
this.publicKeys ?? PublicKeys.default(),
this.wallet,
this.artifact,
postDeployCtor,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec/src/cli/cmds/start_pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export async function addPXE(
address,
deployer: AztecAddress.ZERO,
contractClassId: getContractClassFromArtifact(artifact!).id,
publicKeys: PublicKeys.empty(),
publicKeys: PublicKeys.default(),
};
userLog(`Registering ${name} at ${address.toString()}`);
await pxe.registerContract({ artifact, instance });
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/builder/src/contract-interface-gen/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function generateDeploy(input: ContractArtifact) {
* Creates a tx to deploy a new instance of this contract.
*/
public static deploy(wallet: Wallet, ${args}) {
return new DeployMethod<${contractName}>(PublicKeys.empty(), wallet, ${artifactName}, ${contractName}.at, Array.from(arguments).slice(1));
return new DeployMethod<${contractName}>(PublicKeys.default(), wallet, ${artifactName}, ${contractName}.at, Array.from(arguments).slice(1));
}
/**
Expand All @@ -102,7 +102,7 @@ function generateDeploy(input: ContractArtifact) {
...args: Parameters<${contractName}['methods'][M]>
) {
return new DeployMethod<${contractName}>(
opts.publicKeys ?? PublicKeys.empty(),
opts.publicKeys ?? PublicKeys.default(),
opts.wallet,
${artifactName},
${contractName}.at,
Expand Down
7 changes: 4 additions & 3 deletions yarn-project/circuits.js/src/contract/contract_instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getDefaultInitializer,
} from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';
import { Fr, Point } from '@aztec/foundation/fields';
import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
import { type FieldsOf } from '@aztec/foundation/types';

Expand Down Expand Up @@ -80,14 +80,15 @@ export class SerializableContractInstance {
});
}

// This is only used for test purposes. PublicKeys like below will fail in normal use due to the points not being on the curve during ec ops.
static empty() {
return new SerializableContractInstance({
version: VERSION,
salt: Fr.zero(),
deployer: AztecAddress.zero(),
contractClassId: Fr.zero(),
initializationHash: Fr.zero(),
publicKeys: PublicKeys.empty(),
publicKeys: new PublicKeys(Point.ZERO, Point.ZERO, Point.ZERO, Point.ZERO),
});
}
}
Expand Down Expand Up @@ -122,7 +123,7 @@ export function getContractInstanceFromDeployParams(
args,
)
: computeInitializationHash(constructorArtifact, args);
const publicKeys = opts.publicKeys ?? PublicKeys.empty();
const publicKeys = opts.publicKeys ?? PublicKeys.default();

const instance: ContractInstance = {
contractClassId,
Expand Down
18 changes: 13 additions & 5 deletions yarn-project/circuits.js/src/types/public_keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ describe('PublicKeys', () => {
expect(hash).toMatchInlineSnapshot(`"0x0fecd9a32db731fec1fded1b9ff957a1625c069245a3613a2538bd527068b0ad"`);

// Run with AZTEC_GENERATE_TEST_DATA=1 to update noir test data
updateInlineTestData('noir-projects/aztec-nr/aztec/src/keys/public_keys.nr', 'expected_public_keys_hash', hash);
updateInlineTestData(
'noir-projects/noir-protocol-circuits/crates/types/src/public_keys.nr',
'expected_public_keys_hash',
hash,
);
});

it('computes empty keys hash', () => {
const keys = PublicKeys.empty();
it('computes default keys hash', () => {
const keys = PublicKeys.default();

const hash = keys.hash().toString();
expect(hash).toMatchInlineSnapshot(`"0x0000000000000000000000000000000000000000000000000000000000000000"`);
expect(hash).toMatchInlineSnapshot(`"0x2ed36bfe5497fb6aa79ff85ed6657da92c17d50ac1ab5960e79a78c33687156f"`);

// Run with AZTEC_GENERATE_TEST_DATA=1 to update noir test data
updateInlineTestData('noir-projects/aztec-nr/aztec/src/keys/public_keys.nr', 'test_data_empty_hash', hash);
updateInlineTestData(
'noir-projects/noir-protocol-circuits/crates/types/src/public_keys.nr',
'test_data_default_hash',
hash,
);
});
});
36 changes: 33 additions & 3 deletions yarn-project/circuits.js/src/types/public_keys.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { poseidon2HashWithSeparator } from '@aztec/foundation/crypto';
import { Fr, Point } from '@aztec/foundation/fields';
import { Fq, Fr, Point } from '@aztec/foundation/fields';
import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { GeneratorIndex } from '../constants.gen.js';
import { derivePublicKeyFromSecretKey } from '../keys/derivation.js';
import { type PublicKey } from './public_key.js';

export class PublicKeys {
Expand Down Expand Up @@ -41,8 +42,37 @@ export class PublicKeys {
);
}

static empty(): PublicKeys {
return new PublicKeys(Point.ZERO, Point.ZERO, Point.ZERO, Point.ZERO);
static default(): PublicKeys {
// This information is duplicated in noir-protocol-circuits/crates/types/src/public_keys.nr
// We use this because empty will produce a point not on the curve.
// This is:
// "az_null_npk"
// "az_null_ivpk"
// "az_null_ovpk"
// "az_null_tpk"
// as bytes, hashed to curve using grumpkin::g1::affine_element::hash_to_curve(<X>, 0);
return new PublicKeys(
new Point(
new Fr(0x01498945581e0eb9f8427ad6021184c700ef091d570892c437d12c7d90364bbdn),
new Fr(0x170ae506787c5c43d6ca9255d571c10fa9ffa9d141666e290c347c5c9ab7e344n),
false,
),
new Point(
new Fr(0x00c044b05b6ca83b9c2dbae79cc1135155956a64e136819136e9947fe5e5866cn),
new Fr(0x1c1f0ca244c7cd46b682552bff8ae77dea40b966a71de076ec3b7678f2bdb151n),
false,
),
new Point(
new Fr(0x1b00316144359e9a3ec8e49c1cdb7eeb0cedd190dfd9dc90eea5115aa779e287n),
new Fr(0x080ffc74d7a8b0bccb88ac11f45874172f3847eb8b92654aaa58a3d2b8dc7833n),
false,
),
new Point(
new Fr(0x019c111f36ad3fc1d9b7a7a14344314d2864b94f030594cd67f753ef774a1efbn),
new Fr(0x2039907fe37f08d10739255141bb066c506a12f7d1e8dfec21abc58494705b6fn),
false,
),
);
}

static random(): PublicKeys {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/cli-wallet/src/cmds/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function deploy(
);
}

const deployer = new ContractDeployer(contractArtifact, wallet, publicKeys ?? PublicKeys.empty(), initializer);
const deployer = new ContractDeployer(contractArtifact, wallet, publicKeys ?? PublicKeys.default(), initializer);

let args = [];
if (rawArgs.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/cli/src/cmds/pxe/add_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function addContract(
salt,
initializationHash,
contractClassId: getContractClassFromArtifact(artifact).id,
publicKeys: publicKeys ?? PublicKeys.empty(),
publicKeys: publicKeys ?? PublicKeys.default(),
address,
deployer: deployer ?? AztecAddress.ZERO,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ exports[`Data generation for noir tests Computes contract info for defaultContra
artifact_hash: 0x0000000000000000000000000000000000000000000000000000000000003039,
public_bytecode_commitment: 0x0000000000000000000000000000000000000000000000000000000000000005,
private_functions_root: 0x25d76df45434ec75a83321daf941cfc667ff3a9027942e17105da4f50d1d13f9,
address: AztecAddress { inner: 0x1119ce64278d82d5178d977b0921630b2834045c8dc4bec257813bcbafdddb57 },
address: AztecAddress { inner: 0x2ae2dcc65d0b82cc94f1890b16ff60fb7c9f0833b776fc7a519ce6bda2a64893 },
partial_address: PartialAddress { inner: 0x0cf203c94c91bed28440b00ecd888d88cce1f86ddf2aa8d33acbb9b6fc06d382 },
contract_class_id: ContractClassId { inner: 0x28e91aaf764bc6083e2796ff884079ad895d4b948d6ce8f37f01b29d0bc95a21 },
public_keys: PublicKeys { inner: 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 },
public_keys: PublicKeys { inner: 00000000000000000000000000000000000000000000000000000000000000010000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c06ce1b0827aafa85ddeb49cdaa36306d19a74caa311e13d46d8bc688cdbffffe1c122f81a3a14964909ede0ba2a6855fc93faf6fa1a788bf467be7e7a43f80ac2941b0928df1b9480273773b36397da3e495430a2a7a3857661bc7a446c94f4d13ae7e938c892308bef0f45ee7386daa2d3b447349a7d0a11b5aa4cfbe69072c01b06105d7dc31e315550bc6bf0e4e5e7148034f7b957a514537d8c21b2db26a02c095b9dcadf22a6f64bbcb78a223b6e04014a8c8697eccbcf6b35d44452003 },
salted_initialization_hash: SaltedInitializationHash { inner: 0x13a939daa511233e5446905ed2cadbee14948fa75df183b53b5c14b612bffe88 },
deployer: AztecAddress { inner: 0x0000000000000000000000000000000000000000000000000000000000000000 }
}"
Expand All @@ -21,10 +21,10 @@ exports[`Data generation for noir tests Computes contract info for parentContrac
artifact_hash: 0x00000000000000000000000000000000000000000000000000000000000004bc,
public_bytecode_commitment: 0x0000000000000000000000000000000000000000000000000000000000000005,
private_functions_root: 0x1228b39ba6702af03e595300e8484c6373f00790d0148cc3d4ff0fd1c778a83a,
address: AztecAddress { inner: 0x218802a34637b05632108fedc42176dfce00e4daa8aa9aeadbf09f8c7069267a },
address: AztecAddress { inner: 0x190db417e88cd709dab4b82dbbe4e696d939dcd48c3dead80498ed34e6f96d98 },
partial_address: PartialAddress { inner: 0x245df9f519d616473880260dd64b19a838081bb44dc17cd6ea5d870a63d2bf57 },
contract_class_id: ContractClassId { inner: 0x00236b0dc6c537d5106543053c5b85c4cbe95b0474f8238b094bae63f1cbcfee },
public_keys: PublicKeys { inner: 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 },
public_keys: PublicKeys { inner: 00000000000000000000000000000000000000000000000000000000000000010000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c06ce1b0827aafa85ddeb49cdaa36306d19a74caa311e13d46d8bc688cdbffffe1c122f81a3a14964909ede0ba2a6855fc93faf6fa1a788bf467be7e7a43f80ac2941b0928df1b9480273773b36397da3e495430a2a7a3857661bc7a446c94f4d13ae7e938c892308bef0f45ee7386daa2d3b447349a7d0a11b5aa4cfbe69072c01b06105d7dc31e315550bc6bf0e4e5e7148034f7b957a514537d8c21b2db26a02c095b9dcadf22a6f64bbcb78a223b6e04014a8c8697eccbcf6b35d44452003 },
salted_initialization_hash: SaltedInitializationHash { inner: 0x24bd6ac7a182e2cf25e437c72f53544ef81dfd97d9afee23abb07a638e7be749 },
deployer: AztecAddress { inner: 0x0000000000000000000000000000000000000000000000000000000000000000 }
}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Data generation for noir tests', () => {
const defaultContract: FixtureContractData = {
artifactHash: new Fr(12345),
packedBytecode: Buffer.from([3, 4, 5, 6, 7]),
publicKeys: PublicKeys.empty(),
publicKeys: PublicKeys.default(),
salt: new Fr(56789),
privateFunctions: [
{ selector: FunctionSelector.fromField(new Fr(1010101)), vkHash: new Fr(0) },
Expand All @@ -37,7 +37,7 @@ describe('Data generation for noir tests', () => {
const parentContract: FixtureContractData = {
artifactHash: new Fr(1212),
packedBytecode: Buffer.from([3, 4, 3, 4]),
publicKeys: PublicKeys.empty(),
publicKeys: PublicKeys.default(),
salt: new Fr(5656),
privateFunctions: [{ selector: FunctionSelector.fromField(new Fr(334455)), vkHash: new Fr(0) }],
toString: () => 'parentContract',
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/pxe/src/kernel_prover/kernel_prover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('Kernel Prover', () => {

oracle.getContractAddressPreimage.mockResolvedValue({
contractClassId: Fr.random(),
publicKeys: PublicKeys.empty(),
publicKeys: PublicKeys.random(),
saltedInitializationHash: Fr.random(),
});
oracle.getContractClassIdPreimage.mockResolvedValue({
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/txe/src/txe_service/txe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class TXEService {
skipArgsDecoding: true,
salt: Fr.ONE,
// TODO: Modify this to allow for passing public keys.
publicKeys: PublicKeys.empty(),
publicKeys: PublicKeys.default(),
constructorArtifact: initializerStr ? initializerStr : undefined,
deployer: AztecAddress.ZERO,
});
Expand Down

0 comments on commit 41bb3f1

Please sign in to comment.