@proto-kit/protocol 0.1.1-develop.245 → 0.1.1-develop.254

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 (31) hide show
  1. package/dist/model/Option.d.ts +0 -3
  2. package/dist/model/Option.d.ts.map +1 -1
  3. package/dist/model/StateTransition.d.ts +12 -12
  4. package/dist/model/StateTransition.d.ts.map +1 -1
  5. package/dist/model/StateTransition.js +1 -1
  6. package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -1
  7. package/dist/model/StateTransitionProvableBatch.js +11 -1
  8. package/dist/prover/block/BlockProver.js +4 -6
  9. package/dist/prover/statetransition/StateTransitionProvable.d.ts +16 -0
  10. package/dist/prover/statetransition/StateTransitionProvable.d.ts.map +1 -1
  11. package/dist/prover/statetransition/StateTransitionProvable.js +2 -0
  12. package/dist/prover/statetransition/StateTransitionProver.d.ts +2 -1
  13. package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
  14. package/dist/prover/statetransition/StateTransitionProver.js +20 -15
  15. package/dist/state/assert/assert.d.ts.map +1 -1
  16. package/dist/state/assert/assert.js +1 -2
  17. package/dist/state/protocol/ProtocolState.d.ts.map +1 -1
  18. package/package.json +2 -2
  19. package/src/model/Option.ts +0 -5
  20. package/src/model/StateTransition.ts +1 -1
  21. package/src/model/StateTransitionProvableBatch.ts +13 -1
  22. package/src/prover/block/BlockProver.ts +8 -8
  23. package/src/prover/statetransition/StateTransitionProvable.ts +2 -0
  24. package/src/prover/statetransition/StateTransitionProver.ts +59 -20
  25. package/src/state/assert/assert.test.ts +7 -0
  26. package/src/state/assert/assert.ts +2 -3
  27. package/src/state/protocol/ProtocolState.ts +0 -1
  28. package/test/State.test.ts +45 -0
  29. package/dist/utils/merkletree/MemoryMerkleTreeStorage.d.ts +0 -26
  30. package/dist/utils/merkletree/MemoryMerkleTreeStorage.d.ts.map +0 -1
  31. package/dist/utils/merkletree/MemoryMerkleTreeStorage.js +0 -79
@@ -36,9 +36,6 @@ declare const ProvableOption_base: (new (value: {
36
36
  export declare class ProvableOption extends ProvableOption_base {
37
37
  toSome(): this;
38
38
  }
39
- export interface ToFieldable {
40
- toFields: () => Field[];
41
- }
42
39
  /**
43
40
  * Option facilitating in-circuit values that may or may not exist.
44
41
  */
@@ -1 +1 @@
1
- {"version":3,"file":"Option.d.ts","sourceRoot":"","sources":["../../src/model/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,KAAK,EAEL,KAAK,oBAAoB,EAI1B,MAAM,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAElB,qBAAa,cAAe,SAAQ,mBAGlC;IACO,MAAM;CAId;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,KAAK,EAAE,CAAC;CACzB;AAED;;GAEG;AACH,qBAAa,MAAM,CAAC,KAAK;IAyCd,MAAM,EAAE,IAAI;IACZ,KAAK,EAAE,KAAK;IACZ,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IA1C/C;;;;;;;OAOG;WACW,IAAI,CAAC,KAAK,EACtB,MAAM,EAAE,IAAI,EACZ,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAKxC;;;;;;OAMG;WACW,SAAS,CAAC,KAAK,EAC3B,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAKxC;;OAEG;WACW,IAAI;IAIX,YAAY,6CAAe;gBAGzB,MAAM,EAAE,IAAI,EACZ,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAGxC,KAAK;IAIL,SAAS;IAKhB;;OAEG;IACH,IAAW,SAAS,iDAQnB;IAED;;;OAGG;IACI,QAAQ,IAAI,KAAK,EAAE;IAO1B;;OAEG;IACI,UAAU;IAOjB;;;OAGG;IACI,MAAM,CAAC,YAAY,EAAE,KAAK,GAAG,KAAK;IASlC,MAAM;;;;CAYd"}
1
+ {"version":3,"file":"Option.d.ts","sourceRoot":"","sources":["../../src/model/Option.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,KAAK,EACL,KAAK,oBAAoB,EAI1B,MAAM,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAElB,qBAAa,cAAe,SAAQ,mBAGlC;IACO,MAAM;CAId;AAED;;GAEG;AACH,qBAAa,MAAM,CAAC,KAAK;IAyCd,MAAM,EAAE,IAAI;IACZ,KAAK,EAAE,KAAK;IACZ,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IA1C/C;;;;;;;OAOG;WACW,IAAI,CAAC,KAAK,EACtB,MAAM,EAAE,IAAI,EACZ,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAKxC;;;;;;OAMG;WACW,SAAS,CAAC,KAAK,EAC3B,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAKxC;;OAEG;WACW,IAAI;IAIX,YAAY,6CAAe;gBAGzB,MAAM,EAAE,IAAI,EACZ,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAGxC,KAAK;IAIL,SAAS;IAKhB;;OAEG;IACH,IAAW,SAAS,iDAQnB;IAED;;;OAGG;IACI,QAAQ,IAAI,KAAK,EAAE;IAO1B;;OAEG;IACI,UAAU;IAOjB;;;OAGG;IACI,MAAM,CAAC,YAAY,EAAE,KAAK,GAAG,KAAK;IASlC,MAAM;;;;CAYd"}
@@ -1,30 +1,30 @@
1
1
  import { Field } from "snarkyjs";
2
- import { Option, ProvableOption } from "./Option.js";
2
+ import { Option, ProvableOption } from "./Option";
3
3
  declare const ProvableStateTransition_base: (new (value: {
4
- path: import("snarkyjs/dist/node/lib/field.js").Field;
4
+ path: import("snarkyjs/dist/node/lib/field").Field;
5
5
  from: ProvableOption;
6
6
  to: ProvableOption;
7
7
  }) => {
8
- path: import("snarkyjs/dist/node/lib/field.js").Field;
8
+ path: import("snarkyjs/dist/node/lib/field").Field;
9
9
  from: ProvableOption;
10
10
  to: ProvableOption;
11
11
  }) & {
12
12
  _isStruct: true;
13
- } & import("snarkyjs/dist/node/snarky.js").ProvablePure<{
14
- path: import("snarkyjs/dist/node/lib/field.js").Field;
13
+ } & import("snarkyjs/dist/node/snarky").ProvablePure<{
14
+ path: import("snarkyjs/dist/node/lib/field").Field;
15
15
  from: ProvableOption;
16
16
  to: ProvableOption;
17
17
  }> & {
18
18
  toInput: (x: {
19
- path: import("snarkyjs/dist/node/lib/field.js").Field;
19
+ path: import("snarkyjs/dist/node/lib/field").Field;
20
20
  from: ProvableOption;
21
21
  to: ProvableOption;
22
22
  }) => {
23
- fields?: import("snarkyjs/dist/node/lib/field.js").Field[] | undefined;
24
- packed?: [import("snarkyjs/dist/node/lib/field.js").Field, number][] | undefined;
23
+ fields?: import("snarkyjs/dist/node/lib/field").Field[] | undefined;
24
+ packed?: [import("snarkyjs/dist/node/lib/field").Field, number][] | undefined;
25
25
  };
26
26
  toJSON: (x: {
27
- path: import("snarkyjs/dist/node/lib/field.js").Field;
27
+ path: import("snarkyjs/dist/node/lib/field").Field;
28
28
  from: ProvableOption;
29
29
  to: ProvableOption;
30
30
  }) => {
@@ -49,7 +49,7 @@ declare const ProvableStateTransition_base: (new (value: {
49
49
  value: string;
50
50
  };
51
51
  }) => {
52
- path: import("snarkyjs/dist/node/lib/field.js").Field;
52
+ path: import("snarkyjs/dist/node/lib/field").Field;
53
53
  from: ProvableOption;
54
54
  to: ProvableOption;
55
55
  };
@@ -73,8 +73,8 @@ export declare class StateTransition<Value> {
73
73
  static from<Value>(path: Field, fromValue: Option<Value>): StateTransition<Value>;
74
74
  static fromTo<Value>(path: Field, fromValue: Option<Value>, toValue: Option<Value>): StateTransition<Value>;
75
75
  constructor(path: Field, fromValue: Option<Field> | Option<Value>, toValue: Option<Field> | Option<Value>);
76
- get from(): Option<import("snarkyjs/dist/node/lib/field.js").Field> | Option<Value>;
77
- get to(): Option<import("snarkyjs/dist/node/lib/field.js").Field> | Option<Value>;
76
+ get from(): Option<import("snarkyjs/dist/node/lib/field").Field> | Option<Value>;
77
+ get to(): Option<import("snarkyjs/dist/node/lib/field").Field> | Option<Value>;
78
78
  /**
79
79
  * Converts a StateTransition to a ProvableStateTransition,
80
80
  * while enforcing the 'from' property to be 'Some' in all cases.
@@ -1 +1 @@
1
- {"version":3,"file":"StateTransition.d.ts","sourceRoot":"","sources":["../../src/model/StateTransition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAU,MAAM,UAAU,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,cAAc,EAAe,MAAM,aAAa,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAElE;;;;GAIG;AACH,qBAAa,uBAAwB,SAAQ,4BAQ3C;WACc,KAAK,IAAI,uBAAuB;CAO/C;AAED;;;GAGG;AACH,qBAAa,eAAe,CAAC,KAAK;IAcvB,IAAI,EAAE,KAAK;IACX,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;IACxC,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;WAfjC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC;WAIjD,MAAM,CAAC,KAAK,EACxB,IAAI,EAAE,KAAK,EACX,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,EACxB,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC;gBAMf,IAAI,EAAE,KAAK,EACX,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,EACxC,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,MAA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  import { Field, Struct } from "snarkyjs";
2
- import { Option, ProvableOption } from "./Option.js";
2
+ import { Option, ProvableOption } from "./Option";
3
3
  /**
4
4
  * Provable representation of a State Transition, used to
5
5
  * normalize state transitions of various value types for
@@ -1 +1 @@
1
- {"version":3,"file":"StateTransitionProvableBatch.d.ts","sourceRoot":"","sources":["../../src/model/StateTransitionProvableBatch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAoB,MAAM,UAAU,CAAC;AAIlD,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAG/D,qBAAa,mBAAmB;IAC9B,gBAAuB,MAAM,QAAQ;IACrC,gBAAuB,QAAQ,SAAS;WAE1B,QAAQ,CAAC,IAAI,EAAE,OAAO;WAItB,UAAU,CAAC,IAAI,EAAE,OAAO;CAGvC;;;;;;;;;;;;;;;;;;;;;;;;;;;AAED,qBAAa,2BAA4B,SAAQ,gCAE/C;IACA,WAAkB,MAAM,IAAI,2BAA2B,CAItD;IAED,WAAkB,QAAQ,IAAI,2BAA2B,CAIxD;IAEM,QAAQ,IAAI,IAAI;IAIhB,UAAU,IAAI,IAAI;CAG1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAED;;;;;;GAMG;AACH,qBAAa,4BAA6B,SAAQ,iCAUhD;WACc,YAAY,CACxB,WAAW,EAAE;QACX,UAAU,EAAE,uBAAuB,CAAC;QACpC,IAAI,EAAE,2BAA2B,CAAC;KACnC,EAAE,GACF,4BAA4B;WAcjB,eAAe,CAC3B,WAAW,EAAE,uBAAuB,EAAE,EACtC,mBAAmB,EAAE,uBAAuB,EAAE,GAC7C,4BAA4B;IAsB/B,OAAO;CAMR"}
1
+ {"version":3,"file":"StateTransitionProvableBatch.d.ts","sourceRoot":"","sources":["../../src/model/StateTransitionProvableBatch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAoB,MAAM,UAAU,CAAC;AAKlD,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,qBAAa,mBAAmB;IAC9B,gBAAuB,MAAM,QAAQ;IAErC,gBAAuB,QAAQ,SAAS;WAE1B,QAAQ,CAAC,IAAI,EAAE,OAAO;WAItB,UAAU,CAAC,IAAI,EAAE,OAAO;CAGvC;;;;;;;;;;;;;;;;;;;;;;;;;;;AAED,qBAAa,2BAA4B,SAAQ,gCAE/C;IACA,WAAkB,MAAM,IAAI,2BAA2B,CAItD;IAED,WAAkB,QAAQ,IAAI,2BAA2B,CAIxD;IAEM,QAAQ,IAAI,IAAI;IAIhB,UAAU,IAAI,IAAI;CAG1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAED;;;;;;GAMG;AACH,qBAAa,4BAA6B,SAAQ,iCAUhD;WACc,YAAY,CACxB,WAAW,EAAE;QACX,UAAU,EAAE,uBAAuB,CAAC;QACpC,IAAI,EAAE,2BAA2B,CAAC;KACnC,EAAE,GACF,4BAA4B;WAyBjB,eAAe,CAC3B,WAAW,EAAE,uBAAuB,EAAE,EACtC,mBAAmB,EAAE,uBAAuB,EAAE,GAC7C,4BAA4B;IAsB/B,OAAO;CAMR"}
@@ -1,7 +1,7 @@
1
1
  import { Bool, Provable, Struct } from "snarkyjs";
2
+ import { range } from "@proto-kit/common";
2
3
  import { constants } from "../Constants";
3
4
  import { ProvableStateTransition } from "./StateTransition.js";
4
- import { range } from "@proto-kit/common";
5
5
  export class StateTransitionType {
6
6
  static isNormal(type) {
7
7
  return type === StateTransitionType.normal;
@@ -46,6 +46,16 @@ export class StateTransitionProvableBatch extends Struct({
46
46
  static fromMappings(transitions) {
47
47
  const batch = transitions.map((entry) => entry.transition);
48
48
  const transitionTypes = transitions.map((entry) => entry.type);
49
+ // Check that order is correct
50
+ let normalSTsStarted = false;
51
+ transitionTypes.forEach((x) => {
52
+ if (!normalSTsStarted && x.isNormal().toBoolean()) {
53
+ normalSTsStarted = true;
54
+ }
55
+ if (normalSTsStarted && x.isProtocol().toBoolean()) {
56
+ throw new Error("Order in initializing STBatch not correct");
57
+ }
58
+ });
49
59
  while (batch.length < constants.stateTransitionProverBatchSize) {
50
60
  batch.push(ProvableStateTransition.dummy());
51
61
  transitionTypes.push(ProvableStateTransitionType.normal);
@@ -55,22 +55,20 @@ export class BlockProverProgrammable extends ZkProgrammable {
55
55
  appProof.verify();
56
56
  stateTransitionProof.verify();
57
57
  const stateTo = { ...state };
58
- // eslint-disable-next-line no-warning-comments
59
- // TODO Check methodId?
60
58
  // Checks for the stateTransitionProof and appProof matching
61
59
  stateTransitionProof.publicInput.stateTransitionsHash.assertEquals(Field(0), errors.stateProofNotStartingAtZero());
62
60
  appProof.publicOutput.stateTransitionsHash.assertEquals(stateTransitionProof.publicOutput.stateTransitionsHash, errors.stateTransitionsHashNotEqual());
63
- // Apply state if status success
61
+ // Assert from state roots
64
62
  state.stateRoot.assertEquals(stateTransitionProof.publicInput.stateRoot, errors.propertyNotMatching("from state root"));
65
- stateTo.stateRoot = Provable.if(appProof.publicOutput.status, stateTransitionProof.publicOutput.stateRoot, stateTransitionProof.publicInput.stateRoot);
63
+ state.stateRoot.assertEquals(stateTransitionProof.publicInput.protocolStateRoot, errors.propertyNotMatching("from protocol state root"));
64
+ // Apply state if status success
65
+ stateTo.stateRoot = Provable.if(appProof.publicOutput.status, stateTransitionProof.publicOutput.stateRoot, stateTransitionProof.publicOutput.protocolStateRoot);
66
66
  // Apply protocol state transitions
67
67
  this.assertProtocolTransitions(stateTransitionProof, executionData);
68
68
  // Check transaction signature
69
69
  transaction
70
70
  .validateSignature()
71
71
  .assertTrue("Transaction signature not valid");
72
- // Check if the methodId is correct
73
- // to do
74
72
  // Check transaction integrity against appProof
75
73
  const blockTransactionHash = RuntimeTransaction.fromProtocolTransaction(transaction).hash();
76
74
  blockTransactionHash.assertEquals(appProof.publicOutput.transactionHash, "Transactions provided in AppProof and BlockProof do not match");
@@ -6,21 +6,25 @@ declare const StateTransitionProverPublicInput_base: (new (value: {
6
6
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
7
7
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
8
8
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
9
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
9
10
  }) => {
10
11
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
11
12
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
12
13
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
14
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
13
15
  }) & {
14
16
  _isStruct: true;
15
17
  } & import("snarkyjs/dist/node/snarky").ProvablePure<{
16
18
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
17
19
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
18
20
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
21
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
19
22
  }> & {
20
23
  toInput: (x: {
21
24
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
22
25
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
23
26
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
27
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
24
28
  }) => {
25
29
  fields?: import("snarkyjs/dist/node/lib/field").Field[] | undefined;
26
30
  packed?: [import("snarkyjs/dist/node/lib/field").Field, number][] | undefined;
@@ -29,19 +33,23 @@ declare const StateTransitionProverPublicInput_base: (new (value: {
29
33
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
30
34
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
31
35
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
36
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
32
37
  }) => {
33
38
  stateTransitionsHash: string;
34
39
  protocolTransitionsHash: string;
35
40
  stateRoot: string;
41
+ protocolStateRoot: string;
36
42
  };
37
43
  fromJSON: (x: {
38
44
  stateTransitionsHash: string;
39
45
  protocolTransitionsHash: string;
40
46
  stateRoot: string;
47
+ protocolStateRoot: string;
41
48
  }) => {
42
49
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
43
50
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
44
51
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
52
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
45
53
  };
46
54
  };
47
55
  export declare class StateTransitionProverPublicInput extends StateTransitionProverPublicInput_base {
@@ -50,21 +58,25 @@ declare const StateTransitionProverPublicOutput_base: (new (value: {
50
58
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
51
59
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
52
60
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
61
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
53
62
  }) => {
54
63
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
55
64
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
56
65
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
66
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
57
67
  }) & {
58
68
  _isStruct: true;
59
69
  } & import("snarkyjs/dist/node/snarky").ProvablePure<{
60
70
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
61
71
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
62
72
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
73
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
63
74
  }> & {
64
75
  toInput: (x: {
65
76
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
66
77
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
67
78
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
79
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
68
80
  }) => {
69
81
  fields?: import("snarkyjs/dist/node/lib/field").Field[] | undefined;
70
82
  packed?: [import("snarkyjs/dist/node/lib/field").Field, number][] | undefined;
@@ -73,19 +85,23 @@ declare const StateTransitionProverPublicOutput_base: (new (value: {
73
85
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
74
86
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
75
87
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
88
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
76
89
  }) => {
77
90
  stateTransitionsHash: string;
78
91
  protocolTransitionsHash: string;
79
92
  stateRoot: string;
93
+ protocolStateRoot: string;
80
94
  };
81
95
  fromJSON: (x: {
82
96
  stateTransitionsHash: string;
83
97
  protocolTransitionsHash: string;
84
98
  stateRoot: string;
99
+ protocolStateRoot: string;
85
100
  }) => {
86
101
  stateTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
87
102
  protocolTransitionsHash: import("snarkyjs/dist/node/lib/field").Field;
88
103
  stateRoot: import("snarkyjs/dist/node/lib/field").Field;
104
+ protocolStateRoot: import("snarkyjs/dist/node/lib/field").Field;
89
105
  };
90
106
  };
91
107
  export declare class StateTransitionProverPublicOutput extends StateTransitionProverPublicOutput_base {
@@ -1 +1 @@
1
- {"version":3,"file":"StateTransitionProvable.d.ts","sourceRoot":"","sources":["../../../src/prover/statetransition/StateTransitionProvable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,EAAU,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAkB,MAAM,mBAAmB,CAAC;AAEvE,OAAO,EAAE,4BAA4B,EAAE,MAAM,0CAA0C,CAAC;AAExF,OAAO,EAAE,uCAAuC,EAAE,MAAM,2CAA2C,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEpG,qBAAa,gCAAiC,SAAQ,qCAIpD;CAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEL,qBAAa,iCAAkC,SAAQ,sCAIrD;CAAG;AAEL,MAAM,MAAM,oBAAoB,GAAG,KAAK,CACtC,gCAAgC,EAChC,iCAAiC,CAClC,CAAC;AAEF,MAAM,WAAW,uBACf,SAAQ,kBAAkB,CACxB,gCAAgC,EAChC,iCAAiC,CAClC;IACD,wBAAwB,EAAE,uCAAuC,CAAC;IAElE,QAAQ,EAAE,CACR,WAAW,EAAE,gCAAgC,EAC7C,KAAK,EAAE,4BAA4B,KAChC,iCAAiC,CAAC;IAEvC,KAAK,EAAE,CACL,WAAW,EAAE,gCAAgC,EAC7C,MAAM,EAAE,oBAAoB,EAC5B,MAAM,EAAE,oBAAoB,KACzB,iCAAiC,CAAC;CACxC"}
1
+ {"version":3,"file":"StateTransitionProvable.d.ts","sourceRoot":"","sources":["../../../src/prover/statetransition/StateTransitionProvable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,EAAU,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAkB,MAAM,mBAAmB,CAAC;AAEvE,OAAO,EAAE,4BAA4B,EAAE,MAAM,0CAA0C,CAAC;AAExF,OAAO,EAAE,uCAAuC,EAAE,MAAM,2CAA2C,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEpG,qBAAa,gCAAiC,SAAQ,qCAKpD;CAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEL,qBAAa,iCAAkC,SAAQ,sCAKrD;CAAG;AAEL,MAAM,MAAM,oBAAoB,GAAG,KAAK,CACtC,gCAAgC,EAChC,iCAAiC,CAClC,CAAC;AAEF,MAAM,WAAW,uBACf,SAAQ,kBAAkB,CACxB,gCAAgC,EAChC,iCAAiC,CAClC;IACD,wBAAwB,EAAE,uCAAuC,CAAC;IAElE,QAAQ,EAAE,CACR,WAAW,EAAE,gCAAgC,EAC7C,KAAK,EAAE,4BAA4B,KAChC,iCAAiC,CAAC;IAEvC,KAAK,EAAE,CACL,WAAW,EAAE,gCAAgC,EAC7C,MAAM,EAAE,oBAAoB,EAC5B,MAAM,EAAE,oBAAoB,KACzB,iCAAiC,CAAC;CACxC"}
@@ -3,11 +3,13 @@ export class StateTransitionProverPublicInput extends Struct({
3
3
  stateTransitionsHash: Field,
4
4
  protocolTransitionsHash: Field,
5
5
  stateRoot: Field,
6
+ protocolStateRoot: Field,
6
7
  }) {
7
8
  }
8
9
  export class StateTransitionProverPublicOutput extends Struct({
9
10
  stateTransitionsHash: Field,
10
11
  protocolTransitionsHash: Field,
11
12
  stateRoot: Field,
13
+ protocolStateRoot: Field,
12
14
  }) {
13
15
  }
@@ -8,6 +8,7 @@ import { StateTransitionProvable, StateTransitionProverPublicInput, StateTransit
8
8
  import { StateTransitionWitnessProviderReference } from "./StateTransitionWitnessProviderReference";
9
9
  interface StateTransitionProverExecutionState {
10
10
  stateRoot: Field;
11
+ protocolStateRoot: Field;
11
12
  stateTransitionList: ProvableHashList<ProvableStateTransition>;
12
13
  protocolTransitionList: ProvableHashList<ProvableStateTransition>;
13
14
  }
@@ -26,7 +27,7 @@ export declare class StateTransitionProverProgrammable extends ZkProgrammable<St
26
27
  * Applies the state transitions to the current stateRoot
27
28
  * and returns the new prover state
28
29
  */
29
- applyTransitions(stateRoot: Field, stateTransitionCommitmentFrom: Field, protocolTransitionCommitmentFrom: Field, transitionBatch: StateTransitionProvableBatch): StateTransitionProverExecutionState;
30
+ applyTransitions(stateRoot: Field, protocolStateRoot: Field, stateTransitionCommitmentFrom: Field, protocolTransitionCommitmentFrom: Field, transitionBatch: StateTransitionProvableBatch): StateTransitionProverExecutionState;
30
31
  /**
31
32
  * Applies a single state transition to the given state
32
33
  * and mutates it in place
@@ -1 +1 @@
1
- {"version":3,"file":"StateTransitionProver.d.ts","sourceRoot":"","sources":["../../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GG;IACI,eAAe,CACpB,KAAK,EAAE,mCAAmC,EAC1C,UAAU,EAAE,uBAAuB,EACnC,IAAI,EAAE,2BAA2B,EACjC,KAAK,SAAI;IAwCX;;OAEG;IAEI,QAAQ,CACb,WAAW,EAAE,gCAAgC,EAC7C,KAAK,EAAE,4BAA4B,GAClC,iCAAiC;IAgB7B,KAAK,CACV,WAAW,EAAE,gCAAgC,EAC7C,MAAM,EAAE,oBAAoB,EAC5B,MAAM,EAAE,oBAAoB,GAC3B,iCAAiC;CA0CrC;AAED,qBACa,qBACX,SAAQ,cACR,YAAW,uBAAuB;aAMhB,wBAAwB,EAAE,uCAAuC;IAJnF,SAAgB,cAAc,EAAE,iCAAiC,CAAC;gBAIhD,wBAAwB,EAAE,uCAAuC;IAS5E,QAAQ,CACb,WAAW,EAAE,gCAAgC,EAC7C,KAAK,EAAE,4BAA4B,GAClC,iCAAiC;IAI7B,KAAK,CACV,WAAW,EAAE,gCAAgC,EAC7C,MAAM,EAAE,oBAAoB,EAC5B,MAAM,EAAE,oBAAoB,GAC3B,iCAAiC;CAGrC"}
1
+ {"version":3,"file":"StateTransitionProver.d.ts","sourceRoot":"","sources":["../../../src/prover/statetransition/StateTransitionProver.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,EAAuB,MAAM,UAAU,CAAC;AAE1E,OAAO,EACL,gBAAgB,EAEhB,cAAc,EAEd,cAAc,EACf,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EAEL,gBAAgB,EACjB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EACL,2BAA2B,EAC3B,4BAA4B,EAC7B,MAAM,0CAA0C,CAAC;AAElD,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAG/D,OAAO,EACL,uBAAuB,EACvB,gCAAgC,EAChC,oBAAoB,EACpB,iCAAiC,EAClC,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAA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wBAAwB,EAAE,uCAAuC;IAS5E,QAAQ,CACb,WAAW,EAAE,gCAAgC,EAC7C,KAAK,EAAE,4BAA4B,GAClC,iCAAiC;IAI7B,KAAK,CACV,WAAW,EAAE,gCAAgC,EAC7C,MAAM,EAAE,oBAAoB,EAC5B,MAAM,EAAE,oBAAoB,GAC3B,iCAAiC;CAGrC"}
@@ -20,12 +20,9 @@ import { ProtocolModule } from "../../protocol/ProtocolModule";
20
20
  import { StateTransitionProverPublicInput, StateTransitionProverPublicOutput, } from "./StateTransitionProvable";
21
21
  import { StateTransitionWitnessProviderReference } from "./StateTransitionWitnessProviderReference";
22
22
  const errors = {
23
- stateRootNotMatching: (step) => `StateRoots not matching ${step}`,
24
- stateTransitionsHashNotMatching: (step) => `State transitions hash not matching ${step}`,
25
- protocolTransitionsHashNotMatching: (step) => `Protocol transitions hash not matching ${step}`,
26
- merkleWitnessNotCorrect: (index) => `MerkleWitness not valid for StateTransition (${index})`,
23
+ propertyNotMatching: (property, step) => `${property} not matching ${step}`,
24
+ merkleWitnessNotCorrect: (index, type) => `MerkleWitness not valid for StateTransition (${index}, type ${type})`,
27
25
  noWitnessProviderSet: () => new Error("WitnessProvider not set, set it before you use StateTransitionProvider"),
28
- propertyNotMatching: (propertyName) => `${propertyName} not matching`,
29
26
  };
30
27
  const StateTransitionSelfProofClass = (SelfProof);
31
28
  /**
@@ -91,9 +88,10 @@ export class StateTransitionProverProgrammable extends ZkProgrammable {
91
88
  * Applies the state transitions to the current stateRoot
92
89
  * and returns the new prover state
93
90
  */
94
- applyTransitions(stateRoot, stateTransitionCommitmentFrom, protocolTransitionCommitmentFrom, transitionBatch) {
91
+ applyTransitions(stateRoot, protocolStateRoot, stateTransitionCommitmentFrom, protocolTransitionCommitmentFrom, transitionBatch) {
95
92
  const state = {
96
93
  stateRoot,
94
+ protocolStateRoot,
97
95
  stateTransitionList: new DefaultProvableHashList(ProvableStateTransition, stateTransitionCommitmentFrom),
98
96
  protocolTransitionList: new DefaultProvableHashList(ProvableStateTransition, protocolTransitionCommitmentFrom),
99
97
  };
@@ -113,10 +111,12 @@ export class StateTransitionProverProgrammable extends ZkProgrammable {
113
111
  const membershipValid = MerkleTreeUtils.checkMembership(treeWitness, state.stateRoot, transition.path, transition.from.value);
114
112
  membershipValid
115
113
  .or(transition.from.isSome.not())
116
- .assertTrue(errors.merkleWitnessNotCorrect(index));
117
- const t = Date.now();
114
+ .assertTrue(errors.merkleWitnessNotCorrect(index, type.isNormal().toBoolean() ? "normal" : "protocol"));
118
115
  const newRoot = MerkleTreeUtils.computeRoot(treeWitness, transition.to.value);
119
116
  state.stateRoot = Provable.if(transition.to.isSome, newRoot, state.stateRoot);
117
+ // Only update protocol state root if ST is also of type protocol
118
+ // Since protocol STs are all at the start of the batch, this works
119
+ state.protocolStateRoot = Provable.if(transition.to.isSome.and(type.isProtocol()), newRoot, state.protocolStateRoot);
120
120
  const isNotDummy = transition.path.equals(Field(0)).not();
121
121
  state.stateTransitionList.pushIf(transition, isNotDummy.and(type.isNormal()));
122
122
  state.protocolTransitionList.pushIf(transition, isNotDummy.and(type.isProtocol()));
@@ -125,29 +125,34 @@ export class StateTransitionProverProgrammable extends ZkProgrammable {
125
125
  * Applies a whole batch of StateTransitions at once
126
126
  */
127
127
  runBatch(publicInput, batch) {
128
- const result = this.applyTransitions(publicInput.stateRoot, publicInput.stateTransitionsHash, publicInput.protocolTransitionsHash, batch);
128
+ const result = this.applyTransitions(publicInput.stateRoot, publicInput.protocolStateRoot, publicInput.stateTransitionsHash, publicInput.protocolTransitionsHash, batch);
129
129
  return new StateTransitionProverPublicOutput({
130
130
  stateRoot: result.stateRoot,
131
131
  stateTransitionsHash: result.stateTransitionList.commitment,
132
132
  protocolTransitionsHash: result.protocolTransitionList.commitment,
133
+ protocolStateRoot: result.protocolStateRoot,
133
134
  });
134
135
  }
135
136
  merge(publicInput, proof1, proof2) {
136
137
  proof1.verify();
137
138
  proof2.verify();
138
139
  // Check state
139
- publicInput.stateRoot.assertEquals(proof1.publicInput.stateRoot, errors.stateRootNotMatching("publicInput.from -> proof1.from"));
140
- proof1.publicOutput.stateRoot.assertEquals(proof2.publicInput.stateRoot, errors.stateRootNotMatching("proof1.to -> proof2.from"));
140
+ publicInput.stateRoot.assertEquals(proof1.publicInput.stateRoot, errors.propertyNotMatching("stateRoot", "publicInput.from -> proof1.from"));
141
+ proof1.publicOutput.stateRoot.assertEquals(proof2.publicInput.stateRoot, errors.propertyNotMatching("stateRoot", "proof1.to -> proof2.from"));
141
142
  // Check ST list
142
- publicInput.stateTransitionsHash.assertEquals(proof1.publicInput.stateTransitionsHash, errors.stateTransitionsHashNotMatching("publicInput.from -> proof1.from"));
143
- proof1.publicOutput.stateTransitionsHash.assertEquals(proof2.publicInput.stateTransitionsHash, errors.stateTransitionsHashNotMatching("proof1.to -> proof2.from"));
143
+ publicInput.stateTransitionsHash.assertEquals(proof1.publicInput.stateTransitionsHash, errors.propertyNotMatching("stateTransitionsHash", "publicInput.from -> proof1.from"));
144
+ proof1.publicOutput.stateTransitionsHash.assertEquals(proof2.publicInput.stateTransitionsHash, errors.propertyNotMatching("stateTransitionsHash", "proof1.to -> proof2.from"));
144
145
  // Check Protocol ST list
145
- publicInput.protocolTransitionsHash.assertEquals(proof1.publicInput.protocolTransitionsHash, errors.protocolTransitionsHashNotMatching("publicInput.from -> proof1.from"));
146
- proof1.publicOutput.protocolTransitionsHash.assertEquals(proof2.publicInput.protocolTransitionsHash, errors.protocolTransitionsHashNotMatching("proof1.to -> proof2.from"));
146
+ publicInput.protocolTransitionsHash.assertEquals(proof1.publicInput.protocolTransitionsHash, errors.propertyNotMatching("protocolTransitionsHash", "publicInput.from -> proof1.from"));
147
+ proof1.publicOutput.protocolTransitionsHash.assertEquals(proof2.publicInput.protocolTransitionsHash, errors.propertyNotMatching("protocolTransitionsHash", "proof1.to -> proof2.from"));
148
+ // Check protocol state root
149
+ publicInput.protocolStateRoot.assertEquals(proof1.publicInput.protocolStateRoot, errors.propertyNotMatching("protocolStateRoot", "publicInput.from -> proof1.from"));
150
+ proof1.publicOutput.protocolStateRoot.assertEquals(proof2.publicInput.protocolStateRoot, errors.propertyNotMatching("protocolStateRoot", "proof1.to -> proof2.from"));
147
151
  return new StateTransitionProverPublicInput({
148
152
  stateRoot: proof2.publicOutput.stateRoot,
149
153
  stateTransitionsHash: proof2.publicOutput.stateTransitionsHash,
150
154
  protocolTransitionsHash: proof2.publicOutput.protocolTransitionsHash,
155
+ protocolStateRoot: proof2.publicOutput.protocolStateRoot,
151
156
  });
152
157
  }
153
158
  }
@@ -1 +1 @@
1
- {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../src/state/assert/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAY,MAAM,UAAU,CAAC;AAO1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAWvD"}
1
+ {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../src/state/assert/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAMhC;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAWvD"}
@@ -1,4 +1,3 @@
1
- import { Bool, Provable } from "snarkyjs";
2
1
  import { container } from "tsyringe";
3
2
  import { log } from "@proto-kit/common";
4
3
  import { RuntimeMethodExecutionContext } from "../context/RuntimeMethodExecutionContext";
@@ -14,7 +13,7 @@ import { RuntimeMethodExecutionContext } from "../context/RuntimeMethodExecution
14
13
  export function assert(condition, message) {
15
14
  const executionContext = container.resolve(RuntimeMethodExecutionContext);
16
15
  const previousStatus = executionContext.current().result.status;
17
- const status = Provable.if(previousStatus, Bool, condition, previousStatus);
16
+ const status = condition.and(previousStatus);
18
17
  if (!condition.toBoolean()) {
19
18
  log.debug("Assertion failed: ", message);
20
19
  executionContext.setStatusMessage(message);
@@ -1 +1 @@
1
- {"version":3,"file":"ProtocolState.d.ts","sourceRoot":"","sources":["../../../src/state/protocol/ProtocolState.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC;AAgBzF;;;GAGG;AACH,wBAAgB,aAAa,oHAGZ,MAAM,UAoCtB"}
1
+ {"version":3,"file":"ProtocolState.d.ts","sourceRoot":"","sources":["../../../src/state/protocol/ProtocolState.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC;AAgBzF;;;GAGG;AACH,wBAAgB,aAAa,oHAGZ,MAAM,UAoCtB"}
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.245+b1d7d07",
6
+ "version": "0.1.1-develop.254+2fba72e",
7
7
  "scripts": {
8
8
  "build": "tsc -p tsconfig.json",
9
9
  "dev": "tsc -p tsconfig.json --watch",
@@ -31,5 +31,5 @@
31
31
  "@jest/globals": "^29.5.0",
32
32
  "@types/lodash": "^4.14.194"
33
33
  },
34
- "gitHead": "b1d7d0756743006192c20bf6b2bf152b6a81e723"
34
+ "gitHead": "2fba72ec580c20289658d5c831430ca601ea5a71"
35
35
  }
@@ -1,7 +1,6 @@
1
1
  import {
2
2
  Bool,
3
3
  Field,
4
- FlexibleProvable,
5
4
  type FlexibleProvablePure,
6
5
  Poseidon,
7
6
  Provable,
@@ -18,10 +17,6 @@ export class ProvableOption extends Struct({
18
17
  }
19
18
  }
20
19
 
21
- export interface ToFieldable {
22
- toFields: () => Field[];
23
- }
24
-
25
20
  /**
26
21
  * Option facilitating in-circuit values that may or may not exist.
27
22
  */
@@ -1,6 +1,6 @@
1
1
  import { Field, Struct } from "snarkyjs";
2
2
 
3
- import { Option, ProvableOption, ToFieldable } from "./Option.js";
3
+ import { Option, ProvableOption } from "./Option";
4
4
 
5
5
  /**
6
6
  * Provable representation of a State Transition, used to
@@ -1,12 +1,13 @@
1
1
  import { Bool, Provable, Struct } from "snarkyjs";
2
+ import { range } from "@proto-kit/common";
2
3
 
3
4
  import { constants } from "../Constants";
4
5
 
5
6
  import { ProvableStateTransition } from "./StateTransition.js";
6
- import { range } from "@proto-kit/common";
7
7
 
8
8
  export class StateTransitionType {
9
9
  public static readonly normal = true;
10
+
10
11
  public static readonly protocol = false;
11
12
 
12
13
  public static isNormal(type: boolean) {
@@ -69,6 +70,17 @@ export class StateTransitionProvableBatch extends Struct({
69
70
  const batch = transitions.map((entry) => entry.transition);
70
71
  const transitionTypes = transitions.map((entry) => entry.type);
71
72
 
73
+ // Check that order is correct
74
+ let normalSTsStarted = false;
75
+ transitionTypes.forEach((x) => {
76
+ if (!normalSTsStarted && x.isNormal().toBoolean()) {
77
+ normalSTsStarted = true;
78
+ }
79
+ if (normalSTsStarted && x.isProtocol().toBoolean()) {
80
+ throw new Error("Order in initializing STBatch not correct");
81
+ }
82
+ });
83
+
72
84
  while (batch.length < constants.stateTransitionProverBatchSize) {
73
85
  batch.push(ProvableStateTransition.dummy());
74
86
  transitionTypes.push(ProvableStateTransitionType.normal);
@@ -110,9 +110,6 @@ export class BlockProverProgrammable extends ZkProgrammable<
110
110
 
111
111
  const stateTo = { ...state };
112
112
 
113
- // eslint-disable-next-line no-warning-comments
114
- // TODO Check methodId?
115
-
116
113
  // Checks for the stateTransitionProof and appProof matching
117
114
  stateTransitionProof.publicInput.stateTransitionsHash.assertEquals(
118
115
  Field(0),
@@ -124,15 +121,21 @@ export class BlockProverProgrammable extends ZkProgrammable<
124
121
  errors.stateTransitionsHashNotEqual()
125
122
  );
126
123
 
127
- // Apply state if status success
124
+ // Assert from state roots
128
125
  state.stateRoot.assertEquals(
129
126
  stateTransitionProof.publicInput.stateRoot,
130
127
  errors.propertyNotMatching("from state root")
131
128
  );
129
+ state.stateRoot.assertEquals(
130
+ stateTransitionProof.publicInput.protocolStateRoot,
131
+ errors.propertyNotMatching("from protocol state root")
132
+ );
133
+
134
+ // Apply state if status success
132
135
  stateTo.stateRoot = Provable.if(
133
136
  appProof.publicOutput.status,
134
137
  stateTransitionProof.publicOutput.stateRoot,
135
- stateTransitionProof.publicInput.stateRoot
138
+ stateTransitionProof.publicOutput.protocolStateRoot
136
139
  );
137
140
 
138
141
  // Apply protocol state transitions
@@ -143,9 +146,6 @@ export class BlockProverProgrammable extends ZkProgrammable<
143
146
  .validateSignature()
144
147
  .assertTrue("Transaction signature not valid");
145
148
 
146
- // Check if the methodId is correct
147
- // to do
148
-
149
149
  // Check transaction integrity against appProof
150
150
  const blockTransactionHash =
151
151
  RuntimeTransaction.fromProtocolTransaction(transaction).hash();
@@ -9,12 +9,14 @@ export class StateTransitionProverPublicInput extends Struct({
9
9
  stateTransitionsHash: Field,
10
10
  protocolTransitionsHash: Field,
11
11
  stateRoot: Field,
12
+ protocolStateRoot: Field,
12
13
  }) {}
13
14
 
14
15
  export class StateTransitionProverPublicOutput extends Struct({
15
16
  stateTransitionsHash: Field,
16
17
  protocolTransitionsHash: Field,
17
18
  stateRoot: Field,
19
+ protocolStateRoot: Field,
18
20
  }) {}
19
21
 
20
22
  export type StateTransitionProof = Proof<
@@ -1,8 +1,9 @@
1
1
  /* eslint-disable max-lines */
2
- import { Experimental, Field, Provable, SelfProof } from "snarkyjs";
2
+ import { Bool, Experimental, Field, Provable, SelfProof } from "snarkyjs";
3
3
  import { injectable } from "tsyringe";
4
4
  import {
5
5
  AreProofsEnabled,
6
+ log,
6
7
  PlainZkProgram,
7
8
  provableMethod,
8
9
  ZkProgrammable,
@@ -34,27 +35,21 @@ import {
34
35
  import { StateTransitionWitnessProviderReference } from "./StateTransitionWitnessProviderReference";
35
36
 
36
37
  const errors = {
37
- stateRootNotMatching: (step: string) => `StateRoots not matching ${step}`,
38
+ propertyNotMatching: (property: string, step: string) =>
39
+ `${property} not matching ${step}`,
38
40
 
39
- stateTransitionsHashNotMatching: (step: string) =>
40
- `State transitions hash not matching ${step}`,
41
-
42
- protocolTransitionsHashNotMatching: (step: string) =>
43
- `Protocol transitions hash not matching ${step}`,
44
-
45
- merkleWitnessNotCorrect: (index: number) =>
46
- `MerkleWitness not valid for StateTransition (${index})`,
41
+ merkleWitnessNotCorrect: (index: number, type: string) =>
42
+ `MerkleWitness not valid for StateTransition (${index}, type ${type})`,
47
43
 
48
44
  noWitnessProviderSet: () =>
49
45
  new Error(
50
46
  "WitnessProvider not set, set it before you use StateTransitionProvider"
51
47
  ),
52
-
53
- propertyNotMatching: (propertyName: string) => `${propertyName} not matching`,
54
48
  };
55
49
 
56
50
  interface StateTransitionProverExecutionState {
57
51
  stateRoot: Field;
52
+ protocolStateRoot: Field;
58
53
  stateTransitionList: ProvableHashList<ProvableStateTransition>;
59
54
  protocolTransitionList: ProvableHashList<ProvableStateTransition>;
60
55
  }
@@ -154,12 +149,14 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
154
149
  */
155
150
  public applyTransitions(
156
151
  stateRoot: Field,
152
+ protocolStateRoot: Field,
157
153
  stateTransitionCommitmentFrom: Field,
158
154
  protocolTransitionCommitmentFrom: Field,
159
155
  transitionBatch: StateTransitionProvableBatch
160
156
  ): StateTransitionProverExecutionState {
161
157
  const state: StateTransitionProverExecutionState = {
162
158
  stateRoot,
159
+ protocolStateRoot,
163
160
 
164
161
  stateTransitionList: new DefaultProvableHashList(
165
162
  ProvableStateTransition,
@@ -205,11 +202,16 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
205
202
  transition.path,
206
203
  transition.from.value
207
204
  );
205
+
208
206
  membershipValid
209
207
  .or(transition.from.isSome.not())
210
- .assertTrue(errors.merkleWitnessNotCorrect(index));
208
+ .assertTrue(
209
+ errors.merkleWitnessNotCorrect(
210
+ index,
211
+ type.isNormal().toBoolean() ? "normal" : "protocol"
212
+ )
213
+ );
211
214
 
212
- const t = Date.now();
213
215
  const newRoot = MerkleTreeUtils.computeRoot(
214
216
  treeWitness,
215
217
  transition.to.value
@@ -221,6 +223,14 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
221
223
  state.stateRoot
222
224
  );
223
225
 
226
+ // Only update protocol state root if ST is also of type protocol
227
+ // Since protocol STs are all at the start of the batch, this works
228
+ state.protocolStateRoot = Provable.if(
229
+ transition.to.isSome.and(type.isProtocol()),
230
+ newRoot,
231
+ state.protocolStateRoot
232
+ );
233
+
224
234
  const isNotDummy = transition.path.equals(Field(0)).not();
225
235
 
226
236
  state.stateTransitionList.pushIf(
@@ -243,6 +253,7 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
243
253
  ): StateTransitionProverPublicOutput {
244
254
  const result = this.applyTransitions(
245
255
  publicInput.stateRoot,
256
+ publicInput.protocolStateRoot,
246
257
  publicInput.stateTransitionsHash,
247
258
  publicInput.protocolTransitionsHash,
248
259
  batch
@@ -252,6 +263,7 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
252
263
  stateRoot: result.stateRoot,
253
264
  stateTransitionsHash: result.stateTransitionList.commitment,
254
265
  protocolTransitionsHash: result.protocolTransitionList.commitment,
266
+ protocolStateRoot: result.protocolStateRoot,
255
267
  });
256
268
  }
257
269
 
@@ -267,39 +279,66 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
267
279
  // Check state
268
280
  publicInput.stateRoot.assertEquals(
269
281
  proof1.publicInput.stateRoot,
270
- errors.stateRootNotMatching("publicInput.from -> proof1.from")
282
+ errors.propertyNotMatching("stateRoot", "publicInput.from -> proof1.from")
271
283
  );
272
284
  proof1.publicOutput.stateRoot.assertEquals(
273
285
  proof2.publicInput.stateRoot,
274
- errors.stateRootNotMatching("proof1.to -> proof2.from")
286
+ errors.propertyNotMatching("stateRoot", "proof1.to -> proof2.from")
275
287
  );
276
288
 
277
289
  // Check ST list
278
290
  publicInput.stateTransitionsHash.assertEquals(
279
291
  proof1.publicInput.stateTransitionsHash,
280
- errors.stateTransitionsHashNotMatching("publicInput.from -> proof1.from")
292
+ errors.propertyNotMatching(
293
+ "stateTransitionsHash",
294
+ "publicInput.from -> proof1.from"
295
+ )
281
296
  );
282
297
  proof1.publicOutput.stateTransitionsHash.assertEquals(
283
298
  proof2.publicInput.stateTransitionsHash,
284
- errors.stateTransitionsHashNotMatching("proof1.to -> proof2.from")
299
+ errors.propertyNotMatching(
300
+ "stateTransitionsHash",
301
+ "proof1.to -> proof2.from"
302
+ )
285
303
  );
286
304
 
287
305
  // Check Protocol ST list
288
306
  publicInput.protocolTransitionsHash.assertEquals(
289
307
  proof1.publicInput.protocolTransitionsHash,
290
- errors.protocolTransitionsHashNotMatching(
308
+ errors.propertyNotMatching(
309
+ "protocolTransitionsHash",
291
310
  "publicInput.from -> proof1.from"
292
311
  )
293
312
  );
294
313
  proof1.publicOutput.protocolTransitionsHash.assertEquals(
295
314
  proof2.publicInput.protocolTransitionsHash,
296
- errors.protocolTransitionsHashNotMatching("proof1.to -> proof2.from")
315
+ errors.propertyNotMatching(
316
+ "protocolTransitionsHash",
317
+ "proof1.to -> proof2.from"
318
+ )
319
+ );
320
+
321
+ // Check protocol state root
322
+ publicInput.protocolStateRoot.assertEquals(
323
+ proof1.publicInput.protocolStateRoot,
324
+ errors.propertyNotMatching(
325
+ "protocolStateRoot",
326
+ "publicInput.from -> proof1.from"
327
+ )
328
+ );
329
+ proof1.publicOutput.protocolStateRoot.assertEquals(
330
+ proof2.publicInput.protocolStateRoot,
331
+ errors.propertyNotMatching(
332
+ "protocolStateRoot",
333
+ "proof1.to -> proof2.from"
334
+ )
297
335
  );
298
336
 
299
337
  return new StateTransitionProverPublicInput({
300
338
  stateRoot: proof2.publicOutput.stateRoot,
301
339
  stateTransitionsHash: proof2.publicOutput.stateTransitionsHash,
302
340
  protocolTransitionsHash: proof2.publicOutput.protocolTransitionsHash,
341
+ protocolStateRoot: proof2.publicOutput.protocolStateRoot,
303
342
  });
304
343
  }
305
344
  }
@@ -5,6 +5,8 @@ import { container } from "tsyringe";
5
5
 
6
6
  import { assert } from "./assert";
7
7
  import { RuntimeMethodExecutionContext } from "../context/RuntimeMethodExecutionContext";
8
+ import { RuntimeTransaction } from "../../model/transaction/RuntimeTransaction";
9
+ import { NetworkState } from "../../model/network/NetworkState";
8
10
 
9
11
  describe("assert", () => {
10
12
  const defaultStatusMessage = "something went wrong";
@@ -12,6 +14,11 @@ describe("assert", () => {
12
14
 
13
15
  beforeEach(() => {
14
16
  executionContext.beforeMethod("testConstructor", "test", []);
17
+
18
+ executionContext.setup({
19
+ transaction: undefined as unknown as RuntimeTransaction,
20
+ networkState: undefined as unknown as NetworkState,
21
+ });
15
22
  });
16
23
 
17
24
  afterEach(() => {
@@ -1,9 +1,8 @@
1
- import { Bool, Provable } from "snarkyjs";
1
+ import { Bool } from "snarkyjs";
2
2
  import { container } from "tsyringe";
3
3
  import { log } from "@proto-kit/common";
4
4
 
5
5
  import { RuntimeMethodExecutionContext } from "../context/RuntimeMethodExecutionContext";
6
- import { exec } from "child_process";
7
6
 
8
7
  /**
9
8
  * Maintains an execution status of the current runtime module method,
@@ -17,7 +16,7 @@ import { exec } from "child_process";
17
16
  export function assert(condition: Bool, message?: string) {
18
17
  const executionContext = container.resolve(RuntimeMethodExecutionContext);
19
18
  const previousStatus = executionContext.current().result.status;
20
- const status = Provable.if(previousStatus, Bool, condition, previousStatus);
19
+ const status = condition.and(previousStatus);
21
20
 
22
21
  if (!condition.toBoolean()) {
23
22
  log.debug("Assertion failed: ", message);
@@ -1,5 +1,4 @@
1
1
  import { State } from "../State";
2
- import { ToFieldable } from "../../model/Option";
3
2
  import { Path } from "../../model/Path";
4
3
  import { TransitioningProtocolModule } from "../../protocol/TransitioningProtocolModule";
5
4
 
@@ -0,0 +1,45 @@
1
+ import "reflect-metadata";
2
+ import { Bool, Field, Provable, UInt64 } from "snarkyjs";
3
+ import { container } from "tsyringe";
4
+
5
+ import {
6
+ NetworkState,
7
+ noop,
8
+ RuntimeMethodExecutionContext,
9
+ RuntimeTransaction,
10
+ State,
11
+ StateService,
12
+ StateServiceProvider,
13
+ } from "../src";
14
+
15
+ describe("state", () => {
16
+ beforeEach(() => {
17
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
18
+
19
+ executionContext.setup({
20
+ transaction: undefined as unknown as RuntimeTransaction,
21
+ networkState: undefined as unknown as NetworkState,
22
+ });
23
+ });
24
+
25
+ it("should decode state correctly", () => {
26
+ expect.assertions(2);
27
+
28
+ const state = State.from<UInt64>(UInt64);
29
+ const stateService: StateService = {
30
+ get: () => [Field(123)],
31
+
32
+ set: () => {
33
+ noop();
34
+ },
35
+ };
36
+ state.stateServiceProvider = new StateServiceProvider();
37
+ state.stateServiceProvider.setCurrentStateService(stateService);
38
+ state.path = Field(1);
39
+
40
+ const retrieved = state.get();
41
+
42
+ expect(retrieved.isSome).toStrictEqual(Bool(true));
43
+ expect(retrieved.value).toStrictEqual(UInt64.from(123));
44
+ });
45
+ });
@@ -1,26 +0,0 @@
1
- import { type MerkleTreeStore, type SyncMerkleTreeStore } from "./RollupMerkleTree.js";
2
- export declare class NoOpMerkleTreeStorage implements SyncMerkleTreeStore {
3
- parent: MerkleTreeStore;
4
- openTransaction(): void;
5
- commit(): void;
6
- getNode(): bigint | undefined;
7
- setNode(): void;
8
- virtualize(): MerkleTreeStore;
9
- getNodeAsync(): Promise<bigint | undefined>;
10
- setNodeAsync(): Promise<void>;
11
- }
12
- export declare class MemoryMerkleTreeStorage implements SyncMerkleTreeStore {
13
- private readonly nodes;
14
- private readonly cache;
15
- parent: SyncMerkleTreeStore;
16
- constructor(parent?: SyncMerkleTreeStore | undefined);
17
- openTransaction(): void;
18
- commit(): void;
19
- getNode(key: bigint, level: number): bigint | undefined;
20
- setNode(key: bigint, level: number, value: bigint): void;
21
- virtualize(): MerkleTreeStore;
22
- getNodeAsync(key: bigint, level: number): Promise<bigint | undefined>;
23
- setNodeAsync(key: bigint, level: number, value: bigint): Promise<void>;
24
- cacheFromParent(index: bigint): Promise<void>;
25
- }
26
- //# sourceMappingURL=MemoryMerkleTreeStorage.d.ts.map
@@ -1 +0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@
1
- /* eslint-disable @typescript-eslint/no-magic-numbers */
2
- import noop from "lodash/noop";
3
- import { RollupMerkleTree, } from "./RollupMerkleTree.js";
4
- export class NoOpMerkleTreeStorage {
5
- constructor() {
6
- this.parent = this;
7
- }
8
- openTransaction() {
9
- noop();
10
- }
11
- commit() {
12
- noop();
13
- }
14
- getNode() {
15
- return undefined;
16
- }
17
- setNode() {
18
- noop();
19
- }
20
- virtualize() {
21
- return this;
22
- }
23
- async getNodeAsync() {
24
- return undefined;
25
- }
26
- async setNodeAsync() {
27
- return undefined;
28
- }
29
- }
30
- export class MemoryMerkleTreeStorage {
31
- constructor(parent = undefined) {
32
- this.nodes = {};
33
- this.cache = {};
34
- this.parent = parent ?? new NoOpMerkleTreeStorage();
35
- }
36
- openTransaction() {
37
- noop();
38
- }
39
- commit() {
40
- noop();
41
- }
42
- getNode(key, level) {
43
- return (this.nodes[level]?.[key.toString()] ??
44
- this.cache[level]?.[key.toString()] ??
45
- this.parent.getNode(key, level));
46
- }
47
- setNode(key, level, value) {
48
- var _a;
49
- ((_a = this.nodes)[level] ?? (_a[level] = {}))[key.toString()] = value;
50
- }
51
- virtualize() {
52
- return new MemoryMerkleTreeStorage(this);
53
- }
54
- async getNodeAsync(key, level) {
55
- return this.getNode(key, level);
56
- }
57
- async setNodeAsync(key, level, value) {
58
- this.setNode(key, level, value);
59
- return undefined;
60
- }
61
- async cacheFromParent(index) {
62
- var _a;
63
- // Algo from RollupMerkleTree.getWitness()
64
- const { leafCount, height } = RollupMerkleTree;
65
- if (index >= leafCount) {
66
- index %= leafCount;
67
- }
68
- for (let level = 0; level < height - 1; level++) {
69
- const isLeft = index % 2n === 0n;
70
- const key = isLeft ? index + 1n : index - 1n;
71
- // eslint-disable-next-line no-await-in-loop
72
- const value = await this.parent.getNodeAsync(key, level);
73
- if (value !== undefined) {
74
- ((_a = this.nodes)[level] ?? (_a[level] = {}))[key.toString()] = value;
75
- }
76
- index /= 2n;
77
- }
78
- }
79
- }