@proto-kit/protocol 0.1.1-develop.165 → 0.1.1-develop.185
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/dist/model/MethodPublicOutput.d.ts +6 -7
- package/dist/model/MethodPublicOutput.d.ts.map +1 -1
- package/dist/model/Option.d.ts +11 -7
- package/dist/model/Option.d.ts.map +1 -1
- package/dist/model/Option.js +10 -0
- package/dist/model/StateTransition.d.ts +11 -0
- package/dist/model/StateTransition.d.ts.map +1 -1
- package/dist/model/StateTransition.js +7 -0
- package/dist/model/StateTransitionProvableBatch.d.ts +4 -4
- package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -1
- package/dist/model/StateTransitionProvableBatch.js +6 -6
- package/dist/model/network/NetworkState.d.ts.map +1 -1
- package/dist/model/network/NetworkState.js +1 -3
- package/dist/model/transaction/RuntimeTransaction.d.ts +9 -1
- package/dist/model/transaction/RuntimeTransaction.d.ts.map +1 -1
- package/dist/model/transaction/RuntimeTransaction.js +5 -2
- package/dist/protocol/Protocol.d.ts +1 -1
- package/dist/protocol/Protocol.d.ts.map +1 -1
- package/dist/protocol/ProtocolModule.d.ts +1 -1
- package/dist/protocol/ProtocolModule.d.ts.map +1 -1
- package/dist/protocol/ProtocolModule.js +3 -3
- package/dist/prover/block/BlockProvable.d.ts +1 -1
- package/dist/prover/block/BlockProvable.d.ts.map +1 -1
- package/dist/prover/block/BlockProver.d.ts.map +1 -1
- package/dist/prover/statetransition/StateTransitionProver.d.ts +2 -2
- package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
- package/dist/prover/statetransition/StateTransitionProver.js +5 -4
- package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts +1 -1
- package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts.map +1 -1
- package/dist/prover/statetransition/StateTransitionWitnessProvider.js +1 -1
- package/dist/prover/statetransition/StateTransitionWitnessProviderReference.d.ts.map +1 -1
- package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts.map +1 -1
- package/dist/utils/merkletree/InMemoryMerkleTreeStorage.js +3 -3
- package/dist/utils/merkletree/RollupMerkleTree.d.ts +6 -6
- package/dist/utils/merkletree/RollupMerkleTree.d.ts.map +1 -1
- package/dist/utils/merkletree/RollupMerkleTree.js +4 -4
- package/dist/utils/{Utils.d.ts → utils.d.ts} +2 -5
- package/dist/utils/{Utils.d.ts.map → utils.d.ts.map} +1 -1
- package/dist/utils/utils.js +51 -0
- package/package.json +4 -4
- package/src/model/Option.ts +13 -0
- package/src/model/StateTransition.ts +8 -0
- package/src/model/StateTransitionProvableBatch.ts +8 -7
- package/src/model/network/NetworkState.ts +1 -3
- package/src/model/transaction/RuntimeTransaction.ts +5 -1
- package/src/protocol/Protocol.ts +1 -1
- package/src/protocol/ProtocolModule.ts +4 -5
- package/src/prover/block/BlockProvable.ts +2 -2
- package/src/prover/block/BlockProver.ts +6 -1
- package/src/prover/statetransition/StateTransitionProver.ts +6 -9
- package/src/prover/statetransition/StateTransitionWitnessProvider.ts +1 -1
- package/src/prover/statetransition/StateTransitionWitnessProviderReference.ts +2 -2
- package/src/utils/merkletree/InMemoryMerkleTreeStorage.ts +4 -4
- package/src/utils/merkletree/MerkleTreeStore.ts +1 -1
- package/src/utils/merkletree/RollupMerkleTree.ts +9 -4
- package/src/utils/utils.ts +29 -45
- package/test/utils.test.ts +30 -0
- package/dist/config/ConfigurableModule.d.ts +0 -18
- package/dist/config/ConfigurableModule.d.ts.map +0 -1
- package/dist/config/ConfigurableModule.js +0 -20
- package/dist/config/ConfigurationAggregator.d.ts +0 -10
- package/dist/config/ConfigurationAggregator.d.ts.map +0 -1
- package/dist/config/ConfigurationAggregator.js +0 -35
- package/dist/config/ConfigurationReceiver.d.ts +0 -25
- package/dist/config/ConfigurationReceiver.d.ts.map +0 -1
- package/dist/config/ConfigurationReceiver.js +0 -36
- package/dist/config/ModuleContainer.d.ts +0 -44
- package/dist/config/ModuleContainer.d.ts.map +0 -1
- package/dist/config/ModuleContainer.js +0 -89
- package/dist/config/types.d.ts +0 -2
- package/dist/config/types.d.ts.map +0 -1
- package/dist/config/types.js +0 -1
- package/dist/model/MethodPublicInput.d.ts +0 -51
- package/dist/model/MethodPublicInput.d.ts.map +0 -1
- package/dist/model/MethodPublicInput.js +0 -11
- package/dist/prover/block/BlockScopedModule.d.ts +0 -3
- package/dist/prover/block/BlockScopedModule.d.ts.map +0 -1
- package/dist/prover/block/BlockScopedModule.js +0 -6
- package/dist/src/model/Option.d.ts +0 -158
- package/dist/src/model/Option.d.ts.map +0 -1
- package/dist/src/model/Option.js +0 -53
- package/dist/src/model/Path.d.ts +0 -35
- package/dist/src/model/Path.d.ts.map +0 -1
- package/dist/src/model/Path.js +0 -51
- package/dist/src/model/StateTransition.d.ts +0 -201
- package/dist/src/model/StateTransition.d.ts.map +0 -1
- package/dist/src/model/StateTransition.js +0 -43
- package/dist/src/utils/PrefixedHashList.d.ts +0 -15
- package/dist/src/utils/PrefixedHashList.d.ts.map +0 -1
- package/dist/src/utils/PrefixedHashList.js +0 -28
- package/dist/src/utils/ProvableHashList.d.ts +0 -30
- package/dist/src/utils/ProvableHashList.d.ts.map +0 -1
- package/dist/src/utils/ProvableHashList.js +0 -43
- package/dist/utils/PrefixedHashList.d.ts +0 -14
- package/dist/utils/PrefixedHashList.d.ts.map +0 -1
- package/dist/utils/PrefixedHashList.js +0 -12
- package/dist/utils/Utils.js +0 -64
- package/dist/utils/merkletree/MemoryMerkleTreeStorage.d.ts +0 -26
- package/dist/utils/merkletree/MemoryMerkleTreeStorage.d.ts.map +0 -1
- package/dist/utils/merkletree/MemoryMerkleTreeStorage.js +0 -79
|
@@ -11,7 +11,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
/* eslint-disable line-comment-position */
|
|
12
12
|
/* eslint-disable no-inline-comments */
|
|
13
13
|
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
14
|
-
import { Bool,
|
|
14
|
+
import { Bool, Field, Poseidon, Provable, Struct } from "snarkyjs";
|
|
15
15
|
import { notInCircuit } from "../utils";
|
|
16
16
|
// external API
|
|
17
17
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
@@ -24,8 +24,8 @@ export { maybeSwap };
|
|
|
24
24
|
* for [Merkle Witness'](https://computersciencewiki.org/index.php/Merkle_proof).
|
|
25
25
|
*/
|
|
26
26
|
class RollupMerkleWitness extends Struct({
|
|
27
|
-
path:
|
|
28
|
-
isLeft:
|
|
27
|
+
path: Provable.Array(Field, 256 - 1),
|
|
28
|
+
isLeft: Provable.Array(Bool, 256 - 1),
|
|
29
29
|
}) {
|
|
30
30
|
height() {
|
|
31
31
|
return RollupMerkleWitness.height;
|
|
@@ -55,7 +55,7 @@ class RollupMerkleWitness extends Struct({
|
|
|
55
55
|
const n = this.height();
|
|
56
56
|
// eslint-disable-next-line no-underscore-dangle
|
|
57
57
|
for (let index_ = 1; index_ < n; ++index_) {
|
|
58
|
-
index =
|
|
58
|
+
index = Provable.if(this.isLeft[index_ - 1], index, index.add(powerOfTwo));
|
|
59
59
|
powerOfTwo = powerOfTwo.mul(2);
|
|
60
60
|
}
|
|
61
61
|
return index;
|
|
@@ -8,10 +8,7 @@ export type Subclass<Class extends new (...args: any) => any> = (new (...args: a
|
|
|
8
8
|
prototype: InstanceType<Class>;
|
|
9
9
|
};
|
|
10
10
|
export declare function notInCircuit(): MethodDecorator;
|
|
11
|
-
export declare function stringToField(value: string
|
|
12
|
-
|
|
13
|
-
* Note: This only works for strings that have been encoded using the `throwOnOverflow` set to true
|
|
14
|
-
*/
|
|
15
|
-
export declare function fieldToString(value: Field | bigint): string;
|
|
11
|
+
export declare function stringToField(value: string): import("snarkyjs/dist/node/lib/field").Field;
|
|
12
|
+
export declare function singleFieldToString(value: Field | bigint): string;
|
|
16
13
|
export declare function noop(): void;
|
|
17
14
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAsB,MAAM,UAAU,CAAC;AAGrD,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,YAAY,GAAG,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAEvD,MAAM,MAAM,UAAU,CAAC,KAAK,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,KAAK,CAAC;AAE9D,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,CAoB9C;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,gDAiC1C;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,CAKjE;AAED,wBAAgB,IAAI,IAAI,IAAI,CAAG"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// eslint-disable-next-line max-len
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types, @typescript-eslint/no-unsafe-return,@typescript-eslint/no-empty-function */
|
|
3
|
+
import { Field, Poseidon, Provable } from "snarkyjs";
|
|
4
|
+
import floor from "lodash/floor";
|
|
5
|
+
export function notInCircuit() {
|
|
6
|
+
return function ReplacedFunction(target, propertyKey, descriptor) {
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
8
|
+
const childFunction = descriptor.value;
|
|
9
|
+
descriptor.value = function value(...args) {
|
|
10
|
+
if (Provable.inCheckedComputation() || Provable.inProver()) {
|
|
11
|
+
throw new Error(`Method ${propertyKey.toString()} is supposed to be only called outside of the circuit`);
|
|
12
|
+
}
|
|
13
|
+
// eslint-disable-next-line max-len
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
|
15
|
+
return childFunction.apply(this, args);
|
|
16
|
+
};
|
|
17
|
+
return descriptor;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function stringToField(value) {
|
|
21
|
+
const fieldSize = Field.sizeInBytes() - 1;
|
|
22
|
+
// Encode string as byte[]
|
|
23
|
+
const encoder = new TextEncoder();
|
|
24
|
+
const stringBytes = Array.from(encoder.encode(value));
|
|
25
|
+
// Add padding in case the string is not a multiple of Field.sizeInBytes
|
|
26
|
+
const padding = Array.from({
|
|
27
|
+
length: fieldSize - (stringBytes.length % fieldSize),
|
|
28
|
+
}).fill(0);
|
|
29
|
+
const data = stringBytes.concat(padding).reverse();
|
|
30
|
+
// Hash the result Field[] to reduce it to
|
|
31
|
+
const chunks = data.reduce((a, b, index) => {
|
|
32
|
+
const arrayIndex = floor(index / fieldSize);
|
|
33
|
+
a[arrayIndex].push(b);
|
|
34
|
+
return a;
|
|
35
|
+
},
|
|
36
|
+
// eslint-disable-next-line array-func/from-map
|
|
37
|
+
Array.from({ length: floor(data.length / fieldSize) }).map(() => []));
|
|
38
|
+
const fields = chunks.map((x) =>
|
|
39
|
+
// We have to add a zero at the highest byte here, because a Field is
|
|
40
|
+
// a bit smaller than 2^256
|
|
41
|
+
// console.log(x.concat([0]).length);
|
|
42
|
+
Field.fromBytes(x.concat([0])));
|
|
43
|
+
return Poseidon.hash(fields);
|
|
44
|
+
}
|
|
45
|
+
export function singleFieldToString(value) {
|
|
46
|
+
if (typeof value === "bigint") {
|
|
47
|
+
value = Field(value);
|
|
48
|
+
}
|
|
49
|
+
return value.toString();
|
|
50
|
+
}
|
|
51
|
+
export function noop() { }
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
|
-
"version": "0.1.1-develop.
|
|
6
|
+
"version": "0.1.1-develop.185+513fbd8",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc -p tsconfig.json",
|
|
9
9
|
"dev": "tsc -p tsconfig.json --watch",
|
|
@@ -17,18 +17,18 @@
|
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@proto-kit/common": "0.1.1-develop.
|
|
20
|
+
"@proto-kit/common": "0.1.1-develop.185+513fbd8",
|
|
21
21
|
"lodash": "^4.17.21",
|
|
22
22
|
"loglevel": "^1.8.1",
|
|
23
23
|
"reflect-metadata": "^0.1.13"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"snarkyjs": "0.
|
|
26
|
+
"snarkyjs": "0.12.0",
|
|
27
27
|
"tsyringe": "^4.7.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@jest/globals": "^29.5.0",
|
|
31
31
|
"@types/lodash": "^4.14.194"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "513fbd83cb6540fba01736d55ab0b4f7c1f71f78"
|
|
34
34
|
}
|
package/src/model/Option.ts
CHANGED
|
@@ -130,4 +130,17 @@ export class Option<Value> {
|
|
|
130
130
|
value: this.treeValue,
|
|
131
131
|
});
|
|
132
132
|
}
|
|
133
|
+
|
|
134
|
+
public toJSON() {
|
|
135
|
+
const valueContent = this.valueType
|
|
136
|
+
.toFields(this.value)
|
|
137
|
+
.map((field) => field.toString())
|
|
138
|
+
.reduce((a, b) => `${a}, ${b}`);
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
isSome: this.isSome.toBoolean(),
|
|
142
|
+
|
|
143
|
+
value: `[${valueContent}]`,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
133
146
|
}
|
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Provable, Struct } from "snarkyjs";
|
|
2
2
|
|
|
3
|
-
import { ProvableStateTransition } from "./StateTransition.js";
|
|
4
3
|
import { constants } from "../Constants";
|
|
5
4
|
|
|
5
|
+
import { ProvableStateTransition } from "./StateTransition.js";
|
|
6
|
+
|
|
6
7
|
/**
|
|
7
8
|
* A Batch of StateTransitions to be consumed by the StateTransitionProver
|
|
8
9
|
* to prove multiple STs at once
|
|
9
10
|
*/
|
|
10
11
|
export class StateTransitionProvableBatch extends Struct({
|
|
11
|
-
batch:
|
|
12
|
+
batch: Provable.Array(
|
|
12
13
|
ProvableStateTransition,
|
|
13
14
|
constants.stateTransitionProverBatchSize
|
|
14
15
|
),
|
|
15
16
|
}) {
|
|
16
|
-
private constructor(object: { batch: ProvableStateTransition[] }) {
|
|
17
|
-
super(object);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
17
|
public static fromTransitions(
|
|
21
18
|
transitions: ProvableStateTransition[]
|
|
22
19
|
): StateTransitionProvableBatch {
|
|
@@ -28,4 +25,8 @@ export class StateTransitionProvableBatch extends Struct({
|
|
|
28
25
|
|
|
29
26
|
return new StateTransitionProvableBatch({ batch: array });
|
|
30
27
|
}
|
|
28
|
+
|
|
29
|
+
private constructor(object: { batch: ProvableStateTransition[] }) {
|
|
30
|
+
super(object);
|
|
31
|
+
}
|
|
31
32
|
}
|
|
@@ -8,16 +8,19 @@ import { ProtocolTransaction } from "./ProtocolTransaction";
|
|
|
8
8
|
* For example, we don't want to expose the signature or args as fields.
|
|
9
9
|
*/
|
|
10
10
|
export class RuntimeTransaction extends Struct({
|
|
11
|
+
methodId: Field,
|
|
11
12
|
nonce: UInt64,
|
|
12
13
|
sender: PublicKey,
|
|
13
14
|
argsHash: Field,
|
|
14
15
|
}) {
|
|
15
16
|
public static fromProtocolTransaction({
|
|
17
|
+
methodId,
|
|
16
18
|
nonce,
|
|
17
19
|
sender,
|
|
18
20
|
argsHash,
|
|
19
21
|
}: ProtocolTransaction): RuntimeTransaction {
|
|
20
22
|
return new RuntimeTransaction({
|
|
23
|
+
methodId,
|
|
21
24
|
nonce,
|
|
22
25
|
sender,
|
|
23
26
|
argsHash,
|
|
@@ -26,8 +29,9 @@ export class RuntimeTransaction extends Struct({
|
|
|
26
29
|
|
|
27
30
|
public hash(): Field {
|
|
28
31
|
return Poseidon.hash([
|
|
29
|
-
|
|
32
|
+
this.methodId,
|
|
30
33
|
...this.sender.toFields(),
|
|
34
|
+
...this.nonce.toFields(),
|
|
31
35
|
this.argsHash,
|
|
32
36
|
]);
|
|
33
37
|
}
|
package/src/protocol/Protocol.ts
CHANGED
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
} from "@proto-kit/common";
|
|
6
6
|
|
|
7
7
|
import type { Protocol, ProtocolModulesRecord } from "./Protocol";
|
|
8
|
-
import { noop } from "lodash";
|
|
9
8
|
|
|
10
9
|
export abstract class ProtocolModule<PublicInput, PublicOutput>
|
|
11
10
|
extends ZkProgrammable<PublicInput, PublicOutput>
|
|
@@ -15,13 +14,13 @@ export abstract class ProtocolModule<PublicInput, PublicOutput>
|
|
|
15
14
|
|
|
16
15
|
public protocol?: Protocol<ProtocolModulesRecord>;
|
|
17
16
|
|
|
17
|
+
public constructor() {
|
|
18
|
+
super();
|
|
19
|
+
}
|
|
20
|
+
|
|
18
21
|
public get appChain(): AreProofsEnabled | undefined {
|
|
19
22
|
return this.protocol?.dependencyContainer.resolve<AreProofsEnabled>(
|
|
20
23
|
"AppChain"
|
|
21
24
|
);
|
|
22
25
|
}
|
|
23
|
-
|
|
24
|
-
public constructor() {
|
|
25
|
-
super();
|
|
26
|
-
}
|
|
27
26
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Field, Proof, Struct
|
|
1
|
+
import { Field, Proof, Struct } from "snarkyjs";
|
|
2
|
+
import { ZkProgrammable } from "@proto-kit/common";
|
|
2
3
|
|
|
3
4
|
import { StateTransitionProof } from "../statetransition/StateTransitionProvable";
|
|
4
5
|
import { MethodPublicOutput } from "../../model/MethodPublicOutput";
|
|
5
|
-
import { ZkProgrammable } from "@proto-kit/common";
|
|
6
6
|
import { ProtocolTransaction } from "../../model/transaction/ProtocolTransaction";
|
|
7
7
|
import { NetworkState } from "../../model/network/NetworkState";
|
|
8
8
|
|
|
@@ -182,7 +182,12 @@ export class BlockProver
|
|
|
182
182
|
networkStateHash: publicInput.networkStateHash,
|
|
183
183
|
};
|
|
184
184
|
|
|
185
|
-
const stateTo = this.applyTransaction(
|
|
185
|
+
const stateTo = this.applyTransaction(
|
|
186
|
+
state,
|
|
187
|
+
stateProof,
|
|
188
|
+
appProof,
|
|
189
|
+
executionData
|
|
190
|
+
);
|
|
186
191
|
|
|
187
192
|
return new BlockProverPublicOutput({
|
|
188
193
|
stateRoot: stateTo.stateRoot,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Experimental, Field, Provable, SelfProof } from "snarkyjs";
|
|
2
|
-
import {
|
|
2
|
+
import { injectable } from "tsyringe";
|
|
3
|
+
import { PlainZkProgram, provableMethod } from "@proto-kit/common";
|
|
3
4
|
|
|
4
5
|
import {
|
|
5
6
|
MerkleTreeUtils,
|
|
@@ -12,20 +13,15 @@ import {
|
|
|
12
13
|
import { ProvableStateTransition } from "../../model/StateTransition";
|
|
13
14
|
import { StateTransitionProvableBatch } from "../../model/StateTransitionProvableBatch";
|
|
14
15
|
import { constants } from "../../Constants";
|
|
16
|
+
import { ProtocolModule } from "../../protocol/ProtocolModule";
|
|
15
17
|
|
|
16
18
|
import { StateTransitionWitnessProvider } from "./StateTransitionWitnessProvider.js";
|
|
17
|
-
import {
|
|
18
|
-
AreProofsEnabled,
|
|
19
|
-
PlainZkProgram,
|
|
20
|
-
provableMethod,
|
|
21
|
-
} from "@proto-kit/common";
|
|
22
19
|
import {
|
|
23
20
|
StateTransitionProvable,
|
|
24
21
|
StateTransitionProverPublicInput,
|
|
25
22
|
StateTransitionProof,
|
|
26
23
|
StateTransitionProverPublicOutput,
|
|
27
24
|
} from "./StateTransitionProvable";
|
|
28
|
-
import { ProtocolModule } from "../../protocol/ProtocolModule";
|
|
29
25
|
import { StateTransitionWitnessProviderReference } from "./StateTransitionWitnessProviderReference";
|
|
30
26
|
|
|
31
27
|
const errors = {
|
|
@@ -78,6 +74,8 @@ export class StateTransitionProver
|
|
|
78
74
|
StateTransitionProverPublicInput,
|
|
79
75
|
StateTransitionProverPublicOutput
|
|
80
76
|
> {
|
|
77
|
+
// eslint-disable-next-line max-len
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias,consistent-this,unicorn/no-this-assignment
|
|
81
79
|
const instance = this;
|
|
82
80
|
|
|
83
81
|
const program = Experimental.ZkProgram({
|
|
@@ -219,11 +217,10 @@ export class StateTransitionProver
|
|
|
219
217
|
batch
|
|
220
218
|
);
|
|
221
219
|
|
|
222
|
-
|
|
220
|
+
return new StateTransitionProverPublicOutput({
|
|
223
221
|
stateRoot: result.stateRoot,
|
|
224
222
|
stateTransitionsHash: result.stateTransitionList.commitment,
|
|
225
223
|
});
|
|
226
|
-
return output;
|
|
227
224
|
}
|
|
228
225
|
|
|
229
226
|
@provableMethod()
|
|
@@ -18,7 +18,7 @@ export interface StateTransitionWitnessProvider {
|
|
|
18
18
|
export class NoOpStateTransitionWitnessProvider
|
|
19
19
|
implements StateTransitionWitnessProvider
|
|
20
20
|
{
|
|
21
|
-
public getWitness(
|
|
21
|
+
public getWitness(): RollupMerkleWitness {
|
|
22
22
|
return new RollupMerkleWitness({ path: [], isLeft: [] });
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { injectable, Lifecycle, scoped } from "tsyringe";
|
|
2
|
+
|
|
2
3
|
import { StateTransitionWitnessProvider } from "./StateTransitionWitnessProvider";
|
|
3
4
|
|
|
4
5
|
@injectable()
|
|
5
6
|
@scoped(Lifecycle.ContainerScoped)
|
|
6
7
|
export class StateTransitionWitnessProviderReference {
|
|
7
|
-
|
|
8
8
|
private witnessProvider?: StateTransitionWitnessProvider;
|
|
9
9
|
|
|
10
10
|
public setWitnessProvider(provider: StateTransitionWitnessProvider) {
|
|
@@ -14,4 +14,4 @@ export class StateTransitionWitnessProviderReference {
|
|
|
14
14
|
public getWitnessProvider(): StateTransitionWitnessProvider | undefined {
|
|
15
15
|
return this.witnessProvider;
|
|
16
16
|
}
|
|
17
|
-
}
|
|
17
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-magic-numbers,@typescript-eslint/no-unnecessary-condition */
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
3
2
|
import { log } from "@proto-kit/common";
|
|
4
3
|
|
|
5
4
|
import { RollupMerkleTree } from "./RollupMerkleTree.js";
|
|
@@ -49,8 +48,8 @@ export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
|
49
48
|
this.writeCache = {};
|
|
50
49
|
}
|
|
51
50
|
|
|
51
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
52
52
|
public async preloadKey(index: bigint): Promise<void> {
|
|
53
|
-
log.debug(`Preloading MT ${index}`);
|
|
54
53
|
// Algo from RollupMerkleTree.getWitness()
|
|
55
54
|
const { leafCount, height } = RollupMerkleTree;
|
|
56
55
|
|
|
@@ -68,6 +67,7 @@ export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
|
68
67
|
|
|
69
68
|
// eslint-disable-next-line no-await-in-loop
|
|
70
69
|
const value = await this.parent.getNode(key, level);
|
|
70
|
+
// eslint-disable-next-line no-await-in-loop
|
|
71
71
|
const sibling = await this.parent.getNode(siblingKey, level);
|
|
72
72
|
if (level === 0) {
|
|
73
73
|
log.debug(`Preloaded ${key} @ ${level} -> ${value ?? "-"}`);
|
|
@@ -75,7 +75,7 @@ export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
|
75
75
|
if (value !== undefined) {
|
|
76
76
|
this.setNode(key, level, value);
|
|
77
77
|
}
|
|
78
|
-
if(sibling !== undefined) {
|
|
78
|
+
if (sibling !== undefined) {
|
|
79
79
|
this.setNode(siblingKey, level, sibling);
|
|
80
80
|
}
|
|
81
81
|
index /= 2n;
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
/* eslint-disable line-comment-position */
|
|
3
3
|
/* eslint-disable no-inline-comments */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
5
|
-
import { Bool,
|
|
5
|
+
import { Bool, Field, Poseidon, Provable, Struct } from "snarkyjs";
|
|
6
6
|
|
|
7
7
|
import { notInCircuit } from "../utils";
|
|
8
|
+
|
|
8
9
|
import { MerkleTreeStore } from "./MerkleTreeStore";
|
|
9
10
|
|
|
10
11
|
// external API
|
|
@@ -20,8 +21,8 @@ export { maybeSwap };
|
|
|
20
21
|
* for [Merkle Witness'](https://computersciencewiki.org/index.php/Merkle_proof).
|
|
21
22
|
*/
|
|
22
23
|
class RollupMerkleWitness extends Struct({
|
|
23
|
-
path:
|
|
24
|
-
isLeft:
|
|
24
|
+
path: Provable.Array(Field, 256 - 1),
|
|
25
|
+
isLeft: Provable.Array(Bool, 256 - 1),
|
|
25
26
|
}) {
|
|
26
27
|
public static height = 256;
|
|
27
28
|
|
|
@@ -58,7 +59,11 @@ class RollupMerkleWitness extends Struct({
|
|
|
58
59
|
|
|
59
60
|
// eslint-disable-next-line no-underscore-dangle
|
|
60
61
|
for (let index_ = 1; index_ < n; ++index_) {
|
|
61
|
-
index =
|
|
62
|
+
index = Provable.if(
|
|
63
|
+
this.isLeft[index_ - 1],
|
|
64
|
+
index,
|
|
65
|
+
index.add(powerOfTwo)
|
|
66
|
+
);
|
|
62
67
|
powerOfTwo = powerOfTwo.mul(2);
|
|
63
68
|
}
|
|
64
69
|
|
package/src/utils/utils.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// eslint-disable-next-line max-len
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/ban-types, @typescript-eslint/no-unsafe-return,@typescript-eslint/no-empty-function */
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { Field, Poseidon, Provable } from "snarkyjs";
|
|
5
|
+
import floor from "lodash/floor";
|
|
5
6
|
|
|
6
7
|
export type ReturnType<FunctionType extends Function> = FunctionType extends (
|
|
7
8
|
...args: any[]
|
|
@@ -28,7 +29,7 @@ export function notInCircuit(): MethodDecorator {
|
|
|
28
29
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
29
30
|
const childFunction = descriptor.value;
|
|
30
31
|
descriptor.value = function value(this: any, ...args: any[]) {
|
|
31
|
-
if (
|
|
32
|
+
if (Provable.inCheckedComputation() || Provable.inProver()) {
|
|
32
33
|
throw new Error(
|
|
33
34
|
`Method ${propertyKey.toString()} is supposed to be only called outside of the circuit`
|
|
34
35
|
);
|
|
@@ -41,63 +42,46 @@ export function notInCircuit(): MethodDecorator {
|
|
|
41
42
|
};
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
export function stringToField(value: string
|
|
45
|
-
const fieldSize = Field.sizeInBytes();
|
|
45
|
+
export function stringToField(value: string) {
|
|
46
|
+
const fieldSize = Field.sizeInBytes() - 1;
|
|
46
47
|
|
|
47
|
-
//
|
|
48
|
+
// Encode string as byte[]
|
|
48
49
|
const encoder = new TextEncoder();
|
|
49
|
-
|
|
50
50
|
const stringBytes = Array.from(encoder.encode(value));
|
|
51
51
|
|
|
52
|
+
// Add padding in case the string is not a multiple of Field.sizeInBytes
|
|
52
53
|
const padding = Array.from<number>({
|
|
53
|
-
length: fieldSize - stringBytes.length,
|
|
54
|
+
length: fieldSize - (stringBytes.length % fieldSize),
|
|
54
55
|
}).fill(0);
|
|
55
|
-
const data = stringBytes.concat(padding);
|
|
56
|
-
|
|
57
|
-
if (data.length > fieldSize) {
|
|
58
|
-
if (throwOnOverflow) {
|
|
59
|
-
throw new Error(
|
|
60
|
-
"Trying to encode a stringt that is larger than 256 bits"
|
|
61
|
-
);
|
|
62
|
-
}
|
|
56
|
+
const data = stringBytes.concat(padding).reverse();
|
|
63
57
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
a.push([]);
|
|
69
|
-
}
|
|
58
|
+
// Hash the result Field[] to reduce it to
|
|
59
|
+
const chunks = data.reduce<number[][]>(
|
|
60
|
+
(a, b, index) => {
|
|
61
|
+
const arrayIndex = floor(index / fieldSize);
|
|
70
62
|
a[arrayIndex].push(b);
|
|
71
63
|
return a;
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
// eslint-disable-next-line array-func/from-map
|
|
67
|
+
Array.from<number[]>({ length: floor(data.length / fieldSize) }).map(
|
|
68
|
+
() => []
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
const fields = chunks.map((x) =>
|
|
72
|
+
// We have to add a zero at the highest byte here, because a Field is
|
|
73
|
+
// a bit smaller than 2^256
|
|
74
|
+
// console.log(x.concat([0]).length);
|
|
75
|
+
Field.fromBytes(x.concat([0]))
|
|
76
|
+
);
|
|
77
|
+
return Poseidon.hash(fields);
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
|
|
80
|
-
* Note: This only works for strings that have been encoded using the `throwOnOverflow` set to true
|
|
81
|
-
*/
|
|
82
|
-
export function fieldToString(value: Field | bigint): string {
|
|
80
|
+
export function singleFieldToString(value: Field | bigint): string {
|
|
83
81
|
if (typeof value === "bigint") {
|
|
84
82
|
value = Field(value);
|
|
85
83
|
}
|
|
86
|
-
|
|
87
|
-
// Find start of padded zeroes in order to remove them.
|
|
88
|
-
const zeroesStart =
|
|
89
|
-
bytes.length -
|
|
90
|
-
bytes
|
|
91
|
-
.slice()
|
|
92
|
-
.reverse()
|
|
93
|
-
.findIndex((element) => element !== 0);
|
|
94
|
-
|
|
95
|
-
bytes = bytes.slice(0, zeroesStart);
|
|
96
|
-
|
|
97
|
-
// eslint-disable-next-line putout/putout
|
|
98
|
-
const decoder = new TextDecoder();
|
|
99
|
-
|
|
100
|
-
return decoder.decode(new Uint8Array(bytes));
|
|
84
|
+
return value.toString();
|
|
101
85
|
}
|
|
102
86
|
|
|
103
87
|
export function noop(): void {}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { Field } from "snarkyjs";
|
|
3
|
+
|
|
4
|
+
import { stringToField } from "../src";
|
|
5
|
+
|
|
6
|
+
describe("stringToField", () => {
|
|
7
|
+
const stringToFieldInputs: string[] = [];
|
|
8
|
+
|
|
9
|
+
// eslint-disable-next-line jest/require-hook
|
|
10
|
+
[31, 32, 33, 63, 64, 65, 1000].forEach((length) => {
|
|
11
|
+
stringToFieldInputs.push(
|
|
12
|
+
// eslint-disable-next-line array-func/from-map
|
|
13
|
+
Array.from({ length })
|
|
14
|
+
.map(() => "A")
|
|
15
|
+
.reduce((a, b) => a + b)
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it.each(
|
|
20
|
+
["", "a", "Helloasdlsdaglsdiousdioagiosadgoisaudogiusadogusoadgds"].concat(
|
|
21
|
+
stringToFieldInputs
|
|
22
|
+
)
|
|
23
|
+
)("should encode without errors", (input) => {
|
|
24
|
+
expect.assertions(1);
|
|
25
|
+
|
|
26
|
+
const field = stringToField(input);
|
|
27
|
+
|
|
28
|
+
expect(field.toBigInt()).toBeGreaterThan(0n);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export type Preset<Config> = Config | ((...args: any[]) => Config);
|
|
2
|
-
export type Presets<Config> = Record<string, Preset<Config>>;
|
|
3
|
-
export interface Configurable<Config> {
|
|
4
|
-
config: Config;
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* Used by various module sub-types that may need to be configured
|
|
8
|
-
*/
|
|
9
|
-
export declare class ConfigurableModule<Config> implements Configurable<Config> {
|
|
10
|
-
protected currentConfig: Config | undefined;
|
|
11
|
-
constructor(...args: any[]);
|
|
12
|
-
get config(): Config;
|
|
13
|
-
set config(config: Config);
|
|
14
|
-
}
|
|
15
|
-
export interface StaticConfigurableModule<Config> {
|
|
16
|
-
presets: Presets<Config>;
|
|
17
|
-
}
|
|
18
|
-
//# sourceMappingURL=ConfigurableModule.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ConfigurableModule.d.ts","sourceRoot":"","sources":["../../src/config/ConfigurableModule.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,MAAM,CAAC,MAAM,IAAI,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,MAAM,CAAC,CAAC;AACnE,MAAM,MAAM,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;AAG7D,MAAM,WAAW,YAAY,CAAC,MAAM;IAClC,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,qBAAa,kBAAkB,CAAC,MAAM,CAAE,YAAW,YAAY,CAAC,MAAM,CAAC;IACrE,SAAS,CAAC,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;gBAIzB,GAAG,IAAI,EAAE,GAAG,EAAE;IAEjC,IAAW,MAAM,IAAI,MAAM,CAK1B;IAED,IAAW,MAAM,CAAC,MAAM,EAAE,MAAM,EAE/B;CACF;AAGD,MAAM,WAAW,wBAAwB,CAAC,MAAM;IAC9C,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1B"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const errors = {
|
|
2
|
-
configNotSet: (moduleName) => new Error(`Trying to retrieve config of ${moduleName}, which was not yet set`),
|
|
3
|
-
};
|
|
4
|
-
/**
|
|
5
|
-
* Used by various module sub-types that may need to be configured
|
|
6
|
-
*/
|
|
7
|
-
export class ConfigurableModule {
|
|
8
|
-
// eslint-disable-next-line max-len
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/no-useless-constructor, @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
|
|
10
|
-
constructor(...args) { }
|
|
11
|
-
get config() {
|
|
12
|
-
if (this.currentConfig === undefined) {
|
|
13
|
-
throw errors.configNotSet(this.constructor.name);
|
|
14
|
-
}
|
|
15
|
-
return this.currentConfig;
|
|
16
|
-
}
|
|
17
|
-
set config(config) {
|
|
18
|
-
this.currentConfig = config;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ComponentConfig, Components, RemoveUndefinedKeys, UninitializedComponentConfig } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* In this context, a "Component" is just a way of generalizing Modules,
|
|
4
|
-
* because they don't have to be modules, they can be any configurable unit
|
|
5
|
-
*/
|
|
6
|
-
export declare abstract class ConfigurationAggregator<Comps extends Components> {
|
|
7
|
-
protected applyConfig(modules: Comps, currentConfig: UninitializedComponentConfig<ComponentConfig<Comps>>, config: RemoveUndefinedKeys<ComponentConfig<Comps>>): ComponentConfig<Comps>;
|
|
8
|
-
abstract configure(config: RemoveUndefinedKeys<ComponentConfig<Comps>>): void;
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=ConfigurationAggregator.d.ts.map
|