@proto-kit/protocol 0.1.1-develop.1316 → 0.1.1-develop.1347

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.
Files changed (145) hide show
  1. package/dist/hooks/AccountStateHook.d.ts +3 -3
  2. package/dist/hooks/AccountStateHook.d.ts.map +1 -1
  3. package/dist/hooks/AccountStateHook.js +6 -2
  4. package/dist/hooks/AccountStateHook.js.map +1 -1
  5. package/dist/hooks/LastStateRootBlockHook.d.ts +3 -4
  6. package/dist/hooks/LastStateRootBlockHook.d.ts.map +1 -1
  7. package/dist/hooks/LastStateRootBlockHook.js +4 -4
  8. package/dist/hooks/LastStateRootBlockHook.js.map +1 -1
  9. package/dist/hooks/NoopBlockHook.d.ts +3 -4
  10. package/dist/hooks/NoopBlockHook.d.ts.map +1 -1
  11. package/dist/hooks/NoopBlockHook.js +1 -1
  12. package/dist/hooks/NoopBlockHook.js.map +1 -1
  13. package/dist/hooks/NoopTransactionHook.d.ts +2 -2
  14. package/dist/hooks/NoopTransactionHook.d.ts.map +1 -1
  15. package/dist/hooks/NoopTransactionHook.js +4 -1
  16. package/dist/hooks/NoopTransactionHook.js.map +1 -1
  17. package/dist/index.d.ts +6 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +6 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/model/AppliedStateTransitionBatch.d.ts +114 -0
  22. package/dist/model/AppliedStateTransitionBatch.d.ts.map +1 -0
  23. package/dist/model/AppliedStateTransitionBatch.js +16 -0
  24. package/dist/model/AppliedStateTransitionBatch.js.map +1 -0
  25. package/dist/model/StateTransition.d.ts +1 -0
  26. package/dist/model/StateTransition.d.ts.map +1 -1
  27. package/dist/model/StateTransition.js +5 -0
  28. package/dist/model/StateTransition.js.map +1 -1
  29. package/dist/model/StateTransitionProvableBatch.d.ts +249 -97
  30. package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -1
  31. package/dist/model/StateTransitionProvableBatch.js +72 -65
  32. package/dist/model/StateTransitionProvableBatch.js.map +1 -1
  33. package/dist/model/transaction/RuntimeTransaction.d.ts +1 -0
  34. package/dist/model/transaction/RuntimeTransaction.d.ts.map +1 -1
  35. package/dist/model/transaction/RuntimeTransaction.js +3 -0
  36. package/dist/model/transaction/RuntimeTransaction.js.map +1 -1
  37. package/dist/protocol/ProvableBlockHook.d.ts +13 -3
  38. package/dist/protocol/ProvableBlockHook.d.ts.map +1 -1
  39. package/dist/protocol/ProvableBlockHook.js +16 -0
  40. package/dist/protocol/ProvableBlockHook.js.map +1 -1
  41. package/dist/protocol/ProvableTransactionHook.d.ts +29 -2
  42. package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -1
  43. package/dist/protocol/ProvableTransactionHook.js +9 -0
  44. package/dist/protocol/ProvableTransactionHook.js.map +1 -1
  45. package/dist/prover/accumulators/AppliedBatchHashList.d.ts +18 -0
  46. package/dist/prover/accumulators/AppliedBatchHashList.d.ts.map +1 -0
  47. package/dist/prover/accumulators/AppliedBatchHashList.js +25 -0
  48. package/dist/prover/accumulators/AppliedBatchHashList.js.map +1 -0
  49. package/dist/{utils → prover/accumulators}/StateTransitionReductionList.d.ts +3 -2
  50. package/dist/prover/accumulators/StateTransitionReductionList.d.ts.map +1 -0
  51. package/dist/{utils → prover/accumulators}/StateTransitionReductionList.js +6 -3
  52. package/dist/prover/accumulators/StateTransitionReductionList.js.map +1 -0
  53. package/dist/prover/accumulators/TransactionHashList.d.ts +6 -0
  54. package/dist/prover/accumulators/TransactionHashList.d.ts.map +1 -0
  55. package/dist/prover/accumulators/TransactionHashList.js +8 -0
  56. package/dist/prover/accumulators/TransactionHashList.js.map +1 -0
  57. package/dist/prover/accumulators/WitnessedRootHashList.d.ts +137 -0
  58. package/dist/prover/accumulators/WitnessedRootHashList.d.ts.map +1 -0
  59. package/dist/prover/accumulators/WitnessedRootHashList.js +50 -0
  60. package/dist/prover/accumulators/WitnessedRootHashList.js.map +1 -0
  61. package/dist/prover/block/BlockProvable.d.ts +579 -35
  62. package/dist/prover/block/BlockProvable.d.ts.map +1 -1
  63. package/dist/prover/block/BlockProvable.js +55 -3
  64. package/dist/prover/block/BlockProvable.js.map +1 -1
  65. package/dist/prover/block/BlockProver.d.ts +52 -50
  66. package/dist/prover/block/BlockProver.d.ts.map +1 -1
  67. package/dist/prover/block/BlockProver.js +289 -209
  68. package/dist/prover/block/BlockProver.js.map +1 -1
  69. package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts +55 -13
  70. package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts.map +1 -1
  71. package/dist/prover/block/accummulators/BlockHashMerkleTree.js +11 -3
  72. package/dist/prover/block/accummulators/BlockHashMerkleTree.js.map +1 -1
  73. package/dist/prover/statetransition/StateTransitionProvable.d.ts +107 -106
  74. package/dist/prover/statetransition/StateTransitionProvable.d.ts.map +1 -1
  75. package/dist/prover/statetransition/StateTransitionProvable.js +8 -8
  76. package/dist/prover/statetransition/StateTransitionProvable.js.map +1 -1
  77. package/dist/prover/statetransition/StateTransitionProver.d.ts +14 -11
  78. package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
  79. package/dist/prover/statetransition/StateTransitionProver.js +116 -62
  80. package/dist/prover/statetransition/StateTransitionProver.js.map +1 -1
  81. package/dist/settlement/contracts/DispatchContractProtocolModule.d.ts +1 -1
  82. package/dist/settlement/contracts/DispatchSmartContract.d.ts +1 -1
  83. package/dist/settlement/contracts/SettlementSmartContract.d.ts.map +1 -1
  84. package/dist/settlement/contracts/SettlementSmartContract.js +1 -0
  85. package/dist/settlement/contracts/SettlementSmartContract.js.map +1 -1
  86. package/dist/state/assert/assert.d.ts.map +1 -1
  87. package/dist/state/assert/assert.js +5 -3
  88. package/dist/state/assert/assert.js.map +1 -1
  89. package/dist/state/context/RuntimeMethodExecutionContext.d.ts +3 -3
  90. package/dist/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -1
  91. package/dist/state/context/RuntimeMethodExecutionContext.js +7 -4
  92. package/dist/state/context/RuntimeMethodExecutionContext.js.map +1 -1
  93. package/dist/utils/FieldOptions.d.ts +62 -0
  94. package/dist/utils/FieldOptions.d.ts.map +1 -0
  95. package/dist/utils/FieldOptions.js +13 -0
  96. package/dist/utils/FieldOptions.js.map +1 -0
  97. package/dist/utils/ProvableHashList.d.ts +21 -2
  98. package/dist/utils/ProvableHashList.d.ts.map +1 -1
  99. package/dist/utils/ProvableHashList.js +37 -2
  100. package/dist/utils/ProvableHashList.js.map +1 -1
  101. package/dist/utils/ProvableReductionHashList.d.ts +7 -3
  102. package/dist/utils/ProvableReductionHashList.d.ts.map +1 -1
  103. package/dist/utils/ProvableReductionHashList.js +8 -5
  104. package/dist/utils/ProvableReductionHashList.js.map +1 -1
  105. package/dist/utils/utils.d.ts +10 -1
  106. package/dist/utils/utils.d.ts.map +1 -1
  107. package/dist/utils/utils.js +6 -0
  108. package/dist/utils/utils.js.map +1 -1
  109. package/package.json +2 -2
  110. package/src/hooks/AccountStateHook.ts +12 -3
  111. package/src/hooks/LastStateRootBlockHook.ts +7 -8
  112. package/src/hooks/NoopBlockHook.ts +7 -4
  113. package/src/hooks/NoopTransactionHook.ts +5 -2
  114. package/src/index.ts +6 -1
  115. package/src/model/AppliedStateTransitionBatch.ts +16 -0
  116. package/src/model/StateTransition.ts +6 -0
  117. package/src/model/StateTransitionProvableBatch.ts +94 -105
  118. package/src/model/transaction/RuntimeTransaction.ts +4 -0
  119. package/src/protocol/ProvableBlockHook.ts +51 -3
  120. package/src/protocol/ProvableTransactionHook.ts +67 -3
  121. package/src/prover/accumulators/AppliedBatchHashList.ts +32 -0
  122. package/src/{utils → prover/accumulators}/StateTransitionReductionList.ts +7 -4
  123. package/src/prover/accumulators/TransactionHashList.ts +9 -0
  124. package/src/prover/accumulators/WitnessedRootHashList.ts +61 -0
  125. package/src/prover/block/BlockProvable.ts +128 -9
  126. package/src/prover/block/BlockProver.ts +531 -383
  127. package/src/prover/block/accummulators/BlockHashMerkleTree.ts +11 -3
  128. package/src/prover/statetransition/StateTransitionProvable.ts +17 -11
  129. package/src/prover/statetransition/StateTransitionProver.ts +219 -144
  130. package/src/settlement/contracts/SettlementSmartContract.ts +4 -0
  131. package/src/state/assert/assert.ts +6 -3
  132. package/src/state/context/RuntimeMethodExecutionContext.ts +15 -7
  133. package/src/utils/FieldOptions.ts +13 -0
  134. package/src/utils/ProvableHashList.ts +77 -2
  135. package/src/utils/ProvableReductionHashList.ts +12 -3
  136. package/src/utils/utils.ts +18 -1
  137. package/test/BlockProver.test.ts +2 -0
  138. package/test/TestingProtocol.ts +5 -0
  139. package/test/model/StateTransitionProvableBatch.test.ts +137 -0
  140. package/test/prover/block/BlockProver.test.ts +18 -0
  141. package/test/prover/statetransition/StateTransitionProver.test.ts +240 -0
  142. package/test/utils/ProvableHashList.test.ts +44 -0
  143. package/test/utils/ProvableReductionHashList.test.ts +1 -1
  144. package/dist/utils/StateTransitionReductionList.d.ts.map +0 -1
  145. package/dist/utils/StateTransitionReductionList.js.map +0 -1
@@ -1,4 +1,23 @@
1
- import { Field, Poseidon, Bool, Provable, ProvablePure } from "o1js";
1
+ import {
2
+ Field,
3
+ Poseidon,
4
+ Bool,
5
+ Provable,
6
+ ProvablePure,
7
+ Unconstrained,
8
+ } from "o1js";
9
+
10
+ import { NonMethods } from "./utils";
11
+
12
+ export type ProvableHashListData<Value> = {
13
+ preimage: Field;
14
+ value: NonMethods<Value>;
15
+ };
16
+
17
+ export type VerifiedTransition<T> = {
18
+ from: T;
19
+ to: T;
20
+ };
2
21
 
3
22
  /**
4
23
  * Utilities for creating a hash list from a given value type.
@@ -6,11 +25,49 @@ import { Field, Poseidon, Bool, Provable, ProvablePure } from "o1js";
6
25
  export abstract class ProvableHashList<Value> {
7
26
  public constructor(
8
27
  protected readonly valueType: ProvablePure<Value>,
9
- public commitment: Field = Field(0)
28
+ public commitment: Field = Field(0),
29
+ private unconstrainedList: Unconstrained<
30
+ ProvableHashListData<Value>[]
31
+ > = Unconstrained.from([])
10
32
  ) {}
11
33
 
12
34
  protected abstract hash(elements: Field[]): Field;
13
35
 
36
+ private pushUnconstrained(preimage: Field, value: Value) {
37
+ const valueConstant = this.valueType.fromFields(
38
+ this.valueType.toFields(value).map((field) => field.toConstant())
39
+ );
40
+ this.unconstrainedList.get().push({
41
+ preimage: preimage.toConstant(),
42
+ value: valueConstant,
43
+ });
44
+ }
45
+
46
+ /**
47
+ * Fast-forwards the state of the hashlist to a specified new tip.
48
+ * This assumes the transition (from -> to) to be already verified somewhere
49
+ * else that is outside this scope.
50
+ */
51
+ public fastForward(
52
+ transition: VerifiedTransition<Field>,
53
+ message: string = "some hashlist"
54
+ ) {
55
+ const { from, to } = transition;
56
+ from.assertEquals(
57
+ this.commitment,
58
+ `From-commitment for ${message} not matching`
59
+ );
60
+
61
+ this.commitment = to;
62
+ }
63
+
64
+ public witnessTip(preimage: Field, value: Value): Bool {
65
+ return this.hash([
66
+ this.commitment,
67
+ ...this.valueType.toFields(value),
68
+ ]).equals(this.commitment);
69
+ }
70
+
14
71
  /**
15
72
  * Converts the provided value to Field[] and appends it to
16
73
  * the current hashlist.
@@ -19,19 +76,31 @@ export abstract class ProvableHashList<Value> {
19
76
  * @returns Current hash list.
20
77
  */
21
78
  public push(value: Value) {
79
+ Provable.asProver(() => {
80
+ this.pushUnconstrained(this.commitment, value);
81
+ });
82
+
22
83
  this.commitment = this.hash([
23
84
  this.commitment,
24
85
  ...this.valueType.toFields(value),
25
86
  ]);
87
+
26
88
  return this;
27
89
  }
28
90
 
29
91
  public pushIf(value: Value, condition: Bool) {
92
+ Provable.asProver(() => {
93
+ if (condition.toBoolean()) {
94
+ this.pushUnconstrained(this.commitment, value);
95
+ }
96
+ });
97
+
30
98
  const newCommitment = this.hash([
31
99
  this.commitment,
32
100
  ...this.valueType.toFields(value),
33
101
  ]);
34
102
  this.commitment = Provable.if(condition, newCommitment, this.commitment);
103
+
35
104
  return this;
36
105
  }
37
106
 
@@ -41,6 +110,12 @@ export abstract class ProvableHashList<Value> {
41
110
  public toField() {
42
111
  return this.commitment;
43
112
  }
113
+
114
+ public getUnconstrainedValues(): Unconstrained<
115
+ ProvableHashListData<Value>[]
116
+ > {
117
+ return this.unconstrainedList;
118
+ }
44
119
  }
45
120
 
46
121
  export class DefaultProvableHashList<Value> extends ProvableHashList<Value> {
@@ -1,8 +1,13 @@
1
- import { Bool, Field, Poseidon, Provable } from "o1js";
1
+ import { Bool, Field, Poseidon, Provable, ProvablePure } from "o1js";
2
2
 
3
- import { ProvableHashList } from "./ProvableHashList";
3
+ import { NonMethods } from "./utils";
4
+
5
+ export class ProvableReductionHashList<Value> {
6
+ public constructor(
7
+ protected readonly valueType: ProvablePure<Value>,
8
+ public commitment: Field = Field(0)
9
+ ) {}
4
10
 
5
- export class ProvableReductionHashList<Value> extends ProvableHashList<Value> {
6
11
  public unconstrainedList: Value[] = [];
7
12
 
8
13
  private constrainedLastValue: Value | undefined = undefined;
@@ -65,4 +70,8 @@ export class ProvableReductionHashList<Value> extends ProvableHashList<Value> {
65
70
  public hash(elements: Field[]): Field {
66
71
  return Poseidon.hash(elements);
67
72
  }
73
+
74
+ public getUnconstrainedValues(): NonMethods<Value>[] {
75
+ return this.unconstrainedList;
76
+ }
68
77
  }
@@ -1,4 +1,4 @@
1
- import { Field, Poseidon, Provable } from "o1js";
1
+ import { Bool, Field, Poseidon, Provable } from "o1js";
2
2
  import floor from "lodash/floor";
3
3
 
4
4
  export type ReturnType<FunctionType extends Function> = FunctionType extends (
@@ -73,3 +73,20 @@ export function singleFieldToString(value: Field | bigint): string {
73
73
  }
74
74
  return fieldValue.toString();
75
75
  }
76
+
77
+ type NonMethodKeys<T> = {
78
+ [K in keyof T]: T[K] extends Function ? never : K;
79
+ }[keyof T];
80
+ export type NonMethods<T> = Pick<T, NonMethodKeys<T>>;
81
+
82
+ /**
83
+ * Asserts the equality of a and b, but only if doAssertion is true, otherwise it will assert 0 = 0
84
+ */
85
+ export function assertEqualsIf(
86
+ a: Field,
87
+ b: Field,
88
+ doAssertion: Bool,
89
+ msg: string
90
+ ) {
91
+ a.mul(doAssertion.toField()).assertEquals(b.mul(doAssertion.toField()), msg);
92
+ }
@@ -70,6 +70,7 @@ describe("blockProver", () => {
70
70
 
71
71
  const protocol = createAndInitTestingProtocol();
72
72
 
73
+ /*
73
74
  function generateTestProofs(
74
75
  fromStateRoot: Field,
75
76
  toStateRoot: Field,
@@ -120,6 +121,7 @@ describe("blockProver", () => {
120
121
 
121
122
  return [appProof, stProof];
122
123
  }
124
+ */
123
125
 
124
126
  it("dummy", () => {
125
127
  expect(1).toBe(1);
@@ -9,6 +9,7 @@ import {
9
9
  BlockProver,
10
10
  LastStateRootBlockHook,
11
11
  Protocol,
12
+ StateServiceProvider,
12
13
  StateTransitionProver,
13
14
  } from "../src";
14
15
 
@@ -48,5 +49,9 @@ export function createAndInitTestingProtocol() {
48
49
  });
49
50
  protocol.create(() => appChain.createChildContainer());
50
51
 
52
+ protocol.registerValue({
53
+ StateServiceProvider: new StateServiceProvider(),
54
+ });
55
+
51
56
  return protocol;
52
57
  }
@@ -0,0 +1,137 @@
1
+ import "reflect-metadata";
2
+ import { Bool, Field } from "o1js";
3
+
4
+ import {
5
+ ProvableOption,
6
+ ProvableStateTransition,
7
+ ProvableStateTransitionType,
8
+ StateTransitionProvableBatch,
9
+ } from "../../src";
10
+
11
+ describe("StateTransitionProvableBatch", () => {
12
+ function createST(path: Field, from: Field, to: Field) {
13
+ return new ProvableStateTransition({
14
+ path,
15
+ from: new ProvableOption({
16
+ isSome: Bool(true),
17
+ value: from,
18
+ }),
19
+ to: new ProvableOption({
20
+ isSome: Bool(true),
21
+ value: to,
22
+ }),
23
+ });
24
+ }
25
+
26
+ it("should place witnessRoots correctly on empty batch", () => {
27
+ const st = createST(Field(1), Field(2), Field(3));
28
+ const data = [
29
+ {
30
+ stateTransitions: [st, st],
31
+ applied: Bool(true),
32
+ witnessRoot: Bool(false),
33
+ },
34
+ {
35
+ stateTransitions: [],
36
+ applied: Bool(true),
37
+ witnessRoot: Bool(true),
38
+ },
39
+ ];
40
+
41
+ const batches = StateTransitionProvableBatch.fromBatches(data);
42
+ expect(batches).toHaveLength(1);
43
+
44
+ const { batch } = batches[0];
45
+ expect(batch[0].witnessRoot.toBoolean()).toBe(false);
46
+ expect(batch[1].witnessRoot.toBoolean()).toBe(true);
47
+ // Should be dummy
48
+ expect(batch[2].witnessRoot.toBoolean()).toBe(false);
49
+ });
50
+
51
+ it("should place witnessRoots correctly on empty batch", () => {
52
+ const st = createST(Field(1), Field(2), Field(3));
53
+ const data = [
54
+ {
55
+ stateTransitions: [st, st],
56
+ applied: Bool(true),
57
+ witnessRoot: Bool(false),
58
+ },
59
+ {
60
+ stateTransitions: [st],
61
+ applied: Bool(false),
62
+ witnessRoot: Bool(true),
63
+ },
64
+ ];
65
+
66
+ const batches = StateTransitionProvableBatch.fromBatches(data);
67
+ expect(batches).toHaveLength(1);
68
+
69
+ const { batch } = batches[0];
70
+ expect(batch[0].witnessRoot.toBoolean()).toBe(false);
71
+ expect(batch[1].witnessRoot.toBoolean()).toBe(false);
72
+ expect(batch[2].witnessRoot.toBoolean()).toBe(true);
73
+ });
74
+
75
+ it("should transform correctly", () => {
76
+ const st = createST(Field(1), Field(2), Field(3));
77
+ const data = [
78
+ {
79
+ stateTransitions: [st, st, st, st, st],
80
+ applied: Bool(true),
81
+ witnessRoot: Bool(true),
82
+ },
83
+ {
84
+ stateTransitions: [st, st],
85
+ applied: Bool(false),
86
+ witnessRoot: Bool(false),
87
+ },
88
+ ];
89
+
90
+ const batches = StateTransitionProvableBatch.fromBatches(data);
91
+ expect(batches).toHaveLength(2);
92
+ const { nothing, closeAndThrowAway, closeAndApply } =
93
+ ProvableStateTransitionType;
94
+
95
+ const types = batches[0].batch.map(({ type }) => type);
96
+ const stateTransitions = batches[0].batch.map(
97
+ ({ stateTransition }) => stateTransition
98
+ );
99
+ const witnessRoots = batches[0].batch.map(({ witnessRoot }) => witnessRoot);
100
+
101
+ const types2 = batches[1].batch.map(({ type }) => type);
102
+ const stateTransitions2 = batches[1].batch.map(
103
+ ({ stateTransition }) => stateTransition
104
+ );
105
+ const witnessRoots2 = batches[1].batch.map(
106
+ ({ witnessRoot }) => witnessRoot
107
+ );
108
+
109
+ expect(types).toStrictEqual([nothing, nothing, nothing, nothing]);
110
+ expect(stateTransitions).toStrictEqual([st, st, st, st]);
111
+ expect(witnessRoots).toStrictEqual([
112
+ Bool(false),
113
+ Bool(false),
114
+ Bool(false),
115
+ Bool(false),
116
+ ]);
117
+
118
+ expect(stateTransitions2).toStrictEqual([
119
+ st,
120
+ st,
121
+ st,
122
+ ProvableStateTransition.dummy(),
123
+ ]);
124
+ expect(types2).toStrictEqual([
125
+ closeAndApply,
126
+ nothing,
127
+ closeAndThrowAway,
128
+ nothing,
129
+ ]);
130
+ expect(witnessRoots2).toStrictEqual([
131
+ Bool(true),
132
+ Bool(false),
133
+ Bool(false),
134
+ Bool(false),
135
+ ]);
136
+ });
137
+ });
@@ -0,0 +1,18 @@
1
+ /* eslint-disable max-len */
2
+ /**
3
+ * Testing strategy:
4
+ *
5
+ * - Test that hooks are executed and batches are created correctly
6
+ * - Transaction
7
+ * - Block
8
+ * - Test the various static checks on the transaction (signature, verificationKey, network state hash)
9
+ * - Test correct construction of the batch and list commitments
10
+ * - Test correct integration of the STProof - both defer and notDefer
11
+ * - proveBlock: correct blockNumber progression, closed flag (doesn't accepts closed proofs as tx proofs)
12
+ */
13
+
14
+ /* eslint-enable max-len */
15
+
16
+ it("dummy", () => {
17
+ expect(1).toBe(1);
18
+ });
@@ -0,0 +1,240 @@
1
+ import { InMemoryAreProofsEnabled } from "@proto-kit/sdk";
2
+ import { Bool, Field } from "o1js";
3
+ import {
4
+ InMemoryMerkleTreeStorage,
5
+ padArray,
6
+ RollupMerkleTree,
7
+ RollupMerkleTreeWitness,
8
+ } from "@proto-kit/common";
9
+
10
+ import {
11
+ AppliedStateTransitionBatchState,
12
+ ProvableOption,
13
+ ProvableStateTransition,
14
+ StateTransitionProvableBatch,
15
+ StateTransitionProverProgrammable,
16
+ } from "../../../src";
17
+
18
+ describe("StateTransitionProver", () => {
19
+ let prover: StateTransitionProverProgrammable;
20
+
21
+ function setup() {
22
+ prover = new StateTransitionProverProgrammable({
23
+ get areProofsEnabled() {
24
+ return new InMemoryAreProofsEnabled();
25
+ },
26
+ });
27
+ }
28
+
29
+ beforeEach(() => {
30
+ setup();
31
+ });
32
+
33
+ function createST(path: Field, from: Field, to: Field) {
34
+ return new ProvableStateTransition({
35
+ path,
36
+ from: new ProvableOption({
37
+ isSome: Bool(true),
38
+ value: from,
39
+ }),
40
+ to: new ProvableOption({
41
+ isSome: Bool(true),
42
+ value: to,
43
+ }),
44
+ });
45
+ }
46
+
47
+ // function applyToTree(
48
+ // tree: RollupMerkleTree,
49
+ // batches: StateTransitionProvableBatch[],
50
+ // indizes: number[]
51
+ // ) {
52
+ // const flat = batches.flatMap((batch) => batch.batch);
53
+ // indizes.forEach((index) => {
54
+ // const st = flat[index].stateTransition;
55
+ // if (st.to.isSome) {
56
+ // tree.setLeaf(st.path.toBigInt(), st.to.value);
57
+ // }
58
+ // });
59
+ // }
60
+
61
+ describe("currentBatchHash", () => {
62
+ it.each([
63
+ [true, true],
64
+ [true, false],
65
+ [false, false],
66
+ ])(
67
+ "should retain empty currentBatchHash for padded dummies",
68
+ async (applied, witnessRoot) => {
69
+ const batch = StateTransitionProvableBatch.fromBatches([
70
+ {
71
+ stateTransitions: [createST(Field(1), Field(0), Field(2))],
72
+ applied: Bool(applied),
73
+ witnessRoot: Bool(witnessRoot),
74
+ },
75
+ ]);
76
+
77
+ const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
78
+ const witness = tree.getWitness(1n);
79
+
80
+ const result = await prover.proveBatch(
81
+ {
82
+ root: tree.getRoot(),
83
+ witnessedRootsHash: Field(0),
84
+ batchesHash: Field(0),
85
+ currentBatchStateHash: Field(0),
86
+ },
87
+ batch[0],
88
+ {
89
+ witnesses: padArray([witness], 4, () =>
90
+ RollupMerkleTreeWitness.dummy()
91
+ ),
92
+ },
93
+ new AppliedStateTransitionBatchState({
94
+ root: tree.getRoot(),
95
+ batchHash: Field(0),
96
+ })
97
+ );
98
+
99
+ expect(result.currentBatchStateHash.toString()).toStrictEqual("0");
100
+ }
101
+ );
102
+ });
103
+
104
+ describe("dummies", () => {
105
+ it("should fail if dummy is type close", async () => {
106
+ expect.assertions(1);
107
+
108
+ const batch = StateTransitionProvableBatch.fromBatches([
109
+ {
110
+ stateTransitions: [ProvableStateTransition.dummy()],
111
+ applied: Bool(true),
112
+ witnessRoot: Bool(true),
113
+ },
114
+ ]);
115
+
116
+ const prove = async () =>
117
+ await prover.proveBatch(
118
+ {
119
+ root: Field(RollupMerkleTree.EMPTY_ROOT),
120
+ witnessedRootsHash: Field(0),
121
+ batchesHash: Field(0),
122
+ currentBatchStateHash: Field(0),
123
+ },
124
+ batch[0],
125
+ {
126
+ witnesses: padArray([], 4, RollupMerkleTreeWitness.dummy),
127
+ },
128
+ new AppliedStateTransitionBatchState({
129
+ root: Field(RollupMerkleTree.EMPTY_ROOT),
130
+ batchHash: Field(0),
131
+ })
132
+ );
133
+
134
+ await expect(prove).rejects.toThrow(
135
+ /Dummies have to be of type 'nothing'.*/
136
+ );
137
+ });
138
+
139
+ it("should fail if dummy is in the middle", async () => {
140
+ expect.assertions(1);
141
+
142
+ const batch = StateTransitionProvableBatch.fromBatches([
143
+ {
144
+ stateTransitions: [
145
+ createST(Field(1), Field(0), Field(2)),
146
+ ProvableStateTransition.dummy(),
147
+ createST(Field(2), Field(0), Field(3)),
148
+ ],
149
+ applied: Bool(true),
150
+ witnessRoot: Bool(true),
151
+ },
152
+ ]);
153
+
154
+ const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
155
+
156
+ const inputRoot = tree.getRoot();
157
+
158
+ const witness = tree.getWitness(1n);
159
+ tree.setLeaf(1n, Field(2));
160
+ const witness2 = tree.getWitness(2n);
161
+
162
+ const prove = async () =>
163
+ await prover.proveBatch(
164
+ {
165
+ root: inputRoot,
166
+ witnessedRootsHash: Field(0),
167
+ batchesHash: Field(0),
168
+ currentBatchStateHash: Field(0),
169
+ },
170
+ batch[0],
171
+ {
172
+ witnesses: [
173
+ witness,
174
+ RollupMerkleTreeWitness.dummy(),
175
+ witness2,
176
+ RollupMerkleTreeWitness.dummy(),
177
+ ],
178
+ },
179
+ new AppliedStateTransitionBatchState({
180
+ root: inputRoot,
181
+ batchHash: Field(0),
182
+ })
183
+ );
184
+
185
+ await expect(prove).rejects.toThrow(
186
+ /Dummies can only be placed on closed batchLists.*/
187
+ );
188
+ });
189
+ });
190
+
191
+ describe("batch progression", () => {
192
+ it("should throw away non-applied batches", async () => {
193
+ const batch = StateTransitionProvableBatch.fromBatches([
194
+ {
195
+ stateTransitions: [
196
+ createST(Field(1), Field(0), Field(2)),
197
+ createST(Field(2), Field(0), Field(3)),
198
+ ],
199
+ applied: Bool(true),
200
+ witnessRoot: Bool(true),
201
+ },
202
+ {
203
+ stateTransitions: [
204
+ createST(Field(2), Field(3), Field(4)),
205
+ createST(Field(2), Field(4), Field(5)),
206
+ ],
207
+ applied: Bool(false),
208
+ witnessRoot: Bool(true),
209
+ },
210
+ ]);
211
+
212
+ const tree = new RollupMerkleTree(new InMemoryMerkleTreeStorage());
213
+
214
+ const witness1 = tree.getWitness(1n);
215
+ tree.setLeaf(1n, Field(2));
216
+ const witness2 = tree.getWitness(2n);
217
+ tree.setLeaf(2n, Field(3));
218
+
219
+ const result = await prover.proveBatch(
220
+ {
221
+ root: Field(RollupMerkleTree.EMPTY_ROOT),
222
+ witnessedRootsHash: Field(0),
223
+ batchesHash: Field(0),
224
+ currentBatchStateHash: Field(0),
225
+ },
226
+ batch[0],
227
+ {
228
+ witnesses: [witness1, witness2, witness2, witness2],
229
+ },
230
+ new AppliedStateTransitionBatchState({
231
+ root: Field(RollupMerkleTree.EMPTY_ROOT),
232
+ batchHash: Field(0),
233
+ })
234
+ );
235
+
236
+ expect(result.root.toString()).toStrictEqual(tree.getRoot().toString());
237
+ expect(result.currentBatchStateHash.toString()).toStrictEqual("0");
238
+ });
239
+ });
240
+ });
@@ -0,0 +1,44 @@
1
+ import "reflect-metadata";
2
+ import { Bool, Field, Poseidon } from "o1js";
3
+
4
+ import { DefaultProvableHashList } from "../../src";
5
+
6
+ describe("defaultProvableHashList", () => {
7
+ describe.each([
8
+ [0, [{ value: 1n, push: true }]],
9
+ // [
10
+ // 10,
11
+ // [
12
+ // { value: 1n, push: true },
13
+ // { value: 5n, push: false },
14
+ // ],
15
+ // ],
16
+ // [
17
+ // 10,
18
+ // [
19
+ // { value: 1n, push: true },
20
+ // { value: 5n, push: false },
21
+ // { value: 6n, push: true },
22
+ // ],
23
+ // ],
24
+ ])("should correctly append and save", (start, elements) => {
25
+ it("Using only pushIf", () => {
26
+ const hashList = new DefaultProvableHashList(Field, Field(start));
27
+
28
+ // eslint-disable-next-line sonarjs/no-unused-collection
29
+ const appended: bigint[] = [];
30
+ let hash = Field(start);
31
+
32
+ for (const element of elements) {
33
+ hashList.pushIf(Field(element.value), Bool(element.push));
34
+ if (element.push) {
35
+ appended.push(element.value);
36
+
37
+ hash = Poseidon.hash([hash, Field(element.value)]);
38
+ }
39
+ }
40
+
41
+ expect(hash.toString()).toStrictEqual(hashList.commitment.toString());
42
+ });
43
+ });
44
+ });
@@ -4,7 +4,7 @@ import { Bool, Field, Poseidon } from "o1js";
4
4
  import {
5
5
  StateTransitionReductionList,
6
6
  reduceStateTransitions,
7
- } from "../../src/utils/StateTransitionReductionList";
7
+ } from "../../src/prover/accumulators/StateTransitionReductionList";
8
8
  import {
9
9
  DefaultProvableHashList,
10
10
  ProvableStateTransition,
@@ -1 +0,0 @@
1
- {"version":3,"file":"StateTransitionReductionList.d.ts","sourceRoot":"","sources":["../../src/utils/StateTransitionReductionList.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,uBAAuB,EACvB,eAAe,EAChB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAExE,qBAAa,4BAA6B,SAAQ,yBAAyB,CAAC,uBAAuB,CAAC;IAC3F,IAAI,CAAC,KAAK,EAAE,uBAAuB;IAMnC,gBAAgB,CAAC,KAAK,EAAE,uBAAuB;;;;CAyCvD;AAED,wBAAgB,sBAAsB,CACpC,WAAW,EAAE,eAAe,CAAC,OAAO,CAAC,EAAE,GACtC,eAAe,CAAC,OAAO,CAAC,EAAE,CA0B5B"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"StateTransitionReductionList.js","sourceRoot":"","sources":["../../src/utils/StateTransitionReductionList.ts"],"names":[],"mappings":"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"}