@aztec/stdlib 5.0.0-nightly.20260319 → 5.0.0-nightly.20260321
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/abi/abi.d.ts +138 -1
- package/dest/abi/abi.d.ts.map +1 -1
- package/dest/abi/abi.js +10 -2
- package/dest/abi/buffer.d.ts +14 -4
- package/dest/abi/buffer.d.ts.map +1 -1
- package/dest/abi/buffer.js +25 -4
- package/dest/avm/avm.d.ts +300 -300
- package/dest/block/l2_block_source.d.ts +5 -3
- package/dest/block/l2_block_source.d.ts.map +1 -1
- package/dest/contract/index.d.ts +1 -3
- package/dest/contract/index.d.ts.map +1 -1
- package/dest/contract/index.js +0 -2
- package/dest/contract/interfaces/contract_class.d.ts +3 -156
- package/dest/contract/interfaces/contract_class.d.ts.map +1 -1
- package/dest/contract/interfaces/contract_class.js +1 -28
- package/dest/epoch-helpers/index.d.ts +3 -1
- package/dest/epoch-helpers/index.d.ts.map +1 -1
- package/dest/epoch-helpers/index.js +6 -0
- package/dest/interfaces/proving-job.d.ts +34 -34
- package/dest/tests/factories.d.ts +2 -4
- package/dest/tests/factories.d.ts.map +1 -1
- package/dest/tests/factories.js +0 -27
- package/dest/tx/simulated_tx.d.ts +109 -1
- package/dest/tx/simulated_tx.d.ts.map +1 -1
- package/dest/tx/validator/error_texts.d.ts +5 -1
- package/dest/tx/validator/error_texts.d.ts.map +1 -1
- package/dest/tx/validator/error_texts.js +6 -0
- package/package.json +8 -8
- package/src/abi/abi.ts +25 -2
- package/src/abi/buffer.ts +25 -4
- package/src/block/l2_block_source.ts +4 -2
- package/src/contract/index.ts +0 -2
- package/src/contract/interfaces/contract_class.ts +2 -82
- package/src/epoch-helpers/index.ts +11 -0
- package/src/tests/factories.ts +0 -33
- package/src/tx/validator/error_texts.ts +8 -0
- package/dest/contract/private_function_membership_proof.d.ts +0 -32
- package/dest/contract/private_function_membership_proof.d.ts.map +0 -1
- package/dest/contract/private_function_membership_proof.js +0 -124
- package/dest/contract/utility_function_membership_proof.d.ts +0 -27
- package/dest/contract/utility_function_membership_proof.d.ts.map +0 -1
- package/dest/contract/utility_function_membership_proof.js +0 -87
- package/src/contract/private_function_membership_proof.ts +0 -167
- package/src/contract/utility_function_membership_proof.ts +0 -118
|
@@ -175,8 +175,10 @@ export interface L2BlockSource {
|
|
|
175
175
|
getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined>;
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* Returns the last L2 slot number
|
|
179
|
-
*
|
|
178
|
+
* Returns the last L2 slot number for which we have all L1 data needed to build the next checkpoint.
|
|
179
|
+
* Determined by the max of two signals: L1 block sync progress and latest synced checkpoint slot.
|
|
180
|
+
* The checkpoint signal handles missed L1 blocks, since a published checkpoint seals the message tree
|
|
181
|
+
* for the next checkpoint via the inbox LAG mechanism.
|
|
180
182
|
*/
|
|
181
183
|
getSyncedL2SlotNumber(): Promise<SlotNumber | undefined>;
|
|
182
184
|
|
package/src/contract/index.ts
CHANGED
|
@@ -6,8 +6,6 @@ export * from './contract_deployment_data.js';
|
|
|
6
6
|
export * from './contract_instance.js';
|
|
7
7
|
export * from './contract_instance_update.js';
|
|
8
8
|
export * from './private_function.js';
|
|
9
|
-
export * from './private_function_membership_proof.js';
|
|
10
|
-
export * from './utility_function_membership_proof.js';
|
|
11
9
|
export * from './interfaces/index.js';
|
|
12
10
|
export * from './contract_function_dao.js';
|
|
13
11
|
export * from './partial_address.js';
|
|
@@ -42,79 +42,6 @@ const PrivateFunctionSchema = zodFor<PrivateFunction>()(
|
|
|
42
42
|
}),
|
|
43
43
|
);
|
|
44
44
|
|
|
45
|
-
/** Private function definition with executable bytecode. */
|
|
46
|
-
export interface ExecutablePrivateFunction extends PrivateFunction {
|
|
47
|
-
/** ACIR and Brillig bytecode */
|
|
48
|
-
bytecode: Buffer;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const ExecutablePrivateFunctionSchema = zodFor<ExecutablePrivateFunction>()(
|
|
52
|
-
PrivateFunctionSchema.and(z.object({ bytecode: schemas.Buffer })),
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
/** Utility function definition. */
|
|
56
|
-
export interface UtilityFunction {
|
|
57
|
-
/** Selector of the function. Calculated as the hash of the method name and parameters. The specification of this is not enforced by the protocol. */
|
|
58
|
-
selector: FunctionSelector;
|
|
59
|
-
/** Brillig. */
|
|
60
|
-
bytecode: Buffer;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const UtilityFunctionSchema = zodFor<UtilityFunction>()(
|
|
64
|
-
z.object({
|
|
65
|
-
selector: FunctionSelector.schema,
|
|
66
|
-
bytecode: schemas.Buffer,
|
|
67
|
-
}),
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
/** Sibling paths and sibling commitments for proving membership of a private function within a contract class. */
|
|
71
|
-
export type PrivateFunctionMembershipProof = {
|
|
72
|
-
artifactMetadataHash: Fr;
|
|
73
|
-
functionMetadataHash: Fr;
|
|
74
|
-
utilityFunctionsTreeRoot: Fr;
|
|
75
|
-
privateFunctionTreeSiblingPath: Fr[];
|
|
76
|
-
privateFunctionTreeLeafIndex: number;
|
|
77
|
-
artifactTreeSiblingPath: Fr[];
|
|
78
|
-
artifactTreeLeafIndex: number;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const PrivateFunctionMembershipProofSchema = zodFor<PrivateFunctionMembershipProof>()(
|
|
82
|
-
z.object({
|
|
83
|
-
artifactMetadataHash: schemas.Fr,
|
|
84
|
-
functionMetadataHash: schemas.Fr,
|
|
85
|
-
utilityFunctionsTreeRoot: schemas.Fr,
|
|
86
|
-
privateFunctionTreeSiblingPath: z.array(schemas.Fr),
|
|
87
|
-
privateFunctionTreeLeafIndex: schemas.Integer,
|
|
88
|
-
artifactTreeSiblingPath: z.array(schemas.Fr),
|
|
89
|
-
artifactTreeLeafIndex: schemas.Integer,
|
|
90
|
-
}),
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
/** A private function with a membership proof. */
|
|
94
|
-
export type ExecutablePrivateFunctionWithMembershipProof = ExecutablePrivateFunction & PrivateFunctionMembershipProof;
|
|
95
|
-
|
|
96
|
-
/** Sibling paths and commitments for proving membership of a utility function within a contract class. */
|
|
97
|
-
export type UtilityFunctionMembershipProof = {
|
|
98
|
-
artifactMetadataHash: Fr;
|
|
99
|
-
functionMetadataHash: Fr;
|
|
100
|
-
privateFunctionsArtifactTreeRoot: Fr;
|
|
101
|
-
artifactTreeSiblingPath: Fr[];
|
|
102
|
-
artifactTreeLeafIndex: number;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const UtilityFunctionMembershipProofSchema = zodFor<UtilityFunctionMembershipProof>()(
|
|
106
|
-
z.object({
|
|
107
|
-
artifactMetadataHash: schemas.Fr,
|
|
108
|
-
functionMetadataHash: schemas.Fr,
|
|
109
|
-
privateFunctionsArtifactTreeRoot: schemas.Fr,
|
|
110
|
-
artifactTreeSiblingPath: z.array(schemas.Fr),
|
|
111
|
-
artifactTreeLeafIndex: schemas.Integer,
|
|
112
|
-
}),
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
/** A utility function with a membership proof. */
|
|
116
|
-
export type UtilityFunctionWithMembershipProof = UtilityFunction & UtilityFunctionMembershipProof;
|
|
117
|
-
|
|
118
45
|
export const ContractClassSchema = zodFor<ContractClass>()(
|
|
119
46
|
z.object({
|
|
120
47
|
version: z.literal(VERSION),
|
|
@@ -143,11 +70,8 @@ export const ContractClassWithIdSchema = zodFor<ContractClassWithId>()(
|
|
|
143
70
|
}),
|
|
144
71
|
);
|
|
145
72
|
|
|
146
|
-
/** A contract class with public bytecode information
|
|
147
|
-
export type ContractClassPublic =
|
|
148
|
-
privateFunctions: ExecutablePrivateFunctionWithMembershipProof[];
|
|
149
|
-
utilityFunctions: UtilityFunctionWithMembershipProof[];
|
|
150
|
-
} & Pick<ContractClassCommitments, 'id' | 'privateFunctionsRoot'> &
|
|
73
|
+
/** A contract class with public bytecode information. */
|
|
74
|
+
export type ContractClassPublic = Pick<ContractClassCommitments, 'id' | 'privateFunctionsRoot'> &
|
|
151
75
|
Omit<ContractClass, 'privateFunctions'>;
|
|
152
76
|
|
|
153
77
|
export type ContractClassPublicWithCommitment = ContractClassPublic &
|
|
@@ -158,8 +82,6 @@ export const ContractClassPublicSchema = zodFor<ContractClassPublic>()(
|
|
|
158
82
|
.object({
|
|
159
83
|
id: schemas.Fr,
|
|
160
84
|
privateFunctionsRoot: schemas.Fr,
|
|
161
|
-
privateFunctions: z.array(ExecutablePrivateFunctionSchema.and(PrivateFunctionMembershipProofSchema)),
|
|
162
|
-
utilityFunctions: z.array(UtilityFunctionSchema.and(UtilityFunctionMembershipProofSchema)),
|
|
163
85
|
})
|
|
164
86
|
.and(ContractClassSchema.omit({ privateFunctions: true })),
|
|
165
87
|
);
|
|
@@ -179,8 +101,6 @@ export function contractClassPublicFromPlainObject(obj: any): ContractClassPubli
|
|
|
179
101
|
version: 1,
|
|
180
102
|
artifactHash: Fr.fromPlainObject(obj.artifactHash),
|
|
181
103
|
privateFunctionsRoot: Fr.fromPlainObject(obj.privateFunctionsRoot),
|
|
182
|
-
privateFunctions: [],
|
|
183
|
-
utilityFunctions: [],
|
|
184
104
|
packedBytecode: obj.packedBytecode instanceof Buffer ? obj.packedBytecode : Buffer.from(obj.packedBytecode),
|
|
185
105
|
};
|
|
186
106
|
}
|
|
@@ -57,6 +57,17 @@ export function getSlotAtTimestamp(
|
|
|
57
57
|
: SlotNumber.fromBigInt((ts - constants.l1GenesisTime) / BigInt(constants.slotDuration));
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/** Returns the timestamp of the next L1 slot boundary after the given wall-clock time. */
|
|
61
|
+
export function getNextL1SlotTimestamp(
|
|
62
|
+
nowInSeconds: number,
|
|
63
|
+
constants: Pick<L1RollupConstants, 'l1GenesisTime' | 'ethereumSlotDuration'>,
|
|
64
|
+
): bigint {
|
|
65
|
+
const now = BigInt(nowInSeconds);
|
|
66
|
+
const elapsed = now - constants.l1GenesisTime;
|
|
67
|
+
const currentL1Slot = elapsed / BigInt(constants.ethereumSlotDuration);
|
|
68
|
+
return constants.l1GenesisTime + (currentL1Slot + 1n) * BigInt(constants.ethereumSlotDuration);
|
|
69
|
+
}
|
|
70
|
+
|
|
60
71
|
/** Returns the L2 slot number at the next L1 block based on the current timestamp. */
|
|
61
72
|
export function getSlotAtNextL1Block(
|
|
62
73
|
currentL1Timestamp: bigint,
|
package/src/tests/factories.ts
CHANGED
|
@@ -92,10 +92,8 @@ import {
|
|
|
92
92
|
type ContractClassPublic,
|
|
93
93
|
ContractDeploymentData,
|
|
94
94
|
type ContractInstanceWithAddress,
|
|
95
|
-
type ExecutablePrivateFunctionWithMembershipProof,
|
|
96
95
|
type PrivateFunction,
|
|
97
96
|
SerializableContractInstance,
|
|
98
|
-
type UtilityFunctionWithMembershipProof,
|
|
99
97
|
computeContractClassId,
|
|
100
98
|
computePublicBytecodeCommitment,
|
|
101
99
|
} from '../contract/index.js';
|
|
@@ -1183,35 +1181,6 @@ export function makePublicTxBaseRollupPrivateInputs(seed = 0) {
|
|
|
1183
1181
|
});
|
|
1184
1182
|
}
|
|
1185
1183
|
|
|
1186
|
-
export function makeExecutablePrivateFunctionWithMembershipProof(
|
|
1187
|
-
seed = 0,
|
|
1188
|
-
): ExecutablePrivateFunctionWithMembershipProof {
|
|
1189
|
-
return {
|
|
1190
|
-
selector: makeSelector(seed),
|
|
1191
|
-
bytecode: makeBytes(100, seed + 1),
|
|
1192
|
-
artifactTreeSiblingPath: makeTuple(3, fr, seed + 2),
|
|
1193
|
-
artifactTreeLeafIndex: seed + 2,
|
|
1194
|
-
privateFunctionTreeSiblingPath: makeTuple(3, fr, seed + 3),
|
|
1195
|
-
privateFunctionTreeLeafIndex: seed + 3,
|
|
1196
|
-
artifactMetadataHash: fr(seed + 4),
|
|
1197
|
-
functionMetadataHash: fr(seed + 5),
|
|
1198
|
-
utilityFunctionsTreeRoot: fr(seed + 6),
|
|
1199
|
-
vkHash: fr(seed + 7),
|
|
1200
|
-
};
|
|
1201
|
-
}
|
|
1202
|
-
|
|
1203
|
-
export function makeUtilityFunctionWithMembershipProof(seed = 0): UtilityFunctionWithMembershipProof {
|
|
1204
|
-
return {
|
|
1205
|
-
selector: makeSelector(seed),
|
|
1206
|
-
bytecode: makeBytes(100, seed + 1),
|
|
1207
|
-
artifactTreeSiblingPath: makeTuple(3, fr, seed + 2),
|
|
1208
|
-
artifactTreeLeafIndex: seed + 2,
|
|
1209
|
-
artifactMetadataHash: fr(seed + 4),
|
|
1210
|
-
functionMetadataHash: fr(seed + 5),
|
|
1211
|
-
privateFunctionsArtifactTreeRoot: fr(seed + 6),
|
|
1212
|
-
};
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
1184
|
export async function makeContractClassPublic(seed = 0, publicBytecode?: Buffer): Promise<ContractClassPublic> {
|
|
1216
1185
|
const artifactHash = fr(seed + 1);
|
|
1217
1186
|
const privateFunctionsRoot = fr(seed + 3);
|
|
@@ -1223,8 +1192,6 @@ export async function makeContractClassPublic(seed = 0, publicBytecode?: Buffer)
|
|
|
1223
1192
|
artifactHash,
|
|
1224
1193
|
packedBytecode,
|
|
1225
1194
|
privateFunctionsRoot,
|
|
1226
|
-
privateFunctions: [],
|
|
1227
|
-
utilityFunctions: [],
|
|
1228
1195
|
version: 1,
|
|
1229
1196
|
};
|
|
1230
1197
|
}
|
|
@@ -41,5 +41,13 @@ export const TX_ERROR_SIZE_ABOVE_LIMIT = 'Transaction size above size limit';
|
|
|
41
41
|
// Block header
|
|
42
42
|
export const TX_ERROR_BLOCK_HEADER = 'Block header not found';
|
|
43
43
|
|
|
44
|
+
// Contract instance
|
|
45
|
+
export const TX_ERROR_INCORRECT_CONTRACT_ADDRESS = 'Incorrect contract instance deployment address';
|
|
46
|
+
export const TX_ERROR_MALFORMED_CONTRACT_INSTANCE_LOG = 'Failed to parse contract instance deployment log';
|
|
47
|
+
|
|
48
|
+
// Contract class
|
|
49
|
+
export const TX_ERROR_INCORRECT_CONTRACT_CLASS_ID = 'Incorrect contract class id';
|
|
50
|
+
export const TX_ERROR_MALFORMED_CONTRACT_CLASS_LOG = 'Failed to parse contract class registration log';
|
|
51
|
+
|
|
44
52
|
// General
|
|
45
53
|
export const TX_ERROR_DURING_VALIDATION = 'Unexpected error during validation';
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { type ContractArtifact, FunctionSelector } from '../abi/index.js';
|
|
2
|
-
import type { ContractClassPublic, ExecutablePrivateFunctionWithMembershipProof, PrivateFunctionMembershipProof } from './interfaces/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* Creates a membership proof for a private function in a contract class, to be verified via `isValidPrivateFunctionMembershipProof`.
|
|
5
|
-
* @param selector - Selector of the function to create the proof for.
|
|
6
|
-
* @param artifact - Artifact of the contract class where the function is defined.
|
|
7
|
-
*/
|
|
8
|
-
export declare function createPrivateFunctionMembershipProof(selector: FunctionSelector, artifact: ContractArtifact): Promise<PrivateFunctionMembershipProof>;
|
|
9
|
-
/**
|
|
10
|
-
* Verifies that a private function with a membership proof as emitted by the ClassRegistry contract is valid,
|
|
11
|
-
* as defined in the protocol specs at contract-deployment/classes:
|
|
12
|
-
*
|
|
13
|
-
* ```
|
|
14
|
-
* // Load contract class from local db
|
|
15
|
-
* contract_class = db.get_contract_class(contract_class_id)
|
|
16
|
-
*
|
|
17
|
-
* // Compute function leaf and assert it belongs to the private functions tree
|
|
18
|
-
* function_leaf = pedersen([selector as Field, vk_hash], GENERATOR__PRIVATE_FUNCTION_LEAF)
|
|
19
|
-
* computed_private_function_tree_root = compute_root(function_leaf, private_function_tree_sibling_path)
|
|
20
|
-
* assert computed_private_function_tree_root == contract_class.private_functions_root
|
|
21
|
-
*
|
|
22
|
-
* // Compute artifact leaf and assert it belongs to the artifact
|
|
23
|
-
* artifact_function_leaf = sha256(selector, metadata_hash, sha256(bytecode))
|
|
24
|
-
* computed_artifact_private_function_tree_root = compute_root(artifact_function_leaf, artifact_function_tree_sibling_path)
|
|
25
|
-
* computed_artifact_hash = sha256(computed_artifact_private_function_tree_root, utility_functions_artifact_tree_root, artifact_metadata_hash)
|
|
26
|
-
* assert computed_artifact_hash == contract_class.artifact_hash
|
|
27
|
-
* ```
|
|
28
|
-
* @param fn - Function to check membership proof for.
|
|
29
|
-
* @param contractClass - In which contract class the function is expected to be.
|
|
30
|
-
*/
|
|
31
|
-
export declare function isValidPrivateFunctionMembershipProof(fn: ExecutablePrivateFunctionWithMembershipProof, contractClass: Pick<ContractClassPublic, 'privateFunctionsRoot' | 'artifactHash'>): Promise<boolean>;
|
|
32
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJpdmF0ZV9mdW5jdGlvbl9tZW1iZXJzaGlwX3Byb29mLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY29udHJhY3QvcHJpdmF0ZV9mdW5jdGlvbl9tZW1iZXJzaGlwX3Byb29mLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUtBLE9BQU8sRUFBRSxLQUFLLGdCQUFnQixFQUFFLGdCQUFnQixFQUFnQixNQUFNLGlCQUFpQixDQUFDO0FBVXhGLE9BQU8sS0FBSyxFQUNWLG1CQUFtQixFQUNuQiw0Q0FBNEMsRUFDNUMsOEJBQThCLEVBQy9CLE1BQU0sdUJBQXVCLENBQUM7QUFHL0I7Ozs7R0FJRztBQUNILHdCQUFzQixvQ0FBb0MsQ0FDeEQsUUFBUSxFQUFFLGdCQUFnQixFQUMxQixRQUFRLEVBQUUsZ0JBQWdCLEdBQ3pCLE9BQU8sQ0FBQyw4QkFBOEIsQ0FBQyxDQTREekM7QUFFRDs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBcUJHO0FBQ0gsd0JBQXNCLHFDQUFxQyxDQUN6RCxFQUFFLEVBQUUsNENBQTRDLEVBQ2hELGFBQWEsRUFBRSxJQUFJLENBQUMsbUJBQW1CLEVBQUUsc0JBQXNCLEdBQUcsY0FBYyxDQUFDLG9CQWtEbEYifQ==
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"private_function_membership_proof.d.ts","sourceRoot":"","sources":["../../src/contract/private_function_membership_proof.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,gBAAgB,EAAE,gBAAgB,EAAgB,MAAM,iBAAiB,CAAC;AAUxF,OAAO,KAAK,EACV,mBAAmB,EACnB,4CAA4C,EAC5C,8BAA8B,EAC/B,MAAM,uBAAuB,CAAC;AAG/B;;;;GAIG;AACH,wBAAsB,oCAAoC,CACxD,QAAQ,EAAE,gBAAgB,EAC1B,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,8BAA8B,CAAC,CA4DzC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,qCAAqC,CACzD,EAAE,EAAE,4CAA4C,EAChD,aAAa,EAAE,IAAI,CAAC,mBAAmB,EAAE,sBAAsB,GAAG,cAAc,CAAC,oBAkDlF"}
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
2
|
-
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
-
import { createLogger } from '@aztec/foundation/log';
|
|
4
|
-
import { computeRootFromSiblingPath } from '@aztec/foundation/trees';
|
|
5
|
-
import { FunctionSelector, FunctionType } from '../abi/index.js';
|
|
6
|
-
import { computeArtifactFunctionTree, computeArtifactHash, computeArtifactHashPreimage, computeFunctionArtifactHash, computeFunctionMetadataHash, getArtifactMerkleTreeHasher } from './artifact_hash.js';
|
|
7
|
-
import { getContractClassPrivateFunctionFromArtifact } from './contract_class.js';
|
|
8
|
-
import { computePrivateFunctionLeaf, computePrivateFunctionsTree } from './private_function.js';
|
|
9
|
-
/**
|
|
10
|
-
* Creates a membership proof for a private function in a contract class, to be verified via `isValidPrivateFunctionMembershipProof`.
|
|
11
|
-
* @param selector - Selector of the function to create the proof for.
|
|
12
|
-
* @param artifact - Artifact of the contract class where the function is defined.
|
|
13
|
-
*/ export async function createPrivateFunctionMembershipProof(selector, artifact) {
|
|
14
|
-
const log = createLogger('circuits:function_membership_proof');
|
|
15
|
-
// Locate private function definition and artifact
|
|
16
|
-
const privateFunctions = artifact.functions.filter((fn)=>fn.functionType === FunctionType.PRIVATE);
|
|
17
|
-
const privateFunctionsFromArtifact = await Promise.all(privateFunctions.map(getContractClassPrivateFunctionFromArtifact));
|
|
18
|
-
const privateFunction = privateFunctionsFromArtifact.find((fn)=>fn.selector.equals(selector));
|
|
19
|
-
const privateFunctionsAndSelectors = await Promise.all(privateFunctions.map(async (fn)=>({
|
|
20
|
-
fn,
|
|
21
|
-
selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)
|
|
22
|
-
})));
|
|
23
|
-
const privateFunctionArtifact = privateFunctionsAndSelectors.find((fnAndSelector)=>selector.equals(fnAndSelector.selector))?.fn;
|
|
24
|
-
if (!privateFunction || !privateFunctionArtifact) {
|
|
25
|
-
throw new Error(`Private function with selector ${selector.toString()} not found`);
|
|
26
|
-
}
|
|
27
|
-
// Compute preimage for the artifact hash
|
|
28
|
-
const { utilityFunctionRoot: utilityFunctionsTreeRoot, metadataHash: artifactMetadataHash } = await computeArtifactHashPreimage(artifact);
|
|
29
|
-
// We need two sibling paths because private function information is split across two trees:
|
|
30
|
-
// The "private function tree" captures the selectors and verification keys, and is used in the kernel circuit for verifying the proof generated by the app circuit.
|
|
31
|
-
const functionLeaf = await computePrivateFunctionLeaf(privateFunction);
|
|
32
|
-
const functionsTree = await computePrivateFunctionsTree(privateFunctionsFromArtifact);
|
|
33
|
-
const functionsTreeLeafIndex = functionsTree.getIndex(functionLeaf);
|
|
34
|
-
const functionsTreeSiblingPath = functionsTree.getSiblingPath(functionsTreeLeafIndex).map(Fr.fromBuffer);
|
|
35
|
-
// And the "artifact tree" captures function bytecode and metadata, and is used by the pxe to check that its executing the code it's supposed to be executing, but it never goes into circuits.
|
|
36
|
-
const functionMetadataHash = computeFunctionMetadataHash(privateFunctionArtifact);
|
|
37
|
-
const functionArtifactHash = await computeFunctionArtifactHash({
|
|
38
|
-
...privateFunctionArtifact,
|
|
39
|
-
functionMetadataHash
|
|
40
|
-
});
|
|
41
|
-
const artifactTree = await computeArtifactFunctionTree(artifact, FunctionType.PRIVATE);
|
|
42
|
-
const artifactTreeLeafIndex = artifactTree.getIndex(functionArtifactHash.toBuffer());
|
|
43
|
-
const artifactTreeSiblingPath = artifactTree.getSiblingPath(artifactTreeLeafIndex).map(Fr.fromBuffer);
|
|
44
|
-
log.debug(`Computed proof for private function with selector ${selector.toString()}`, {
|
|
45
|
-
functionArtifactHash,
|
|
46
|
-
functionMetadataHash,
|
|
47
|
-
functionLeaf: '0x' + functionLeaf.toString('hex'),
|
|
48
|
-
artifactMetadataHash,
|
|
49
|
-
privateFunctionsTreeRoot: '0x' + functionsTree.root.toString('hex'),
|
|
50
|
-
utilityFunctionsTreeRoot,
|
|
51
|
-
artifactFunctionTreeSiblingPath: artifactTreeSiblingPath.map((fr)=>fr.toString()).join(','),
|
|
52
|
-
privateFunctionTreeSiblingPath: functionsTreeSiblingPath.map((fr)=>fr.toString()).join(',')
|
|
53
|
-
});
|
|
54
|
-
return {
|
|
55
|
-
artifactTreeSiblingPath,
|
|
56
|
-
artifactTreeLeafIndex,
|
|
57
|
-
artifactMetadataHash,
|
|
58
|
-
functionMetadataHash,
|
|
59
|
-
utilityFunctionsTreeRoot,
|
|
60
|
-
privateFunctionTreeSiblingPath: functionsTreeSiblingPath,
|
|
61
|
-
privateFunctionTreeLeafIndex: functionsTreeLeafIndex
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Verifies that a private function with a membership proof as emitted by the ClassRegistry contract is valid,
|
|
66
|
-
* as defined in the protocol specs at contract-deployment/classes:
|
|
67
|
-
*
|
|
68
|
-
* ```
|
|
69
|
-
* // Load contract class from local db
|
|
70
|
-
* contract_class = db.get_contract_class(contract_class_id)
|
|
71
|
-
*
|
|
72
|
-
* // Compute function leaf and assert it belongs to the private functions tree
|
|
73
|
-
* function_leaf = pedersen([selector as Field, vk_hash], GENERATOR__PRIVATE_FUNCTION_LEAF)
|
|
74
|
-
* computed_private_function_tree_root = compute_root(function_leaf, private_function_tree_sibling_path)
|
|
75
|
-
* assert computed_private_function_tree_root == contract_class.private_functions_root
|
|
76
|
-
*
|
|
77
|
-
* // Compute artifact leaf and assert it belongs to the artifact
|
|
78
|
-
* artifact_function_leaf = sha256(selector, metadata_hash, sha256(bytecode))
|
|
79
|
-
* computed_artifact_private_function_tree_root = compute_root(artifact_function_leaf, artifact_function_tree_sibling_path)
|
|
80
|
-
* computed_artifact_hash = sha256(computed_artifact_private_function_tree_root, utility_functions_artifact_tree_root, artifact_metadata_hash)
|
|
81
|
-
* assert computed_artifact_hash == contract_class.artifact_hash
|
|
82
|
-
* ```
|
|
83
|
-
* @param fn - Function to check membership proof for.
|
|
84
|
-
* @param contractClass - In which contract class the function is expected to be.
|
|
85
|
-
*/ export async function isValidPrivateFunctionMembershipProof(fn, contractClass) {
|
|
86
|
-
const log = createLogger('circuits:function_membership_proof');
|
|
87
|
-
// Check private function tree membership
|
|
88
|
-
const functionLeaf = await computePrivateFunctionLeaf(fn);
|
|
89
|
-
const rootBuffer = await computeRootFromSiblingPath(functionLeaf, fn.privateFunctionTreeSiblingPath.map((fr)=>fr.toBuffer()), fn.privateFunctionTreeLeafIndex, async (left, right)=>(await poseidon2Hash([
|
|
90
|
-
left,
|
|
91
|
-
right
|
|
92
|
-
])).toBuffer());
|
|
93
|
-
const computedPrivateFunctionTreeRoot = Fr.fromBuffer(rootBuffer);
|
|
94
|
-
if (!contractClass.privateFunctionsRoot.equals(computedPrivateFunctionTreeRoot)) {
|
|
95
|
-
log.debug(`Private function tree root mismatch`, {
|
|
96
|
-
expectedPrivateFunctionTreeRoot: contractClass.privateFunctionsRoot,
|
|
97
|
-
computedPrivateFunctionTreeRoot: computedPrivateFunctionTreeRoot,
|
|
98
|
-
computedFunctionLeaf: '0x' + functionLeaf.toString('hex')
|
|
99
|
-
});
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
// Check artifact hash
|
|
103
|
-
const functionArtifactHash = await computeFunctionArtifactHash(fn);
|
|
104
|
-
const computedArtifactPrivateFunctionTreeRootBuffer = await computeRootFromSiblingPath(functionArtifactHash.toBuffer(), fn.artifactTreeSiblingPath.map((fr)=>fr.toBuffer()), fn.artifactTreeLeafIndex, getArtifactMerkleTreeHasher());
|
|
105
|
-
const computedArtifactPrivateFunctionTreeRoot = Fr.fromBuffer(computedArtifactPrivateFunctionTreeRootBuffer);
|
|
106
|
-
const computedArtifactHash = await computeArtifactHash({
|
|
107
|
-
privateFunctionRoot: computedArtifactPrivateFunctionTreeRoot,
|
|
108
|
-
utilityFunctionRoot: fn.utilityFunctionsTreeRoot,
|
|
109
|
-
metadataHash: fn.artifactMetadataHash
|
|
110
|
-
});
|
|
111
|
-
if (!contractClass.artifactHash.equals(computedArtifactHash)) {
|
|
112
|
-
log.debug(`Artifact hash mismatch`, {
|
|
113
|
-
expected: contractClass.artifactHash,
|
|
114
|
-
computedArtifactHash,
|
|
115
|
-
computedFunctionArtifactHash: functionArtifactHash,
|
|
116
|
-
computedArtifactPrivateFunctionTreeRoot,
|
|
117
|
-
utilityFunctionRoot: fn.utilityFunctionsTreeRoot,
|
|
118
|
-
metadataHash: fn.artifactMetadataHash,
|
|
119
|
-
artifactFunctionTreeSiblingPath: fn.artifactTreeSiblingPath.map((fr)=>fr.toString()).join(',')
|
|
120
|
-
});
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { type ContractArtifact, FunctionSelector } from '../abi/index.js';
|
|
2
|
-
import type { ContractClassPublic, UtilityFunctionMembershipProof, UtilityFunctionWithMembershipProof } from './interfaces/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* Creates a membership proof for a utility function in a contract class, to be verified via `isValidUtilityFunctionMembershipProof`.
|
|
5
|
-
* @param selector - Selector of the function to create the proof for.
|
|
6
|
-
* @param artifact - Artifact of the contract class where the function is defined.
|
|
7
|
-
*/
|
|
8
|
-
export declare function createUtilityFunctionMembershipProof(selector: FunctionSelector, artifact: ContractArtifact): Promise<UtilityFunctionMembershipProof>;
|
|
9
|
-
/**
|
|
10
|
-
* Verifies that a utility function with a membership proof as emitted by the ClassRegistry contract is valid,
|
|
11
|
-
* as defined in the protocol specs at contract-deployment/classes:
|
|
12
|
-
*
|
|
13
|
-
* ```
|
|
14
|
-
* // Load contract class from local db
|
|
15
|
-
* contract_class = db.get_contract_class(contract_class_id)
|
|
16
|
-
*
|
|
17
|
-
* // Compute artifact leaf and assert it belongs to the artifact
|
|
18
|
-
* artifact_function_leaf = sha256(selector, metadata_hash, sha256(bytecode))
|
|
19
|
-
* computed_artifact_utility_function_tree_root = compute_root(artifact_function_leaf, artifact_function_tree_sibling_path, artifact_function_tree_leaf_index)
|
|
20
|
-
* computed_artifact_hash = sha256(private_functions_artifact_tree_root, computed_artifact_utility_function_tree_root, artifact_metadata_hash)
|
|
21
|
-
* assert computed_artifact_hash == contract_class.artifact_hash
|
|
22
|
-
* ```
|
|
23
|
-
* @param fn - Function to check membership proof for.
|
|
24
|
-
* @param contractClass - In which contract class the function is expected to be.
|
|
25
|
-
*/
|
|
26
|
-
export declare function isValidUtilityFunctionMembershipProof(fn: UtilityFunctionWithMembershipProof, contractClass: Pick<ContractClassPublic, 'artifactHash'>): Promise<boolean>;
|
|
27
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbGl0eV9mdW5jdGlvbl9tZW1iZXJzaGlwX3Byb29mLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY29udHJhY3QvdXRpbGl0eV9mdW5jdGlvbl9tZW1iZXJzaGlwX3Byb29mLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlBLE9BQU8sRUFBRSxLQUFLLGdCQUFnQixFQUFFLGdCQUFnQixFQUFnQixNQUFNLGlCQUFpQixDQUFDO0FBU3hGLE9BQU8sS0FBSyxFQUNWLG1CQUFtQixFQUNuQiw4QkFBOEIsRUFDOUIsa0NBQWtDLEVBQ25DLE1BQU0sdUJBQXVCLENBQUM7QUFFL0I7Ozs7R0FJRztBQUNILHdCQUFzQixvQ0FBb0MsQ0FDeEQsUUFBUSxFQUFFLGdCQUFnQixFQUMxQixRQUFRLEVBQUUsZ0JBQWdCLEdBQ3pCLE9BQU8sQ0FBQyw4QkFBOEIsQ0FBQyxDQXNDekM7QUFFRDs7Ozs7Ozs7Ozs7Ozs7OztHQWdCRztBQUNILHdCQUFzQixxQ0FBcUMsQ0FDekQsRUFBRSxFQUFFLGtDQUFrQyxFQUN0QyxhQUFhLEVBQUUsSUFBSSxDQUFDLG1CQUFtQixFQUFFLGNBQWMsQ0FBQyxvQkErQnpEIn0=
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utility_function_membership_proof.d.ts","sourceRoot":"","sources":["../../src/contract/utility_function_membership_proof.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,gBAAgB,EAAE,gBAAgB,EAAgB,MAAM,iBAAiB,CAAC;AASxF,OAAO,KAAK,EACV,mBAAmB,EACnB,8BAA8B,EAC9B,kCAAkC,EACnC,MAAM,uBAAuB,CAAC;AAE/B;;;;GAIG;AACH,wBAAsB,oCAAoC,CACxD,QAAQ,EAAE,gBAAgB,EAC1B,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,8BAA8B,CAAC,CAsCzC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,qCAAqC,CACzD,EAAE,EAAE,kCAAkC,EACtC,aAAa,EAAE,IAAI,CAAC,mBAAmB,EAAE,cAAc,CAAC,oBA+BzD"}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
|
-
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
-
import { computeRootFromSiblingPath } from '@aztec/foundation/trees';
|
|
4
|
-
import { FunctionSelector, FunctionType } from '../abi/index.js';
|
|
5
|
-
import { computeArtifactFunctionTree, computeArtifactHash, computeArtifactHashPreimage, computeFunctionArtifactHash, computeFunctionMetadataHash, getArtifactMerkleTreeHasher } from './artifact_hash.js';
|
|
6
|
-
/**
|
|
7
|
-
* Creates a membership proof for a utility function in a contract class, to be verified via `isValidUtilityFunctionMembershipProof`.
|
|
8
|
-
* @param selector - Selector of the function to create the proof for.
|
|
9
|
-
* @param artifact - Artifact of the contract class where the function is defined.
|
|
10
|
-
*/ export async function createUtilityFunctionMembershipProof(selector, artifact) {
|
|
11
|
-
const log = createLogger('circuits:function_membership_proof');
|
|
12
|
-
// Locate function artifact
|
|
13
|
-
const utilityFunctions = artifact.functions.filter((fn)=>fn.functionType === FunctionType.UTILITY);
|
|
14
|
-
const utilityFunctionsAndSelectors = await Promise.all(utilityFunctions.map(async (fn)=>({
|
|
15
|
-
fn,
|
|
16
|
-
selector: await FunctionSelector.fromNameAndParameters(fn)
|
|
17
|
-
})));
|
|
18
|
-
const fn = utilityFunctionsAndSelectors.find((fnAndSelector)=>selector.equals(fnAndSelector.selector))?.fn;
|
|
19
|
-
if (!fn) {
|
|
20
|
-
throw new Error(`Utility function with selector ${selector.toString()} not found`);
|
|
21
|
-
}
|
|
22
|
-
// Compute preimage for the artifact hash
|
|
23
|
-
const { privateFunctionRoot: privateFunctionsArtifactTreeRoot, metadataHash: artifactMetadataHash } = await computeArtifactHashPreimage(artifact);
|
|
24
|
-
// Compute the sibling path for the "artifact tree"
|
|
25
|
-
const functionMetadataHash = computeFunctionMetadataHash(fn);
|
|
26
|
-
const functionArtifactHash = await computeFunctionArtifactHash({
|
|
27
|
-
...fn,
|
|
28
|
-
functionMetadataHash
|
|
29
|
-
});
|
|
30
|
-
const artifactTree = await computeArtifactFunctionTree(artifact, FunctionType.UTILITY);
|
|
31
|
-
const artifactTreeLeafIndex = artifactTree.getIndex(functionArtifactHash.toBuffer());
|
|
32
|
-
const artifactTreeSiblingPath = artifactTree.getSiblingPath(artifactTreeLeafIndex).map(Fr.fromBuffer);
|
|
33
|
-
log.debug(`Computed proof for utility function with selector ${selector.toString()}`, {
|
|
34
|
-
functionArtifactHash,
|
|
35
|
-
functionMetadataHash,
|
|
36
|
-
artifactMetadataHash,
|
|
37
|
-
artifactFunctionTreeSiblingPath: artifactTreeSiblingPath.map((fr)=>fr.toString()).join(','),
|
|
38
|
-
privateFunctionsArtifactTreeRoot
|
|
39
|
-
});
|
|
40
|
-
return {
|
|
41
|
-
artifactTreeSiblingPath,
|
|
42
|
-
artifactTreeLeafIndex,
|
|
43
|
-
artifactMetadataHash,
|
|
44
|
-
functionMetadataHash,
|
|
45
|
-
privateFunctionsArtifactTreeRoot
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Verifies that a utility function with a membership proof as emitted by the ClassRegistry contract is valid,
|
|
50
|
-
* as defined in the protocol specs at contract-deployment/classes:
|
|
51
|
-
*
|
|
52
|
-
* ```
|
|
53
|
-
* // Load contract class from local db
|
|
54
|
-
* contract_class = db.get_contract_class(contract_class_id)
|
|
55
|
-
*
|
|
56
|
-
* // Compute artifact leaf and assert it belongs to the artifact
|
|
57
|
-
* artifact_function_leaf = sha256(selector, metadata_hash, sha256(bytecode))
|
|
58
|
-
* computed_artifact_utility_function_tree_root = compute_root(artifact_function_leaf, artifact_function_tree_sibling_path, artifact_function_tree_leaf_index)
|
|
59
|
-
* computed_artifact_hash = sha256(private_functions_artifact_tree_root, computed_artifact_utility_function_tree_root, artifact_metadata_hash)
|
|
60
|
-
* assert computed_artifact_hash == contract_class.artifact_hash
|
|
61
|
-
* ```
|
|
62
|
-
* @param fn - Function to check membership proof for.
|
|
63
|
-
* @param contractClass - In which contract class the function is expected to be.
|
|
64
|
-
*/ export async function isValidUtilityFunctionMembershipProof(fn, contractClass) {
|
|
65
|
-
const log = createLogger('circuits:function_membership_proof');
|
|
66
|
-
const functionArtifactHash = await computeFunctionArtifactHash(fn);
|
|
67
|
-
const computedArtifactFunctionTreeRootBuffer = await computeRootFromSiblingPath(functionArtifactHash.toBuffer(), fn.artifactTreeSiblingPath.map((fr)=>fr.toBuffer()), fn.artifactTreeLeafIndex, getArtifactMerkleTreeHasher());
|
|
68
|
-
const computedArtifactFunctionTreeRoot = Fr.fromBuffer(computedArtifactFunctionTreeRootBuffer);
|
|
69
|
-
const computedArtifactHash = await computeArtifactHash({
|
|
70
|
-
privateFunctionRoot: fn.privateFunctionsArtifactTreeRoot,
|
|
71
|
-
utilityFunctionRoot: computedArtifactFunctionTreeRoot,
|
|
72
|
-
metadataHash: fn.artifactMetadataHash
|
|
73
|
-
});
|
|
74
|
-
if (!contractClass.artifactHash.equals(computedArtifactHash)) {
|
|
75
|
-
log.debug(`Artifact hash mismatch`, {
|
|
76
|
-
expected: contractClass.artifactHash,
|
|
77
|
-
computedArtifactHash,
|
|
78
|
-
computedFunctionArtifactHash: functionArtifactHash,
|
|
79
|
-
computedArtifactFunctionTreeRoot,
|
|
80
|
-
privateFunctionsArtifactTreeRoot: fn.privateFunctionsArtifactTreeRoot,
|
|
81
|
-
metadataHash: fn.artifactMetadataHash,
|
|
82
|
-
artifactFunctionTreeSiblingPath: fn.artifactTreeSiblingPath.map((fr)=>fr.toString()).join(',')
|
|
83
|
-
});
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
}
|