@proto-kit/protocol 0.1.1-develop.651 → 0.1.1-develop.833
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/LICENSE.md +201 -201
- package/dist/blockmodules/AccountStateModule.d.ts +37 -37
- package/dist/blockmodules/AccountStateModule.js +43 -43
- package/dist/blockmodules/BlockHeightHook.d.ts +6 -6
- package/dist/blockmodules/BlockHeightHook.js +15 -15
- package/dist/blockmodules/LastStateRootBlockHook.d.ts +7 -7
- package/dist/blockmodules/LastStateRootBlockHook.js +15 -15
- package/dist/blockmodules/NoopBlockHook.d.ts +8 -8
- package/dist/blockmodules/NoopBlockHook.js +9 -9
- package/dist/blockmodules/NoopSettlementHook.d.ts +5 -5
- package/dist/blockmodules/NoopSettlementHook.d.ts.map +1 -1
- package/dist/blockmodules/NoopSettlementHook.js +18 -18
- package/dist/blockmodules/NoopTransactionHook.d.ts +5 -5
- package/dist/blockmodules/NoopTransactionHook.js +4 -4
- package/dist/hooks/AccountStateHook.d.ts +19 -5
- package/dist/hooks/AccountStateHook.d.ts.map +1 -1
- package/dist/hooks/AccountStateHook.js +9 -10
- package/dist/hooks/BlockHeightHook.d.ts +2 -2
- package/dist/hooks/BlockHeightHook.d.ts.map +1 -1
- package/dist/hooks/BlockHeightHook.js +3 -3
- package/dist/hooks/LastStateRootBlockHook.d.ts +2 -2
- package/dist/hooks/LastStateRootBlockHook.d.ts.map +1 -1
- package/dist/hooks/LastStateRootBlockHook.js +2 -2
- package/dist/hooks/NoopBlockHook.d.ts +3 -3
- package/dist/hooks/NoopBlockHook.d.ts.map +1 -1
- package/dist/hooks/NoopBlockHook.js +2 -2
- package/dist/hooks/NoopSettlementHook.d.ts +1 -1
- package/dist/hooks/NoopSettlementHook.d.ts.map +1 -1
- package/dist/hooks/NoopSettlementHook.js +2 -3
- package/dist/hooks/NoopTransactionHook.d.ts +1 -1
- package/dist/hooks/NoopTransactionHook.d.ts.map +1 -1
- package/dist/hooks/NoopTransactionHook.js +4 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -2
- package/dist/model/MethodPublicOutput.d.ts +68 -34
- package/dist/model/MethodPublicOutput.d.ts.map +1 -1
- package/dist/model/Option.d.ts +39 -20
- package/dist/model/Option.d.ts.map +1 -1
- package/dist/model/Option.js +1 -0
- package/dist/model/Path.d.ts +1 -1
- package/dist/model/Path.d.ts.map +1 -1
- package/dist/model/Path.js +0 -2
- package/dist/model/StateTransition.d.ts +48 -12
- package/dist/model/StateTransition.d.ts.map +1 -1
- package/dist/model/StateTransitionProvableBatch.d.ts +71 -14
- package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -1
- package/dist/model/StateTransitionReduction.d.ts +2 -2
- package/dist/model/StateTransitionReduction.js +26 -26
- package/dist/model/network/NetworkState.d.ts +73 -18
- package/dist/model/network/NetworkState.d.ts.map +1 -1
- package/dist/model/network/NetworkState.js +2 -2
- package/dist/model/transaction/ProtocolTransaction.d.ts +69 -69
- package/dist/model/transaction/ProtocolTransaction.js +18 -18
- package/dist/model/transaction/RuntimeTransaction.d.ts +50 -16
- package/dist/model/transaction/RuntimeTransaction.d.ts.map +1 -1
- package/dist/model/transaction/RuntimeTransaction.js +4 -4
- package/dist/model/transaction/SignedTransaction.d.ts +39 -3
- package/dist/model/transaction/SignedTransaction.d.ts.map +1 -1
- package/dist/model/transaction/SignedTransaction.js +1 -1
- package/dist/model/transaction/ValueOption.d.ts +86 -32
- package/dist/model/transaction/ValueOption.d.ts.map +1 -1
- package/dist/model/transaction/ValueOption.js +1 -1
- package/dist/protocol/Protocol.d.ts +6 -5
- package/dist/protocol/Protocol.d.ts.map +1 -1
- package/dist/protocol/Protocol.js +12 -3
- package/dist/protocol/ProtocolEnvironment.d.ts +3 -3
- package/dist/protocol/ProtocolEnvironment.d.ts.map +1 -1
- package/dist/protocol/ProtocolModule.d.ts +1 -0
- package/dist/protocol/ProtocolModule.d.ts.map +1 -1
- package/dist/protocol/ProtocolModule.js +3 -1
- package/dist/protocol/ProvableBlockHook.d.ts +2 -2
- package/dist/protocol/ProvableBlockHook.d.ts.map +1 -1
- package/dist/protocol/ProvableTransactionHook.d.ts +1 -1
- package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -1
- package/dist/prover/block/BlockProvable.d.ts +240 -98
- package/dist/prover/block/BlockProvable.d.ts.map +1 -1
- package/dist/prover/block/BlockProver.d.ts +8 -8
- package/dist/prover/block/BlockProver.d.ts.map +1 -1
- package/dist/prover/block/BlockProver.js +48 -40
- package/dist/prover/block/BlockTransactionPosition.d.ts +35 -35
- package/dist/prover/block/BlockTransactionPosition.js +25 -25
- package/dist/prover/block/BundleTransactionPosition.d.ts +36 -0
- package/dist/prover/block/BundleTransactionPosition.d.ts.map +1 -0
- package/dist/prover/block/BundleTransactionPosition.js +25 -0
- package/dist/prover/block/SplitPrefilledStateService.d.ts +11 -0
- package/dist/prover/block/SplitPrefilledStateService.d.ts.map +1 -0
- package/dist/prover/block/SplitPrefilledStateService.js +1 -0
- package/dist/prover/block/SplitStateService.d.ts +11 -0
- package/dist/prover/block/SplitStateService.d.ts.map +1 -0
- package/dist/prover/block/SplitStateService.js +1 -0
- package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts +35 -16
- package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts.map +1 -1
- package/dist/prover/block/acummulators/BlockHashMerkleTree.d.ts +45 -0
- package/dist/prover/block/acummulators/BlockHashMerkleTree.d.ts.map +1 -0
- package/dist/prover/block/acummulators/BlockHashMerkleTree.js +16 -0
- package/dist/prover/statetransition/StateTransitionProvable.d.ts +116 -58
- package/dist/prover/statetransition/StateTransitionProvable.d.ts.map +1 -1
- package/dist/prover/statetransition/StateTransitionProver.d.ts +8 -8
- package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
- package/dist/prover/statetransition/StateTransitionProver.js +17 -22
- package/dist/prover/statetransition/StateTransitionWitnessProvider.js +1 -2
- package/dist/prover/statetransition/StateTransitionWitnessProviderReference.js +1 -2
- package/dist/settlement/DispatchContract.d.ts +20 -0
- package/dist/settlement/DispatchContract.d.ts.map +1 -0
- package/dist/settlement/DispatchContract.js +112 -0
- package/dist/settlement/OutgoingMessageArgument.d.ts +109 -0
- package/dist/settlement/OutgoingMessageArgument.d.ts.map +1 -0
- package/dist/settlement/OutgoingMessageArgument.js +32 -0
- package/dist/settlement/ProvableSettlementHook.d.ts +25 -25
- package/dist/settlement/ProvableSettlementHook.js +3 -3
- package/dist/settlement/SettlementContract.d.ts +229 -229
- package/dist/settlement/SettlementContract.js +346 -346
- package/dist/settlement/SettlementContractModule.d.ts +1 -0
- package/dist/settlement/SettlementContractModule.d.ts.map +1 -1
- package/dist/settlement/SettlementContractModule.js +7 -4
- package/dist/settlement/contracts/DispatchContract.d.ts +21 -0
- package/dist/settlement/contracts/DispatchContract.d.ts.map +1 -0
- package/dist/settlement/contracts/DispatchContract.js +111 -0
- package/dist/settlement/contracts/DispatchContractProtocolModule.js +1 -2
- package/dist/settlement/contracts/DispatchSmartContract.d.ts +8 -8
- package/dist/settlement/contracts/DispatchSmartContract.d.ts.map +1 -1
- package/dist/settlement/contracts/DispatchSmartContract.js +14 -14
- package/dist/settlement/contracts/SettlementContract.d.ts +19 -0
- package/dist/settlement/contracts/SettlementContract.d.ts.map +1 -0
- package/dist/settlement/contracts/SettlementContract.js +43 -0
- package/dist/settlement/contracts/SettlementContractProtocolModule.d.ts.map +1 -1
- package/dist/settlement/contracts/SettlementContractProtocolModule.js +4 -4
- package/dist/settlement/contracts/SettlementSmartContract.d.ts +18 -17
- package/dist/settlement/contracts/SettlementSmartContract.d.ts.map +1 -1
- package/dist/settlement/contracts/SettlementSmartContract.js +46 -35
- package/dist/settlement/messages/Deposit.d.ts +29 -4
- package/dist/settlement/messages/Deposit.d.ts.map +1 -1
- package/dist/settlement/messages/OutgoingMessageArgument.d.ts +94 -14
- package/dist/settlement/messages/OutgoingMessageArgument.d.ts.map +1 -1
- package/dist/settlement/messages/Withdrawal.d.ts +29 -4
- package/dist/settlement/messages/Withdrawal.d.ts.map +1 -1
- package/dist/settlement/modularity/ProvableSettlementHook.d.ts +2 -2
- package/dist/settlement/modularity/ProvableSettlementHook.d.ts.map +1 -1
- package/dist/settlement/modules/NetworkStateSettlementModule.d.ts +1 -1
- package/dist/settlement/modules/NetworkStateSettlementModule.d.ts.map +1 -1
- package/dist/settlement/modules/NetworkStateSettlementModule.js +3 -1
- package/dist/state/State.d.ts +4 -3
- package/dist/state/State.d.ts.map +1 -1
- package/dist/state/State.js +16 -11
- package/dist/state/StateMap.d.ts +3 -3
- package/dist/state/StateMap.d.ts.map +1 -1
- package/dist/state/StateMap.js +4 -5
- package/dist/state/StateService.d.ts +3 -3
- package/dist/state/StateService.d.ts.map +1 -1
- package/dist/state/StateServiceProvider.d.ts +3 -3
- package/dist/state/StateServiceProvider.d.ts.map +1 -1
- package/dist/state/StateServiceProvider.js +1 -3
- package/dist/state/assert/assert.d.ts +1 -1
- package/dist/state/assert/assert.d.ts.map +1 -1
- package/dist/state/assert/assert.js +4 -1
- package/dist/state/context/ProtocolMethodExecutionContext.d.ts +1 -1
- package/dist/state/context/ProtocolMethodExecutionContext.d.ts.map +1 -1
- package/dist/state/context/ProtocolMethodExecutionContext.js +1 -1
- package/dist/state/context/RuntimeMethodExecutionContext.d.ts +54 -3
- package/dist/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -1
- package/dist/state/context/RuntimeMethodExecutionContext.js +4 -5
- package/dist/state/context/TransitionMethodExecutionContext.d.ts.map +1 -1
- package/dist/state/context/TransitionMethodExecutionContext.js +0 -1
- package/dist/state/context/contextExecution.d.ts +14 -0
- package/dist/state/context/contextExecution.d.ts.map +1 -0
- package/dist/state/context/contextExecution.js +30 -0
- package/dist/state/protocol/ProtocolState.d.ts.map +1 -1
- package/dist/state/protocol/ProtocolState.js +0 -3
- package/dist/utils/AppliedBatchHashList.d.ts +17 -0
- package/dist/utils/AppliedBatchHashList.d.ts.map +1 -0
- package/dist/utils/AppliedBatchHashList.js +24 -0
- package/dist/utils/MinaPrefixedProvableHashList.d.ts.map +1 -1
- package/dist/utils/MinaPrefixedProvableHashList.js +1 -0
- package/dist/utils/ProvableHashList.d.ts +1 -1
- package/dist/utils/StateTransitionReductionList.d.ts +1 -1
- package/dist/utils/StateTransitionReductionList.d.ts.map +1 -1
- package/dist/utils/StateTransitionReductionList.js +3 -3
- package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts +11 -0
- package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts.map +1 -0
- package/dist/utils/merkletree/InMemoryMerkleTreeStorage.js +12 -0
- package/dist/utils/merkletree/MerkleTreeStore.d.ts +5 -0
- package/dist/utils/merkletree/MerkleTreeStore.d.ts.map +1 -0
- package/dist/utils/merkletree/MerkleTreeStore.js +1 -0
- package/dist/utils/merkletree/RollupMerkleTree.d.ts +133 -0
- package/dist/utils/merkletree/RollupMerkleTree.d.ts.map +1 -0
- package/dist/utils/merkletree/RollupMerkleTree.js +255 -0
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.d.ts.map +1 -1
- package/dist/utils/utils.js +5 -10
- package/package.json +5 -5
- package/src/hooks/AccountStateHook.ts +11 -9
- package/src/hooks/BlockHeightHook.ts +3 -4
- package/src/hooks/LastStateRootBlockHook.ts +4 -4
- package/src/hooks/NoopBlockHook.ts +6 -5
- package/src/hooks/NoopSettlementHook.ts +2 -2
- package/src/hooks/NoopTransactionHook.ts +5 -1
- package/src/index.ts +0 -2
- package/src/model/MethodPublicOutput.ts +2 -2
- package/src/model/Option.ts +5 -1
- package/src/model/Path.ts +1 -3
- package/src/model/StateTransition.ts +1 -1
- package/src/model/Transaction.ts +2 -0
- package/src/model/network/NetworkState.ts +2 -2
- package/src/model/transaction/RuntimeTransaction.ts +5 -12
- package/src/model/transaction/SignedTransaction.ts +1 -8
- package/src/model/transaction/ValueOption.ts +1 -1
- package/src/protocol/Protocol.ts +24 -13
- package/src/protocol/ProtocolEnvironment.ts +7 -6
- package/src/protocol/ProtocolModule.ts +5 -2
- package/src/protocol/ProvableBlockHook.ts +3 -3
- package/src/protocol/ProvableTransactionHook.ts +4 -2
- package/src/protocol/TransitioningProtocolModule.ts +1 -1
- package/src/prover/block/BlockProvable.ts +3 -3
- package/src/prover/block/BlockProver.ts +78 -55
- package/src/prover/block/accummulators/BlockHashMerkleTree.ts +1 -1
- package/src/prover/statetransition/StateTransitionProvable.ts +3 -3
- package/src/prover/statetransition/StateTransitionProver.ts +23 -27
- package/src/settlement/ContractModule.ts +1 -1
- package/src/settlement/SettlementContractModule.ts +14 -9
- package/src/settlement/contracts/DispatchSmartContract.ts +15 -14
- package/src/settlement/contracts/SettlementContractProtocolModule.ts +6 -4
- package/src/settlement/contracts/SettlementSmartContract.ts +55 -34
- package/src/settlement/messages/OutgoingMessageArgument.ts +1 -0
- package/src/settlement/modularity/ProvableSettlementHook.ts +3 -4
- package/src/settlement/modules/NetworkStateSettlementModule.ts +5 -2
- package/src/state/State.ts +19 -12
- package/src/state/StateMap.ts +4 -5
- package/src/state/StateService.ts +3 -3
- package/src/state/StateServiceProvider.ts +6 -7
- package/src/state/assert/assert.ts +6 -2
- package/src/state/context/RuntimeMethodExecutionContext.ts +5 -2
- package/src/state/context/TransitionMethodExecutionContext.ts +0 -1
- package/src/state/protocol/ProtocolState.ts +1 -4
- package/src/utils/MinaPrefixedProvableHashList.ts +2 -1
- package/src/utils/ProvableHashList.ts +1 -1
- package/src/utils/StateTransitionReductionList.ts +4 -3
- package/src/utils/utils.ts +4 -9
- package/test/BlockProver.test.ts +110 -128
- package/test/Protocol.test.ts +8 -20
- package/test/State.test.ts +8 -8
- package/test/StateTransition.test.ts +56 -66
- package/test/TestingProtocol.ts +48 -0
- package/test/model/Option.test.ts +72 -0
- package/{src → test}/state/assert/assert.test.ts +4 -4
- package/test/tsconfig.json +7 -0
- package/test/utils/ProvableReductionHashList.test.ts +5 -2
- package/test/utils.test.ts +0 -3
- package/dist/hooks/TransactionFeeHook.d.ts +0 -16
- package/dist/hooks/TransactionFeeHook.d.ts.map +0 -1
- package/dist/hooks/TransactionFeeHook.js +0 -39
- package/src/hooks/SequenceStateTransactionModule.ts +0 -25
- package/src/model/Option.test.ts +0 -21
- package/src/state/context/ProtocolMethodExecutionContext.ts +0 -36
- package/test/Option.test.ts +0 -64
- package/test/settlement/SettlementContract.test.ts +0 -45
- package/tsconfig.test.json +0 -9
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Bool, Field } from "o1js";
|
|
2
|
+
import { MerkleTreeStore } from "./MerkleTreeStore";
|
|
3
|
+
export { RollupMerkleTree, RollupMerkleWitness };
|
|
4
|
+
export { maybeSwap };
|
|
5
|
+
declare const RollupMerkleWitness_base: (new (value: {
|
|
6
|
+
path: import("o1js/dist/node/lib/field").Field[];
|
|
7
|
+
isLeft: import("o1js/dist/node/lib/bool").Bool[];
|
|
8
|
+
}) => {
|
|
9
|
+
path: import("o1js/dist/node/lib/field").Field[];
|
|
10
|
+
isLeft: import("o1js/dist/node/lib/bool").Bool[];
|
|
11
|
+
}) & {
|
|
12
|
+
_isStruct: true;
|
|
13
|
+
} & import("o1js/dist/node/snarky").ProvablePure<{
|
|
14
|
+
path: import("o1js/dist/node/lib/field").Field[];
|
|
15
|
+
isLeft: import("o1js/dist/node/lib/bool").Bool[];
|
|
16
|
+
}> & {
|
|
17
|
+
toInput: (x: {
|
|
18
|
+
path: import("o1js/dist/node/lib/field").Field[];
|
|
19
|
+
isLeft: import("o1js/dist/node/lib/bool").Bool[];
|
|
20
|
+
}) => {
|
|
21
|
+
fields?: import("o1js/dist/node/lib/field").Field[] | undefined;
|
|
22
|
+
packed?: [import("o1js/dist/node/lib/field").Field, number][] | undefined;
|
|
23
|
+
};
|
|
24
|
+
toJSON: (x: {
|
|
25
|
+
path: import("o1js/dist/node/lib/field").Field[];
|
|
26
|
+
isLeft: import("o1js/dist/node/lib/bool").Bool[];
|
|
27
|
+
}) => {
|
|
28
|
+
path: string[];
|
|
29
|
+
isLeft: boolean[];
|
|
30
|
+
};
|
|
31
|
+
fromJSON: (x: {
|
|
32
|
+
path: string[];
|
|
33
|
+
isLeft: boolean[];
|
|
34
|
+
}) => {
|
|
35
|
+
path: import("o1js/dist/node/lib/field").Field[];
|
|
36
|
+
isLeft: import("o1js/dist/node/lib/bool").Bool[];
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* The {@link BaseMerkleWitness} class defines a circuit-compatible base class
|
|
41
|
+
* for [Merkle Witness'](https://computersciencewiki.org/index.php/Merkle_proof).
|
|
42
|
+
*/
|
|
43
|
+
declare class RollupMerkleWitness extends RollupMerkleWitness_base {
|
|
44
|
+
static height: number;
|
|
45
|
+
height(): number;
|
|
46
|
+
/**
|
|
47
|
+
* Calculates a root depending on the leaf value.
|
|
48
|
+
* @param leaf Value of the leaf node that belongs to this Witness.
|
|
49
|
+
* @returns The calculated root.
|
|
50
|
+
*/
|
|
51
|
+
calculateRoot(hash: Field): Field;
|
|
52
|
+
/**
|
|
53
|
+
* Calculates the index of the leaf node that belongs to this Witness.
|
|
54
|
+
* @returns Index of the leaf.
|
|
55
|
+
*/
|
|
56
|
+
calculateIndex(): Field;
|
|
57
|
+
toShortenedEntries(): string[];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree) is a binary tree in
|
|
61
|
+
* which every leaf is the cryptography hash of a piece of data,
|
|
62
|
+
* and every node is the hash of the concatenation of its two child nodes.
|
|
63
|
+
*
|
|
64
|
+
* A Merkle Tree allows developers to easily and securely verify
|
|
65
|
+
* the integrity of large amounts of data.
|
|
66
|
+
*
|
|
67
|
+
* Take a look at our [documentation](https://docs.minaprotocol.com/en/zkapps)
|
|
68
|
+
* on how to use Merkle Trees in combination with zkApps and
|
|
69
|
+
* zero knowledge programming!
|
|
70
|
+
*
|
|
71
|
+
* Levels are indexed from leaves (level 0) to root (level N - 1).
|
|
72
|
+
*/
|
|
73
|
+
declare class RollupMerkleTree {
|
|
74
|
+
static height: number;
|
|
75
|
+
static EMPTY_ROOT: bigint;
|
|
76
|
+
static get leafCount(): bigint;
|
|
77
|
+
private readonly zeroes;
|
|
78
|
+
readonly store: MerkleTreeStore;
|
|
79
|
+
constructor(store: MerkleTreeStore);
|
|
80
|
+
/**
|
|
81
|
+
* Returns a node which lives at a given index and level.
|
|
82
|
+
* @param level Level of the node.
|
|
83
|
+
* @param index Index of the node.
|
|
84
|
+
* @returns The data of the node.
|
|
85
|
+
*/
|
|
86
|
+
getNode(level: number, index: bigint): Field;
|
|
87
|
+
/**
|
|
88
|
+
* Returns the root of the [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree).
|
|
89
|
+
* @returns The root of the Merkle Tree.
|
|
90
|
+
*/
|
|
91
|
+
getRoot(): Field;
|
|
92
|
+
private setNode;
|
|
93
|
+
/**
|
|
94
|
+
* TODO: if this is passed an index bigger than the max, it will set a couple
|
|
95
|
+
* of out-of-bounds nodes but not affect the real Merkle root. OK?
|
|
96
|
+
*/
|
|
97
|
+
/**
|
|
98
|
+
* Sets the value of a leaf node at a given index to a given value.
|
|
99
|
+
* @param index Position of the leaf node.
|
|
100
|
+
* @param leaf New value.
|
|
101
|
+
*/
|
|
102
|
+
setLeaf(index: bigint, leaf: Field): void;
|
|
103
|
+
/**
|
|
104
|
+
* Returns the witness (also known as
|
|
105
|
+
* [Merkle Proof or Merkle Witness](https://computersciencewiki.org/index.php/Merkle_proof))
|
|
106
|
+
* for the leaf at the given index.
|
|
107
|
+
* @param index Position of the leaf node.
|
|
108
|
+
* @returns The witness that belongs to the leaf.
|
|
109
|
+
*/
|
|
110
|
+
getWitness(index: bigint): RollupMerkleWitness;
|
|
111
|
+
/**
|
|
112
|
+
* Fills all leaves of the tree.
|
|
113
|
+
* @param leaves Values to fill the leaves with.
|
|
114
|
+
*/
|
|
115
|
+
fill(leaves: Field[]): void;
|
|
116
|
+
/**
|
|
117
|
+
* Returns the amount of leaf nodes.
|
|
118
|
+
* @returns Amount of leaf nodes.
|
|
119
|
+
*/
|
|
120
|
+
get leafCount(): bigint;
|
|
121
|
+
}
|
|
122
|
+
export declare namespace MerkleTreeUtils {
|
|
123
|
+
function normalizeKey(key: Field): Field;
|
|
124
|
+
function checkMembership(witness: RollupMerkleWitness, root: Field, key: Field, value: Field): Bool;
|
|
125
|
+
function checkMembershipGetRoots(witness: RollupMerkleWitness, root: Field, key: Field, value: Field): [Bool, Field, Field];
|
|
126
|
+
function computeRoot(witness: RollupMerkleWitness, value: Field): Field;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* More efficient version of `maybeSwapBad` which
|
|
130
|
+
* reuses an intermediate variable
|
|
131
|
+
*/
|
|
132
|
+
declare function maybeSwap(b: Bool, x: Field, y: Field): [Field, Field];
|
|
133
|
+
//# sourceMappingURL=RollupMerkleTree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RollupMerkleTree.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/RollupMerkleTree.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,KAAK,EAA8B,MAAM,MAAM,CAAC;AAK/D,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKpD,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,CAAC;AAIjD,OAAO,EAAE,SAAS,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAErB;;;GAGG;AACH,cAAM,mBAAoB,SAAQ,wBAGhC;IACA,OAAc,MAAM,SAAO;IAEpB,MAAM,IAAI,MAAM;IAIvB;;;;OAIG;IACI,aAAa,CAAC,IAAI,EAAE,KAAK,GAAG,KAAK;IAaxC;;;OAGG;IACI,cAAc,IAAI,KAAK;IAkBvB,kBAAkB;CAO1B;AAED;;;;;;;;;;;;;GAaG;AACH,cAAM,gBAAgB;IACpB,OAAc,MAAM,SAAO;IAE3B,OAAc,UAAU,SAIV;IAEd,WAAkB,SAAS,IAAI,MAAM,CAEpC;IAED,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAElC,SAAgB,KAAK,EAAE,eAAe,CAAC;gBAEpB,KAAK,EAAE,eAAe;IAYzC;;;;;OAKG;IAEI,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,KAAK;IAInD;;;OAGG;IAEI,OAAO,IAAI,KAAK;IAMvB,OAAO,CAAC,OAAO;IAIf;;;OAGG;IAEH;;;;OAIG;IAEI,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK;IAgBzC;;;;;;OAMG;IACI,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,mBAAmB;IAqBrD;;;OAGG;IAEI,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE;IAM3B;;;OAGG;IACH,IAAW,SAAS,IAAI,MAAM,CAE7B;CACF;AAGD,yBAAiB,eAAe,CAAC;IAC/B,SAAgB,YAAY,CAAC,GAAG,EAAE,KAAK,GAAG,KAAK,CAS9C;IAED,SAAgB,eAAe,CAC7B,OAAO,EAAE,mBAAmB,EAC5B,IAAI,EAAE,KAAK,EACX,GAAG,EAAE,KAAK,EACV,KAAK,EAAE,KAAK,GACX,IAAI,CAKN;IAED,SAAgB,uBAAuB,CACrC,OAAO,EAAE,mBAAmB,EAC5B,IAAI,EAAE,KAAK,EACX,GAAG,EAAE,KAAK,EACV,KAAK,EAAE,KAAK,GACX,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,CAKtB;IAED,SAAgB,WAAW,CACzB,OAAO,EAAE,mBAAmB,EAC5B,KAAK,EAAE,KAAK,GACX,KAAK,CAEP;CACF;AAED;;;GAGG;AACH,iBAAS,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAK9D"}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
/* eslint-disable id-length */
|
|
11
|
+
/* eslint-disable line-comment-position */
|
|
12
|
+
/* eslint-disable no-inline-comments */
|
|
13
|
+
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
14
|
+
import { Bool, Field, Poseidon, Provable, Struct } from "o1js";
|
|
15
|
+
import { range } from "@proto-kit/common";
|
|
16
|
+
import { notInCircuit } from "../utils";
|
|
17
|
+
import { InMemoryMerkleTreeStorage } from "./InMemoryMerkleTreeStorage";
|
|
18
|
+
// external API
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
20
|
+
export { RollupMerkleTree, RollupMerkleWitness };
|
|
21
|
+
// internal API
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
23
|
+
export { maybeSwap };
|
|
24
|
+
/**
|
|
25
|
+
* The {@link BaseMerkleWitness} class defines a circuit-compatible base class
|
|
26
|
+
* for [Merkle Witness'](https://computersciencewiki.org/index.php/Merkle_proof).
|
|
27
|
+
*/
|
|
28
|
+
class RollupMerkleWitness extends Struct({
|
|
29
|
+
path: Provable.Array(Field, 256 - 1),
|
|
30
|
+
isLeft: Provable.Array(Bool, 256 - 1),
|
|
31
|
+
}) {
|
|
32
|
+
height() {
|
|
33
|
+
return RollupMerkleWitness.height;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Calculates a root depending on the leaf value.
|
|
37
|
+
* @param leaf Value of the leaf node that belongs to this Witness.
|
|
38
|
+
* @returns The calculated root.
|
|
39
|
+
*/
|
|
40
|
+
calculateRoot(hash) {
|
|
41
|
+
const n = this.height();
|
|
42
|
+
for (let index = 1; index < n; ++index) {
|
|
43
|
+
const isLeft = this.isLeft[index - 1];
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
45
|
+
const [left, right] = maybeSwap(isLeft, hash, this.path[index - 1]);
|
|
46
|
+
hash = Poseidon.hash([left, right]);
|
|
47
|
+
}
|
|
48
|
+
return hash;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Calculates the index of the leaf node that belongs to this Witness.
|
|
52
|
+
* @returns Index of the leaf.
|
|
53
|
+
*/
|
|
54
|
+
calculateIndex() {
|
|
55
|
+
let powerOfTwo = Field(1);
|
|
56
|
+
let index = Field(0);
|
|
57
|
+
const n = this.height();
|
|
58
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
59
|
+
for (let index_ = 1; index_ < n; ++index_) {
|
|
60
|
+
index = Provable.if(this.isLeft[index_ - 1], index, index.add(powerOfTwo));
|
|
61
|
+
powerOfTwo = powerOfTwo.mul(2);
|
|
62
|
+
}
|
|
63
|
+
return index;
|
|
64
|
+
}
|
|
65
|
+
toShortenedEntries() {
|
|
66
|
+
return range(0, 5)
|
|
67
|
+
.concat(range(251, 255))
|
|
68
|
+
.map((index) => [this.path[index].toString(), this.isLeft[index].toString()].toString());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
RollupMerkleWitness.height = 256;
|
|
72
|
+
/**
|
|
73
|
+
* A [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree) is a binary tree in
|
|
74
|
+
* which every leaf is the cryptography hash of a piece of data,
|
|
75
|
+
* and every node is the hash of the concatenation of its two child nodes.
|
|
76
|
+
*
|
|
77
|
+
* A Merkle Tree allows developers to easily and securely verify
|
|
78
|
+
* the integrity of large amounts of data.
|
|
79
|
+
*
|
|
80
|
+
* Take a look at our [documentation](https://docs.minaprotocol.com/en/zkapps)
|
|
81
|
+
* on how to use Merkle Trees in combination with zkApps and
|
|
82
|
+
* zero knowledge programming!
|
|
83
|
+
*
|
|
84
|
+
* Levels are indexed from leaves (level 0) to root (level N - 1).
|
|
85
|
+
*/
|
|
86
|
+
class RollupMerkleTree {
|
|
87
|
+
static get leafCount() {
|
|
88
|
+
return 2n ** BigInt(RollupMerkleTree.height - 1);
|
|
89
|
+
}
|
|
90
|
+
constructor(store) {
|
|
91
|
+
this.store = store;
|
|
92
|
+
// eslint-disable-next-line @shopify/prefer-class-properties
|
|
93
|
+
this.zeroes = [0n];
|
|
94
|
+
for (let index = 1; index < RollupMerkleTree.height; index += 1) {
|
|
95
|
+
const previousLevel = Field(this.zeroes[index - 1]);
|
|
96
|
+
this.zeroes.push(Poseidon.hash([previousLevel, previousLevel]).toBigInt());
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns a node which lives at a given index and level.
|
|
101
|
+
* @param level Level of the node.
|
|
102
|
+
* @param index Index of the node.
|
|
103
|
+
* @returns The data of the node.
|
|
104
|
+
*/
|
|
105
|
+
getNode(level, index) {
|
|
106
|
+
return Field(this.store.getNode(index, level) ?? this.zeroes[level]);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns the root of the [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree).
|
|
110
|
+
* @returns The root of the Merkle Tree.
|
|
111
|
+
*/
|
|
112
|
+
getRoot() {
|
|
113
|
+
return this.getNode(RollupMerkleTree.height - 1, 0n);
|
|
114
|
+
}
|
|
115
|
+
// eslint-disable-next-line no-warning-comments
|
|
116
|
+
// TODO: this allows to set a node at an index larger than the size. OK?
|
|
117
|
+
setNode(level, index, value) {
|
|
118
|
+
this.store.setNode(index, level, value.toBigInt());
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* TODO: if this is passed an index bigger than the max, it will set a couple
|
|
122
|
+
* of out-of-bounds nodes but not affect the real Merkle root. OK?
|
|
123
|
+
*/
|
|
124
|
+
/**
|
|
125
|
+
* Sets the value of a leaf node at a given index to a given value.
|
|
126
|
+
* @param index Position of the leaf node.
|
|
127
|
+
* @param leaf New value.
|
|
128
|
+
*/
|
|
129
|
+
setLeaf(index, leaf) {
|
|
130
|
+
if (index >= this.leafCount) {
|
|
131
|
+
index %= this.leafCount;
|
|
132
|
+
}
|
|
133
|
+
this.setNode(0, index, leaf);
|
|
134
|
+
let currentIndex = index;
|
|
135
|
+
for (let level = 1; level < RollupMerkleTree.height; level += 1) {
|
|
136
|
+
currentIndex /= 2n;
|
|
137
|
+
const left = this.getNode(level - 1, currentIndex * 2n);
|
|
138
|
+
const right = this.getNode(level - 1, currentIndex * 2n + 1n);
|
|
139
|
+
this.setNode(level, currentIndex, Poseidon.hash([left, right]));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns the witness (also known as
|
|
144
|
+
* [Merkle Proof or Merkle Witness](https://computersciencewiki.org/index.php/Merkle_proof))
|
|
145
|
+
* for the leaf at the given index.
|
|
146
|
+
* @param index Position of the leaf node.
|
|
147
|
+
* @returns The witness that belongs to the leaf.
|
|
148
|
+
*/
|
|
149
|
+
getWitness(index) {
|
|
150
|
+
if (index >= this.leafCount) {
|
|
151
|
+
throw new Error("Index too large for merkle height");
|
|
152
|
+
}
|
|
153
|
+
const path = [];
|
|
154
|
+
const isLefts = [];
|
|
155
|
+
for (let level = 0; level < RollupMerkleTree.height - 1; level += 1) {
|
|
156
|
+
const isLeft = index % 2n === 0n;
|
|
157
|
+
const sibling = this.getNode(level, isLeft ? index + 1n : index - 1n);
|
|
158
|
+
isLefts.push(Bool(isLeft));
|
|
159
|
+
path.push(sibling);
|
|
160
|
+
index /= 2n;
|
|
161
|
+
}
|
|
162
|
+
return new RollupMerkleWitness({
|
|
163
|
+
isLeft: isLefts,
|
|
164
|
+
path,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
// eslint-disable-next-line no-warning-comments, max-len
|
|
168
|
+
// TODO: should this take an optional offset? should it fail if the array is too long?
|
|
169
|
+
/**
|
|
170
|
+
* Fills all leaves of the tree.
|
|
171
|
+
* @param leaves Values to fill the leaves with.
|
|
172
|
+
*/
|
|
173
|
+
fill(leaves) {
|
|
174
|
+
leaves.forEach((value, index) => {
|
|
175
|
+
this.setLeaf(BigInt(index), value);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Returns the amount of leaf nodes.
|
|
180
|
+
* @returns Amount of leaf nodes.
|
|
181
|
+
*/
|
|
182
|
+
get leafCount() {
|
|
183
|
+
return RollupMerkleTree.leafCount;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
RollupMerkleTree.height = 256;
|
|
187
|
+
RollupMerkleTree.EMPTY_ROOT = new RollupMerkleTree(new InMemoryMerkleTreeStorage())
|
|
188
|
+
.getRoot()
|
|
189
|
+
.toBigInt();
|
|
190
|
+
__decorate([
|
|
191
|
+
notInCircuit(),
|
|
192
|
+
__metadata("design:type", Function),
|
|
193
|
+
__metadata("design:paramtypes", [Number, BigInt]),
|
|
194
|
+
__metadata("design:returntype", Field)
|
|
195
|
+
], RollupMerkleTree.prototype, "getNode", null);
|
|
196
|
+
__decorate([
|
|
197
|
+
notInCircuit(),
|
|
198
|
+
__metadata("design:type", Function),
|
|
199
|
+
__metadata("design:paramtypes", []),
|
|
200
|
+
__metadata("design:returntype", Field)
|
|
201
|
+
], RollupMerkleTree.prototype, "getRoot", null);
|
|
202
|
+
__decorate([
|
|
203
|
+
notInCircuit(),
|
|
204
|
+
__metadata("design:type", Function),
|
|
205
|
+
__metadata("design:paramtypes", [BigInt, Field]),
|
|
206
|
+
__metadata("design:returntype", void 0)
|
|
207
|
+
], RollupMerkleTree.prototype, "setLeaf", null);
|
|
208
|
+
__decorate([
|
|
209
|
+
notInCircuit(),
|
|
210
|
+
__metadata("design:type", Function),
|
|
211
|
+
__metadata("design:paramtypes", [Array]),
|
|
212
|
+
__metadata("design:returntype", void 0)
|
|
213
|
+
], RollupMerkleTree.prototype, "fill", null);
|
|
214
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
215
|
+
export var MerkleTreeUtils;
|
|
216
|
+
(function (MerkleTreeUtils) {
|
|
217
|
+
function normalizeKey(key) {
|
|
218
|
+
// if(NJORD_MERKLE_TREE_HEIGHT < 256){
|
|
219
|
+
// return fieldMod(key, Field(RollupMerkleTree.leafCount).toConstant())
|
|
220
|
+
// eslint-disable-next-line max-len
|
|
221
|
+
// // return modPower2(key, RollupMerkleTree.height - 1) //TODO Fix modPower2
|
|
222
|
+
// }else{
|
|
223
|
+
return key;
|
|
224
|
+
// }
|
|
225
|
+
}
|
|
226
|
+
MerkleTreeUtils.normalizeKey = normalizeKey;
|
|
227
|
+
function checkMembership(witness, root, key, value) {
|
|
228
|
+
const root2 = witness.calculateRoot(value);
|
|
229
|
+
const key2 = witness.calculateIndex();
|
|
230
|
+
key.assertEquals(key2, "Keys of MerkleWitness does not match");
|
|
231
|
+
return root.equals(root2);
|
|
232
|
+
}
|
|
233
|
+
MerkleTreeUtils.checkMembership = checkMembership;
|
|
234
|
+
function checkMembershipGetRoots(witness, root, key, value) {
|
|
235
|
+
const root2 = witness.calculateRoot(value);
|
|
236
|
+
const key2 = witness.calculateIndex();
|
|
237
|
+
key.assertEquals(key2, "Keys of MerkleWitness does not match");
|
|
238
|
+
return [root.equals(root2), root, root2];
|
|
239
|
+
}
|
|
240
|
+
MerkleTreeUtils.checkMembershipGetRoots = checkMembershipGetRoots;
|
|
241
|
+
function computeRoot(witness, value) {
|
|
242
|
+
return witness.calculateRoot(value);
|
|
243
|
+
}
|
|
244
|
+
MerkleTreeUtils.computeRoot = computeRoot;
|
|
245
|
+
})(MerkleTreeUtils || (MerkleTreeUtils = {}));
|
|
246
|
+
/**
|
|
247
|
+
* More efficient version of `maybeSwapBad` which
|
|
248
|
+
* reuses an intermediate variable
|
|
249
|
+
*/
|
|
250
|
+
function maybeSwap(b, x, y) {
|
|
251
|
+
const m = b.toField().mul(x.sub(y)); // b*(x - y)
|
|
252
|
+
const x1 = y.add(m); // y + b*(x - y)
|
|
253
|
+
const y2 = x.sub(m); // x - b*(x - y) = x + b*(y - x)
|
|
254
|
+
return [x1, y2];
|
|
255
|
+
}
|
package/dist/utils/utils.d.ts
CHANGED
|
@@ -6,6 +6,6 @@ export type Subclass<Class extends new (...args: any) => any> = (new (...args: a
|
|
|
6
6
|
prototype: InstanceType<Class>;
|
|
7
7
|
};
|
|
8
8
|
export declare function notInCircuit(): MethodDecorator;
|
|
9
|
-
export declare function stringToField(value: string): import("o1js/dist/node/lib/field").Field;
|
|
9
|
+
export declare function stringToField(value: string): import("o1js/dist/node/lib/provable/field").Field;
|
|
10
10
|
export declare function singleFieldToString(value: Field | bigint): string;
|
|
11
11
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAsB,MAAM,MAAM,CAAC;AAGjD,MAAM,MAAM,UAAU,CAAC,YAAY,SAAS,QAAQ,IAAI,YAAY,SAAS,CAC3E,GAAG,IAAI,EAAE,GAAG,EAAE,KACX,MAAM,MAAM,GACb,MAAM,GACN,GAAG,CAAC;AAER,MAAM,MAAM,QAAQ,CAAC,KAAK,SAAS,KAAK,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,IAAI,CAAC,KAC/D,GAAG,IAAI,EAAE,GAAG,KACT,YAAY,CAAC,KAAK,CAAC,CAAC,GAAG;KACzB,GAAG,IAAI,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC;CACjC,GAAG;IAAE,SAAS,EAAE,YAAY,CAAC,KAAK,CAAC,CAAA;CAAE,CAAC;AAEvC,wBAAgB,YAAY,IAAI,eAAe,CAkB9C;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,qDA+B1C;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,CAMjE"}
|
package/dist/utils/utils.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// eslint-disable-next-line max-len
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types, @typescript-eslint/no-unsafe-return */
|
|
3
1
|
import { Field, Poseidon, Provable } from "o1js";
|
|
4
2
|
import floor from "lodash/floor";
|
|
5
3
|
export function notInCircuit() {
|
|
@@ -10,15 +8,13 @@ export function notInCircuit() {
|
|
|
10
8
|
if (Provable.inCheckedComputation() || Provable.inProver()) {
|
|
11
9
|
throw new Error(`Method ${propertyKey.toString()} is supposed to be only called outside of the circuit`);
|
|
12
10
|
}
|
|
13
|
-
// eslint-disable-next-line max-len
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
|
15
11
|
return childFunction.apply(this, args);
|
|
16
12
|
};
|
|
17
13
|
return descriptor;
|
|
18
14
|
};
|
|
19
15
|
}
|
|
20
16
|
export function stringToField(value) {
|
|
21
|
-
const fieldSize = Field.sizeInBytes
|
|
17
|
+
const fieldSize = Field.sizeInBytes - 1;
|
|
22
18
|
// Encode string as byte[]
|
|
23
19
|
const encoder = new TextEncoder();
|
|
24
20
|
const stringBytes = Array.from(encoder.encode(value));
|
|
@@ -32,9 +28,7 @@ export function stringToField(value) {
|
|
|
32
28
|
const arrayIndex = floor(index / fieldSize);
|
|
33
29
|
a[arrayIndex].push(b);
|
|
34
30
|
return a;
|
|
35
|
-
},
|
|
36
|
-
// eslint-disable-next-line array-func/from-map
|
|
37
|
-
Array.from({ length: floor(data.length / fieldSize) }).map(() => []));
|
|
31
|
+
}, Array.from({ length: floor(data.length / fieldSize) }).map(() => []));
|
|
38
32
|
const fields = chunks.map((x) =>
|
|
39
33
|
// We have to add a zero at the highest byte here, because a Field is
|
|
40
34
|
// a bit smaller than 2^256
|
|
@@ -42,8 +36,9 @@ export function stringToField(value) {
|
|
|
42
36
|
return Poseidon.hash(fields);
|
|
43
37
|
}
|
|
44
38
|
export function singleFieldToString(value) {
|
|
39
|
+
let fieldValue = value;
|
|
45
40
|
if (typeof value === "bigint") {
|
|
46
|
-
|
|
41
|
+
fieldValue = Field(value);
|
|
47
42
|
}
|
|
48
|
-
return
|
|
43
|
+
return fieldValue.toString();
|
|
49
44
|
}
|
package/package.json
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
|
-
"version": "0.1.1-develop.
|
|
6
|
+
"version": "0.1.1-develop.833+397881ed",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc -p tsconfig.json",
|
|
9
9
|
"dev": "tsc -p tsconfig.json --watch",
|
|
10
10
|
"lint": "eslint ./src ./test",
|
|
11
11
|
"test:file": "node --experimental-vm-modules --experimental-wasm-modules --experimental-wasm-threads ../../node_modules/jest/bin/jest.js",
|
|
12
|
-
"test": "npm run test:file -- ./
|
|
13
|
-
"test:watch": "npm run test:file -- ./
|
|
12
|
+
"test": "npm run test:file -- ./test/**",
|
|
13
|
+
"test:watch": "npm run test:file -- ./test/** --watch"
|
|
14
14
|
},
|
|
15
15
|
"main": "dist/index.js",
|
|
16
16
|
"publishConfig": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"@proto-kit/common": "*",
|
|
27
|
-
"o1js": "
|
|
27
|
+
"o1js": "^1.1.0",
|
|
28
28
|
"ts-pattern": "^4.3.0",
|
|
29
29
|
"tsyringe": "^4.7.0"
|
|
30
30
|
},
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"@jest/globals": "^29.5.0",
|
|
33
33
|
"@types/lodash": "^4.14.194"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "397881ed5d8f98f5005bcd7be7f5a12b3bc6f956"
|
|
36
36
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PublicKey, Struct, UInt64 } from "o1js";
|
|
2
|
+
import { injectable } from "tsyringe";
|
|
2
3
|
|
|
3
4
|
import { BlockProverExecutionData } from "../prover/block/BlockProvable";
|
|
4
5
|
import { StateMap } from "../state/StateMap";
|
|
5
6
|
import { protocolState } from "../state/protocol/ProtocolState";
|
|
6
7
|
import { ProvableTransactionHook } from "../protocol/ProvableTransactionHook";
|
|
7
8
|
import { assert } from "../state/assert/assert";
|
|
8
|
-
import { injectable } from "tsyringe";
|
|
9
9
|
|
|
10
10
|
export class AccountState extends Struct({
|
|
11
11
|
nonce: UInt64,
|
|
@@ -18,12 +18,12 @@ export class AccountStateHook extends ProvableTransactionHook {
|
|
|
18
18
|
AccountState
|
|
19
19
|
);
|
|
20
20
|
|
|
21
|
-
public onTransaction({ transaction }: BlockProverExecutionData)
|
|
21
|
+
public async onTransaction({ transaction }: BlockProverExecutionData) {
|
|
22
22
|
const sender = transaction.sender.value;
|
|
23
23
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
const aso = await this.accountState.get(sender);
|
|
25
|
+
|
|
26
|
+
const accountState = aso.orElse(new AccountState({ nonce: UInt64.zero }));
|
|
27
27
|
|
|
28
28
|
const currentNonce = accountState.nonce;
|
|
29
29
|
|
|
@@ -32,15 +32,17 @@ export class AccountStateHook extends ProvableTransactionHook {
|
|
|
32
32
|
currentNonce
|
|
33
33
|
.equals(transaction.nonce.value)
|
|
34
34
|
.or(transaction.sender.isSome.not()),
|
|
35
|
-
|
|
35
|
+
() =>
|
|
36
|
+
`Nonce not matching: tx sent ${transaction.nonce.value.toString()}, onchain value is ${currentNonce.toString()}`
|
|
36
37
|
);
|
|
37
38
|
|
|
38
39
|
// Optimized version of transaction.sender.isSome ? currentNonce.add(1) : Field(0)
|
|
39
40
|
// Bcs Bool(true).toField() == 1
|
|
40
|
-
|
|
41
|
+
// TODO Think about if we want to rangecheck this. If not, we should store it as Field
|
|
42
|
+
const newNonce = UInt64.Unsafe.fromField(
|
|
41
43
|
currentNonce.value.add(1).mul(transaction.sender.isSome.toField())
|
|
42
44
|
);
|
|
43
45
|
|
|
44
|
-
this.accountState.set(sender, new AccountState({ nonce: newNonce }));
|
|
46
|
+
await this.accountState.set(sender, new AccountState({ nonce: newNonce }));
|
|
45
47
|
}
|
|
46
48
|
}
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { Provable } from "o1js";
|
|
2
1
|
import { NetworkState } from "../model/network/NetworkState";
|
|
3
2
|
import { ProvableBlockHook } from "../protocol/ProvableBlockHook";
|
|
4
3
|
|
|
5
4
|
export class BlockHeightHook extends ProvableBlockHook<Record<string, never>> {
|
|
6
|
-
public afterBlock(networkState: NetworkState): NetworkState {
|
|
5
|
+
public async afterBlock(networkState: NetworkState): Promise<NetworkState> {
|
|
7
6
|
return new NetworkState({
|
|
8
7
|
block: {
|
|
9
8
|
height: networkState.block.height.add(1),
|
|
10
9
|
},
|
|
11
|
-
previous: networkState.previous
|
|
10
|
+
previous: networkState.previous,
|
|
12
11
|
});
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
public beforeBlock(networkState: NetworkState): NetworkState {
|
|
14
|
+
public async beforeBlock(networkState: NetworkState): Promise<NetworkState> {
|
|
16
15
|
return networkState;
|
|
17
16
|
}
|
|
18
17
|
}
|
|
@@ -5,10 +5,10 @@ import { BlockProverState } from "../prover/block/BlockProver";
|
|
|
5
5
|
export class LastStateRootBlockHook extends ProvableBlockHook<
|
|
6
6
|
Record<string, never>
|
|
7
7
|
> {
|
|
8
|
-
public afterBlock(
|
|
8
|
+
public async afterBlock(
|
|
9
9
|
networkState: NetworkState,
|
|
10
10
|
state: BlockProverState
|
|
11
|
-
): NetworkState {
|
|
11
|
+
): Promise<NetworkState> {
|
|
12
12
|
return new NetworkState({
|
|
13
13
|
block: networkState.block,
|
|
14
14
|
previous: {
|
|
@@ -17,10 +17,10 @@ export class LastStateRootBlockHook extends ProvableBlockHook<
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
public beforeBlock(
|
|
20
|
+
public async beforeBlock(
|
|
21
21
|
networkState: NetworkState,
|
|
22
22
|
state: BlockProverState
|
|
23
|
-
): NetworkState {
|
|
23
|
+
): Promise<NetworkState> {
|
|
24
24
|
return networkState;
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -1,20 +1,21 @@
|
|
|
1
|
+
import { NoConfig } from "@proto-kit/common";
|
|
2
|
+
|
|
1
3
|
import { ProvableBlockHook } from "../protocol/ProvableBlockHook";
|
|
2
4
|
import { NetworkState } from "../model/network/NetworkState";
|
|
3
5
|
import { BlockProverState } from "../prover/block/BlockProver";
|
|
4
|
-
import { NoConfig } from "@proto-kit/common";
|
|
5
6
|
|
|
6
7
|
export class NoopBlockHook extends ProvableBlockHook<NoConfig> {
|
|
7
|
-
public afterBlock(
|
|
8
|
+
public async afterBlock(
|
|
8
9
|
networkState: NetworkState,
|
|
9
10
|
state: BlockProverState
|
|
10
|
-
): NetworkState {
|
|
11
|
+
): Promise<NetworkState> {
|
|
11
12
|
return networkState;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
public beforeBlock(
|
|
15
|
+
public async beforeBlock(
|
|
15
16
|
networkState: NetworkState,
|
|
16
17
|
state: BlockProverState
|
|
17
|
-
): NetworkState {
|
|
18
|
+
): Promise<NetworkState> {
|
|
18
19
|
return networkState;
|
|
19
20
|
}
|
|
20
21
|
}
|
|
@@ -11,10 +11,10 @@ import {
|
|
|
11
11
|
export class NoopSettlementHook extends ProvableSettlementHook<
|
|
12
12
|
Record<string, never>
|
|
13
13
|
> {
|
|
14
|
-
public beforeSettlement(
|
|
14
|
+
public async beforeSettlement(
|
|
15
15
|
contract: SmartContract,
|
|
16
16
|
state: SettlementHookInputs
|
|
17
|
-
)
|
|
17
|
+
) {
|
|
18
18
|
noop();
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import { noop } from "@proto-kit/common";
|
|
2
|
+
|
|
1
3
|
import { ProvableTransactionHook } from "../protocol/ProvableTransactionHook";
|
|
2
4
|
import { BlockProverExecutionData } from "../prover/block/BlockProvable";
|
|
3
5
|
|
|
4
6
|
export class NoopTransactionHook extends ProvableTransactionHook {
|
|
5
|
-
public onTransaction(executionData: BlockProverExecutionData)
|
|
7
|
+
public async onTransaction(executionData: BlockProverExecutionData) {
|
|
8
|
+
noop();
|
|
9
|
+
}
|
|
6
10
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export * from "./hooks/AccountStateHook";
|
|
2
2
|
export * from "./hooks/BlockHeightHook";
|
|
3
|
-
export * from "./utils/ProvableHashList";
|
|
4
3
|
export * from "./hooks/LastStateRootBlockHook";
|
|
5
4
|
export * from "./model/StateTransition";
|
|
6
5
|
export * from "./model/StateTransitionProvableBatch";
|
|
@@ -30,7 +29,6 @@ export * from "./protocol/ProtocolModule";
|
|
|
30
29
|
export * from "./protocol/ProtocolEnvironment";
|
|
31
30
|
export * from "./protocol/ProvableTransactionHook";
|
|
32
31
|
export * from "./protocol/ProvableBlockHook";
|
|
33
|
-
export * from "./state/context/ProtocolMethodExecutionContext";
|
|
34
32
|
export * from "./state/context/TransitionMethodExecutionContext";
|
|
35
33
|
export * from "./state/context/RuntimeMethodExecutionContext";
|
|
36
34
|
export * from "./state/protocol/ProtocolState";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bool, Field,
|
|
1
|
+
import { Bool, Field, Struct } from "o1js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Public input used to link in-circuit execution with
|
|
@@ -10,4 +10,4 @@ export class MethodPublicOutput extends Struct({
|
|
|
10
10
|
transactionHash: Field,
|
|
11
11
|
networkStateHash: Field,
|
|
12
12
|
isMessage: Bool,
|
|
13
|
-
}) {}
|
|
13
|
+
}) {}
|