1
- import { Bool , Provable , Struct } from "o1js" ;
1
+ import { Bool , Field , Poseidon , Provable , Struct } from "o1js" ;
2
2
import {
3
- InMemoryMerkleTreeStorage ,
4
3
range ,
4
+ InMemoryMerkleTreeStorage ,
5
5
RollupMerkleTree ,
6
- RollupMerkleTreeWitness ,
6
+ RollupMerkleTreeWitness
7
7
} from "@proto-kit/common" ;
8
8
9
9
import { constants } from "../Constants" ;
10
10
11
11
import { ProvableStateTransition } from "./StateTransition.js" ;
12
12
13
13
export class StateTransitionType {
14
- public static readonly normal = true ;
15
-
16
- public static readonly protocol = false ;
14
+ public static readonly nothing = 2 ;
15
+ // The reason these are 0 and 1 is to efficiently check
16
+ // x in [inside, closing] in-circuit via the boolean trick
17
+ public static readonly closeAndApply = 1 ;
18
+ public static readonly closeAndThrowAway = 0 ;
19
+ }
17
20
18
- public static isNormal ( type : boolean ) {
19
- return type === StateTransitionType . normal ;
21
+ export class ProvableStateTransitionType extends Struct ( {
22
+ type : Field ,
23
+ } ) {
24
+ public static get nothing ( ) : ProvableStateTransitionType {
25
+ return this . from ( StateTransitionType . nothing ) ;
20
26
}
21
27
22
- public static isProtocol ( type : boolean ) {
23
- return type === StateTransitionType . protocol ;
28
+ public static get closeAndApply ( ) : ProvableStateTransitionType {
29
+ return this . from ( StateTransitionType . closeAndApply ) ;
24
30
}
25
- }
26
31
27
- export class ProvableStateTransitionType extends Struct ( {
28
- type : Bool ,
29
- } ) {
30
- public static get normal ( ) : ProvableStateTransitionType {
31
- return new ProvableStateTransitionType ( {
32
- type : Bool ( StateTransitionType . normal ) ,
33
- } ) ;
32
+ public static get closeAndThrowAway ( ) : ProvableStateTransitionType {
33
+ return this . from ( StateTransitionType . closeAndThrowAway ) ;
34
34
}
35
35
36
- public static get protocol ( ) : ProvableStateTransitionType {
36
+ private static from ( constant : number ) {
37
37
return new ProvableStateTransitionType ( {
38
- type : Bool ( StateTransitionType . protocol ) ,
38
+ type : Field ( constant ) ,
39
39
} ) ;
40
40
}
41
41
42
- public isNormal ( ) : Bool {
43
- return this . type ;
42
+ public isClosing ( ) {
43
+ const { type } = this ;
44
+ // check if base is 0 or 1
45
+ // 0^2 == 0 && 1^2 == 1
46
+ return type . mul ( type ) . equals ( type ) ;
44
47
}
48
+ }
45
49
46
- public isProtocol ( ) : Bool {
47
- return this . type . not ( ) ;
50
+ export class AppliedStateTransitionBatch extends Struct ( {
51
+ batchHash : Field ,
52
+ applied : Bool ,
53
+ } ) { }
54
+
55
+ export class AppliedStateTransitionBatchState extends Struct ( {
56
+ batchHash : Field ,
57
+ root : Field ,
58
+ } ) {
59
+ public hashOrZero ( ) : Field {
60
+ const hash = Poseidon . hash ( AppliedStateTransitionBatchState . toFields ( this ) ) ;
61
+ return Provable . if ( this . batchHash . equals ( 0 ) , Field ( 0 ) , hash ) ;
48
62
}
49
63
}
50
64
65
+ export class MerkleWitnessBatch extends Struct ( {
66
+ witnesses : Provable . Array (
67
+ RollupMerkleTreeWitness ,
68
+ constants . stateTransitionProverBatchSize
69
+ ) ,
70
+ } ) { }
71
+
51
72
/**
52
73
* A Batch of StateTransitions to be consumed by the StateTransitionProver
53
74
* to prove multiple STs at once
54
75
*
55
- * transitionType:
56
- * true == normal ST, false == protocol ST
76
+ * bases: Describes the state root on which the ST will be applied on
77
+ * If it is zero, this means that this ST should connect with the previous one
78
+ * If it is one, this means that the batch should be closed
57
79
*/
58
80
export class StateTransitionProvableBatch extends Struct ( {
59
81
batch : Provable . Array (
60
82
ProvableStateTransition ,
61
83
constants . stateTransitionProverBatchSize
62
84
) ,
63
85
64
- transitionTypes : Provable . Array (
86
+ // bases: Provable.Array(Field, constants.stateTransitionProverBatchSize),
87
+
88
+ types : Provable . Array (
65
89
ProvableStateTransitionType ,
66
90
constants . stateTransitionProverBatchSize
67
91
) ,
@@ -71,75 +95,75 @@ export class StateTransitionProvableBatch extends Struct({
71
95
constants . stateTransitionProverBatchSize
72
96
) ,
73
97
} ) {
74
- public static fromMappings (
75
- transitions : {
76
- transition : ProvableStateTransition ;
77
- type : ProvableStateTransitionType ;
78
- } [ ] ,
79
- merkleWitnesses : RollupMerkleTreeWitness [ ]
80
- ) : StateTransitionProvableBatch {
81
- const batch = transitions . map ( ( entry ) => entry . transition ) ;
82
- const transitionTypes = transitions . map ( ( entry ) => entry . type ) ;
83
- const witnesses = merkleWitnesses . slice ( ) ;
84
- // Check that order is correct
85
- let normalSTsStarted = false ;
86
- transitionTypes . forEach ( ( x ) => {
87
- if ( ! normalSTsStarted && x . isNormal ( ) . toBoolean ( ) ) {
88
- normalSTsStarted = true ;
89
- }
90
- if ( normalSTsStarted && x . isProtocol ( ) . toBoolean ( ) ) {
91
- throw new Error ( "Order in initializing STBatch not correct" ) ;
92
- }
93
- } ) ;
94
-
95
- while ( batch . length < constants . stateTransitionProverBatchSize ) {
96
- batch . push ( ProvableStateTransition . dummy ( ) ) ;
97
- transitionTypes . push ( ProvableStateTransitionType . normal ) ;
98
- witnesses . push (
99
- new RollupMerkleTree ( new InMemoryMerkleTreeStorage ( ) ) . getWitness (
100
- BigInt ( 0 )
101
- )
102
- ) ;
103
- }
104
- return new StateTransitionProvableBatch ( {
105
- batch,
106
- transitionTypes,
107
- merkleWitnesses : witnesses ,
108
- } ) ;
109
- }
110
-
111
- public static fromTransitions (
112
- transitions : ProvableStateTransition [ ] ,
113
- protocolTransitions : ProvableStateTransition [ ] ,
114
- merkleWitnesses : RollupMerkleTreeWitness [ ]
115
- ) : StateTransitionProvableBatch {
116
- const array = transitions . slice ( ) . concat ( protocolTransitions ) ;
117
-
118
- const transitionTypes = range ( 0 , transitions . length )
119
- . map ( ( ) => ProvableStateTransitionType . normal )
120
- . concat (
121
- range ( 0 , protocolTransitions . length ) . map (
122
- ( ) => ProvableStateTransitionType . protocol
123
- )
124
- ) ;
125
-
126
- while ( array . length < constants . stateTransitionProverBatchSize ) {
127
- array . push ( ProvableStateTransition . dummy ( ) ) ;
128
- transitionTypes . push ( ProvableStateTransitionType . normal ) ;
129
- }
130
-
131
- return new StateTransitionProvableBatch ( {
132
- batch : array ,
133
- transitionTypes,
134
- merkleWitnesses,
135
- } ) ;
136
- }
137
-
138
- private constructor ( object : {
139
- batch : ProvableStateTransition [ ] ;
140
- transitionTypes : ProvableStateTransitionType [ ] ;
141
- merkleWitnesses : RollupMerkleTreeWitness [ ] ;
142
- } ) {
143
- super ( object ) ;
144
- }
98
+ // public static fromMappings(
99
+ // transitions: {
100
+ // transition: ProvableStateTransition;
101
+ // type: ProvableStateTransitionType;
102
+ // }[],
103
+ // merkleWitnesses: RollupMerkleTreeWitness[]
104
+ // ): StateTransitionProvableBatch {
105
+ // const batch = transitions.map((entry) => entry.transition);
106
+ // const transitionTypes = transitions.map((entry) => entry.type);
107
+ // const witnesses = merkleWitnesses.slice();
108
+ // // Check that order is correct
109
+ // let normalSTsStarted = false;
110
+ // transitionTypes.forEach((x) => {
111
+ // if (!normalSTsStarted && x.isNormal().toBoolean()) {
112
+ // normalSTsStarted = true;
113
+ // }
114
+ // if (normalSTsStarted && x.isProtocol().toBoolean()) {
115
+ // throw new Error("Order in initializing STBatch not correct");
116
+ // }
117
+ // });
118
+ //
119
+ // while (batch.length < constants.stateTransitionProverBatchSize) {
120
+ // batch.push(ProvableStateTransition.dummy());
121
+ // transitionTypes.push(ProvableStateTransitionType.normal);
122
+ // witnesses.push(
123
+ // new RollupMerkleTree(new InMemoryMerkleTreeStorage()).getWitness(
124
+ // BigInt(0)
125
+ // )
126
+ // );
127
+ // }
128
+ // return new StateTransitionProvableBatch({
129
+ // batch,
130
+ // transitionTypes,
131
+ // merkleWitnesses: witnesses,
132
+ // });
133
+ // }
134
+ //
135
+ // public static fromTransitions(
136
+ // transitions: ProvableStateTransition[],
137
+ // protocolTransitions: ProvableStateTransition[],
138
+ // merkleWitnesses: RollupMerkleTreeWitness[]
139
+ // ): StateTransitionProvableBatch {
140
+ // const array = transitions.slice().concat(protocolTransitions);
141
+ //
142
+ // const transitionTypes = range(0, transitions.length)
143
+ // .map(() => ProvableStateTransitionType.normal)
144
+ // .concat(
145
+ // range(0, protocolTransitions.length).map(
146
+ // () => ProvableStateTransitionType.protocol
147
+ // )
148
+ // );
149
+ //
150
+ // while (array.length < constants.stateTransitionProverBatchSize) {
151
+ // array.push(ProvableStateTransition.dummy());
152
+ // transitionTypes.push(ProvableStateTransitionType.normal);
153
+ // }
154
+ //
155
+ // return new StateTransitionProvableBatch({
156
+ // batch: array,
157
+ // transitionTypes,
158
+ // merkleWitnesses,
159
+ // });
160
+ // }
161
+ //
162
+ // private constructor(object: {
163
+ // batch: ProvableStateTransition[];
164
+ // transitionTypes: ProvableStateTransitionType[];
165
+ // merkleWitnesses: RollupMerkleTreeWitness[];
166
+ // }) {
167
+ // super(object);
168
+ // }
145
169
}
0 commit comments