-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
176 lines (152 loc) · 8.67 KB
/
test.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import test from "brittle"
import serialize from "./index.js"
import { BigNumber } from "@ethersproject/bignumber"
test("serializing a trivial input", function (t) {
const inputs = { x: 3n, y: 35n, z: 2n }
const publics = { y: true }
const expected =
"000000010000000200000003000000000000000000000000000000000000000000000000000000000000002300000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000002"
const buf = serialize(inputs, publics)
t.is(buf.toString("hex"), expected)
})
test("serializing an input containing arrays", function (t) {
const inputs = {
a: 55n,
b: [
340282366920938463463374607431768211455n,
204169420152563078078024764459060926873n,
],
}
const publics = { a: true }
const expected =
"000000010000000200000003000000000000000000000000000000000000000000000000000000000000003700000000000000000000000000000000ffffffffffffffffffffffffffffffff0000000000000000000000000000000099999999999999999999999999999999"
const buf = serialize(inputs, publics)
t.is(buf.toString("hex"), expected)
})
test("serializing public inputs only", function (t) {
const inputs = {
a: 55n,
b: [
340282366920938463463374607431768211455n,
204169420152563078078024764459060926873n,
],
}
const publics = { a: true }
const expected =
"0000000100000000000000010000000000000000000000000000000000000000000000000000000000000037"
const buf = serialize(inputs, publics, { publicOnly: true })
t.is(buf.toString("hex"), expected)
})
test("2d nested arrays", function (t) {
const inputs = {
a: 55n,
b: [
[
340282366920938463463374607431768211455n,
204169420152563078078024764459060926873n,
],
[4n, 5n],
],
}
const publics = { a: true }
const expected =
"000000010000000400000005000000000000000000000000000000000000000000000000000000000000003700000000000000000000000000000000ffffffffffffffffffffffffffffffff000000000000000000000000000000009999999999999999999999999999999900000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000005"
const buf = serialize(inputs, publics)
t.is(buf.toString("hex"), expected)
})
test("handles bignums", function (t) {
const inputs = { x: BigNumber.from(3) }
const publics = { x: true }
const expected =
"0000000100000000000000010000000000000000000000000000000000000000000000000000000000000003"
const buf = serialize(inputs, publics)
t.is(buf.toString("hex"), expected)
})
test("handles inputs gt bn254 curve order", function (t) {
const inputs = {
x: 24198242871839275222246405745257275088696311157297823662689037894645226208583n,
}
const publics = { x: true }
const expected =
"000000010000000000000001051b69e614b483bbfe613f3d36f699b5d66afd5a8fde9ffbf83e9682e87cfd46"
const buf = serialize(inputs, publics)
t.is(buf.toString("hex"), expected)
})
test("failing outpubkey input", function (t) {
const inputs = {
x: 9696282034755050002249722442370417427348178450830409466063672603382572608895n,
}
const publics = { x: true }
const expected =
"000000010000000000000001156fe6b19f029f65eb92dc427880cde3dc64c4d2997b162d09b6aeb2d6d36d7f"
const buf = serialize(inputs, publics)
t.is(buf.toString("hex"), expected)
})
test("complex input", function (t) {
const inputs = {
Root: 2082739236611797325080453634369239079537600201645783730211441698227947076239n,
PublicAmount: 0n,
ExtDataHash:
4071348178776681774860420475568358368210912676584726714277361302891768785228n,
InputNullifier: [
7835388296314843636187257767424759997658499119928008289820475271954976476538n,
3798899406525323241895673860647988473012094951874558620586799079123249345528n,
],
InSafe: [0n, 0n],
InAmount: [100000000000000000n, 0n],
InPrivateKey: [
83954673773626130592400192362215810034236850567608633372771137553253627070073n,
94428126036374894755549601146157230593599630755378005406122897062030651793161n,
],
InBlinding: [
289184164427839320485306849001486046229521124595132064080744981764368187374n,
356702006048331821443953090649120129330156729196723143421731616507451708350n,
],
InToken: [
1184589422945421143511828701991100965039074119625n,
1184589422945421143511828701991100965039074119625n,
],
InNote: [0n, 0n],
InPathIndices: [1n, 0n],
InPathElements: [
[
11400236893008971901885143124958521084356975869654317251799391654791837928021n,
8995896153219992062710898675021891003404871425075198597897889079729967997688n,
15126246733515326086631621937388047923581111613947275249184377560170833782629n,
6404200169958188928270149728908101781856690902670925316782889389790091378414n,
17903822129909817717122288064678017104411031693253675943446999432073303897479n,
],
[0n, 0n, 0n, 0n, 0n],
],
OutputCommitment: [
17832654511599493864034414631554061075425736116204972551912983877495022265823n,
16603373619688897155654344480692315483931086991942797004076400322909681237079n,
],
OutSafe: [0n, 0n],
OutAmount: [40000000000000000n, 60000000000000000n],
OutPubkey: [
6411407462841906536816137161518299250774801015168708148698846593039083327999n,
9696282034755050002249722442370417427348178450830409466063672603382572608895n,
],
OutBlinding: [
345579407108627460644481188701062114034593599050047198764705599357632517673n,
249017094180520992379219241055561384781249373505220481029720605701328279418n,
],
OutToken: [
1184589422945421143511828701991100965039074119625n,
1184589422945421143511828701991100965039074119625n,
],
OutNote: [0n, 0n],
}
const publics = {
Root: true,
PublicAmount: true,
ExtDataHash: true,
InputNullifier: true,
OutputCommitment: true,
}
const expected =
"00000007000000240000002b049ac9e3ddffd69c02a7d2b46408d80b964c73daf352d4cc54023d32b9607a8f000000000000000000000000000000000000000000000000000000000000000009004d291026568ec7dffd9413463b847a718483de63ef9671d5e77cb7a7294c1152ac309dfdb570baad6dbb158f21c0ff7224c96a385ca5ff60e95f77040d7a086619d271fce712fe025d4a2a8c0f36f096c1375d8039c6ca1c4cb3ad1d33f8276cecbdf99f42a833a97dbf6ee9fbdb578539d8c94922499d7d23021beda5df24b52d2b85eac13fbf4e488cbe40488d9214ba2fdff1bb68b51a59c394303c5700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000000000000000000000286fbdfac30c7fa83111975463d765441538a0fb7aaf0cafde33fad33fd006760f33331766c461e3e21531e640db1fb30da4c85ec8c1cd062f1a68408ae59f0500a3ac24d053534292bd8001c3c85c6cbf75e9bd4a88a05c9821c7cae88f3fee00c9e2dcbf7af8a14553fe901631c6bb32ffa09fc780ea70a08fcd578c4c93be000000000000000000000000cf7ed3acca5a467e9e704c703e8d87f634fb0fc9000000000000000000000000cf7ed3acca5a467e9e704c703e8d87f634fb0fc9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000019344e300746e29c3f29011567966fab4d1c2b5af0983615795919084b104a5513e37f2d6cb86c78ccc1788607c2b199788c6bb0a615a21f2e7a8e88384222f8217126fa352c326896e8c2803eec8fd63ad50cf65edfef27a41a9e32dc6227650e28a61a9b3e91007d5a9e3ada18e1b24d6d230c618388ee5df34cacd7397eee27953447a6979839536badc5425ed15fadb0e292e9bc36f92f0aa5cfa50135870000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e1bc9bf04000000000000000000000000000000000000000000000000000000d529ae9e8600000e2cba5ff515d4754da63e4983061642c8d9a4720b540bc89dd473aa930951ff156fe6b19f029f65eb92dc427880cde3dc64c4d2997b162d09b6aeb2d6d36d7f00c3974c9fbd4e6485ea06965ab9a942951babbc15b440ebd8faabfdf69e1629008cf04d5fa4b4e4c96e0814be7e7c664d0a8afa3432a29e89e59d67c7bf177a000000000000000000000000cf7ed3acca5a467e9e704c703e8d87f634fb0fc9000000000000000000000000cf7ed3acca5a467e9e704c703e8d87f634fb0fc900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
const buf = serialize(inputs, publics)
t.is(buf.toString("hex"), expected)
})