-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyRollup.ts
272 lines (246 loc) · 7.42 KB
/
myRollup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* This file contains a dummy ZkApp and the associated implementation
* of the required abstract classes (please refer to ./generics.ts)
* for using the sequencer and hadoop. This ZkApp adds a number a
* number to a MerkleMap and holds the state of the latest and previous
* MerkleMap.
*
* @note Make sure to create your own implementation of all classes in this file.
* Refer to (this file)[./generics.ts] for more information
*/
import {
Field,
ZkProgram,
SelfProof,
Struct,
Empty,
MerkleMapWitness,
MerkleMap,
} from 'o1js';
import { RollupProofBase, TransactionBase } from './generics';
/**
* A helper class for working with {@link Rollup}, our dummy ZkApp
*/
class RollupState extends Struct({
initialRoot: Field,
latestRoot: Field,
}) {
static createOneStep(
initialRoot: Field,
latestRoot: Field,
key: Field,
currentValue: Field,
newValue: Field,
merkleMapWitness: MerkleMapWitness,
): RollupState {
const [witnessRootBefore, witnessKey] =
merkleMapWitness.computeRootAndKey(currentValue);
initialRoot.assertEquals(
witnessRootBefore,
'createOneStep: initialRoot == witnessRootBefore',
);
witnessKey.assertEquals(key, 'createOneStep: witnessKey == key');
const [witnessRootAfter] = merkleMapWitness.computeRootAndKey(newValue);
latestRoot.assertEquals(
witnessRootAfter,
'createOneStep: latestRoot == witnessRootAfter',
);
return new RollupState({
initialRoot,
latestRoot,
});
}
static createMerged(state1: RollupState, state2: RollupState): RollupState {
return new RollupState({
initialRoot: state1.initialRoot,
latestRoot: state2.latestRoot,
});
}
static assertEquals(state1: RollupState, state2: RollupState): void {
state1.initialRoot.assertEquals(
state2.initialRoot,
'RollupState: initialRoot1 == initialRoot2',
);
state1.latestRoot.assertEquals(
state2.latestRoot,
'RollupState: latestRoot1 == latestRoot2',
);
}
}
/**
* Our dummy ZkApp; you should replace it with your own ZkApp.
*/
export const Rollup = ZkProgram({
name: 'DummyZkAppRollup',
publicInput: RollupState,
methods: {
oneStep: {
privateInputs: [Field, Field, Field, Field, Field, MerkleMapWitness],
method(
newState: RollupState,
initialRoot: Field,
latestRoot: Field,
key: Field,
currentValue: Field,
newValue: Field,
merkleMapWitness: MerkleMapWitness,
) {
const computedState = RollupState.createOneStep(
initialRoot,
latestRoot,
key,
currentValue,
newValue,
merkleMapWitness,
);
RollupState.assertEquals(newState, computedState);
return undefined;
},
},
merge: {
privateInputs: [SelfProof, SelfProof],
method(
newState: RollupState,
rollup1proof: SelfProof<RollupState, Empty>,
rollup2proof: SelfProof<RollupState, Empty>,
) {
rollup1proof.verify(); // A -> B
rollup2proof.verify(); // B -> C
rollup1proof.publicInput.initialRoot.assertEquals(
newState.initialRoot,
'merge: rollup1Proof.initialRoot == newState.initialRoot',
);
rollup1proof.publicInput.latestRoot.assertEquals(
rollup2proof.publicInput.initialRoot,
'merge: rollup1Proof.latestRoot == rollup2Proof.initialRoot',
);
rollup2proof.publicInput.latestRoot.assertEquals(
newState.latestRoot,
'merge: rollup2Proof.latestRoot == newState.latestRoot',
);
},
},
},
});
/**
* An implementation of {@link RollupProofBase}
*/
export class MyRollupProof
extends ZkProgram.Proof(Rollup)
implements RollupProofBase
{
public async merge(newProof: MyRollupProof): Promise<MyRollupProof> {
const currentState = new RollupState({
initialRoot: this.publicInput.initialRoot,
latestRoot: this.publicInput.latestRoot,
});
const newState = RollupState.createMerged(
currentState,
new RollupState({
initialRoot: newProof.publicInput.initialRoot,
latestRoot: newProof.publicInput.latestRoot,
}),
);
const mergedProof = await Rollup.merge(newState, this, newProof);
return new MyRollupProof(mergedProof);
}
public fromJSON(json: any): MyRollupProof {
return new MyRollupProof(MyRollupProof.fromJSON(json));
}
}
/**
* An implementation of {@link TransactionBase}
*/
export class MyTransaction implements TransactionBase {
initialRoot: Field;
latestRoot: Field;
key: Field;
currentValue: Field;
newValue: Field;
merkleMapWitness: MerkleMapWitness;
constructor(params: {
initialRoot: Field;
latestRoot: Field;
key: Field;
currentValue: Field;
newValue: Field;
merkleMapWitness: MerkleMapWitness;
}) {
if (params != null) {
this.initialRoot = params.initialRoot;
this.latestRoot = params.latestRoot;
this.key = params.key;
this.currentValue = params.currentValue;
this.newValue = params.newValue;
this.merkleMapWitness = params.merkleMapWitness;
}
}
public serialize(): string {
return JSON.stringify({
initialRoot: this.initialRoot.toJSON(),
latestRoot: this.latestRoot.toJSON(),
key: this.key.toJSON(),
currentValue: this.currentValue.toJSON(),
newValue: this.newValue.toJSON(),
merkleMapWitness: this.merkleMapWitness.toJSON(),
});
}
public deserialize(serialized: string): void {
const txJson = JSON.parse(serialized);
this.initialRoot = Field(txJson.initialRoot);
this.latestRoot = Field(txJson.latestRoot);
this.key = Field(txJson.key);
this.currentValue = Field(txJson.currentValue);
this.newValue = Field(txJson.newValue);
this.merkleMapWitness = MerkleMapWitness.fromJSON(txJson.merkleMapWitness);
}
public async baseFn(): Promise<MyRollupProof> {
const state = new RollupState({
initialRoot: this.initialRoot,
latestRoot: this.latestRoot,
});
const proof = await Rollup.oneStep(
state,
this.initialRoot,
this.latestRoot,
this.key,
this.currentValue,
this.newValue,
this.merkleMapWitness,
);
return new MyRollupProof(proof);
}
}
/**
* A helper to convert a plain user transaction into a transaction that can be provable.
* In our case a user transaction is simply an integer sent by the user.
* The role of {@link TransactionPreProcessor} is from a set of these plain user transactions generate
* each transaction's merkle root and convert the transaction into {@link MyTransaction}
*
*
* @note you will want to implement your own TransactionPreProcessor according to the structure of your transactions
*/
export class TransactionPreProcessor {
merkleMap: MerkleMap;
currentValue: Field;
constructor() {
this.merkleMap = new MerkleMap();
this.currentValue = Field(0);
}
public processTx(tx: number): MyTransaction {
const initialRoot = this.merkleMap.getRoot();
const newValue = Field(tx);
const key = Field(this.merkleMap.tree.leafCount);
const currentValue = Field(this.currentValue.value);
this.merkleMap.set(key, newValue);
this.currentValue = newValue;
return new MyTransaction({
initialRoot: initialRoot,
latestRoot: this.merkleMap.getRoot(),
key,
currentValue,
newValue,
merkleMapWitness: this.merkleMap.getWitness(key),
});
}
}