@aztec/pxe 0.23.0 → 0.26.1
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/config/index.js +2 -2
- package/dest/database/deferred_note_dao.d.ts +8 -4
- package/dest/database/deferred_note_dao.d.ts.map +1 -1
- package/dest/database/deferred_note_dao.js +9 -6
- package/dest/database/note_dao.d.ts +4 -0
- package/dest/database/note_dao.d.ts.map +1 -1
- package/dest/database/note_dao.js +7 -2
- package/dest/database/pxe_database_test_suite.js +5 -5
- package/dest/kernel_oracle/index.d.ts +1 -0
- package/dest/kernel_oracle/index.d.ts.map +1 -1
- package/dest/kernel_oracle/index.js +4 -1
- package/dest/kernel_prover/hints_builder.d.ts +36 -0
- package/dest/kernel_prover/hints_builder.d.ts.map +1 -0
- package/dest/kernel_prover/hints_builder.js +115 -0
- package/dest/kernel_prover/kernel_prover.d.ts +2 -24
- package/dest/kernel_prover/kernel_prover.d.ts.map +1 -1
- package/dest/kernel_prover/kernel_prover.js +32 -103
- package/dest/kernel_prover/proof_creator.d.ts +11 -11
- package/dest/kernel_prover/proof_creator.d.ts.map +1 -1
- package/dest/kernel_prover/proof_creator.js +6 -6
- package/dest/kernel_prover/proving_data_oracle.d.ts +2 -0
- package/dest/kernel_prover/proving_data_oracle.d.ts.map +1 -1
- package/dest/note_processor/note_processor.d.ts.map +1 -1
- package/dest/note_processor/note_processor.js +15 -14
- package/dest/note_processor/produce_note_dao.d.ts +2 -2
- package/dest/note_processor/produce_note_dao.d.ts.map +1 -1
- package/dest/note_processor/produce_note_dao.js +8 -8
- package/dest/pxe_http/pxe_http_server.d.ts.map +1 -1
- package/dest/pxe_http/pxe_http_server.js +7 -6
- package/dest/pxe_service/create_pxe_service.d.ts.map +1 -1
- package/dest/pxe_service/create_pxe_service.js +5 -1
- package/dest/pxe_service/pxe_service.d.ts +5 -3
- package/dest/pxe_service/pxe_service.d.ts.map +1 -1
- package/dest/pxe_service/pxe_service.js +51 -44
- package/dest/pxe_service/test/pxe_test_suite.d.ts.map +1 -1
- package/dest/pxe_service/test/pxe_test_suite.js +3 -3
- package/dest/simulator_oracle/index.d.ts +7 -4
- package/dest/simulator_oracle/index.d.ts.map +1 -1
- package/dest/simulator_oracle/index.js +17 -8
- package/dest/synchronizer/synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/synchronizer.js +14 -42
- package/package.json +14 -13
- package/src/bin/index.ts +42 -0
- package/src/config/index.ts +40 -0
- package/src/contract_data_oracle/index.ts +185 -0
- package/src/contract_data_oracle/private_functions_tree.ts +127 -0
- package/src/contract_database/index.ts +1 -0
- package/src/contract_database/memory_contract_database.ts +58 -0
- package/src/database/contracts/contract_artifact_db.ts +19 -0
- package/src/database/contracts/contract_instance_db.ts +18 -0
- package/src/database/deferred_note_dao.ts +55 -0
- package/src/database/index.ts +1 -0
- package/src/database/kv_pxe_database.ts +409 -0
- package/src/database/note_dao.ts +97 -0
- package/src/database/pxe_database.ts +162 -0
- package/src/database/pxe_database_test_suite.ts +258 -0
- package/src/index.ts +11 -0
- package/src/kernel_oracle/index.ts +65 -0
- package/src/kernel_prover/hints_builder.ts +170 -0
- package/src/kernel_prover/index.ts +2 -0
- package/src/kernel_prover/kernel_prover.ts +309 -0
- package/src/kernel_prover/proof_creator.ts +157 -0
- package/src/kernel_prover/proving_data_oracle.ts +79 -0
- package/src/note_processor/index.ts +1 -0
- package/src/note_processor/note_processor.ts +275 -0
- package/src/note_processor/produce_note_dao.ts +132 -0
- package/src/pxe_http/index.ts +1 -0
- package/src/pxe_http/pxe_http_server.ts +75 -0
- package/src/pxe_service/create_pxe_service.ts +53 -0
- package/src/pxe_service/index.ts +3 -0
- package/src/pxe_service/pxe_service.ts +769 -0
- package/src/pxe_service/test/pxe_test_suite.ts +140 -0
- package/src/simulator/index.ts +24 -0
- package/src/simulator_oracle/index.ts +222 -0
- package/src/synchronizer/index.ts +1 -0
- package/src/synchronizer/synchronizer.ts +385 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { ContractDao } from '@aztec/circuit-types';
|
|
2
|
+
import {
|
|
3
|
+
FUNCTION_TREE_HEIGHT,
|
|
4
|
+
MembershipWitness,
|
|
5
|
+
computePrivateFunctionLeaf,
|
|
6
|
+
computePrivateFunctionsTree,
|
|
7
|
+
getContractClassFromArtifact,
|
|
8
|
+
} from '@aztec/circuits.js';
|
|
9
|
+
import { MerkleTree } from '@aztec/circuits.js/merkle';
|
|
10
|
+
import { FunctionSelector } from '@aztec/foundation/abi';
|
|
11
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
12
|
+
import { assertLength } from '@aztec/foundation/serialize';
|
|
13
|
+
import { ContractClassWithId } from '@aztec/types/contracts';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represents a Merkle tree of functions for a particular Contract Class.
|
|
17
|
+
* It manages the construction of the function tree, computes its root, and generates membership witnesses
|
|
18
|
+
* for constrained functions. This class also enables lookup of specific function artifact using selectors.
|
|
19
|
+
* It is used in combination with the AztecNode to compute various data for executing private transactions.
|
|
20
|
+
*/
|
|
21
|
+
export class PrivateFunctionsTree {
|
|
22
|
+
private tree?: MerkleTree;
|
|
23
|
+
private contractClass?: ContractClassWithId;
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
/**
|
|
27
|
+
* The contract data object containing the artifact and contract address.
|
|
28
|
+
* TODO(@spalladino) Replace with contract class sourced from db.
|
|
29
|
+
*/
|
|
30
|
+
public readonly contract: ContractDao,
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Retrieve the artifact of a given function.
|
|
35
|
+
* The function is identified by its selector, which represents a unique identifier for the function's signature.
|
|
36
|
+
* Throws an error if the function with the provided selector is not found in the contract.
|
|
37
|
+
*
|
|
38
|
+
* @param selector - The function selector.
|
|
39
|
+
* @returns The artifact object containing relevant information about the targeted function.
|
|
40
|
+
*/
|
|
41
|
+
public getFunctionArtifact(selector: FunctionSelector) {
|
|
42
|
+
const artifact = this.contract.functions.find(f => f.selector.equals(selector));
|
|
43
|
+
if (!artifact) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`Unknown function. Selector ${selector.toString()} not found in the artifact of contract ${this.contract.instance.address.toString()}. Expected one of: ${this.contract.functions
|
|
46
|
+
.map(f => `${f.name} (${f.selector.toString()})`)
|
|
47
|
+
.join(', ')}`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return artifact;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Retrieve the bytecode of a function in the contract by its function selector.
|
|
55
|
+
* The function selector is a unique identifier for each function in a contract.
|
|
56
|
+
* Throws an error if the function with the given selector is not found in the contract.
|
|
57
|
+
*
|
|
58
|
+
* @param selector - The selector of a function to get bytecode for.
|
|
59
|
+
* @returns The bytecode of the function as a string.
|
|
60
|
+
*/
|
|
61
|
+
public getBytecode(selector: FunctionSelector) {
|
|
62
|
+
return this.getFunctionArtifact(selector).bytecode;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Calculate and return the root of the function tree for the current contract.
|
|
67
|
+
* This root is a cryptographic commitment to the set of constrained functions within the contract,
|
|
68
|
+
* which is used in the Aztec node's proof system. The root will be cached after the first call.
|
|
69
|
+
*
|
|
70
|
+
* @returns A promise that resolves to the Fr (finite field element) representation of the function tree root.
|
|
71
|
+
*/
|
|
72
|
+
public getFunctionTreeRoot() {
|
|
73
|
+
return this.getTree();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Returns the contract class object. */
|
|
77
|
+
public getContractClass() {
|
|
78
|
+
if (!this.contractClass) {
|
|
79
|
+
this.contractClass = getContractClassFromArtifact(this.contract);
|
|
80
|
+
}
|
|
81
|
+
return this.contractClass;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Returns the contract class identifier for the given artifact.
|
|
86
|
+
*/
|
|
87
|
+
public getContractClassId() {
|
|
88
|
+
return this.getContractClass().id;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Retrieve the membership witness of a function within a contract's function tree.
|
|
93
|
+
* A membership witness represents the position and authentication path of a target function
|
|
94
|
+
* in the Merkle tree of constrained functions. It is required to prove the existence of the
|
|
95
|
+
* function within the contract during execution. Throws if fn does not exist or is not private.
|
|
96
|
+
*
|
|
97
|
+
* @param selector - The function selector.
|
|
98
|
+
* @returns A MembershipWitness instance representing the position and authentication path of the function in the function tree.
|
|
99
|
+
*/
|
|
100
|
+
public getFunctionMembershipWitness(
|
|
101
|
+
selector: FunctionSelector,
|
|
102
|
+
): Promise<MembershipWitness<typeof FUNCTION_TREE_HEIGHT>> {
|
|
103
|
+
const fn = this.getContractClass().privateFunctions.find(f => f.selector.equals(selector));
|
|
104
|
+
if (!fn) {
|
|
105
|
+
throw new Error(`Private function with selector ${selector.toString()} not found in contract class.`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const leaf = computePrivateFunctionLeaf(fn);
|
|
109
|
+
const index = this.getTree().getIndex(leaf);
|
|
110
|
+
const path = this.getTree().getSiblingPath(index);
|
|
111
|
+
return Promise.resolve(
|
|
112
|
+
new MembershipWitness<typeof FUNCTION_TREE_HEIGHT>(
|
|
113
|
+
FUNCTION_TREE_HEIGHT,
|
|
114
|
+
BigInt(index),
|
|
115
|
+
assertLength(path.map(Fr.fromBuffer), FUNCTION_TREE_HEIGHT),
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private getTree() {
|
|
121
|
+
if (!this.tree) {
|
|
122
|
+
const fns = this.getContractClass().privateFunctions;
|
|
123
|
+
this.tree = computePrivateFunctionsTree(fns);
|
|
124
|
+
}
|
|
125
|
+
return this.tree;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './memory_contract_database.js';
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ContractDao, ContractDatabase } from '@aztec/circuit-types';
|
|
2
|
+
import { FunctionSelector } from '@aztec/circuits.js';
|
|
3
|
+
import { AztecAddress } from '@aztec/foundation/aztec-address';
|
|
4
|
+
import { DebugLogger } from '@aztec/foundation/log';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The MemoryContractDatabase class serves as an in-memory implementation of the ContractDatabase interface.
|
|
8
|
+
* It allows for storing and retrieving contract data, such as ContractDao objects and associated function bytecodes,
|
|
9
|
+
* within a contracts array. This class is particularly useful for testing and development purposes where a
|
|
10
|
+
* persistent storage may not be required.
|
|
11
|
+
*/
|
|
12
|
+
export class MemoryContractDatabase implements ContractDatabase {
|
|
13
|
+
private contracts: ContractDao[] = [];
|
|
14
|
+
|
|
15
|
+
constructor(protected log: DebugLogger) {}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Adds a new ContractDao instance to the memory-based contract database.
|
|
19
|
+
* The function stores the contract in an array and returns a resolved promise indicating successful addition.
|
|
20
|
+
*
|
|
21
|
+
* @param contract - The ContractDao instance to be added to the memory database.
|
|
22
|
+
* @returns A Promise that resolves when the contract is successfully added.
|
|
23
|
+
*/
|
|
24
|
+
public addContract(contract: ContractDao) {
|
|
25
|
+
this.log(`Adding contract ${contract.instance.address.toString()}`);
|
|
26
|
+
this.contracts.push(contract);
|
|
27
|
+
return Promise.resolve();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Retrieve a ContractDao instance with the specified AztecAddress from the in-memory contracts list.
|
|
32
|
+
* Returns the first match found or undefined if no contract with the given address is found.
|
|
33
|
+
*
|
|
34
|
+
* @param address - The AztecAddress to search for in the stored contracts.
|
|
35
|
+
* @returns A Promise resolving to the ContractDao instance matching the given address or undefined.
|
|
36
|
+
*/
|
|
37
|
+
public getContract(address: AztecAddress): Promise<ContractDao | undefined> {
|
|
38
|
+
return Promise.resolve(this.contracts.find(c => c.instance.address.equals(address)));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public getContracts(): Promise<ContractDao[]> {
|
|
42
|
+
return Promise.resolve(this.contracts);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Retrieve the bytecode associated with a given contract address and function selector.
|
|
47
|
+
* This function searches through the stored contracts to find a matching contract and function,
|
|
48
|
+
* then returns the corresponding bytecode. If no match is found, it returns undefined.
|
|
49
|
+
*
|
|
50
|
+
* @param contractAddress - The AztecAddress representing the contract address to look for.
|
|
51
|
+
* @param selector - The function selector.
|
|
52
|
+
* @returns A Promise that resolves to the bytecode of the matching function or undefined if not found.
|
|
53
|
+
*/
|
|
54
|
+
public async getCode(contractAddress: AztecAddress, selector: FunctionSelector) {
|
|
55
|
+
const contract = await this.getContract(contractAddress);
|
|
56
|
+
return contract?.functions.find(f => f.selector.equals(selector))?.bytecode;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ContractArtifact } from '@aztec/foundation/abi';
|
|
2
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* PXE database for managing contract artifacts.
|
|
6
|
+
*/
|
|
7
|
+
export interface ContractArtifactDatabase {
|
|
8
|
+
/**
|
|
9
|
+
* Adds a new contract artifact to the database or updates an existing one.
|
|
10
|
+
* @param id - Id of the corresponding contract class.
|
|
11
|
+
* @param contract - Contract artifact to add.
|
|
12
|
+
*/
|
|
13
|
+
addContractArtifact(id: Fr, contract: ContractArtifact): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Gets a contract artifact given its resulting contract class id.
|
|
16
|
+
* @param id - Contract class id for the given artifact.
|
|
17
|
+
*/
|
|
18
|
+
getContractArtifact(id: Fr): Promise<ContractArtifact | undefined>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AztecAddress } from '@aztec/circuits.js';
|
|
2
|
+
import { ContractInstanceWithAddress } from '@aztec/types/contracts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* PXE database for managing contract instances.
|
|
6
|
+
*/
|
|
7
|
+
export interface ContractInstanceDatabase {
|
|
8
|
+
/**
|
|
9
|
+
* Adds a new contract to the db or updates an existing one.
|
|
10
|
+
* @param contract - Contract to insert.
|
|
11
|
+
*/
|
|
12
|
+
addContractInstance(contract: ContractInstanceWithAddress): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Gets a contract given its address.
|
|
15
|
+
* @param address - Address of the contract.
|
|
16
|
+
*/
|
|
17
|
+
getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Note, TxHash } from '@aztec/circuit-types';
|
|
2
|
+
import { AztecAddress, Fr, Point, PublicKey, Vector } from '@aztec/circuits.js';
|
|
3
|
+
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A note that is intended for us, but we cannot decode it yet because the contract is not yet in our database.
|
|
7
|
+
*
|
|
8
|
+
* So keep the state that we need to decode it later.
|
|
9
|
+
*/
|
|
10
|
+
export class DeferredNoteDao {
|
|
11
|
+
constructor(
|
|
12
|
+
/** The public key associated with this note */
|
|
13
|
+
public publicKey: PublicKey,
|
|
14
|
+
/** The note as emitted from the Noir contract. */
|
|
15
|
+
public note: Note,
|
|
16
|
+
/** The contract address this note is created in. */
|
|
17
|
+
public contractAddress: AztecAddress,
|
|
18
|
+
/** The specific storage location of the note on the contract. */
|
|
19
|
+
public storageSlot: Fr,
|
|
20
|
+
/** The type ID of the note on the contract. */
|
|
21
|
+
public noteTypeId: Fr,
|
|
22
|
+
/** The hash of the tx the note was created in. Equal to the first nullifier */
|
|
23
|
+
public txHash: TxHash,
|
|
24
|
+
/** New note hashes in this transaction, one of which belongs to this note */
|
|
25
|
+
public newNoteHashes: Fr[],
|
|
26
|
+
/** The next available leaf index for the note hash tree for this transaction */
|
|
27
|
+
public dataStartIndexForTx: number,
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
toBuffer(): Buffer {
|
|
31
|
+
return serializeToBuffer(
|
|
32
|
+
this.publicKey.toBuffer(),
|
|
33
|
+
this.note.toBuffer(),
|
|
34
|
+
this.contractAddress.toBuffer(),
|
|
35
|
+
this.storageSlot.toBuffer(),
|
|
36
|
+
this.noteTypeId.toBuffer(),
|
|
37
|
+
this.txHash.toBuffer(),
|
|
38
|
+
new Vector(this.newNoteHashes),
|
|
39
|
+
this.dataStartIndexForTx,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
43
|
+
const reader = BufferReader.asReader(buffer);
|
|
44
|
+
return new DeferredNoteDao(
|
|
45
|
+
reader.readObject(Point),
|
|
46
|
+
reader.readObject(Note),
|
|
47
|
+
reader.readObject(AztecAddress),
|
|
48
|
+
reader.readObject(Fr),
|
|
49
|
+
reader.readObject(Fr),
|
|
50
|
+
reader.readObject(TxHash),
|
|
51
|
+
reader.readVector(Fr),
|
|
52
|
+
reader.readNumber(),
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './pxe_database.js';
|