-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparseMerkleTree.js
155 lines (132 loc) · 8.35 KB
/
sparseMerkleTree.js
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
const SparseMerkleTree = artifacts.require("SparseMerkleTree");
const SmtLib = require('./helpers/SmtLib.js');
contract("SparseMerkleTree", () => {
const leafOne = '0xa59a60d98b69a32028020fbf69c27dc2188b5756975e93b330a3f1513f383076';
const leafTwo = '0x95d22ccdd977e992e4a530ce4f1304e1a7a1840823ea1b4f7bf3841049d197e0';
const leafThree = '0x3d32085b3de13667b43fd7cecf200b347041918e259cbcc86796422a47fec794';
const leafZero = '0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563';
it("should allow to verify proofs with single intersection at 1 - test1", async() => {
const smt = await SparseMerkleTree.new();
const tree = new SmtLib(160, {
'0x8A36e7eE34972ffC688f238Ff7154C729b7D026F': leafOne,
'0x09ACF5301cb4EE676ed5ad771259bDc0360C1415': leafTwo,
});
let rsp = await smt.getRoot(leafOne, '0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'));
assert.equal(rsp, tree.root);
rsp = await smt.getRoot(leafTwo, '0x09ACF5301cb4EE676ed5ad771259bDc0360C1415', tree.createMerkleProof('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415'));
assert.equal(rsp, tree.root);
rsp = await smt.getRoot(leafZero, 2, tree.createMerkleProof('2'));
assert.equal(rsp, tree.root);
});
it("should allow to verify proofs with single intersection at 1 - test2", async() => {
const smt = await SparseMerkleTree.new();
const tree = new SmtLib(160, {
'0x000000000000000000000000000000008a36e7ee': leafOne,
'0x0000000000000000000000000000000009acf530': leafTwo,
});
let rsp = await smt.getRoot(leafOne, 0x000000000000000000000000000000008a36e7ee, tree.createMerkleProof('0x000000000000000000000000000000008a36e7ee'));
assert.equal(rsp, tree.root);
rsp = await smt.getRoot(leafTwo, 0x0000000000000000000000000000000009acf530, tree.createMerkleProof('0x0000000000000000000000000000000009acf530'));
assert.equal(rsp, tree.root);
rsp = await smt.getRoot(leafZero, 2, tree.createMerkleProof('2'));
assert.equal(rsp, tree.root);
});
it("should allow to verify proofs with multiple intersections - test1", async() => {
const smt = await SparseMerkleTree.new();
const tree = new SmtLib(160, {
'0x8A36e7eE34972ffC688f238Ff7154C729b7D026F': leafOne,
'0x09ACF5301cb4EE676ed5ad771259bDc0360C1415': leafTwo,
'0x14BC7a8f8919472B5019A54e7fE7090785d41f85': leafThree,
});
let rsp = await smt.getRoot(leafOne, '0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'));
assert.equal(rsp, tree.root);
rsp = await smt.getRoot(leafTwo, '0x09ACF5301cb4EE676ed5ad771259bDc0360C1415', tree.createMerkleProof('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415'));
assert.equal(rsp, tree.root);
rsp = await smt.getRoot(leafThree, '0x14BC7a8f8919472B5019A54e7fE7090785d41f85', tree.createMerkleProof('0x14BC7a8f8919472B5019A54e7fE7090785d41f85'));
assert.equal(rsp, tree.root);
});
it("should allow to verify proofs with multiple intersections - test2", async() => {
const smt = await SparseMerkleTree.new();
const tree = new SmtLib(160, {
'0x000000000000000000000000000000008a36e7ee': leafOne,
'0x0000000000000000000000000000000009acf530': leafTwo,
'0x0000000000000000000000000000000014bc7a8f': leafThree,
});
let rsp = await smt.getRoot(leafOne, 0x000000000000000000000000000000008a36e7ee, tree.createMerkleProof('0x000000000000000000000000000000008a36e7ee'));
assert.equal(rsp, tree.root);
rsp = await smt.getRoot(leafTwo, 0x0000000000000000000000000000000009acf530, tree.createMerkleProof('0x0000000000000000000000000000000009acf530'));
assert.equal(rsp, tree.root);
rsp = await smt.getRoot(leafThree, 0x0000000000000000000000000000000014bc7a8f, tree.createMerkleProof('0x0000000000000000000000000000000014bc7a8f'));
assert.equal(rsp, tree.root);
});
it("should allow to update root - test1", async() => {
const smt = await SparseMerkleTree.new();
// write first leaf
let tree = new SmtLib(160);
await smt.write('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', leafZero, tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'), leafOne);
// write second leaf
tree = new SmtLib(160, {'0x8A36e7eE34972ffC688f238Ff7154C729b7D026F': leafOne});
await smt.write('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415', leafZero, tree.createMerkleProof('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415'), leafTwo);
// read first leaf back
tree = new SmtLib(160, {
'0x8A36e7eE34972ffC688f238Ff7154C729b7D026F': leafOne,
'0x09ACF5301cb4EE676ed5ad771259bDc0360C1415': leafTwo,
});
let rsp = await smt.read('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', leafOne, tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'));
assert(rsp);
// negative read test
rsp = await smt.read('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', leafTwo, tree.createMerkleProof('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415'));
assert(!rsp);
// delete test
await smt.del('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', leafOne, tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'));
rsp = await smt.read('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', leafZero, tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'));
assert(rsp);
});
it("should allow to update root - test2", async() => {
const smt = await SparseMerkleTree.new();
// write first leaf
let tree = new SmtLib(160);
await smt.write(0x000000000000000000000000000000008a36e7ee, leafZero, tree.createMerkleProof('0x000000000000000000000000000000008a36e7ee'), leafOne);
// write second leaf
tree = new SmtLib(160, {'0x000000000000000000000000000000008a36e7ee': leafOne});
await smt.write(0x0000000000000000000000000000000009acf530, leafZero, tree.createMerkleProof('0x0000000000000000000000000000000009acf530'), leafTwo);
// read first leaf back
tree = new SmtLib(160, {
'0x000000000000000000000000000000008a36e7ee': leafOne,
'0x0000000000000000000000000000000009acf530': leafTwo,
});
let rsp = await smt.read(0x000000000000000000000000000000008a36e7ee, leafOne, tree.createMerkleProof('0x000000000000000000000000000000008a36e7ee'));
assert(rsp);
// negative read test
rsp = await smt.read(0x000000000000000000000000000000008a36e7ee, leafTwo, tree.createMerkleProof('0x000000000000000000000000000000008a36e7ee'));
assert(!rsp);
// delete test
await smt.del(0x000000000000000000000000000000008a36e7ee, leafOne, tree.createMerkleProof('0x000000000000000000000000000000008a36e7ee'));
rsp = await smt.read(0x000000000000000000000000000000008a36e7ee, leafZero, tree.createMerkleProof('0x000000000000000000000000000000008a36e7ee'));
assert(rsp);
});
it("The issue with the delete function", async() => {
const smt = await SparseMerkleTree.new();
// write first leaf
let tree = new SmtLib(160);
await smt.write('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', leafZero, tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'), leafOne);
// write second leaf
tree = new SmtLib(160, {'0x8A36e7eE34972ffC688f238Ff7154C729b7D026F': leafOne});
await smt.write('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415', leafZero, tree.createMerkleProof('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415'), leafTwo);
// read first leaf back
tree = new SmtLib(160, {
'0x8A36e7eE34972ffC688f238Ff7154C729b7D026F': leafOne,
'0x09ACF5301cb4EE676ed5ad771259bDc0360C1415': leafTwo,
});
let rsp = await smt.read('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', leafOne, tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'));
assert(rsp);
// read second leaf
rsp = await smt.read('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415', leafTwo, tree.createMerkleProof('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415'));
assert(rsp);
// delete test
await smt.del('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F', leafOne, tree.createMerkleProof('0x8A36e7eE34972ffC688f238Ff7154C729b7D026F'));
// After calling the delete function with one key, the root takes zerohashes value. So, others writes deletes too and you can't use read function.
rsp = await smt.read('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415', leafTwo, tree.createMerkleProof('0x09ACF5301cb4EE676ed5ad771259bDc0360C1415'));
assert(!rsp);
});
});