-
Notifications
You must be signed in to change notification settings - Fork 2
/
merkle_subtree.ts
155 lines (127 loc) · 3.9 KB
/
merkle_subtree.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
import {
AccountUpdate,
Circuit,
DeployArgs,
Field,
method,
Mina,
Permissions,
PrivateKey,
Provable,
SmartContract,
State,
state,
} from 'o1js';
import { ProvableDeepMerkleSubTree } from '../lib/merkle/deep_subtree_circuit.js';
import { MerkleTree } from '../lib/merkle/merkle_tree.js';
import { ProvableMerkleTreeUtils } from '../lib/merkle/verify_circuit.js';
import { MemoryStore } from '../lib/store/memory_store.js';
const doProofs = true;
const treeHeight = 10;
class MerkleProof extends ProvableMerkleTreeUtils.MerkleProof(treeHeight) {}
class TestZkapp extends SmartContract {
@state(Field) commitment = State<Field>();
async deploy(args?: DeployArgs) {
super.deploy(args);
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
});
this.commitment.set(root);
}
@method
async merkle(
proof1: MerkleProof,
index1: Field,
value1: Field,
proof2: MerkleProof,
index2: Field,
value2: Field,
proof3: MerkleProof,
index3: Field,
value3: Field
) {
let commitment = this.commitment.getAndRequireEquals();
commitment.assertEquals(commitment);
let tree = new ProvableDeepMerkleSubTree(proof1.root, treeHeight, Field);
// let tree = new NumIndexDeepSparseMerkleSubTree<Field>(
// proof1.root,
// Field,
// treeHeight
// );
tree.addBranch(proof1, index1, value1);
tree.addBranch(proof2, index2, value2);
tree.addBranch(proof3, index3, value3);
// tree.addBranch(proof1, Poseidon.hash([value1]));
// tree.addBranch(proof2, Poseidon.hash([value2]));
// tree.addBranch(proof3, Poseidon.hash([value3]));
let finalRoot = tree.update(index1, Field(88));
finalRoot = tree.update(index2, Field(99));
finalRoot = tree.update(index3, Field(1010));
// let finalRoot = tree.update(proof1.path, Poseidon.hash([Field(88)]));
// finalRoot = tree.update(proof2.path, Poseidon.hash([Field(99)]));
// finalRoot = tree.update(proof3.path, Poseidon.hash([Field(1010)]));
Provable.asProver(() => {
console.log('finalRoot by field: ', finalRoot.toString());
});
finalRoot.assertEquals(commitment);
}
}
let local = await Mina.LocalBlockchain({ proofsEnabled: doProofs });
Mina.setActiveInstance(local);
let feePayer = local.testAccounts[0];
let feePayerKey = local.testAccounts[0].key;
let zkappKey = PrivateKey.random();
let zkappAddress = zkappKey.toPublicKey();
let tree = await MerkleTree.build(new MemoryStore<Field>(), treeHeight, Field);
const key1 = 0n;
const value1 = Field(33);
const key2 = 1n;
const value2 = Field(55);
const key3 = 2n;
const value3 = Field(999);
let root = await tree.update(key1, value1);
root = await tree.update(key2, value2);
root = await tree.update(key3, value3);
console.log('start root: ', root.toString());
let proof1 = await tree.prove(key1);
let proof2 = await tree.prove(key2);
let proof3 = await tree.prove(key3);
root = await tree.update(key1, Field(88));
root = await tree.update(key2, Field(99));
root = await tree.update(key3, Field(1010));
console.log('after root: ', root.toString());
async function test() {
let zkapp = new TestZkapp(zkappAddress);
if (doProofs) {
console.time('compile');
await TestZkapp.compile();
console.timeEnd('compile');
}
console.log('deploying');
let tx = await local.transaction(feePayer, async () => {
AccountUpdate.fundNewAccount(feePayer);
await zkapp.deploy();
});
await tx.prove();
await tx.sign([feePayerKey, zkappKey]).send();
console.log('deploy done');
console.log('start method');
tx = await local.transaction(feePayer, async () => {
await zkapp.merkle(
proof1,
Field(key1),
value1,
proof2,
Field(key2),
value2,
proof3,
Field(key3),
value3
);
});
await tx.prove();
await tx.sign([feePayerKey]).send();
console.log('end method');
}
await test();