mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Compare commits
876 Commits
polkadot-s
...
ca93c82156
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca93c82156 | ||
|
|
5b1875dae6 | ||
|
|
bcd68441be | ||
|
|
4ebf9ad9c7 | ||
|
|
807572199c | ||
|
|
3cdc1536c5 | ||
|
|
9e13e5ebff | ||
|
|
9b2c254eee | ||
|
|
0883479068 | ||
|
|
c5480c63be | ||
|
|
4280ee6987 | ||
|
|
91673d7ae3 | ||
|
|
927f07b62b | ||
|
|
7e774d6d2d | ||
|
|
fccd06b376 | ||
|
|
e3edc0a7fc | ||
|
|
9c47ef2658 | ||
|
|
e1b6b638c6 | ||
|
|
c24768f922 | ||
|
|
87ee879dea | ||
|
|
b5603560e8 | ||
|
|
5818f1a41c | ||
|
|
1b781b4b57 | ||
|
|
94faf098b6 | ||
|
|
03e45f73cd | ||
|
|
63f7e220c0 | ||
|
|
7d49366373 | ||
|
|
55ed33d2d1 | ||
|
|
138a0e9b40 | ||
|
|
4fc7263ac3 | ||
|
|
f27fd59fa6 | ||
|
|
437f0e9a93 | ||
|
|
cc5d38f1ce | ||
|
|
0ce025e0c2 | ||
|
|
224cf4ea21 | ||
|
|
a9b1e5293c | ||
|
|
80009ab67f | ||
|
|
df9fda2971 | ||
|
|
ca8afb83a1 | ||
|
|
18a9cf2535 | ||
|
|
10c126ad92 | ||
|
|
19305aebc9 | ||
|
|
be68e27551 | ||
|
|
d6d96fe8ff | ||
|
|
95909d83a4 | ||
|
|
3bd48974f3 | ||
|
|
29093715e3 | ||
|
|
87b4dfc8f3 | ||
|
|
4db78b1787 | ||
|
|
02a5f15535 | ||
|
|
865e351f96 | ||
|
|
ea275df26c | ||
|
|
2216ade8c4 | ||
|
|
5265cc69de | ||
|
|
a141deaf36 | ||
|
|
215e41fdb6 | ||
|
|
41c34d7f11 | ||
|
|
974bc82387 | ||
|
|
47ef24a7cc | ||
|
|
c0e48867e1 | ||
|
|
0066b94d38 | ||
|
|
7d54c02ec6 | ||
|
|
568324f631 | ||
|
|
2a02a8dc59 | ||
|
|
eaa9a0e5a6 | ||
|
|
251996c1b0 | ||
|
|
98b9cc82a7 | ||
|
|
263d75d380 | ||
|
|
030185c7fc | ||
|
|
e2dc5db7aa | ||
|
|
90bc364f9f | ||
|
|
a4811c9a41 | ||
|
|
12cfa6b2a5 | ||
|
|
0c71b6fc4d | ||
|
|
ffe1b60a11 | ||
|
|
5526b8d439 | ||
|
|
beac35c119 | ||
|
|
62bb75e09a | ||
|
|
45bd376c08 | ||
|
|
da190759a9 | ||
|
|
f2d399ba1e | ||
|
|
220bcbc592 | ||
|
|
85949f4b04 | ||
|
|
f8adfb56ad | ||
|
|
2f833dec77 | ||
|
|
e3e41324c9 | ||
|
|
6ed7c5d65e | ||
|
|
9dddfd91c8 | ||
|
|
c24b694fb2 | ||
|
|
738babf7e9 | ||
|
|
33faa53b56 | ||
|
|
8c366107ae | ||
|
|
7a790f3a20 | ||
|
|
a7c77f8b5f | ||
|
|
da3095ed15 | ||
|
|
758d422595 | ||
|
|
9841061b49 | ||
|
|
4122a0135f | ||
|
|
b63ef32864 | ||
|
|
8be03a8fc2 | ||
|
|
677a2e5749 | ||
|
|
38bda1d586 | ||
|
|
2bc2ca6906 | ||
|
|
900a6612d7 | ||
|
|
17c1d5cd6b | ||
|
|
8a1b56a928 | ||
|
|
75964cf6da | ||
|
|
d407e35cee | ||
|
|
c8ef044acb | ||
|
|
ddbc32de4d | ||
|
|
e5ccfac19e | ||
|
|
432daae1d1 | ||
|
|
da3a85efe5 | ||
|
|
1e0240123d | ||
|
|
f6d4d1b084 | ||
|
|
1b37dd2951 | ||
|
|
f32e0609f1 | ||
|
|
ca85f9ba0c | ||
|
|
cfd1cb3a37 | ||
|
|
f2c13a0040 | ||
|
|
961f46bc04 | ||
|
|
2c4de3bab4 | ||
|
|
95c30720d2 | ||
|
|
ceede14f5c | ||
|
|
5e60ea9718 | ||
|
|
153f6f2f2f | ||
|
|
104c0d4492 | ||
|
|
7c8f13ab28 | ||
|
|
cb0deadf9a | ||
|
|
cb489f9cef | ||
|
|
cc662cb591 | ||
|
|
a8b8844e3f | ||
|
|
82b543ef75 | ||
|
|
72e80c1a3d | ||
|
|
b6edc94bcd | ||
|
|
cfce2b26e2 | ||
|
|
e87bbcda64 | ||
|
|
9f84adf8b3 | ||
|
|
3919cf55ae | ||
|
|
38dd8cb191 | ||
|
|
f2563d39cb | ||
|
|
15a9cbef40 | ||
|
|
078d6e51e5 | ||
|
|
6c33e18745 | ||
|
|
b743c9a43e | ||
|
|
0c2f2979a9 | ||
|
|
971951a1a6 | ||
|
|
92d9e908cb | ||
|
|
a32b97be88 | ||
|
|
e3809b2ff1 | ||
|
|
fd2d8b4f0a | ||
|
|
bc81614894 | ||
|
|
8df5aa2e2d | ||
|
|
b000740470 | ||
|
|
b9f554111d | ||
|
|
354c408e3e | ||
|
|
df3b60376a | ||
|
|
8d209c652e | ||
|
|
9ddad794b4 | ||
|
|
b934e484cc | ||
|
|
f8aee9b3c8 | ||
|
|
f51d77d26a | ||
|
|
0780deb643 | ||
|
|
75c38560f4 | ||
|
|
9f1c5268a5 | ||
|
|
35b113768b | ||
|
|
f2595c4939 | ||
|
|
8fcfa6d3d5 | ||
|
|
54c9d19726 | ||
|
|
25324c3cd5 | ||
|
|
ecb7df85b0 | ||
|
|
68c7acdbef | ||
|
|
8b60feed92 | ||
|
|
5c895efcd0 | ||
|
|
60e55656aa | ||
|
|
9536282418 | ||
|
|
8297d0679d | ||
|
|
d9f854b08a | ||
|
|
8aaf7f7dc6 | ||
|
|
ce447558ac | ||
|
|
fc850da30e | ||
|
|
d6f6cf1965 | ||
|
|
4438b51881 | ||
|
|
6ae0d9fad7 | ||
|
|
ad08b410a8 | ||
|
|
ec3cfd3ab7 | ||
|
|
01eb2daa0b | ||
|
|
885000f970 | ||
|
|
4be506414b | ||
|
|
1143d84e1d | ||
|
|
336922101f | ||
|
|
ffa033d978 | ||
|
|
23f986f57a | ||
|
|
bb726b58af | ||
|
|
387615705c | ||
|
|
c7f825a192 | ||
|
|
d363b1c173 | ||
|
|
d5077ae966 | ||
|
|
188fcc3cb4 | ||
|
|
cbab9486c6 | ||
|
|
a5f4c450c6 | ||
|
|
4f65a0b147 | ||
|
|
feb18d64a7 | ||
|
|
cb1e6535cb | ||
|
|
6b8cf6653a | ||
|
|
b426bfcfe8 | ||
|
|
21ce50ecf7 | ||
|
|
a4ceb2e756 | ||
|
|
b59b1f59dd | ||
|
|
cc4a65e82a | ||
|
|
eab5d9e64f | ||
|
|
4e0c58464f | ||
|
|
205da3fd38 | ||
|
|
f7e63d4944 | ||
|
|
b5608fc3d2 | ||
|
|
33018bf6da | ||
|
|
bef90b2f1a | ||
|
|
184c02714a | ||
|
|
5a7b815e2e | ||
|
|
22e411981a | ||
|
|
11d48d0685 | ||
|
|
e4cc23b72d | ||
|
|
52d853c8ba | ||
|
|
9c33a711d7 | ||
|
|
a275023cfc | ||
|
|
258c02ff39 | ||
|
|
3655dc723f | ||
|
|
315d4fb356 | ||
|
|
2bc880e372 | ||
|
|
19422de231 | ||
|
|
fa0dadc9bd | ||
|
|
f004c8726f | ||
|
|
835b5bb06f | ||
|
|
0484113254 | ||
|
|
17cc10b3f7 | ||
|
|
7e01589fba | ||
|
|
f8c3acae7b | ||
|
|
0957460f27 | ||
|
|
ea00ba9ff8 | ||
|
|
a9625364df | ||
|
|
75c6427d7c | ||
|
|
e742a6b0ec | ||
|
|
5164a710a2 | ||
|
|
27c1dc4646 | ||
|
|
3892fa30b7 | ||
|
|
ed599c8ab5 | ||
|
|
29bb5e21ab | ||
|
|
604a4b2442 | ||
|
|
977dcad86d | ||
|
|
cefc542744 | ||
|
|
164fe9a14f | ||
|
|
f948881eba | ||
|
|
201b675031 | ||
|
|
3d44766eff | ||
|
|
a63a86ba79 | ||
|
|
e922264ebf | ||
|
|
7e53eff642 | ||
|
|
669b8b776b | ||
|
|
6508957cbc | ||
|
|
373e794d2c | ||
|
|
c8f3a32fdf | ||
|
|
f690bf831f | ||
|
|
0b30ac175e | ||
|
|
47560fa9a9 | ||
|
|
9d57c4eb4d | ||
|
|
642ba00952 | ||
|
|
3c9c12d320 | ||
|
|
f6b52b3fd3 | ||
|
|
0d906363a0 | ||
|
|
8222ce78d8 | ||
|
|
cb906242e7 | ||
|
|
2a19e9da93 | ||
|
|
2226dd59cc | ||
|
|
be2098d2e1 | ||
|
|
6b41f32371 | ||
|
|
19b87c7f5a | ||
|
|
505f1b20a4 | ||
|
|
8b52b921f3 | ||
|
|
f36bbcba25 | ||
|
|
167826aa88 | ||
|
|
bea4f92b7a | ||
|
|
7312fa8d3c | ||
|
|
92a4cceeeb | ||
|
|
3357181fe2 | ||
|
|
7ce5bdad44 | ||
|
|
0de3fda921 | ||
|
|
cb410cc4e0 | ||
|
|
6c145a5ec3 | ||
|
|
a7fef2ba7a | ||
|
|
291ebf5e24 | ||
|
|
5e0e91c85d | ||
|
|
b5a6b0693e | ||
|
|
3cc2abfedc | ||
|
|
0ce9aad9b2 | ||
|
|
e35aa04afb | ||
|
|
e7de5125a2 | ||
|
|
158140c3a7 | ||
|
|
df9a9adaa8 | ||
|
|
d854807edd | ||
|
|
f501d46d44 | ||
|
|
74106b025f | ||
|
|
e731b546ab | ||
|
|
77d60660d2 | ||
|
|
3c664ff05f | ||
|
|
c05b0c9eba | ||
|
|
6d5049cab2 | ||
|
|
1419ba570a | ||
|
|
542bf2170a | ||
|
|
378d6b90cf | ||
|
|
cbe83956aa | ||
|
|
091d485fd8 | ||
|
|
2a3eaf4d7e | ||
|
|
23122712cb | ||
|
|
47eb793ce9 | ||
|
|
9b0b5fd1e2 | ||
|
|
893a24a1cc | ||
|
|
b101e2211a | ||
|
|
201a444e89 | ||
|
|
9833911e06 | ||
|
|
465e8498c4 | ||
|
|
adf20773ac | ||
|
|
295c1bd044 | ||
|
|
dda6e3e899 | ||
|
|
75a00f2a1a | ||
|
|
6cde2bb6ef | ||
|
|
20326bba73 | ||
|
|
ce83b41712 | ||
|
|
b2bd5d3a44 | ||
|
|
de2d6568a4 | ||
|
|
fd9b464b35 | ||
|
|
376a66b000 | ||
|
|
2121a9b131 | ||
|
|
419223c54e | ||
|
|
a731c0005d | ||
|
|
f27e4e3202 | ||
|
|
f55165e016 | ||
|
|
d9e9887d34 | ||
|
|
82e753db30 | ||
|
|
052388285b | ||
|
|
47a4e534ef | ||
|
|
257f691277 | ||
|
|
c6d0fb477c | ||
|
|
96518500b1 | ||
|
|
2b8f481364 | ||
|
|
479ca0410a | ||
|
|
9a5a661d04 | ||
|
|
3daeea09e6 | ||
|
|
a64e2004ab | ||
|
|
f9f6d40695 | ||
|
|
4836c1676b | ||
|
|
985261574c | ||
|
|
3f3b0255f8 | ||
|
|
5fc8500f8d | ||
|
|
49c221cca2 | ||
|
|
906e2fb669 | ||
|
|
ce676efb1f | ||
|
|
0a611cb155 | ||
|
|
bcd3f14f4f | ||
|
|
6272c40561 | ||
|
|
2240a50a0c | ||
|
|
7e2b31e5da | ||
|
|
8c9441a1a5 | ||
|
|
5a42f66dc2 | ||
|
|
b584a2beab | ||
|
|
26ccff25a1 | ||
|
|
f0094b3c7c | ||
|
|
458f4fe170 | ||
|
|
1de8136739 | ||
|
|
445c49f030 | ||
|
|
5b74fc8ac1 | ||
|
|
e67e301fc2 | ||
|
|
1d50792eed | ||
|
|
9c92709e62 | ||
|
|
3d15710a43 | ||
|
|
df06da5552 | ||
|
|
cef5bc95b0 | ||
|
|
f336ab1ece | ||
|
|
2aebfb21af | ||
|
|
56af6c44eb | ||
|
|
4b34be05bf | ||
|
|
5b337c3ce8 | ||
|
|
e119fb4c16 | ||
|
|
ef972b2658 | ||
|
|
4de1a5804d | ||
|
|
147a6e43d0 | ||
|
|
066aa9eda4 | ||
|
|
9593a428e3 | ||
|
|
5b3c5ec02b | ||
|
|
9ccfa8a9f5 | ||
|
|
18897978d0 | ||
|
|
3192370484 | ||
|
|
8013c56195 | ||
|
|
834c16930b | ||
|
|
2920987173 | ||
|
|
26230377b0 | ||
|
|
2f5c0c68d0 | ||
|
|
8de42cc2d4 | ||
|
|
cf4123b0f8 | ||
|
|
6a520a7412 | ||
|
|
b2ec58a445 | ||
|
|
8e800885fb | ||
|
|
2a427382f1 | ||
|
|
e9c1235b76 | ||
|
|
dc1b8dfccd | ||
|
|
ce1689b325 | ||
|
|
d0201cf2e5 | ||
|
|
f3d20e60b3 | ||
|
|
dafba81b40 | ||
|
|
91f8ec53d9 | ||
|
|
fc9a4a08b8 | ||
|
|
45fadb21ac | ||
|
|
28619fbee1 | ||
|
|
bbe014c3a7 | ||
|
|
fb3fadb3d3 | ||
|
|
f481d20773 | ||
|
|
599b2dec8f | ||
|
|
435f1d9ae1 | ||
|
|
0b61a75afc | ||
|
|
2aee21e507 | ||
|
|
d7ecab605e | ||
|
|
b3e003bd5d | ||
|
|
251a6e96e8 | ||
|
|
805fea52ec | ||
|
|
48db06f901 | ||
|
|
e9d0a5e0ed | ||
|
|
44d05518aa | ||
|
|
23b433fe6c | ||
|
|
2e57168a97 | ||
|
|
5c6160c398 | ||
|
|
9eee1d971e | ||
|
|
e6300847d6 | ||
|
|
e0a3e7bea6 | ||
|
|
cbebaa1349 | ||
|
|
2c8af04781 | ||
|
|
a0ed043372 | ||
|
|
2984d2f8cf | ||
|
|
554c5778e4 | ||
|
|
7e4c59a0a3 | ||
|
|
294462641e | ||
|
|
ae76749513 | ||
|
|
1e1b821d34 | ||
|
|
702b4c860c | ||
|
|
bc1bbf9951 | ||
|
|
ec9211fd84 | ||
|
|
4292660eda | ||
|
|
8ea5acbacb | ||
|
|
1b1aa74770 | ||
|
|
861a8352e5 | ||
|
|
e64827b6d7 | ||
|
|
c27aaf8658 | ||
|
|
53567e91c8 | ||
|
|
1a08d50e16 | ||
|
|
855e53164e | ||
|
|
1367e41510 | ||
|
|
a691be21c8 | ||
|
|
673cf8fd47 | ||
|
|
118d81bc90 | ||
|
|
e75c4ec6ed | ||
|
|
9e628d217f | ||
|
|
a717ae9ea7 | ||
|
|
98c3f75fa2 | ||
|
|
18178f3764 | ||
|
|
bdc3bda04a | ||
|
|
433beac93a | ||
|
|
8f2a9301cf | ||
|
|
d21034c349 | ||
|
|
381495618c | ||
|
|
ee0efe7cde | ||
|
|
7feb7aed22 | ||
|
|
cc75a92641 | ||
|
|
a7d5640642 | ||
|
|
ae61f3d359 | ||
|
|
4bcea31c2a | ||
|
|
eb9bce6862 | ||
|
|
39be23d807 | ||
|
|
3f0f4d520d | ||
|
|
80ca2b780a | ||
|
|
0813351f1f | ||
|
|
a38d135059 | ||
|
|
67f9f76fdf | ||
|
|
1c5bc2259e | ||
|
|
bdf89f5350 | ||
|
|
239127aae5 | ||
|
|
d9543bee40 | ||
|
|
8746b54a43 | ||
|
|
7761798a78 | ||
|
|
72a18bf8bb | ||
|
|
0616085109 | ||
|
|
e23176deeb | ||
|
|
5551521e58 | ||
|
|
a2d9aeaed7 | ||
|
|
e1ad897f7e | ||
|
|
2edc2f3612 | ||
|
|
e56af7fc51 | ||
|
|
947e1067d9 | ||
|
|
b4e94f3d51 | ||
|
|
1b39138472 | ||
|
|
e78236276a | ||
|
|
2c4c33e632 | ||
|
|
02409c5735 | ||
|
|
f2cf03cedf | ||
|
|
0d4c8cf032 | ||
|
|
b6811f9015 | ||
|
|
fcd5fb85df | ||
|
|
3ac0265f07 | ||
|
|
9b8c8f8231 | ||
|
|
59fa49f750 | ||
|
|
723f529659 | ||
|
|
73af09effb | ||
|
|
4054e44471 | ||
|
|
a8159e9070 | ||
|
|
b61ba9d1bb | ||
|
|
776cbbb9a4 | ||
|
|
76a3f3ec4b | ||
|
|
93c7d06684 | ||
|
|
4cb838e248 | ||
|
|
c988b7cdb0 | ||
|
|
017aab2258 | ||
|
|
ba3a6f9e91 | ||
|
|
e36b671f37 | ||
|
|
2d4b775b6e | ||
|
|
247cc8f0cc | ||
|
|
0ccf71df1e | ||
|
|
8aba71b9c4 | ||
|
|
46c12c0e66 | ||
|
|
3cc7b49492 | ||
|
|
0078858c1c | ||
|
|
a3cb514400 | ||
|
|
ed0221d804 | ||
|
|
4152bcacb2 | ||
|
|
f07ec7bee0 | ||
|
|
7484eadbbb | ||
|
|
59ff944152 | ||
|
|
8f848b1abc | ||
|
|
100c80be9f | ||
|
|
a353f9e2da | ||
|
|
b62fc3a1fa | ||
|
|
8380653855 | ||
|
|
b50b889918 | ||
|
|
d570c1d277 | ||
|
|
2da24506a2 | ||
|
|
6e9cb74022 | ||
|
|
0c1aec29bb | ||
|
|
653ead1e8c | ||
|
|
8ff019265f | ||
|
|
0601d47789 | ||
|
|
ebef38d93b | ||
|
|
75b4707002 | ||
|
|
3c787e005f | ||
|
|
f11a6b4ff1 | ||
|
|
fadc88d2ad | ||
|
|
c88ebe985e | ||
|
|
6deb60513c | ||
|
|
bd277e7032 | ||
|
|
fc765bb9e0 | ||
|
|
13b74195f7 | ||
|
|
f21838e0d5 | ||
|
|
76cbe6cf1e | ||
|
|
5999f5d65a | ||
|
|
d429a0bae6 | ||
|
|
775824f373 | ||
|
|
41a74cb513 | ||
|
|
e26da1ec34 | ||
|
|
7266e7f7ea | ||
|
|
a8b9b7bad3 | ||
|
|
2ca7fccb08 | ||
|
|
4f6d91037e | ||
|
|
8db76ed67c | ||
|
|
920303e1b4 | ||
|
|
9f4b28e5ae | ||
|
|
f9d02d43c2 | ||
|
|
8ac501028d | ||
|
|
612c67c537 | ||
|
|
04a971a024 | ||
|
|
738636c238 | ||
|
|
65f3f48517 | ||
|
|
7cc07d64d1 | ||
|
|
fdfe520f9d | ||
|
|
77ef25416b | ||
|
|
7c1025dbcb | ||
|
|
a771fbe1c6 | ||
|
|
9cebdf7c68 | ||
|
|
75251f04b4 | ||
|
|
6196642beb | ||
|
|
2bddf00222 | ||
|
|
9ab8ba0215 | ||
|
|
33e0c85f34 | ||
|
|
1e8f4e6156 | ||
|
|
66f3428051 | ||
|
|
7e71840822 | ||
|
|
b65dbacd6a | ||
|
|
2fcd9530dd | ||
|
|
379780a3c9 | ||
|
|
945f31dfc7 | ||
|
|
d5d1fc3eea | ||
|
|
fd12cc0213 | ||
|
|
ce805c8cc8 | ||
|
|
bc0cc5a754 | ||
|
|
f2ee4daf43 | ||
|
|
4e29678799 | ||
|
|
74d3075dae | ||
|
|
155ad48f4c | ||
|
|
951872b026 | ||
|
|
2b47feafed | ||
|
|
a2717d73f0 | ||
|
|
8763ef23ed | ||
|
|
57a0ba966b | ||
|
|
e843b4a2a0 | ||
|
|
2f3bd7a02a | ||
|
|
1e8a9ec5bd | ||
|
|
2f29c91d30 | ||
|
|
f3b91bd44f | ||
|
|
e4e4245ee3 | ||
|
|
669b2fef72 | ||
|
|
3af430d8de | ||
|
|
dfb5a053ae | ||
|
|
bdcc061bb4 | ||
|
|
2c7148d636 | ||
|
|
6b270bc6aa | ||
|
|
875c669a7a | ||
|
|
0d399ecb28 | ||
|
|
88440807e1 | ||
|
|
c1a9256cc5 | ||
|
|
0d5756ffcf | ||
|
|
ac7b98daac | ||
|
|
efc7d70ab1 | ||
|
|
4e834873d3 | ||
|
|
a506d74d69 | ||
|
|
394db44b30 | ||
|
|
a2df54dd6a | ||
|
|
efc45c391b | ||
|
|
cccc1fc7e6 | ||
|
|
bf1c493d9a | ||
|
|
3de1e4dee2 | ||
|
|
2591b5ade9 | ||
|
|
e6620963c7 | ||
|
|
d5205ce231 | ||
|
|
0f6878567f | ||
|
|
880565cb81 | ||
|
|
6f34c2ff77 | ||
|
|
1493f49416 | ||
|
|
2ccb0cd90d | ||
|
|
b33a6487aa | ||
|
|
491500057b | ||
|
|
d9f85fab26 | ||
|
|
7d2d739042 | ||
|
|
40cc180853 | ||
|
|
2aac6f6998 | ||
|
|
149c2a4437 | ||
|
|
e772b8a5f7 | ||
|
|
c0200df75a | ||
|
|
9955ef54a5 | ||
|
|
8e7e61adbd | ||
|
|
0cb24dde02 | ||
|
|
97bfb183e8 | ||
|
|
85fc31fd82 | ||
|
|
7b8bcae396 | ||
|
|
70fe52437c | ||
|
|
ba657e23d1 | ||
|
|
32c24917c4 | ||
|
|
4ba961b2cb | ||
|
|
c59be46e2f | ||
|
|
2c165e19ae | ||
|
|
ee10692b23 | ||
|
|
7a68b065e0 | ||
|
|
3ddf1eec0c | ||
|
|
84f0e6c26e | ||
|
|
5bb3256d1f | ||
|
|
774424b70b | ||
|
|
ed662568e2 | ||
|
|
b744ac9a76 | ||
|
|
d7f7f69738 | ||
|
|
a2c3aba82b | ||
|
|
703c6a2358 | ||
|
|
52bb918cc9 | ||
|
|
ba244e8090 | ||
|
|
3e99d68cfe | ||
|
|
4d9c2df38c | ||
|
|
8ab6f9c36e | ||
|
|
253cf3253d | ||
|
|
03445b3020 | ||
|
|
9af111b4aa | ||
|
|
41ce5b1738 | ||
|
|
2a05cf3225 | ||
|
|
f4147c39b2 | ||
|
|
cd69f3b9d6 | ||
|
|
1d2beb3ee4 | ||
|
|
ac709b2945 | ||
|
|
a473800c26 | ||
|
|
09aac20293 | ||
|
|
f93214012d | ||
|
|
400319cd29 | ||
|
|
a0a7d63dad | ||
|
|
fb7d12ee6e | ||
|
|
11ec9e3535 | ||
|
|
ae8a27b876 | ||
|
|
af79586488 | ||
|
|
d27d93480a | ||
|
|
02c4417a46 | ||
|
|
79a79db399 | ||
|
|
0c9dd5048e | ||
|
|
5501de1f3a | ||
|
|
21123590bb | ||
|
|
bc1dec7991 | ||
|
|
cef63a631a | ||
|
|
d57fef8999 | ||
|
|
d1474e9188 | ||
|
|
b39c751403 | ||
|
|
cc7202e0bf | ||
|
|
19e68f7f75 | ||
|
|
d94c9a4a5e | ||
|
|
43dc036660 | ||
|
|
95591218bb | ||
|
|
7dd587a864 | ||
|
|
023275bcb6 | ||
|
|
8cef9eff6f | ||
|
|
b5e22dca8f | ||
|
|
a41329c027 | ||
|
|
a25e6330bd | ||
|
|
558a2bfa46 | ||
|
|
c73acb3d62 | ||
|
|
933b17aa91 | ||
|
|
5fa7e3d450 | ||
|
|
749d783b1e | ||
|
|
5a3ea80943 | ||
|
|
fddbebc7c0 | ||
|
|
e01848aa9e | ||
|
|
320b5627b5 | ||
|
|
be7780e69d | ||
|
|
0ddbaefb38 | ||
|
|
0f0db14f05 | ||
|
|
43083dfd49 | ||
|
|
523d2ac911 | ||
|
|
fd4f247917 | ||
|
|
ac9e356af4 | ||
|
|
bba7d2a356 | ||
|
|
4c349ae605 | ||
|
|
a4428761f7 | ||
|
|
940e9553fd | ||
|
|
593aefd229 | ||
|
|
5830c2463d | ||
|
|
bcc88c3e86 | ||
|
|
fea16df567 | ||
|
|
4960c3222e | ||
|
|
6b4df4f2c0 | ||
|
|
dac46c8d7d | ||
|
|
db2e8376df | ||
|
|
33dd412e67 | ||
|
|
fcad402186 | ||
|
|
ab4d79628d | ||
|
|
93be7a3067 | ||
|
|
63521f6a96 | ||
|
|
3d855c75be | ||
|
|
07df9aa035 | ||
|
|
bc44fbdbac | ||
|
|
4cacce5e55 | ||
|
|
7408e26781 | ||
|
|
1f92e1cbda | ||
|
|
333a9571b8 | ||
|
|
b7d49af1d5 | ||
|
|
5ea3b1bf97 | ||
|
|
2a31d8552e | ||
|
|
bca3728a10 | ||
|
|
4914420a37 | ||
|
|
f11a08c436 | ||
|
|
35b58a45bd | ||
|
|
af9b1ad5f9 | ||
|
|
e5afcda76b | ||
|
|
08c7c1b413 | ||
|
|
bdf5a66e95 | ||
|
|
e861859dec | ||
|
|
6658d95c85 | ||
|
|
2f07d04d88 | ||
|
|
e0259f2fe5 | ||
|
|
fab7a0a7cb | ||
|
|
84cee06ac1 | ||
|
|
c706d8664a | ||
|
|
1f2b9376f9 | ||
|
|
13b147cbf6 | ||
|
|
4a6496a90b | ||
|
|
9662d94bf9 | ||
|
|
233164cefd | ||
|
|
442d8c02fc | ||
|
|
d1be9eaa2d | ||
|
|
c32d3413ba | ||
|
|
a3a009a7e9 | ||
|
|
0889627e60 | ||
|
|
ace41c79fd | ||
|
|
f7d16b3fc5 | ||
|
|
157acc47ca | ||
|
|
ae0ecf9efe | ||
|
|
6374d9987e | ||
|
|
c93f6bf901 | ||
|
|
61a81e53e1 | ||
|
|
68dc872b88 | ||
|
|
89b237af7e | ||
|
|
2347bf5fd3 | ||
|
|
97f433c694 | ||
|
|
10f5ec51ca | ||
|
|
454bebaa77 | ||
|
|
0d569ff7a3 | ||
|
|
480acfd430 | ||
|
|
e266bc2e32 | ||
|
|
6c8a0bfda6 | ||
|
|
06c23368f2 | ||
|
|
5629c94b8b | ||
|
|
b427f4b8ab | ||
|
|
1096ddb7ea | ||
|
|
5487844b9e | ||
|
|
627e7e6210 | ||
|
|
019b42c0e0 | ||
|
|
079fddbaa6 | ||
|
|
92d8b91be9 | ||
|
|
4f1f7984a6 | ||
|
|
cda14ac8b9 | ||
|
|
6f5d794f10 | ||
|
|
34b93b882c | ||
|
|
0880453f82 | ||
|
|
ebdfc9afb4 | ||
|
|
f6409d08f3 | ||
|
|
c41a8ac8f2 | ||
|
|
d88aa90ec2 | ||
|
|
c05c511938 | ||
|
|
df85c09435 | ||
|
|
62a619a312 | ||
|
|
95b7460907 | ||
|
|
95c3cfc52e | ||
|
|
f0694172ef | ||
|
|
29633ada1b | ||
|
|
337e54c672 | ||
|
|
347d4cf413 | ||
|
|
aaff74575f | ||
|
|
ad0ecc5185 | ||
|
|
af12cec3b9 | ||
|
|
89788be034 | ||
|
|
745075af6e | ||
|
|
9b25d0dad7 | ||
|
|
2b76e41c9a | ||
|
|
05219c3ce8 | ||
|
|
cc75b52a43 | ||
|
|
4913873b10 | ||
|
|
0b8c7ade6e | ||
|
|
21262d41e6 | ||
|
|
508f7eb23a | ||
|
|
90df391170 | ||
|
|
9d3d47fc9f | ||
|
|
6691f16292 | ||
|
|
9c06cbccad | ||
|
|
c507ab9fd6 | ||
|
|
3aa8007700 | ||
|
|
1ba2d8d832 | ||
|
|
e7b0ed3e7e | ||
|
|
f3429ec1ef | ||
|
|
1cff9b4264 | ||
|
|
3c5a82e915 | ||
|
|
93e85c5ce6 | ||
|
|
617ec604ee | ||
|
|
265261d3ba | ||
|
|
7eb388e546 | ||
|
|
6c8040f723 | ||
|
|
02776c54a8 | ||
|
|
ec8dfd4639 | ||
|
|
99e05e4e5e | ||
|
|
a72b547824 | ||
|
|
bad3d210ba | ||
|
|
8c676d98c5 | ||
|
|
890b70212a | ||
|
|
9f7140c3db | ||
|
|
8b26a85faa | ||
|
|
24ea65eae9 | ||
|
|
fff8dcb827 | ||
|
|
2b23252b4c | ||
|
|
b493e3e31f | ||
|
|
00774c29d7 | ||
|
|
a4c82632fb | ||
|
|
c8747e23c5 |
2
.github/actions/LICENSE → .github/LICENSE
vendored
2
.github/actions/LICENSE → .github/LICENSE
vendored
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2022-2023 Luke Parker
|
Copyright (c) 2022-2025 Luke Parker
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
13
.github/actions/bitcoin/action.yml
vendored
13
.github/actions/bitcoin/action.yml
vendored
@@ -5,14 +5,14 @@ inputs:
|
|||||||
version:
|
version:
|
||||||
description: "Version to download and run"
|
description: "Version to download and run"
|
||||||
required: false
|
required: false
|
||||||
default: 24.0.1
|
default: "30.0"
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Bitcoin Daemon Cache
|
- name: Bitcoin Daemon Cache
|
||||||
id: cache-bitcoind
|
id: cache-bitcoind
|
||||||
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84
|
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809
|
||||||
with:
|
with:
|
||||||
path: bitcoin.tar.gz
|
path: bitcoin.tar.gz
|
||||||
key: bitcoind-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
|
key: bitcoind-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
|
||||||
@@ -37,11 +37,4 @@ runs:
|
|||||||
|
|
||||||
- name: Bitcoin Regtest Daemon
|
- name: Bitcoin Regtest Daemon
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: PATH=$PATH:/usr/bin ./orchestration/dev/networks/bitcoin/run.sh -txindex -daemon
|
||||||
RPC_USER=serai
|
|
||||||
RPC_PASS=seraidex
|
|
||||||
|
|
||||||
bitcoind -txindex -regtest \
|
|
||||||
-rpcuser=$RPC_USER -rpcpassword=$RPC_PASS \
|
|
||||||
-rpcbind=127.0.0.1 -rpcbind=$(hostname) -rpcallowip=0.0.0.0/0 \
|
|
||||||
-daemon
|
|
||||||
|
|||||||
82
.github/actions/build-dependencies/action.yml
vendored
82
.github/actions/build-dependencies/action.yml
vendored
@@ -1,41 +1,85 @@
|
|||||||
name: build-dependencies
|
name: build-dependencies
|
||||||
description: Installs build dependencies for Serai
|
description: Installs build dependencies for Serai
|
||||||
|
|
||||||
inputs:
|
|
||||||
github-token:
|
|
||||||
description: "GitHub token to install Protobuf with"
|
|
||||||
require: true
|
|
||||||
default:
|
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Remove unused packages
|
- name: Remove unused packages
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
sudo apt remove -y "*msbuild*" "*powershell*" "*nuget*" "*bazel*" "*ansible*" "*terraform*" "*heroku*" "*aws*" azure-cli
|
# Ensure the repositories are synced
|
||||||
|
sudo apt update -y
|
||||||
|
|
||||||
|
# Actually perform the removals
|
||||||
|
sudo apt remove -y "*powershell*" "*nuget*" "*bazel*" "*ansible*" "*terraform*" "*heroku*" "*aws*" azure-cli
|
||||||
sudo apt remove -y "*nodejs*" "*npm*" "*yarn*" "*java*" "*kotlin*" "*golang*" "*swift*" "*julia*" "*fortran*" "*android*"
|
sudo apt remove -y "*nodejs*" "*npm*" "*yarn*" "*java*" "*kotlin*" "*golang*" "*swift*" "*julia*" "*fortran*" "*android*"
|
||||||
sudo apt remove -y "*apache2*" "*nginx*" "*firefox*" "*chromium*" "*chrome*" "*edge*"
|
sudo apt remove -y "*apache2*" "*nginx*" "*firefox*" "*chromium*" "*chrome*" "*edge*"
|
||||||
|
|
||||||
|
sudo apt remove -y --allow-remove-essential -f shim-signed *python3*
|
||||||
|
# This removal command requires the prior removals due to unmet dependencies otherwise
|
||||||
sudo apt remove -y "*qemu*" "*sql*" "*texinfo*" "*imagemagick*"
|
sudo apt remove -y "*qemu*" "*sql*" "*texinfo*" "*imagemagick*"
|
||||||
sudo apt autoremove -y
|
|
||||||
sudo apt clean
|
|
||||||
docker system prune -a --volumes
|
|
||||||
|
|
||||||
- name: Install apt dependencies
|
# Reinstall python3 as a general dependency of a functional operating system
|
||||||
|
sudo apt install -y python3 --fix-missing
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
|
||||||
|
- name: Remove unused packages
|
||||||
shell: bash
|
shell: bash
|
||||||
run: sudo apt install -y ca-certificates
|
run: |
|
||||||
|
(gem uninstall -aIx) || (exit 0)
|
||||||
|
brew uninstall --force "*msbuild*" "*powershell*" "*nuget*" "*bazel*" "*ansible*" "*terraform*" "*heroku*" "*aws*" azure-cli
|
||||||
|
brew uninstall --force "*nodejs*" "*npm*" "*yarn*" "*java*" "*kotlin*" "*golang*" "*swift*" "*julia*" "*fortran*" "*android*"
|
||||||
|
brew uninstall --force "*apache2*" "*nginx*" "*firefox*" "*chromium*" "*chrome*" "*edge*"
|
||||||
|
brew uninstall --force "*qemu*" "*sql*" "*texinfo*" "*imagemagick*"
|
||||||
|
brew cleanup
|
||||||
|
if: runner.os == 'macOS'
|
||||||
|
|
||||||
- name: Install Protobuf
|
- name: Install dependencies
|
||||||
uses: arduino/setup-protoc@a8b67ba40b37d35169e222f3bb352603327985b6
|
shell: bash
|
||||||
with:
|
run: |
|
||||||
repo-token: ${{ inputs.github-token }}
|
if [ "$RUNNER_OS" == "Linux" ]; then
|
||||||
|
sudo apt install -y ca-certificates protobuf-compiler libclang-dev
|
||||||
|
elif [ "$RUNNER_OS" == "Windows" ]; then
|
||||||
|
choco install protoc
|
||||||
|
elif [ "$RUNNER_OS" == "macOS" ]; then
|
||||||
|
brew install protobuf llvm
|
||||||
|
HOMEBREW_ROOT_PATH=/opt/homebrew # Apple Silicon
|
||||||
|
if [ $(uname -m) = "x86_64" ]; then HOMEBREW_ROOT_PATH=/usr/local; fi # Intel
|
||||||
|
ls $HOMEBREW_ROOT_PATH/opt/llvm/lib | grep "libclang.dylib" # Make sure this installed `libclang`
|
||||||
|
echo "DYLD_LIBRARY_PATH=$HOMEBREW_ROOT_PATH/opt/llvm/lib:$DYLD_LIBRARY_PATH" >> "$GITHUB_ENV"
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Install solc
|
- name: Install solc
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
cargo install svm-rs
|
cargo +1.91 install svm-rs --version =0.5.19
|
||||||
svm install 0.8.16
|
svm install 0.8.29
|
||||||
svm use 0.8.16
|
svm use 0.8.29
|
||||||
|
|
||||||
|
- name: Remove preinstalled Docker
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
docker system prune -a --volumes
|
||||||
|
sudo apt remove -y *docker*
|
||||||
|
# Install uidmap which will be required for the explicitly installed Docker
|
||||||
|
sudo apt install uidmap
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
|
||||||
|
- name: Update system dependencies
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
sudo apt update -y
|
||||||
|
sudo apt upgrade -y
|
||||||
|
sudo apt autoremove -y
|
||||||
|
sudo apt clean
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
|
||||||
|
- name: Install rootless Docker
|
||||||
|
uses: docker/setup-docker-action@b60f85385d03ac8acfca6d9996982511d8620a19
|
||||||
|
with:
|
||||||
|
rootless: true
|
||||||
|
set-host: true
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
|
||||||
# - name: Cache Rust
|
# - name: Cache Rust
|
||||||
# uses: Swatinem/rust-cache@a95ba195448af2da9b00fb742d14ffaaf3c21f43
|
# uses: Swatinem/rust-cache@a95ba195448af2da9b00fb742d14ffaaf3c21f43
|
||||||
|
|||||||
11
.github/actions/monero-wallet-rpc/action.yml
vendored
11
.github/actions/monero-wallet-rpc/action.yml
vendored
@@ -5,14 +5,14 @@ inputs:
|
|||||||
version:
|
version:
|
||||||
description: "Version to download and run"
|
description: "Version to download and run"
|
||||||
required: false
|
required: false
|
||||||
default: v0.18.2.0
|
default: v0.18.4.3
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Monero Wallet RPC Cache
|
- name: Monero Wallet RPC Cache
|
||||||
id: cache-monero-wallet-rpc
|
id: cache-monero-wallet-rpc
|
||||||
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84
|
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809
|
||||||
with:
|
with:
|
||||||
path: monero-wallet-rpc
|
path: monero-wallet-rpc
|
||||||
key: monero-wallet-rpc-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
|
key: monero-wallet-rpc-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
|
||||||
@@ -41,4 +41,9 @@ runs:
|
|||||||
|
|
||||||
- name: Monero Wallet RPC
|
- name: Monero Wallet RPC
|
||||||
shell: bash
|
shell: bash
|
||||||
run: ./monero-wallet-rpc --disable-rpc-login --rpc-bind-port 6061 --allow-mismatched-daemon-version --wallet-dir ./ --detach
|
run: |
|
||||||
|
./monero-wallet-rpc --allow-mismatched-daemon-version \
|
||||||
|
--daemon-address 0.0.0.0:18081 --daemon-login serai:seraidex \
|
||||||
|
--disable-rpc-login --rpc-bind-port 18082 \
|
||||||
|
--wallet-dir ./ \
|
||||||
|
--detach
|
||||||
|
|||||||
12
.github/actions/monero/action.yml
vendored
12
.github/actions/monero/action.yml
vendored
@@ -5,16 +5,16 @@ inputs:
|
|||||||
version:
|
version:
|
||||||
description: "Version to download and run"
|
description: "Version to download and run"
|
||||||
required: false
|
required: false
|
||||||
default: v0.18.2.0
|
default: v0.18.4.3
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Monero Daemon Cache
|
- name: Monero Daemon Cache
|
||||||
id: cache-monerod
|
id: cache-monerod
|
||||||
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84
|
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809
|
||||||
with:
|
with:
|
||||||
path: monerod
|
path: /usr/bin/monerod
|
||||||
key: monerod-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
|
key: monerod-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}
|
||||||
|
|
||||||
- name: Download the Monero Daemon
|
- name: Download the Monero Daemon
|
||||||
@@ -37,8 +37,10 @@ runs:
|
|||||||
wget https://downloads.getmonero.org/cli/$FILE
|
wget https://downloads.getmonero.org/cli/$FILE
|
||||||
tar -xvf $FILE
|
tar -xvf $FILE
|
||||||
|
|
||||||
mv monero-x86_64-linux-gnu-${{ inputs.version }}/monerod monerod
|
sudo mv monero-x86_64-linux-gnu-${{ inputs.version }}/monerod /usr/bin/monerod
|
||||||
|
sudo chmod 777 /usr/bin/monerod
|
||||||
|
sudo chmod +x /usr/bin/monerod
|
||||||
|
|
||||||
- name: Monero Regtest Daemon
|
- name: Monero Regtest Daemon
|
||||||
shell: bash
|
shell: bash
|
||||||
run: ./monerod --regtest --offline --fixed-difficulty=1 --detach
|
run: PATH=$PATH:/usr/bin ./orchestration/dev/networks/monero/run.sh --detach
|
||||||
|
|||||||
15
.github/actions/test-dependencies/action.yml
vendored
15
.github/actions/test-dependencies/action.yml
vendored
@@ -2,33 +2,26 @@ name: test-dependencies
|
|||||||
description: Installs test dependencies for Serai
|
description: Installs test dependencies for Serai
|
||||||
|
|
||||||
inputs:
|
inputs:
|
||||||
github-token:
|
|
||||||
description: "GitHub token to install Protobuf with"
|
|
||||||
require: true
|
|
||||||
default:
|
|
||||||
|
|
||||||
monero-version:
|
monero-version:
|
||||||
description: "Monero version to download and run as a regtest node"
|
description: "Monero version to download and run as a regtest node"
|
||||||
required: false
|
required: false
|
||||||
default: v0.18.2.0
|
default: v0.18.4.3
|
||||||
|
|
||||||
bitcoin-version:
|
bitcoin-version:
|
||||||
description: "Bitcoin version to download and run as a regtest node"
|
description: "Bitcoin version to download and run as a regtest node"
|
||||||
required: false
|
required: false
|
||||||
default: 24.0.1
|
default: "30.0"
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Install Build Dependencies
|
- name: Install Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ inputs.github-token }}
|
|
||||||
|
|
||||||
- name: Install Foundry
|
- name: Install Foundry
|
||||||
uses: foundry-rs/foundry-toolchain@cb603ca0abb544f301eaed59ac0baf579aa6aecf
|
uses: foundry-rs/foundry-toolchain@8f1998e9878d786675189ef566a2e4bf24869773
|
||||||
with:
|
with:
|
||||||
version: nightly-09fe3e041369a816365a020f715ad6f94dbce9f2
|
version: nightly-f625d0fa7c51e65b4bf1e8f7931cd1c6e2e285e9
|
||||||
cache: false
|
cache: false
|
||||||
|
|
||||||
- name: Run a Monero Regtest Node
|
- name: Run a Monero Regtest Node
|
||||||
|
|||||||
2
.github/nightly-version
vendored
2
.github/nightly-version
vendored
@@ -1 +1 @@
|
|||||||
nightly-2023-12-04
|
nightly-2025-11-11
|
||||||
|
|||||||
7
.github/workflows/common-tests.yml
vendored
7
.github/workflows/common-tests.yml
vendored
@@ -21,13 +21,14 @@ jobs:
|
|||||||
|
|
||||||
- name: Build Dependencies
|
- name: Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
run: |
|
run: |
|
||||||
GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features \
|
GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features \
|
||||||
-p std-shims \
|
-p std-shims \
|
||||||
-p zalloc \
|
-p zalloc \
|
||||||
|
-p patchable-async-sleep \
|
||||||
-p serai-db \
|
-p serai-db \
|
||||||
-p serai-env
|
-p serai-env \
|
||||||
|
-p serai-task \
|
||||||
|
-p simple-request
|
||||||
|
|||||||
14
.github/workflows/coordinator-tests.yml
vendored
14
.github/workflows/coordinator-tests.yml
vendored
@@ -7,11 +7,10 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
- "message-queue/**"
|
- "message-queue/**"
|
||||||
- "orchestration/message-queue/**"
|
|
||||||
- "coordinator/**"
|
- "coordinator/**"
|
||||||
- "orchestration/coordinator/**"
|
- "orchestration/**"
|
||||||
- "tests/docker/**"
|
- "tests/docker/**"
|
||||||
- "tests/coordinator/**"
|
- "tests/coordinator/**"
|
||||||
|
|
||||||
@@ -19,11 +18,10 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
- "message-queue/**"
|
- "message-queue/**"
|
||||||
- "orchestration/message-queue/**"
|
|
||||||
- "coordinator/**"
|
- "coordinator/**"
|
||||||
- "orchestration/coordinator/**"
|
- "orchestration/**"
|
||||||
- "tests/docker/**"
|
- "tests/docker/**"
|
||||||
- "tests/coordinator/**"
|
- "tests/coordinator/**"
|
||||||
|
|
||||||
@@ -37,8 +35,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Install Build Dependencies
|
- name: Install Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ inputs.github-token }}
|
|
||||||
|
|
||||||
- name: Run coordinator Docker tests
|
- name: Run coordinator Docker tests
|
||||||
run: cd tests/coordinator && GITHUB_CI=true RUST_BACKTRACE=1 cargo test
|
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p serai-coordinator-tests
|
||||||
|
|||||||
12
.github/workflows/crypto-tests.yml
vendored
12
.github/workflows/crypto-tests.yml
vendored
@@ -23,8 +23,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Build Dependencies
|
- name: Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
run: |
|
run: |
|
||||||
@@ -34,9 +32,17 @@ jobs:
|
|||||||
-p dalek-ff-group \
|
-p dalek-ff-group \
|
||||||
-p minimal-ed448 \
|
-p minimal-ed448 \
|
||||||
-p ciphersuite \
|
-p ciphersuite \
|
||||||
|
-p ciphersuite-kp256 \
|
||||||
-p multiexp \
|
-p multiexp \
|
||||||
-p schnorr-signatures \
|
-p schnorr-signatures \
|
||||||
-p dleq \
|
-p prime-field \
|
||||||
|
-p short-weierstrass \
|
||||||
|
-p secq256k1 \
|
||||||
|
-p embedwards25519 \
|
||||||
-p dkg \
|
-p dkg \
|
||||||
|
-p dkg-recovery \
|
||||||
|
-p dkg-dealer \
|
||||||
|
-p dkg-musig \
|
||||||
|
-p dkg-evrf \
|
||||||
-p modular-frost \
|
-p modular-frost \
|
||||||
-p frost-schnorrkel
|
-p frost-schnorrkel
|
||||||
|
|||||||
6
.github/workflows/daily-deny.yml
vendored
6
.github/workflows/daily-deny.yml
vendored
@@ -12,13 +12,13 @@ jobs:
|
|||||||
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
||||||
|
|
||||||
- name: Advisory Cache
|
- name: Advisory Cache
|
||||||
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84
|
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809
|
||||||
with:
|
with:
|
||||||
path: ~/.cargo/advisory-db
|
path: ~/.cargo/advisory-db
|
||||||
key: rust-advisory-db
|
key: rust-advisory-db
|
||||||
|
|
||||||
- name: Install cargo deny
|
- name: Install cargo deny
|
||||||
run: cargo install --locked cargo-deny
|
run: cargo +1.91 install cargo-deny --version =0.18.5
|
||||||
|
|
||||||
- name: Run cargo deny
|
- name: Run cargo deny
|
||||||
run: cargo deny -L error --all-features check
|
run: cargo deny -L error --all-features check --hide-inclusion-graph
|
||||||
|
|||||||
4
.github/workflows/full-stack-tests.yml
vendored
4
.github/workflows/full-stack-tests.yml
vendored
@@ -17,8 +17,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Install Build Dependencies
|
- name: Install Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ inputs.github-token }}
|
|
||||||
|
|
||||||
- name: Run Full Stack Docker tests
|
- name: Run Full Stack Docker tests
|
||||||
run: cd tests/full-stack && GITHUB_CI=true RUST_BACKTRACE=1 cargo test
|
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p serai-full-stack-tests
|
||||||
|
|||||||
157
.github/workflows/lint.yml
vendored
157
.github/workflows/lint.yml
vendored
@@ -9,21 +9,24 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
clippy:
|
clippy:
|
||||||
runs-on: ubuntu-latest
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, macos-15-intel, macos-latest, windows-latest]
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
||||||
|
|
||||||
- name: Get nightly version to use
|
- name: Get nightly version to use
|
||||||
id: nightly
|
id: nightly
|
||||||
|
shell: bash
|
||||||
run: echo "version=$(cat .github/nightly-version)" >> $GITHUB_OUTPUT
|
run: echo "version=$(cat .github/nightly-version)" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Build Dependencies
|
- name: Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Install nightly rust
|
- name: Install nightly rust
|
||||||
run: rustup toolchain install ${{ steps.nightly.outputs.version }} --profile minimal -t wasm32-unknown-unknown -c clippy
|
run: rustup toolchain install ${{ steps.nightly.outputs.version }} --profile minimal -t wasm32v1-none -c clippy
|
||||||
|
|
||||||
- name: Run Clippy
|
- name: Run Clippy
|
||||||
run: cargo +${{ steps.nightly.outputs.version }} clippy --all-features --all-targets -- -D warnings -A clippy::items_after_test_module
|
run: cargo +${{ steps.nightly.outputs.version }} clippy --all-features --all-targets -- -D warnings -A clippy::items_after_test_module
|
||||||
@@ -34,7 +37,8 @@ jobs:
|
|||||||
# The above clippy run will cause it to be updated, so checking there's
|
# The above clippy run will cause it to be updated, so checking there's
|
||||||
# no differences present now performs the desired check
|
# no differences present now performs the desired check
|
||||||
- name: Verify lockfile
|
- name: Verify lockfile
|
||||||
run: git diff | wc -l | grep -x "0"
|
shell: bash
|
||||||
|
run: git diff | wc -l | LC_ALL="en_US.utf8" grep -x -e "^[ ]*0"
|
||||||
|
|
||||||
deny:
|
deny:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -42,16 +46,16 @@ jobs:
|
|||||||
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
||||||
|
|
||||||
- name: Advisory Cache
|
- name: Advisory Cache
|
||||||
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84
|
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809
|
||||||
with:
|
with:
|
||||||
path: ~/.cargo/advisory-db
|
path: ~/.cargo/advisory-db
|
||||||
key: rust-advisory-db
|
key: rust-advisory-db
|
||||||
|
|
||||||
- name: Install cargo deny
|
- name: Install cargo deny
|
||||||
run: cargo install --locked cargo-deny
|
run: cargo +1.91 install cargo-deny --version =0.18.5
|
||||||
|
|
||||||
- name: Run cargo deny
|
- name: Run cargo deny
|
||||||
run: cargo deny -L error --all-features check
|
run: cargo deny -L error --all-features check --hide-inclusion-graph
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -60,6 +64,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Get nightly version to use
|
- name: Get nightly version to use
|
||||||
id: nightly
|
id: nightly
|
||||||
|
shell: bash
|
||||||
run: echo "version=$(cat .github/nightly-version)" >> $GITHUB_OUTPUT
|
run: echo "version=$(cat .github/nightly-version)" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Install nightly rust
|
- name: Install nightly rust
|
||||||
@@ -68,13 +73,14 @@ jobs:
|
|||||||
- name: Run rustfmt
|
- name: Run rustfmt
|
||||||
run: cargo +${{ steps.nightly.outputs.version }} fmt -- --check
|
run: cargo +${{ steps.nightly.outputs.version }} fmt -- --check
|
||||||
|
|
||||||
dockerfiles:
|
- name: Install foundry
|
||||||
runs-on: ubuntu-latest
|
uses: foundry-rs/foundry-toolchain@8f1998e9878d786675189ef566a2e4bf24869773
|
||||||
steps:
|
with:
|
||||||
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
version: nightly-41d4e5437107f6f42c7711123890147bc736a609
|
||||||
- name: Verify Dockerfiles are up to date
|
cache: false
|
||||||
# Runs the file which generates them and checks the diff has no lines
|
|
||||||
run: cd orchestration && ./dockerfiles.sh && git diff | wc -l | grep -x "0"
|
- name: Run forge fmt
|
||||||
|
run: FOUNDRY_FMT_SORT_INPUTS=false FOUNDRY_FMT_LINE_LENGTH=100 FOUNDRY_FMT_TAB_WIDTH=2 FOUNDRY_FMT_BRACKET_SPACING=true FOUNDRY_FMT_INT_TYPES=preserve forge fmt --check $(find . -iname "*.sol")
|
||||||
|
|
||||||
machete:
|
machete:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -82,5 +88,122 @@ jobs:
|
|||||||
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
||||||
- name: Verify all dependencies are in use
|
- name: Verify all dependencies are in use
|
||||||
run: |
|
run: |
|
||||||
cargo install cargo-machete
|
cargo +1.91 install cargo-machete --version =0.9.1
|
||||||
cargo machete
|
cargo +1.91 machete
|
||||||
|
|
||||||
|
msrv:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
||||||
|
- name: Verify claimed `rust-version`
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
cargo +1.91 install cargo-msrv --version =0.18.4
|
||||||
|
|
||||||
|
function check_msrv {
|
||||||
|
# We `cd` into the directory passed as the first argument, but will return to the
|
||||||
|
# directory called from.
|
||||||
|
return_to=$(pwd)
|
||||||
|
echo "Checking $1"
|
||||||
|
cd $1
|
||||||
|
|
||||||
|
# We then find the existing `rust-version` using `grep` (for the right line) and then a
|
||||||
|
# regex (to strip to just the major and minor version).
|
||||||
|
existing=$(cat ./Cargo.toml | grep "rust-version" | grep -Eo "[0-9]+\.[0-9]+")
|
||||||
|
|
||||||
|
# We then backup the `Cargo.toml`, allowing us to restore it after, saving time on future
|
||||||
|
# MSRV checks (as they'll benefit from immediately exiting if the queried version is less
|
||||||
|
# than the declared MSRV).
|
||||||
|
mv ./Cargo.toml ./Cargo.toml.bak
|
||||||
|
|
||||||
|
# We then use an inverted (`-v`) grep to remove the existing `rust-version` from the
|
||||||
|
# `Cargo.toml`, as required because else earlier versions of Rust won't even attempt to
|
||||||
|
# compile this crate.
|
||||||
|
cat ./Cargo.toml.bak | grep -v "rust-version" > Cargo.toml
|
||||||
|
|
||||||
|
# We then find the actual `rust-version` using `cargo-msrv` (again stripping to just the
|
||||||
|
# major and minor version).
|
||||||
|
actual=$(cargo msrv find --output-format minimal | grep -Eo "^[0-9]+\.[0-9]+")
|
||||||
|
|
||||||
|
# Finally, we compare the two.
|
||||||
|
echo "Declared rust-version: $existing"
|
||||||
|
echo "Actual rust-version: $actual"
|
||||||
|
[ $existing == $actual ]
|
||||||
|
result=$?
|
||||||
|
|
||||||
|
# Restore the original `Cargo.toml`.
|
||||||
|
rm Cargo.toml
|
||||||
|
mv ./Cargo.toml.bak ./Cargo.toml
|
||||||
|
|
||||||
|
# Return to the directory called from and return the result.
|
||||||
|
cd $return_to
|
||||||
|
return $result
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check each member of the workspace
|
||||||
|
function check_workspace {
|
||||||
|
# Get the members array from the workspace's `Cargo.toml`
|
||||||
|
cargo_toml_lines=$(cat ./Cargo.toml | wc -l)
|
||||||
|
# Keep all lines after the start of the array, then keep all lines before the next "]"
|
||||||
|
members=$(cat Cargo.toml | grep "members\ \=\ \[" -m1 -A$cargo_toml_lines | grep "]" -m1 -B$cargo_toml_lines)
|
||||||
|
|
||||||
|
# Parse out any comments, whitespace, including comments post-fixed on the same line as an entry
|
||||||
|
# We accomplish the latter by pruning all characters after the entry's ","
|
||||||
|
members=$(echo "$members" | grep -Ev "^[[:space:]]*(#|$)" | awk -F',' '{print $1","}')
|
||||||
|
# Replace the first line, which was "members = [" and is now "members = [,", with "["
|
||||||
|
members=$(echo "$members" | sed "1s/.*/\[/")
|
||||||
|
# Correct the last line, which was malleated to "],"
|
||||||
|
members=$(echo "$members" | sed "$(echo "$members" | wc -l)s/\]\,/\]/")
|
||||||
|
|
||||||
|
# Don't check the following
|
||||||
|
# Most of these are binaries, with the exception of the Substrate runtime which has a
|
||||||
|
# bespoke build pipeline
|
||||||
|
members=$(echo "$members" | grep -v "networks/ethereum/relayer\"")
|
||||||
|
members=$(echo "$members" | grep -v "message-queue\"")
|
||||||
|
members=$(echo "$members" | grep -v "processor/bin\"")
|
||||||
|
members=$(echo "$members" | grep -v "processor/bitcoin\"")
|
||||||
|
members=$(echo "$members" | grep -v "processor/ethereum\"")
|
||||||
|
members=$(echo "$members" | grep -v "processor/monero\"")
|
||||||
|
members=$(echo "$members" | grep -v "coordinator\"")
|
||||||
|
members=$(echo "$members" | grep -v "substrate/runtime\"")
|
||||||
|
members=$(echo "$members" | grep -v "substrate/node\"")
|
||||||
|
members=$(echo "$members" | grep -v "orchestration\"")
|
||||||
|
|
||||||
|
# Don't check the tests
|
||||||
|
members=$(echo "$members" | grep -v "mini\"")
|
||||||
|
members=$(echo "$members" | grep -v "tests/")
|
||||||
|
|
||||||
|
# Remove the trailing comma by replacing the last line's "," with ""
|
||||||
|
members=$(echo "$members" | sed "$(($(echo "$members" | wc -l) - 1))s/\,//")
|
||||||
|
|
||||||
|
echo $members | jq -r ".[]" | while read -r member; do
|
||||||
|
check_msrv $member
|
||||||
|
correct=$?
|
||||||
|
if [ $correct -ne 0 ]; then
|
||||||
|
return $correct
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
check_workspace
|
||||||
|
|
||||||
|
slither:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
||||||
|
|
||||||
|
- name: Build Dependencies
|
||||||
|
uses: ./.github/actions/build-dependencies
|
||||||
|
|
||||||
|
- name: Slither
|
||||||
|
run: |
|
||||||
|
python3 -m pip install slither-analyzer
|
||||||
|
|
||||||
|
slither --include-paths ./networks/ethereum/schnorr/contracts/Schnorr.sol
|
||||||
|
slither --include-paths ./networks/ethereum/schnorr/contracts ./networks/ethereum/schnorr/contracts/tests/Schnorr.sol
|
||||||
|
slither processor/ethereum/deployer/contracts/Deployer.sol
|
||||||
|
slither processor/ethereum/erc20/contracts/IERC20.sol
|
||||||
|
|
||||||
|
cp networks/ethereum/schnorr/contracts/Schnorr.sol processor/ethereum/router/contracts/
|
||||||
|
cp processor/ethereum/erc20/contracts/IERC20.sol processor/ethereum/router/contracts/
|
||||||
|
cd processor/ethereum/router/contracts
|
||||||
|
slither Router.sol
|
||||||
|
|||||||
8
.github/workflows/message-queue-tests.yml
vendored
8
.github/workflows/message-queue-tests.yml
vendored
@@ -8,7 +8,7 @@ on:
|
|||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "message-queue/**"
|
- "message-queue/**"
|
||||||
- "orchestration/message-queue/**"
|
- "orchestration/**"
|
||||||
- "tests/docker/**"
|
- "tests/docker/**"
|
||||||
- "tests/message-queue/**"
|
- "tests/message-queue/**"
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ on:
|
|||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "message-queue/**"
|
- "message-queue/**"
|
||||||
- "orchestration/message-queue/**"
|
- "orchestration/**"
|
||||||
- "tests/docker/**"
|
- "tests/docker/**"
|
||||||
- "tests/message-queue/**"
|
- "tests/message-queue/**"
|
||||||
|
|
||||||
@@ -31,8 +31,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Install Build Dependencies
|
- name: Install Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ inputs.github-token }}
|
|
||||||
|
|
||||||
- name: Run message-queue Docker tests
|
- name: Run message-queue Docker tests
|
||||||
run: cd tests/message-queue && GITHUB_CI=true RUST_BACKTRACE=1 cargo test
|
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p serai-message-queue-tests
|
||||||
|
|||||||
2
.github/workflows/mini-tests.yml
vendored
2
.github/workflows/mini-tests.yml
vendored
@@ -21,8 +21,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Build Dependencies
|
- name: Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p mini-serai
|
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p mini-serai
|
||||||
|
|||||||
59
.github/workflows/monero-tests.yaml
vendored
59
.github/workflows/monero-tests.yaml
vendored
@@ -1,59 +0,0 @@
|
|||||||
name: Monero Tests
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- develop
|
|
||||||
paths:
|
|
||||||
- "coins/monero/**"
|
|
||||||
- "processor/**"
|
|
||||||
|
|
||||||
pull_request:
|
|
||||||
paths:
|
|
||||||
- "coins/monero/**"
|
|
||||||
- "processor/**"
|
|
||||||
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
# Only run these once since they will be consistent regardless of any node
|
|
||||||
unit-tests:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
|
||||||
|
|
||||||
- name: Test Dependencies
|
|
||||||
uses: ./.github/actions/test-dependencies
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Run Unit Tests Without Features
|
|
||||||
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --package monero-serai --lib
|
|
||||||
|
|
||||||
# Doesn't run unit tests with features as the tests workflow will
|
|
||||||
|
|
||||||
integration-tests:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
# Test against all supported protocol versions
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
version: [v0.17.3.2, v0.18.2.0]
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
|
||||||
|
|
||||||
- name: Test Dependencies
|
|
||||||
uses: ./.github/actions/test-dependencies
|
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
monero-version: ${{ matrix.version }}
|
|
||||||
|
|
||||||
- name: Run Integration Tests Without Features
|
|
||||||
# Runs with the binaries feature so the binaries build
|
|
||||||
# https://github.com/rust-lang/cargo/issues/8396
|
|
||||||
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --package monero-serai --features binaries --test '*'
|
|
||||||
|
|
||||||
- name: Run Integration Tests
|
|
||||||
# Don't run if the the tests workflow also will
|
|
||||||
if: ${{ matrix.version != 'v0.18.2.0' }}
|
|
||||||
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --package monero-serai --all-features --test '*'
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
name: coins/ Tests
|
name: networks/ Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -7,31 +7,30 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
|
|
||||||
pull_request:
|
pull_request:
|
||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test-coins:
|
test-networks:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
||||||
|
|
||||||
- name: Test Dependencies
|
- name: Test Dependencies
|
||||||
uses: ./.github/actions/test-dependencies
|
uses: ./.github/actions/test-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
run: |
|
run: |
|
||||||
GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features \
|
GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features \
|
||||||
-p bitcoin-serai \
|
-p bitcoin-serai \
|
||||||
-p ethereum-serai \
|
-p build-solidity-contracts \
|
||||||
-p monero-generators \
|
-p ethereum-schnorr-contract \
|
||||||
-p monero-serai
|
-p alloy-simple-request-transport \
|
||||||
|
-p serai-ethereum-relayer \
|
||||||
20
.github/workflows/no-std.yml
vendored
20
.github/workflows/no-std.yml
vendored
@@ -7,14 +7,14 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
- "tests/no-std/**"
|
- "tests/no-std/**"
|
||||||
|
|
||||||
pull_request:
|
pull_request:
|
||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
- "tests/no-std/**"
|
- "tests/no-std/**"
|
||||||
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
@@ -27,11 +27,19 @@ jobs:
|
|||||||
|
|
||||||
- name: Install Build Dependencies
|
- name: Install Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ inputs.github-token }}
|
- name: Get nightly version to use
|
||||||
|
id: nightly
|
||||||
|
shell: bash
|
||||||
|
run: echo "version=$(cat .github/nightly-version)" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Install RISC-V Toolchain
|
- name: Install RISC-V Toolchain
|
||||||
run: sudo apt update && sudo apt install -y gcc-riscv64-unknown-elf gcc-multilib && rustup target add riscv32imac-unknown-none-elf
|
run: |
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install -y gcc-riscv64-unknown-elf gcc-multilib
|
||||||
|
rustup toolchain install ${{ steps.nightly.outputs.version }} --profile minimal --component rust-src --target riscv32imac-unknown-none-elf
|
||||||
|
|
||||||
- name: Verify no-std builds
|
- name: Verify no-std builds
|
||||||
run: cd tests/no-std && CFLAGS=-I/usr/include cargo build --target riscv32imac-unknown-none-elf
|
run: |
|
||||||
|
CFLAGS=-I/usr/include cargo +${{ steps.nightly.outputs.version }} build --target riscv32imac-unknown-none-elf -Z build-std=core -p serai-no-std-tests
|
||||||
|
CFLAGS=-I/usr/include cargo +${{ steps.nightly.outputs.version }} build --target riscv32imac-unknown-none-elf -Z build-std=core,alloc -p serai-no-std-tests --features "alloc"
|
||||||
|
|||||||
91
.github/workflows/pages.yml
vendored
Normal file
91
.github/workflows/pages.yml
vendored
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
# MIT License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2022 just-the-docs
|
||||||
|
# Copyright (c) 2022-2024 Luke Parker
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
|
# copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
name: Deploy Rust docs and Jekyll site to Pages
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- "develop"
|
||||||
|
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
pages: write
|
||||||
|
id-token: write
|
||||||
|
|
||||||
|
# Only allow one concurrent deployment
|
||||||
|
concurrency:
|
||||||
|
group: "pages"
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
# Build job
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
|
||||||
|
- name: Setup Ruby
|
||||||
|
uses: ruby/setup-ruby@44511735964dcb71245e7e55f72539531f7bc0eb
|
||||||
|
with:
|
||||||
|
bundler-cache: true
|
||||||
|
cache-version: 0
|
||||||
|
working-directory: "${{ github.workspace }}/docs"
|
||||||
|
- name: Setup Pages
|
||||||
|
id: pages
|
||||||
|
uses: actions/configure-pages@983d7736d9b0ae728b81ab479565c72886d7745b
|
||||||
|
- name: Build with Jekyll
|
||||||
|
run: cd ${{ github.workspace }}/docs && bundle exec jekyll build --baseurl "${{ steps.pages.outputs.base_path }}"
|
||||||
|
env:
|
||||||
|
JEKYLL_ENV: production
|
||||||
|
|
||||||
|
- name: Get nightly version to use
|
||||||
|
id: nightly
|
||||||
|
shell: bash
|
||||||
|
run: echo "version=$(cat .github/nightly-version)" >> $GITHUB_OUTPUT
|
||||||
|
- name: Build Dependencies
|
||||||
|
uses: ./.github/actions/build-dependencies
|
||||||
|
- name: Buld Rust docs
|
||||||
|
run: |
|
||||||
|
rustup toolchain install ${{ steps.nightly.outputs.version }} --profile minimal -t wasm32v1-none -c rust-docs
|
||||||
|
RUSTDOCFLAGS="--cfg docsrs" cargo +${{ steps.nightly.outputs.version }} doc --workspace --no-deps --all-features
|
||||||
|
mv target/doc docs/_site/rust
|
||||||
|
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-pages-artifact@7b1f4a764d45c48632c6b24a0339c27f5614fb0b
|
||||||
|
with:
|
||||||
|
path: "docs/_site/"
|
||||||
|
|
||||||
|
# Deployment job
|
||||||
|
deploy:
|
||||||
|
environment:
|
||||||
|
name: github-pages
|
||||||
|
url: ${{ steps.deployment.outputs.page_url }}
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: build
|
||||||
|
steps:
|
||||||
|
- name: Deploy to GitHub Pages
|
||||||
|
id: deployment
|
||||||
|
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e
|
||||||
14
.github/workflows/processor-tests.yml
vendored
14
.github/workflows/processor-tests.yml
vendored
@@ -7,11 +7,10 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
- "message-queue/**"
|
- "message-queue/**"
|
||||||
- "orchestration/message-queue/**"
|
|
||||||
- "processor/**"
|
- "processor/**"
|
||||||
- "orchestration/processor/**"
|
- "orchestration/**"
|
||||||
- "tests/docker/**"
|
- "tests/docker/**"
|
||||||
- "tests/processor/**"
|
- "tests/processor/**"
|
||||||
|
|
||||||
@@ -19,11 +18,10 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
- "message-queue/**"
|
- "message-queue/**"
|
||||||
- "orchestration/message-queue/**"
|
|
||||||
- "processor/**"
|
- "processor/**"
|
||||||
- "orchestration/processor/**"
|
- "orchestration/**"
|
||||||
- "tests/docker/**"
|
- "tests/docker/**"
|
||||||
- "tests/processor/**"
|
- "tests/processor/**"
|
||||||
|
|
||||||
@@ -37,8 +35,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Install Build Dependencies
|
- name: Install Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ inputs.github-token }}
|
|
||||||
|
|
||||||
- name: Run processor Docker tests
|
- name: Run processor Docker tests
|
||||||
run: cd tests/processor && GITHUB_CI=true RUST_BACKTRACE=1 cargo test
|
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p serai-processor-tests
|
||||||
|
|||||||
4
.github/workflows/reproducible-runtime.yml
vendored
4
.github/workflows/reproducible-runtime.yml
vendored
@@ -31,8 +31,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Install Build Dependencies
|
- name: Install Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ inputs.github-token }}
|
|
||||||
|
|
||||||
- name: Run Reproducible Runtime tests
|
- name: Run Reproducible Runtime tests
|
||||||
run: cd tests/reproducible-runtime && GITHUB_CI=true RUST_BACKTRACE=1 cargo test
|
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p serai-reproducible-runtime-tests
|
||||||
|
|||||||
46
.github/workflows/tests.yml
vendored
46
.github/workflows/tests.yml
vendored
@@ -7,7 +7,7 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
- "message-queue/**"
|
- "message-queue/**"
|
||||||
- "processor/**"
|
- "processor/**"
|
||||||
- "coordinator/**"
|
- "coordinator/**"
|
||||||
@@ -17,7 +17,7 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "common/**"
|
- "common/**"
|
||||||
- "crypto/**"
|
- "crypto/**"
|
||||||
- "coins/**"
|
- "networks/**"
|
||||||
- "message-queue/**"
|
- "message-queue/**"
|
||||||
- "processor/**"
|
- "processor/**"
|
||||||
- "coordinator/**"
|
- "coordinator/**"
|
||||||
@@ -33,18 +33,41 @@ jobs:
|
|||||||
|
|
||||||
- name: Build Dependencies
|
- name: Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
run: |
|
run: |
|
||||||
GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features \
|
GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features \
|
||||||
-p serai-message-queue \
|
-p serai-message-queue \
|
||||||
-p serai-processor-messages \
|
-p serai-processor-messages \
|
||||||
-p serai-processor \
|
-p serai-processor-key-gen \
|
||||||
|
-p serai-processor-view-keys \
|
||||||
|
-p serai-processor-frost-attempt-manager \
|
||||||
|
-p serai-processor-primitives \
|
||||||
|
-p serai-processor-scanner \
|
||||||
|
-p serai-processor-scheduler-primitives \
|
||||||
|
-p serai-processor-utxo-scheduler-primitives \
|
||||||
|
-p serai-processor-utxo-scheduler \
|
||||||
|
-p serai-processor-transaction-chaining-scheduler \
|
||||||
|
-p serai-processor-smart-contract-scheduler \
|
||||||
|
-p serai-processor-signers \
|
||||||
|
-p serai-processor-bin \
|
||||||
|
-p serai-bitcoin-processor \
|
||||||
|
-p serai-processor-ethereum-primitives \
|
||||||
|
-p serai-processor-ethereum-test-primitives \
|
||||||
|
-p serai-processor-ethereum-deployer \
|
||||||
|
-p serai-processor-ethereum-router \
|
||||||
|
-p serai-processor-ethereum-erc20 \
|
||||||
|
-p serai-ethereum-processor \
|
||||||
|
-p serai-monero-processor \
|
||||||
-p tendermint-machine \
|
-p tendermint-machine \
|
||||||
-p tributary-chain \
|
-p tributary-sdk \
|
||||||
|
-p serai-cosign \
|
||||||
|
-p serai-coordinator-substrate \
|
||||||
|
-p serai-coordinator-tributary \
|
||||||
|
-p serai-coordinator-p2p \
|
||||||
|
-p serai-coordinator-libp2p-p2p \
|
||||||
-p serai-coordinator \
|
-p serai-coordinator \
|
||||||
|
-p serai-orchestrator \
|
||||||
-p serai-docker-tests
|
-p serai-docker-tests
|
||||||
|
|
||||||
test-substrate:
|
test-substrate:
|
||||||
@@ -54,8 +77,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Build Dependencies
|
- name: Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
run: |
|
run: |
|
||||||
@@ -66,9 +87,16 @@ jobs:
|
|||||||
-p serai-dex-pallet \
|
-p serai-dex-pallet \
|
||||||
-p serai-validator-sets-primitives \
|
-p serai-validator-sets-primitives \
|
||||||
-p serai-validator-sets-pallet \
|
-p serai-validator-sets-pallet \
|
||||||
|
-p serai-genesis-liquidity-primitives \
|
||||||
|
-p serai-genesis-liquidity-pallet \
|
||||||
|
-p serai-emissions-primitives \
|
||||||
|
-p serai-emissions-pallet \
|
||||||
|
-p serai-economic-security-pallet \
|
||||||
-p serai-in-instructions-primitives \
|
-p serai-in-instructions-primitives \
|
||||||
-p serai-in-instructions-pallet \
|
-p serai-in-instructions-pallet \
|
||||||
|
-p serai-signals-primitives \
|
||||||
-p serai-signals-pallet \
|
-p serai-signals-pallet \
|
||||||
|
-p serai-abi \
|
||||||
-p serai-runtime \
|
-p serai-runtime \
|
||||||
-p serai-node
|
-p serai-node
|
||||||
|
|
||||||
@@ -79,8 +107,6 @@ jobs:
|
|||||||
|
|
||||||
- name: Build Dependencies
|
- name: Build Dependencies
|
||||||
uses: ./.github/actions/build-dependencies
|
uses: ./.github/actions/build-dependencies
|
||||||
with:
|
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p serai-client
|
run: GITHUB_CI=true RUST_BACKTRACE=1 cargo test --all-features -p serai-client
|
||||||
|
|||||||
13
.gitignore
vendored
13
.gitignore
vendored
@@ -1,3 +1,14 @@
|
|||||||
target
|
target
|
||||||
.vscode
|
|
||||||
|
# Don't commit any `Cargo.lock` which aren't the workspace's
|
||||||
|
Cargo.lock
|
||||||
|
!./Cargo.lock
|
||||||
|
|
||||||
|
# Don't commit any `Dockerfile`, as they're auto-generated, except the only one which isn't
|
||||||
|
Dockerfile
|
||||||
|
Dockerfile.fast-epoch
|
||||||
|
!orchestration/runtime/Dockerfile
|
||||||
|
|
||||||
.test-logs
|
.test-logs
|
||||||
|
|
||||||
|
.vscode
|
||||||
|
|||||||
9140
Cargo.lock
generated
9140
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
171
Cargo.toml
171
Cargo.toml
@@ -3,8 +3,10 @@ resolver = "2"
|
|||||||
members = [
|
members = [
|
||||||
"common/std-shims",
|
"common/std-shims",
|
||||||
"common/zalloc",
|
"common/zalloc",
|
||||||
|
"common/patchable-async-sleep",
|
||||||
"common/db",
|
"common/db",
|
||||||
"common/env",
|
"common/env",
|
||||||
|
"common/task",
|
||||||
"common/request",
|
"common/request",
|
||||||
|
|
||||||
"crypto/transcript",
|
"crypto/transcript",
|
||||||
@@ -13,27 +15,65 @@ members = [
|
|||||||
"crypto/dalek-ff-group",
|
"crypto/dalek-ff-group",
|
||||||
"crypto/ed448",
|
"crypto/ed448",
|
||||||
"crypto/ciphersuite",
|
"crypto/ciphersuite",
|
||||||
|
"crypto/ciphersuite/kp256",
|
||||||
|
|
||||||
"crypto/multiexp",
|
"crypto/multiexp",
|
||||||
|
|
||||||
"crypto/schnorr",
|
"crypto/schnorr",
|
||||||
"crypto/dleq",
|
|
||||||
|
"crypto/prime-field",
|
||||||
|
"crypto/short-weierstrass",
|
||||||
|
"crypto/secq256k1",
|
||||||
|
"crypto/embedwards25519",
|
||||||
|
|
||||||
"crypto/dkg",
|
"crypto/dkg",
|
||||||
|
"crypto/dkg/recovery",
|
||||||
|
"crypto/dkg/dealer",
|
||||||
|
"crypto/dkg/musig",
|
||||||
|
"crypto/dkg/evrf",
|
||||||
"crypto/frost",
|
"crypto/frost",
|
||||||
"crypto/schnorrkel",
|
"crypto/schnorrkel",
|
||||||
|
|
||||||
"coins/bitcoin",
|
"networks/bitcoin",
|
||||||
"coins/ethereum",
|
|
||||||
"coins/monero/generators",
|
"networks/ethereum/build-contracts",
|
||||||
"coins/monero",
|
"networks/ethereum/schnorr",
|
||||||
|
"networks/ethereum/alloy-simple-request-transport",
|
||||||
|
"networks/ethereum/relayer",
|
||||||
|
|
||||||
"message-queue",
|
"message-queue",
|
||||||
|
|
||||||
"processor/messages",
|
"processor/messages",
|
||||||
"processor",
|
|
||||||
|
|
||||||
"coordinator/tributary/tendermint",
|
"processor/key-gen",
|
||||||
|
"processor/view-keys",
|
||||||
|
"processor/frost-attempt-manager",
|
||||||
|
|
||||||
|
"processor/primitives",
|
||||||
|
"processor/scanner",
|
||||||
|
"processor/scheduler/primitives",
|
||||||
|
"processor/scheduler/utxo/primitives",
|
||||||
|
"processor/scheduler/utxo/standard",
|
||||||
|
"processor/scheduler/utxo/transaction-chaining",
|
||||||
|
"processor/scheduler/smart-contract",
|
||||||
|
"processor/signers",
|
||||||
|
|
||||||
|
"processor/bin",
|
||||||
|
"processor/bitcoin",
|
||||||
|
"processor/ethereum/primitives",
|
||||||
|
"processor/ethereum/test-primitives",
|
||||||
|
"processor/ethereum/deployer",
|
||||||
|
"processor/ethereum/router",
|
||||||
|
"processor/ethereum/erc20",
|
||||||
|
"processor/ethereum",
|
||||||
|
"processor/monero",
|
||||||
|
|
||||||
|
"coordinator/tributary-sdk/tendermint",
|
||||||
|
"coordinator/tributary-sdk",
|
||||||
|
"coordinator/cosign",
|
||||||
|
"coordinator/substrate",
|
||||||
"coordinator/tributary",
|
"coordinator/tributary",
|
||||||
|
"coordinator/p2p",
|
||||||
|
"coordinator/p2p/libp2p",
|
||||||
"coordinator",
|
"coordinator",
|
||||||
|
|
||||||
"substrate/primitives",
|
"substrate/primitives",
|
||||||
@@ -41,12 +81,22 @@ members = [
|
|||||||
"substrate/coins/primitives",
|
"substrate/coins/primitives",
|
||||||
"substrate/coins/pallet",
|
"substrate/coins/pallet",
|
||||||
|
|
||||||
"substrate/in-instructions/primitives",
|
"substrate/dex/pallet",
|
||||||
"substrate/in-instructions/pallet",
|
|
||||||
|
|
||||||
"substrate/validator-sets/primitives",
|
"substrate/validator-sets/primitives",
|
||||||
"substrate/validator-sets/pallet",
|
"substrate/validator-sets/pallet",
|
||||||
|
|
||||||
|
"substrate/genesis-liquidity/primitives",
|
||||||
|
"substrate/genesis-liquidity/pallet",
|
||||||
|
|
||||||
|
"substrate/emissions/primitives",
|
||||||
|
"substrate/emissions/pallet",
|
||||||
|
|
||||||
|
"substrate/economic-security/pallet",
|
||||||
|
|
||||||
|
"substrate/in-instructions/primitives",
|
||||||
|
"substrate/in-instructions/pallet",
|
||||||
|
|
||||||
"substrate/signals/primitives",
|
"substrate/signals/primitives",
|
||||||
"substrate/signals/pallet",
|
"substrate/signals/pallet",
|
||||||
|
|
||||||
@@ -57,48 +107,121 @@ members = [
|
|||||||
|
|
||||||
"substrate/client",
|
"substrate/client",
|
||||||
|
|
||||||
|
"orchestration",
|
||||||
|
|
||||||
"mini",
|
"mini",
|
||||||
|
|
||||||
"tests/no-std",
|
"tests/no-std",
|
||||||
|
|
||||||
"tests/docker",
|
"tests/docker",
|
||||||
"tests/message-queue",
|
"tests/message-queue",
|
||||||
"tests/processor",
|
# TODO "tests/processor",
|
||||||
"tests/coordinator",
|
# TODO "tests/coordinator",
|
||||||
"tests/full-stack",
|
# TODO "tests/full-stack",
|
||||||
"tests/reproducible-runtime",
|
"tests/reproducible-runtime",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[profile.dev.package]
|
||||||
# Always compile Monero (and a variety of dependencies) with optimizations due
|
# Always compile Monero (and a variety of dependencies) with optimizations due
|
||||||
# to the extensive operations required for Bulletproofs
|
# to the extensive operations required for Bulletproofs
|
||||||
[profile.dev.package]
|
|
||||||
subtle = { opt-level = 3 }
|
subtle = { opt-level = 3 }
|
||||||
curve25519-dalek = { opt-level = 3 }
|
|
||||||
|
sha3 = { opt-level = 3 }
|
||||||
|
blake2 = { opt-level = 3 }
|
||||||
|
|
||||||
ff = { opt-level = 3 }
|
ff = { opt-level = 3 }
|
||||||
group = { opt-level = 3 }
|
group = { opt-level = 3 }
|
||||||
|
|
||||||
crypto-bigint = { opt-level = 3 }
|
crypto-bigint = { opt-level = 3 }
|
||||||
|
curve25519-dalek = { opt-level = 3 }
|
||||||
dalek-ff-group = { opt-level = 3 }
|
dalek-ff-group = { opt-level = 3 }
|
||||||
minimal-ed448 = { opt-level = 3 }
|
|
||||||
|
|
||||||
multiexp = { opt-level = 3 }
|
multiexp = { opt-level = 3 }
|
||||||
|
|
||||||
monero-serai = { opt-level = 3 }
|
monero-generators = { opt-level = 3 }
|
||||||
|
monero-borromean = { opt-level = 3 }
|
||||||
|
monero-bulletproofs = { opt-level = 3 }
|
||||||
|
monero-mlsag = { opt-level = 3 }
|
||||||
|
monero-clsag = { opt-level = 3 }
|
||||||
|
monero-oxide = { opt-level = 3 }
|
||||||
|
|
||||||
|
# Always compile the eVRF DKG tree with optimizations as well
|
||||||
|
secp256k1 = { opt-level = 3 }
|
||||||
|
secq256k1 = { opt-level = 3 }
|
||||||
|
embedwards25519 = { opt-level = 3 }
|
||||||
|
generalized-bulletproofs = { opt-level = 3 }
|
||||||
|
generalized-bulletproofs-circuit-abstraction = { opt-level = 3 }
|
||||||
|
generalized-bulletproofs-ec-gadgets = { opt-level = 3 }
|
||||||
|
|
||||||
|
# revm also effectively requires being built with optimizations
|
||||||
|
revm = { opt-level = 3 }
|
||||||
|
revm-bytecode = { opt-level = 3 }
|
||||||
|
revm-context = { opt-level = 3 }
|
||||||
|
revm-context-interface = { opt-level = 3 }
|
||||||
|
revm-database = { opt-level = 3 }
|
||||||
|
revm-database-interface = { opt-level = 3 }
|
||||||
|
revm-handler = { opt-level = 3 }
|
||||||
|
revm-inspector = { opt-level = 3 }
|
||||||
|
revm-interpreter = { opt-level = 3 }
|
||||||
|
revm-precompile = { opt-level = 3 }
|
||||||
|
revm-primitives = { opt-level = 3 }
|
||||||
|
revm-state = { opt-level = 3 }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
panic = "unwind"
|
panic = "unwind"
|
||||||
|
overflow-checks = true
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
|
# Point to empty crates for unused crates in our tree
|
||||||
|
ark-ff-3 = { package = "ark-ff", path = "patches/ethereum/ark-ff-0.3" }
|
||||||
|
ark-ff-4 = { package = "ark-ff", path = "patches/ethereum/ark-ff-0.4" }
|
||||||
|
c-kzg = { path = "patches/ethereum/c-kzg" }
|
||||||
|
secp256k1-30 = { package = "secp256k1", path = "patches/ethereum/secp256k1-30" }
|
||||||
|
|
||||||
|
# Dependencies from monero-oxide which originate from within our own tree
|
||||||
|
std-shims = { path = "patches/std-shims" }
|
||||||
|
simple-request = { path = "patches/simple-request" }
|
||||||
|
multiexp = { path = "crypto/multiexp" }
|
||||||
|
flexible-transcript = { path = "crypto/transcript" }
|
||||||
|
ciphersuite = { path = "patches/ciphersuite" }
|
||||||
|
dalek-ff-group = { path = "patches/dalek-ff-group" }
|
||||||
|
minimal-ed448 = { path = "crypto/ed448" }
|
||||||
|
modular-frost = { path = "crypto/frost" }
|
||||||
|
|
||||||
|
# This has a non-deprecated `std` alternative since Rust's 2024 edition
|
||||||
|
home = { path = "patches/home" }
|
||||||
|
|
||||||
|
# Updates to the latest version
|
||||||
|
darling = { path = "patches/darling" }
|
||||||
|
thiserror = { path = "patches/thiserror" }
|
||||||
|
|
||||||
# https://github.com/rust-lang-nursery/lazy-static.rs/issues/201
|
# https://github.com/rust-lang-nursery/lazy-static.rs/issues/201
|
||||||
lazy_static = { git = "https://github.com/rust-lang-nursery/lazy-static.rs", rev = "5735630d46572f1e5377c8f2ba0f79d18f53b10c" }
|
lazy_static = { git = "https://github.com/rust-lang-nursery/lazy-static.rs", rev = "5735630d46572f1e5377c8f2ba0f79d18f53b10c" }
|
||||||
|
|
||||||
# subxt *can* pull these off crates.io yet there's no benefit to this
|
# directories-next was created because directories was unmaintained
|
||||||
sp-core-hashing = { git = "https://github.com/serai-dex/substrate" }
|
# directories-next is now unmaintained while directories is maintained
|
||||||
sp-std = { git = "https://github.com/serai-dex/substrate" }
|
# The directories author pulls in ridiculously pointless crates and prefers
|
||||||
|
# copyleft licenses
|
||||||
|
# The following two patches resolve everything
|
||||||
|
option-ext = { path = "patches/option-ext" }
|
||||||
|
directories-next = { path = "patches/directories-next" }
|
||||||
|
|
||||||
|
# Patch from a fork back to upstream
|
||||||
|
parity-bip39 = { path = "patches/parity-bip39" }
|
||||||
|
|
||||||
|
# Patch to include `FromUniformBytes<64>` over `Scalar`
|
||||||
|
k256 = { git = "https://github.com/kayabaNerve/elliptic-curves", rev = "4994c9ab163781a88cd4a49beae812a89a44e8c3" }
|
||||||
|
p256 = { git = "https://github.com/kayabaNerve/elliptic-curves", rev = "4994c9ab163781a88cd4a49beae812a89a44e8c3" }
|
||||||
|
|
||||||
|
# `jemalloc` conflicts with `mimalloc`, so patch to a `rocksdb` which never uses `jemalloc`
|
||||||
|
librocksdb-sys = { path = "patches/librocksdb-sys" }
|
||||||
|
|
||||||
[workspace.lints.clippy]
|
[workspace.lints.clippy]
|
||||||
unwrap_or_default = "allow"
|
unwrap_or_default = "allow"
|
||||||
|
map_unwrap_or = "allow"
|
||||||
|
needless_continue = "allow"
|
||||||
|
manual_is_multiple_of = "allow"
|
||||||
|
incompatible_msrv = "allow" # Manually verified with a GitHub workflow
|
||||||
borrow_as_ptr = "deny"
|
borrow_as_ptr = "deny"
|
||||||
cast_lossless = "deny"
|
cast_lossless = "deny"
|
||||||
cast_possible_truncation = "deny"
|
cast_possible_truncation = "deny"
|
||||||
@@ -126,11 +249,9 @@ manual_instant_elapsed = "deny"
|
|||||||
manual_let_else = "deny"
|
manual_let_else = "deny"
|
||||||
manual_ok_or = "deny"
|
manual_ok_or = "deny"
|
||||||
manual_string_new = "deny"
|
manual_string_new = "deny"
|
||||||
map_unwrap_or = "deny"
|
|
||||||
match_bool = "deny"
|
match_bool = "deny"
|
||||||
match_same_arms = "deny"
|
match_same_arms = "deny"
|
||||||
missing_fields_in_debug = "deny"
|
missing_fields_in_debug = "deny"
|
||||||
needless_continue = "deny"
|
|
||||||
needless_pass_by_value = "deny"
|
needless_pass_by_value = "deny"
|
||||||
ptr_cast_constness = "deny"
|
ptr_cast_constness = "deny"
|
||||||
range_minus_one = "deny"
|
range_minus_one = "deny"
|
||||||
@@ -138,7 +259,8 @@ range_plus_one = "deny"
|
|||||||
redundant_closure_for_method_calls = "deny"
|
redundant_closure_for_method_calls = "deny"
|
||||||
redundant_else = "deny"
|
redundant_else = "deny"
|
||||||
string_add_assign = "deny"
|
string_add_assign = "deny"
|
||||||
unchecked_duration_subtraction = "deny"
|
string_slice = "deny"
|
||||||
|
unchecked_time_subtraction = "deny"
|
||||||
uninlined_format_args = "deny"
|
uninlined_format_args = "deny"
|
||||||
unnecessary_box_returns = "deny"
|
unnecessary_box_returns = "deny"
|
||||||
unnecessary_join = "deny"
|
unnecessary_join = "deny"
|
||||||
@@ -147,3 +269,6 @@ unnested_or_patterns = "deny"
|
|||||||
unused_async = "deny"
|
unused_async = "deny"
|
||||||
unused_self = "deny"
|
unused_self = "deny"
|
||||||
zero_sized_map_values = "deny"
|
zero_sized_map_values = "deny"
|
||||||
|
|
||||||
|
[workspace.lints.rust]
|
||||||
|
unused = "allow" # TODO: https://github.com/rust-lang/rust/issues/147648
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -5,4 +5,4 @@ a full copy of the AGPL-3.0 License is included in the root of this repository
|
|||||||
as a reference text. This copy should be provided with any distribution of a
|
as a reference text. This copy should be provided with any distribution of a
|
||||||
crate licensed under the AGPL-3.0, as per its terms.
|
crate licensed under the AGPL-3.0, as per its terms.
|
||||||
|
|
||||||
The GitHub actions (`.github/actions`) are licensed under the MIT license.
|
The GitHub actions/workflows (`.github`) are licensed under the MIT license.
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -5,13 +5,16 @@ Bitcoin, Ethereum, DAI, and Monero, offering a liquidity-pool-based trading
|
|||||||
experience. Funds are stored in an economically secured threshold-multisig
|
experience. Funds are stored in an economically secured threshold-multisig
|
||||||
wallet.
|
wallet.
|
||||||
|
|
||||||
[Getting Started](docs/Getting%20Started.md)
|
[Getting Started](spec/Getting%20Started.md)
|
||||||
|
|
||||||
### Layout
|
### Layout
|
||||||
|
|
||||||
- `audits`: Audits for various parts of Serai.
|
- `audits`: Audits for various parts of Serai.
|
||||||
|
|
||||||
- `docs`: Documentation on the Serai protocol.
|
- `spec`: The specification of the Serai protocol, both internally and as
|
||||||
|
networked.
|
||||||
|
|
||||||
|
- `docs`: User-facing documentation on the Serai protocol.
|
||||||
|
|
||||||
- `common`: Crates containing utilities common to a variety of areas under
|
- `common`: Crates containing utilities common to a variety of areas under
|
||||||
Serai, none neatly fitting under another category.
|
Serai, none neatly fitting under another category.
|
||||||
@@ -21,7 +24,7 @@ wallet.
|
|||||||
infrastructure, to our IETF-compliant FROST implementation, to a DLEq proof as
|
infrastructure, to our IETF-compliant FROST implementation, to a DLEq proof as
|
||||||
needed for Bitcoin-Monero atomic swaps.
|
needed for Bitcoin-Monero atomic swaps.
|
||||||
|
|
||||||
- `coins`: Various coin libraries intended for usage in Serai yet also by the
|
- `networks`: Various libraries intended for usage in Serai yet also by the
|
||||||
wider community. This means they will always support the functionality Serai
|
wider community. This means they will always support the functionality Serai
|
||||||
needs, yet won't disadvantage other use cases when possible.
|
needs, yet won't disadvantage other use cases when possible.
|
||||||
|
|
||||||
@@ -56,7 +59,6 @@ issued at the discretion of the Immunefi program managers.
|
|||||||
- [Website](https://serai.exchange/): https://serai.exchange/
|
- [Website](https://serai.exchange/): https://serai.exchange/
|
||||||
- [Immunefi](https://immunefi.com/bounty/serai/): https://immunefi.com/bounty/serai/
|
- [Immunefi](https://immunefi.com/bounty/serai/): https://immunefi.com/bounty/serai/
|
||||||
- [Twitter](https://twitter.com/SeraiDEX): https://twitter.com/SeraiDEX
|
- [Twitter](https://twitter.com/SeraiDEX): https://twitter.com/SeraiDEX
|
||||||
- [Mastodon](https://cryptodon.lol/@serai): https://cryptodon.lol/@serai
|
|
||||||
- [Discord](https://discord.gg/mpEUtJR3vz): https://discord.gg/mpEUtJR3vz
|
- [Discord](https://discord.gg/mpEUtJR3vz): https://discord.gg/mpEUtJR3vz
|
||||||
- [Matrix](https://matrix.to/#/#serai:matrix.org): https://matrix.to/#/#serai:matrix.org
|
- [Matrix](https://matrix.to/#/#serai:matrix.org): https://matrix.to/#/#serai:matrix.org
|
||||||
- [Reddit](https://www.reddit.com/r/SeraiDEX/): https://www.reddit.com/r/SeraiDEX/
|
- [Reddit](https://www.reddit.com/r/SeraiDEX/): https://www.reddit.com/r/SeraiDEX/
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
# Cypher Stack /coins/bitcoin Audit, August 2023
|
|
||||||
|
|
||||||
This audit was over the /coins/bitcoin folder. It is encompassing up to commit
|
|
||||||
5121ca75199dff7bd34230880a1fdd793012068c.
|
|
||||||
|
|
||||||
Please see https://github.com/cypherstack/serai-btc-audit for provenance.
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# Cypher Stack /networks/bitcoin Audit, August 2023
|
||||||
|
|
||||||
|
This audit was over the `/networks/bitcoin` folder (at the time located at
|
||||||
|
`/coins/bitcoin`). It is encompassing up to commit
|
||||||
|
5121ca75199dff7bd34230880a1fdd793012068c.
|
||||||
|
|
||||||
|
Please see https://github.com/cypherstack/serai-btc-audit for provenance.
|
||||||
14
audits/Trail of Bits ethereum contracts April 2025/README.md
Normal file
14
audits/Trail of Bits ethereum contracts April 2025/README.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# Trail of Bits Ethereum Contracts Audit, June 2025
|
||||||
|
|
||||||
|
This audit included:
|
||||||
|
- Our Schnorr contract and associated library (/networks/ethereum/schnorr)
|
||||||
|
- Our Ethereum primitives library (/processor/ethereum/primitives)
|
||||||
|
- Our Deployer contract and associated library (/processor/ethereum/deployer)
|
||||||
|
- Our ERC20 library (/processor/ethereum/erc20)
|
||||||
|
- Our Router contract and associated library (/processor/ethereum/router)
|
||||||
|
|
||||||
|
It is encompassing up to commit 4e0c58464fc4673623938335f06e2e9ea96ca8dd.
|
||||||
|
|
||||||
|
Please see
|
||||||
|
https://github.com/trailofbits/publications/blob/30c4fa3ebf39ff8e4d23ba9567344ec9691697b5/reviews/2025-04-serai-dex-security-review.pdf
|
||||||
|
for the actual report.
|
||||||
50
audits/crypto/dkg/evrf/README.md
Normal file
50
audits/crypto/dkg/evrf/README.md
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
# eVRF DKG
|
||||||
|
|
||||||
|
In 2024, the [eVRF paper](https://eprint.iacr.org/2024/397) was published to
|
||||||
|
the IACR preprint server. Within it was a one-round unbiased DKG and a
|
||||||
|
one-round unbiased threshold DKG. Unfortunately, both simply describe
|
||||||
|
communication of the secret shares as 'Alice sends $s_b$ to Bob'. This causes,
|
||||||
|
in practice, the need for an additional round of communication to occur where
|
||||||
|
all participants confirm they received their secret shares.
|
||||||
|
|
||||||
|
Within Serai, it was posited to use the same premises as the DDH eVRF itself to
|
||||||
|
achieve a verifiable encryption scheme. This allows the secret shares to be
|
||||||
|
posted to any 'bulletin board' (such as a blockchain) and for all observers to
|
||||||
|
confirm:
|
||||||
|
|
||||||
|
- A participant participated
|
||||||
|
- The secret shares sent can be received by the intended recipient so long as
|
||||||
|
they can access the bulletin board
|
||||||
|
|
||||||
|
Additionally, Serai desired a robust scheme (albeit with an biased key as the
|
||||||
|
output, which is fine for our purposes). Accordingly, our implementation
|
||||||
|
instantiates the threshold eVRF DKG from the eVRF paper, with our own proposal
|
||||||
|
for verifiable encryption, with the caller allowed to decide the set of
|
||||||
|
participants. They may:
|
||||||
|
|
||||||
|
- Select everyone, collapsing to the non-threshold unbiased DKG from the eVRF
|
||||||
|
paper
|
||||||
|
- Select a pre-determined set, collapsing to the threshold unbaised DKG from
|
||||||
|
the eVRF paper
|
||||||
|
- Select a post-determined set (with any solution for the Common Subset
|
||||||
|
problem), allowing achieving a robust threshold biased DKG
|
||||||
|
|
||||||
|
Note that the eVRF paper proposes using the eVRF to sample coefficients yet
|
||||||
|
this is unnecessary when the resulting key will be biased. Any proof of
|
||||||
|
knowledge for the coefficients, as necessary for their extraction within the
|
||||||
|
security proofs, would be sufficient.
|
||||||
|
|
||||||
|
MAGIC Grants contracted HashCloak to formalize Serai's proposal for a DKG and
|
||||||
|
provide proofs for its security. This resulted in
|
||||||
|
[this paper](<./Security Proofs.pdf>).
|
||||||
|
|
||||||
|
Our implementation itself is then built on top of the audited
|
||||||
|
[`generalized-bulletproofs`](https://github.com/kayabaNerve/monero-oxide/tree/generalized-bulletproofs/audits/crypto/generalized-bulletproofs)
|
||||||
|
and
|
||||||
|
[`generalized-bulletproofs-ec-gadgets`](https://github.com/monero-oxide/monero-oxide/tree/fcmp%2B%2B/audits/fcmps).
|
||||||
|
|
||||||
|
Note we do not use the originally premised DDH eVRF yet the one premised on
|
||||||
|
elliptic curve divisors, the methodology of which is commented on
|
||||||
|
[here](https://github.com/monero-oxide/monero-oxide/tree/fcmp%2B%2B/audits/divisors).
|
||||||
|
|
||||||
|
Our implementation itself is unaudited at this time however.
|
||||||
BIN
audits/crypto/dkg/evrf/Security Proofs.pdf
Normal file
BIN
audits/crypto/dkg/evrf/Security Proofs.pdf
Normal file
Binary file not shown.
@@ -1,166 +0,0 @@
|
|||||||
use k256::{
|
|
||||||
elliptic_curve::sec1::{Tag, ToEncodedPoint},
|
|
||||||
ProjectivePoint,
|
|
||||||
};
|
|
||||||
|
|
||||||
use bitcoin::key::XOnlyPublicKey;
|
|
||||||
|
|
||||||
/// Get the x coordinate of a non-infinity, even point. Panics on invalid input.
|
|
||||||
pub fn x(key: &ProjectivePoint) -> [u8; 32] {
|
|
||||||
let encoded = key.to_encoded_point(true);
|
|
||||||
assert_eq!(encoded.tag(), Tag::CompressedEvenY, "x coordinate of odd key");
|
|
||||||
(*encoded.x().expect("point at infinity")).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convert a non-infinity even point to a XOnlyPublicKey. Panics on invalid input.
|
|
||||||
pub fn x_only(key: &ProjectivePoint) -> XOnlyPublicKey {
|
|
||||||
XOnlyPublicKey::from_slice(&x(key)).expect("x_only was passed a point which was infinity or odd")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Make a point even by adding the generator until it is even.
|
|
||||||
///
|
|
||||||
/// Returns the even point and the amount of additions required.
|
|
||||||
#[cfg(any(feature = "std", feature = "hazmat"))]
|
|
||||||
pub fn make_even(mut key: ProjectivePoint) -> (ProjectivePoint, u64) {
|
|
||||||
let mut c = 0;
|
|
||||||
while key.to_encoded_point(true).tag() == Tag::CompressedOddY {
|
|
||||||
key += ProjectivePoint::GENERATOR;
|
|
||||||
c += 1;
|
|
||||||
}
|
|
||||||
(key, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
mod frost_crypto {
|
|
||||||
use core::fmt::Debug;
|
|
||||||
use std_shims::{vec::Vec, io};
|
|
||||||
|
|
||||||
use zeroize::Zeroizing;
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use bitcoin::hashes::{HashEngine, Hash, sha256::Hash as Sha256};
|
|
||||||
|
|
||||||
use transcript::Transcript;
|
|
||||||
|
|
||||||
use k256::{elliptic_curve::ops::Reduce, U256, Scalar};
|
|
||||||
|
|
||||||
use frost::{
|
|
||||||
curve::{Ciphersuite, Secp256k1},
|
|
||||||
Participant, ThresholdKeys, ThresholdView, FrostError,
|
|
||||||
algorithm::{Hram as HramTrait, Algorithm, Schnorr as FrostSchnorr},
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
/// A BIP-340 compatible HRAm for use with the modular-frost Schnorr Algorithm.
|
|
||||||
///
|
|
||||||
/// If passed an odd nonce, it will have the generator added until it is even.
|
|
||||||
///
|
|
||||||
/// If the key is odd, this will panic.
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub struct Hram;
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
impl HramTrait<Secp256k1> for Hram {
|
|
||||||
fn hram(R: &ProjectivePoint, A: &ProjectivePoint, m: &[u8]) -> Scalar {
|
|
||||||
// Convert the nonce to be even
|
|
||||||
let (R, _) = make_even(*R);
|
|
||||||
|
|
||||||
const TAG_HASH: Sha256 = Sha256::const_hash(b"BIP0340/challenge");
|
|
||||||
|
|
||||||
let mut data = Sha256::engine();
|
|
||||||
data.input(TAG_HASH.as_ref());
|
|
||||||
data.input(TAG_HASH.as_ref());
|
|
||||||
data.input(&x(&R));
|
|
||||||
data.input(&x(A));
|
|
||||||
data.input(m);
|
|
||||||
|
|
||||||
Scalar::reduce(U256::from_be_slice(Sha256::from_engine(data).as_ref()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// BIP-340 Schnorr signature algorithm.
|
|
||||||
///
|
|
||||||
/// This must be used with a ThresholdKeys whose group key is even. If it is odd, this will panic.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Schnorr<T: Sync + Clone + Debug + Transcript>(FrostSchnorr<Secp256k1, T, Hram>);
|
|
||||||
impl<T: Sync + Clone + Debug + Transcript> Schnorr<T> {
|
|
||||||
/// Construct a Schnorr algorithm continuing the specified transcript.
|
|
||||||
pub fn new(transcript: T) -> Schnorr<T> {
|
|
||||||
Schnorr(FrostSchnorr::new(transcript))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Sync + Clone + Debug + Transcript> Algorithm<Secp256k1> for Schnorr<T> {
|
|
||||||
type Transcript = T;
|
|
||||||
type Addendum = ();
|
|
||||||
type Signature = [u8; 64];
|
|
||||||
|
|
||||||
fn transcript(&mut self) -> &mut Self::Transcript {
|
|
||||||
self.0.transcript()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn nonces(&self) -> Vec<Vec<ProjectivePoint>> {
|
|
||||||
self.0.nonces()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn preprocess_addendum<R: RngCore + CryptoRng>(
|
|
||||||
&mut self,
|
|
||||||
rng: &mut R,
|
|
||||||
keys: &ThresholdKeys<Secp256k1>,
|
|
||||||
) {
|
|
||||||
self.0.preprocess_addendum(rng, keys)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_addendum<R: io::Read>(&self, reader: &mut R) -> io::Result<Self::Addendum> {
|
|
||||||
self.0.read_addendum(reader)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process_addendum(
|
|
||||||
&mut self,
|
|
||||||
view: &ThresholdView<Secp256k1>,
|
|
||||||
i: Participant,
|
|
||||||
addendum: (),
|
|
||||||
) -> Result<(), FrostError> {
|
|
||||||
self.0.process_addendum(view, i, addendum)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sign_share(
|
|
||||||
&mut self,
|
|
||||||
params: &ThresholdView<Secp256k1>,
|
|
||||||
nonce_sums: &[Vec<<Secp256k1 as Ciphersuite>::G>],
|
|
||||||
nonces: Vec<Zeroizing<<Secp256k1 as Ciphersuite>::F>>,
|
|
||||||
msg: &[u8],
|
|
||||||
) -> <Secp256k1 as Ciphersuite>::F {
|
|
||||||
self.0.sign_share(params, nonce_sums, nonces, msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
fn verify(
|
|
||||||
&self,
|
|
||||||
group_key: ProjectivePoint,
|
|
||||||
nonces: &[Vec<ProjectivePoint>],
|
|
||||||
sum: Scalar,
|
|
||||||
) -> Option<Self::Signature> {
|
|
||||||
self.0.verify(group_key, nonces, sum).map(|mut sig| {
|
|
||||||
// Make the R of the final signature even
|
|
||||||
let offset;
|
|
||||||
(sig.R, offset) = make_even(sig.R);
|
|
||||||
// s = r + cx. Since we added to the r, add to s
|
|
||||||
sig.s += Scalar::from(offset);
|
|
||||||
// Convert to a Bitcoin signature by dropping the byte for the point's sign bit
|
|
||||||
sig.serialize()[1 ..].try_into().unwrap()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn verify_share(
|
|
||||||
&self,
|
|
||||||
verification_share: ProjectivePoint,
|
|
||||||
nonces: &[Vec<ProjectivePoint>],
|
|
||||||
share: Scalar,
|
|
||||||
) -> Result<Vec<(Scalar, ProjectivePoint)>, ()> {
|
|
||||||
self.0.verify_share(verification_share, nonces, share)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use frost_crypto::*;
|
|
||||||
3
coins/ethereum/.gitignore
vendored
3
coins/ethereum/.gitignore
vendored
@@ -1,3 +0,0 @@
|
|||||||
# solidity build outputs
|
|
||||||
cache
|
|
||||||
artifacts
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "ethereum-serai"
|
|
||||||
version = "0.1.0"
|
|
||||||
description = "An Ethereum library supporting Schnorr signing and on-chain verification"
|
|
||||||
license = "AGPL-3.0-only"
|
|
||||||
repository = "https://github.com/serai-dex/serai/tree/develop/coins/ethereum"
|
|
||||||
authors = ["Luke Parker <lukeparker5132@gmail.com>", "Elizabeth Binks <elizabethjbinks@gmail.com>"]
|
|
||||||
edition = "2021"
|
|
||||||
publish = false
|
|
||||||
rust-version = "1.74"
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
|
||||||
all-features = true
|
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
|
||||||
|
|
||||||
[lints]
|
|
||||||
workspace = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
thiserror = { version = "1", default-features = false }
|
|
||||||
eyre = { version = "0.6", default-features = false }
|
|
||||||
|
|
||||||
sha3 = { version = "0.10", default-features = false, features = ["std"] }
|
|
||||||
|
|
||||||
group = { version = "0.13", default-features = false }
|
|
||||||
k256 = { version = "^0.13.1", default-features = false, features = ["std", "ecdsa"] }
|
|
||||||
frost = { package = "modular-frost", path = "../../crypto/frost", features = ["secp256k1", "tests"] }
|
|
||||||
|
|
||||||
ethers-core = { version = "2", default-features = false }
|
|
||||||
ethers-providers = { version = "2", default-features = false }
|
|
||||||
ethers-contract = { version = "2", default-features = false, features = ["abigen", "providers"] }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
rand_core = { version = "0.6", default-features = false, features = ["std"] }
|
|
||||||
|
|
||||||
hex = { version = "0.4", default-features = false, features = ["std"] }
|
|
||||||
serde = { version = "1", default-features = false, features = ["std"] }
|
|
||||||
serde_json = { version = "1", default-features = false, features = ["std"] }
|
|
||||||
|
|
||||||
sha2 = { version = "0.10", default-features = false, features = ["std"] }
|
|
||||||
|
|
||||||
tokio = { version = "1", features = ["macros"] }
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
# Ethereum
|
|
||||||
|
|
||||||
This package contains Ethereum-related functionality, specifically deploying and
|
|
||||||
interacting with Serai contracts.
|
|
||||||
|
|
||||||
### Dependencies
|
|
||||||
|
|
||||||
- solc
|
|
||||||
- [Foundry](https://github.com/foundry-rs/foundry)
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
println!("cargo:rerun-if-changed=contracts");
|
|
||||||
println!("cargo:rerun-if-changed=artifacts");
|
|
||||||
|
|
||||||
#[rustfmt::skip]
|
|
||||||
let args = [
|
|
||||||
"--base-path", ".",
|
|
||||||
"-o", "./artifacts", "--overwrite",
|
|
||||||
"--bin", "--abi",
|
|
||||||
"--optimize",
|
|
||||||
"./contracts/Schnorr.sol"
|
|
||||||
];
|
|
||||||
|
|
||||||
assert!(std::process::Command::new("solc").args(args).status().unwrap().success());
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
//SPDX-License-Identifier: AGPLv3
|
|
||||||
pragma solidity ^0.8.0;
|
|
||||||
|
|
||||||
// see https://github.com/noot/schnorr-verify for implementation details
|
|
||||||
contract Schnorr {
|
|
||||||
// secp256k1 group order
|
|
||||||
uint256 constant public Q =
|
|
||||||
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141;
|
|
||||||
|
|
||||||
// parity := public key y-coord parity (27 or 28)
|
|
||||||
// px := public key x-coord
|
|
||||||
// message := 32-byte message
|
|
||||||
// s := schnorr signature
|
|
||||||
// e := schnorr signature challenge
|
|
||||||
function verify(
|
|
||||||
uint8 parity,
|
|
||||||
bytes32 px,
|
|
||||||
bytes32 message,
|
|
||||||
bytes32 s,
|
|
||||||
bytes32 e
|
|
||||||
) public view returns (bool) {
|
|
||||||
// ecrecover = (m, v, r, s);
|
|
||||||
bytes32 sp = bytes32(Q - mulmod(uint256(s), uint256(px), Q));
|
|
||||||
bytes32 ep = bytes32(Q - mulmod(uint256(e), uint256(px), Q));
|
|
||||||
|
|
||||||
require(sp != 0);
|
|
||||||
// the ecrecover precompile implementation checks that the `r` and `s`
|
|
||||||
// inputs are non-zero (in this case, `px` and `ep`), thus we don't need to
|
|
||||||
// check if they're zero.will make me
|
|
||||||
address R = ecrecover(sp, parity, px, ep);
|
|
||||||
require(R != address(0), "ecrecover failed");
|
|
||||||
return e == keccak256(
|
|
||||||
abi.encodePacked(R, uint8(parity), px, block.chainid, message)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
use thiserror::Error;
|
|
||||||
use eyre::{eyre, Result};
|
|
||||||
|
|
||||||
use ethers_providers::{Provider, Http};
|
|
||||||
use ethers_contract::abigen;
|
|
||||||
|
|
||||||
use crate::crypto::ProcessedSignature;
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
pub enum EthereumError {
|
|
||||||
#[error("failed to verify Schnorr signature")]
|
|
||||||
VerificationError,
|
|
||||||
}
|
|
||||||
|
|
||||||
abigen!(Schnorr, "./artifacts/Schnorr.abi");
|
|
||||||
|
|
||||||
pub async fn call_verify(
|
|
||||||
contract: &Schnorr<Provider<Http>>,
|
|
||||||
params: &ProcessedSignature,
|
|
||||||
) -> Result<()> {
|
|
||||||
if contract
|
|
||||||
.verify(
|
|
||||||
params.parity + 27,
|
|
||||||
params.px.to_bytes().into(),
|
|
||||||
params.message,
|
|
||||||
params.s.to_bytes().into(),
|
|
||||||
params.e.to_bytes().into(),
|
|
||||||
)
|
|
||||||
.call()
|
|
||||||
.await?
|
|
||||||
{
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(eyre!(EthereumError::VerificationError))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
use sha3::{Digest, Keccak256};
|
|
||||||
|
|
||||||
use group::Group;
|
|
||||||
use k256::{
|
|
||||||
elliptic_curve::{
|
|
||||||
bigint::ArrayEncoding, ops::Reduce, point::DecompressPoint, sec1::ToEncodedPoint,
|
|
||||||
},
|
|
||||||
AffinePoint, ProjectivePoint, Scalar, U256,
|
|
||||||
};
|
|
||||||
|
|
||||||
use frost::{algorithm::Hram, curve::Secp256k1};
|
|
||||||
|
|
||||||
pub fn keccak256(data: &[u8]) -> [u8; 32] {
|
|
||||||
Keccak256::digest(data).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hash_to_scalar(data: &[u8]) -> Scalar {
|
|
||||||
Scalar::reduce(U256::from_be_slice(&keccak256(data)))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn address(point: &ProjectivePoint) -> [u8; 20] {
|
|
||||||
let encoded_point = point.to_encoded_point(false);
|
|
||||||
keccak256(&encoded_point.as_ref()[1 .. 65])[12 .. 32].try_into().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ecrecover(message: Scalar, v: u8, r: Scalar, s: Scalar) -> Option<[u8; 20]> {
|
|
||||||
if r.is_zero().into() || s.is_zero().into() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let R = AffinePoint::decompress(&r.to_bytes(), v.into());
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
if let Some(R) = Option::<AffinePoint>::from(R) {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let R = ProjectivePoint::from(R);
|
|
||||||
|
|
||||||
let r = r.invert().unwrap();
|
|
||||||
let u1 = ProjectivePoint::GENERATOR * (-message * r);
|
|
||||||
let u2 = R * (s * r);
|
|
||||||
let key: ProjectivePoint = u1 + u2;
|
|
||||||
if !bool::from(key.is_identity()) {
|
|
||||||
return Some(address(&key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
|
||||||
pub struct EthereumHram {}
|
|
||||||
impl Hram<Secp256k1> for EthereumHram {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
fn hram(R: &ProjectivePoint, A: &ProjectivePoint, m: &[u8]) -> Scalar {
|
|
||||||
let a_encoded_point = A.to_encoded_point(true);
|
|
||||||
let mut a_encoded = a_encoded_point.as_ref().to_owned();
|
|
||||||
a_encoded[0] += 25; // Ethereum uses 27/28 for point parity
|
|
||||||
let mut data = address(R).to_vec();
|
|
||||||
data.append(&mut a_encoded);
|
|
||||||
data.append(&mut m.to_vec());
|
|
||||||
Scalar::reduce(U256::from_be_slice(&keccak256(&data)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ProcessedSignature {
|
|
||||||
pub s: Scalar,
|
|
||||||
pub px: Scalar,
|
|
||||||
pub parity: u8,
|
|
||||||
pub message: [u8; 32],
|
|
||||||
pub e: Scalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub fn preprocess_signature_for_ecrecover(
|
|
||||||
m: [u8; 32],
|
|
||||||
R: &ProjectivePoint,
|
|
||||||
s: Scalar,
|
|
||||||
A: &ProjectivePoint,
|
|
||||||
chain_id: U256,
|
|
||||||
) -> (Scalar, Scalar) {
|
|
||||||
let processed_sig = process_signature_for_contract(m, R, s, A, chain_id);
|
|
||||||
let sr = processed_sig.s.mul(&processed_sig.px).negate();
|
|
||||||
let er = processed_sig.e.mul(&processed_sig.px).negate();
|
|
||||||
(sr, er)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub fn process_signature_for_contract(
|
|
||||||
m: [u8; 32],
|
|
||||||
R: &ProjectivePoint,
|
|
||||||
s: Scalar,
|
|
||||||
A: &ProjectivePoint,
|
|
||||||
chain_id: U256,
|
|
||||||
) -> ProcessedSignature {
|
|
||||||
let encoded_pk = A.to_encoded_point(true);
|
|
||||||
let px = &encoded_pk.as_ref()[1 .. 33];
|
|
||||||
let px_scalar = Scalar::reduce(U256::from_be_slice(px));
|
|
||||||
let e = EthereumHram::hram(R, A, &[chain_id.to_be_byte_array().as_slice(), &m].concat());
|
|
||||||
ProcessedSignature {
|
|
||||||
s,
|
|
||||||
px: px_scalar,
|
|
||||||
parity: &encoded_pk.as_ref()[0] - 2,
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
message: m,
|
|
||||||
e,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
pub mod contract;
|
|
||||||
pub mod crypto;
|
|
||||||
@@ -1,128 +0,0 @@
|
|||||||
use std::{convert::TryFrom, sync::Arc, time::Duration, fs::File};
|
|
||||||
|
|
||||||
use rand_core::OsRng;
|
|
||||||
|
|
||||||
use ::k256::{
|
|
||||||
elliptic_curve::{bigint::ArrayEncoding, PrimeField},
|
|
||||||
U256,
|
|
||||||
};
|
|
||||||
|
|
||||||
use ethers_core::{
|
|
||||||
types::Signature,
|
|
||||||
abi::Abi,
|
|
||||||
utils::{keccak256, Anvil, AnvilInstance},
|
|
||||||
};
|
|
||||||
use ethers_contract::ContractFactory;
|
|
||||||
use ethers_providers::{Middleware, Provider, Http};
|
|
||||||
|
|
||||||
use frost::{
|
|
||||||
curve::Secp256k1,
|
|
||||||
Participant,
|
|
||||||
algorithm::IetfSchnorr,
|
|
||||||
tests::{key_gen, algorithm_machines, sign},
|
|
||||||
};
|
|
||||||
|
|
||||||
use ethereum_serai::{
|
|
||||||
crypto,
|
|
||||||
contract::{Schnorr, call_verify},
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Replace with a contract deployment from an unknown account, so the environment solely has
|
|
||||||
// to fund the deployer, not create/pass a wallet
|
|
||||||
pub async fn deploy_schnorr_verifier_contract(
|
|
||||||
chain_id: u32,
|
|
||||||
client: Arc<Provider<Http>>,
|
|
||||||
wallet: &k256::ecdsa::SigningKey,
|
|
||||||
) -> eyre::Result<Schnorr<Provider<Http>>> {
|
|
||||||
let abi: Abi = serde_json::from_reader(File::open("./artifacts/Schnorr.abi").unwrap()).unwrap();
|
|
||||||
|
|
||||||
let hex_bin_buf = std::fs::read_to_string("./artifacts/Schnorr.bin").unwrap();
|
|
||||||
let hex_bin =
|
|
||||||
if let Some(stripped) = hex_bin_buf.strip_prefix("0x") { stripped } else { &hex_bin_buf };
|
|
||||||
let bin = hex::decode(hex_bin).unwrap();
|
|
||||||
let factory = ContractFactory::new(abi, bin.into(), client.clone());
|
|
||||||
|
|
||||||
let mut deployment_tx = factory.deploy(())?.tx;
|
|
||||||
deployment_tx.set_chain_id(chain_id);
|
|
||||||
deployment_tx.set_gas(500_000);
|
|
||||||
let (max_fee_per_gas, max_priority_fee_per_gas) = client.estimate_eip1559_fees(None).await?;
|
|
||||||
deployment_tx.as_eip1559_mut().unwrap().max_fee_per_gas = Some(max_fee_per_gas);
|
|
||||||
deployment_tx.as_eip1559_mut().unwrap().max_priority_fee_per_gas = Some(max_priority_fee_per_gas);
|
|
||||||
|
|
||||||
let sig_hash = deployment_tx.sighash();
|
|
||||||
let (sig, rid) = wallet.sign_prehash_recoverable(sig_hash.as_ref()).unwrap();
|
|
||||||
|
|
||||||
// EIP-155 v
|
|
||||||
let mut v = u64::from(rid.to_byte());
|
|
||||||
assert!((v == 0) || (v == 1));
|
|
||||||
v += u64::from((chain_id * 2) + 35);
|
|
||||||
|
|
||||||
let r = sig.r().to_repr();
|
|
||||||
let r_ref: &[u8] = r.as_ref();
|
|
||||||
let s = sig.s().to_repr();
|
|
||||||
let s_ref: &[u8] = s.as_ref();
|
|
||||||
let deployment_tx = deployment_tx.rlp_signed(&Signature { r: r_ref.into(), s: s_ref.into(), v });
|
|
||||||
|
|
||||||
let pending_tx = client.send_raw_transaction(deployment_tx).await?;
|
|
||||||
|
|
||||||
let mut receipt;
|
|
||||||
while {
|
|
||||||
receipt = client.get_transaction_receipt(pending_tx.tx_hash()).await?;
|
|
||||||
receipt.is_none()
|
|
||||||
} {
|
|
||||||
tokio::time::sleep(Duration::from_secs(6)).await;
|
|
||||||
}
|
|
||||||
let receipt = receipt.unwrap();
|
|
||||||
assert!(receipt.status == Some(1.into()));
|
|
||||||
|
|
||||||
let contract = Schnorr::new(receipt.contract_address.unwrap(), client.clone());
|
|
||||||
Ok(contract)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn deploy_test_contract() -> (u32, AnvilInstance, Schnorr<Provider<Http>>) {
|
|
||||||
let anvil = Anvil::new().spawn();
|
|
||||||
|
|
||||||
let provider =
|
|
||||||
Provider::<Http>::try_from(anvil.endpoint()).unwrap().interval(Duration::from_millis(10u64));
|
|
||||||
let chain_id = provider.get_chainid().await.unwrap().as_u32();
|
|
||||||
let wallet = anvil.keys()[0].clone().into();
|
|
||||||
let client = Arc::new(provider);
|
|
||||||
|
|
||||||
(chain_id, anvil, deploy_schnorr_verifier_contract(chain_id, client, &wallet).await.unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_deploy_contract() {
|
|
||||||
deploy_test_contract().await;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_ecrecover_hack() {
|
|
||||||
let (chain_id, _anvil, contract) = deploy_test_contract().await;
|
|
||||||
let chain_id = U256::from(chain_id);
|
|
||||||
|
|
||||||
let keys = key_gen::<_, Secp256k1>(&mut OsRng);
|
|
||||||
let group_key = keys[&Participant::new(1).unwrap()].group_key();
|
|
||||||
|
|
||||||
const MESSAGE: &[u8] = b"Hello, World!";
|
|
||||||
let hashed_message = keccak256(MESSAGE);
|
|
||||||
|
|
||||||
let full_message = &[chain_id.to_be_byte_array().as_slice(), &hashed_message].concat();
|
|
||||||
|
|
||||||
let algo = IetfSchnorr::<Secp256k1, crypto::EthereumHram>::ietf();
|
|
||||||
let sig = sign(
|
|
||||||
&mut OsRng,
|
|
||||||
&algo,
|
|
||||||
keys.clone(),
|
|
||||||
algorithm_machines(&mut OsRng, &algo, &keys),
|
|
||||||
full_message,
|
|
||||||
);
|
|
||||||
let mut processed_sig =
|
|
||||||
crypto::process_signature_for_contract(hashed_message, &sig.R, sig.s, &group_key, chain_id);
|
|
||||||
|
|
||||||
call_verify(&contract, &processed_sig).await.unwrap();
|
|
||||||
|
|
||||||
// test invalid signature fails
|
|
||||||
processed_sig.message[0] = 0;
|
|
||||||
assert!(call_verify(&contract, &processed_sig).await.is_err());
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
use k256::{
|
|
||||||
elliptic_curve::{bigint::ArrayEncoding, ops::Reduce, sec1::ToEncodedPoint},
|
|
||||||
ProjectivePoint, Scalar, U256,
|
|
||||||
};
|
|
||||||
use frost::{curve::Secp256k1, Participant};
|
|
||||||
|
|
||||||
use ethereum_serai::crypto::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_ecrecover() {
|
|
||||||
use rand_core::OsRng;
|
|
||||||
use sha2::Sha256;
|
|
||||||
use sha3::{Digest, Keccak256};
|
|
||||||
use k256::ecdsa::{hazmat::SignPrimitive, signature::DigestVerifier, SigningKey, VerifyingKey};
|
|
||||||
|
|
||||||
let private = SigningKey::random(&mut OsRng);
|
|
||||||
let public = VerifyingKey::from(&private);
|
|
||||||
|
|
||||||
const MESSAGE: &[u8] = b"Hello, World!";
|
|
||||||
let (sig, recovery_id) = private
|
|
||||||
.as_nonzero_scalar()
|
|
||||||
.try_sign_prehashed_rfc6979::<Sha256>(&Keccak256::digest(MESSAGE), b"")
|
|
||||||
.unwrap();
|
|
||||||
#[allow(clippy::unit_cmp)] // Intended to assert this wasn't changed to Result<bool>
|
|
||||||
{
|
|
||||||
assert_eq!(public.verify_digest(Keccak256::new_with_prefix(MESSAGE), &sig).unwrap(), ());
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
ecrecover(hash_to_scalar(MESSAGE), recovery_id.unwrap().is_y_odd().into(), *sig.r(), *sig.s())
|
|
||||||
.unwrap(),
|
|
||||||
address(&ProjectivePoint::from(public.as_affine()))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_signing() {
|
|
||||||
use frost::{
|
|
||||||
algorithm::IetfSchnorr,
|
|
||||||
tests::{algorithm_machines, key_gen, sign},
|
|
||||||
};
|
|
||||||
use rand_core::OsRng;
|
|
||||||
|
|
||||||
let keys = key_gen::<_, Secp256k1>(&mut OsRng);
|
|
||||||
let _group_key = keys[&Participant::new(1).unwrap()].group_key();
|
|
||||||
|
|
||||||
const MESSAGE: &[u8] = b"Hello, World!";
|
|
||||||
|
|
||||||
let algo = IetfSchnorr::<Secp256k1, EthereumHram>::ietf();
|
|
||||||
let _sig =
|
|
||||||
sign(&mut OsRng, &algo, keys.clone(), algorithm_machines(&mut OsRng, &algo, &keys), MESSAGE);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_ecrecover_hack() {
|
|
||||||
use frost::{
|
|
||||||
algorithm::IetfSchnorr,
|
|
||||||
tests::{algorithm_machines, key_gen, sign},
|
|
||||||
};
|
|
||||||
use rand_core::OsRng;
|
|
||||||
|
|
||||||
let keys = key_gen::<_, Secp256k1>(&mut OsRng);
|
|
||||||
let group_key = keys[&Participant::new(1).unwrap()].group_key();
|
|
||||||
let group_key_encoded = group_key.to_encoded_point(true);
|
|
||||||
let group_key_compressed = group_key_encoded.as_ref();
|
|
||||||
let group_key_x = Scalar::reduce(U256::from_be_slice(&group_key_compressed[1 .. 33]));
|
|
||||||
|
|
||||||
const MESSAGE: &[u8] = b"Hello, World!";
|
|
||||||
let hashed_message = keccak256(MESSAGE);
|
|
||||||
let chain_id = U256::ONE;
|
|
||||||
|
|
||||||
let full_message = &[chain_id.to_be_byte_array().as_slice(), &hashed_message].concat();
|
|
||||||
|
|
||||||
let algo = IetfSchnorr::<Secp256k1, EthereumHram>::ietf();
|
|
||||||
let sig = sign(
|
|
||||||
&mut OsRng,
|
|
||||||
&algo,
|
|
||||||
keys.clone(),
|
|
||||||
algorithm_machines(&mut OsRng, &algo, &keys),
|
|
||||||
full_message,
|
|
||||||
);
|
|
||||||
|
|
||||||
let (sr, er) =
|
|
||||||
preprocess_signature_for_ecrecover(hashed_message, &sig.R, sig.s, &group_key, chain_id);
|
|
||||||
let q = ecrecover(sr, group_key_compressed[0] - 2, group_key_x, er).unwrap();
|
|
||||||
assert_eq!(q, address(&sig.R));
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
mod contract;
|
|
||||||
mod crypto;
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "monero-serai"
|
|
||||||
version = "0.1.4-alpha"
|
|
||||||
description = "A modern Monero transaction library"
|
|
||||||
license = "MIT"
|
|
||||||
repository = "https://github.com/serai-dex/serai/tree/develop/coins/monero"
|
|
||||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
|
||||||
edition = "2021"
|
|
||||||
rust-version = "1.74"
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
|
||||||
all-features = true
|
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
|
||||||
|
|
||||||
[lints]
|
|
||||||
workspace = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
std-shims = { path = "../../common/std-shims", version = "^0.1.1", default-features = false }
|
|
||||||
|
|
||||||
async-trait = { version = "0.1", default-features = false }
|
|
||||||
thiserror = { version = "1", default-features = false, optional = true }
|
|
||||||
|
|
||||||
zeroize = { version = "^1.5", default-features = false, features = ["zeroize_derive"] }
|
|
||||||
subtle = { version = "^2.4", default-features = false }
|
|
||||||
|
|
||||||
rand_core = { version = "0.6", default-features = false }
|
|
||||||
# Used to send transactions
|
|
||||||
rand = { version = "0.8", default-features = false }
|
|
||||||
rand_chacha = { version = "0.3", default-features = false }
|
|
||||||
# Used to select decoys
|
|
||||||
rand_distr = { version = "0.4", default-features = false }
|
|
||||||
|
|
||||||
sha3 = { version = "0.10", default-features = false }
|
|
||||||
pbkdf2 = { version = "0.12", features = ["simple"], default-features = false }
|
|
||||||
|
|
||||||
curve25519-dalek = { version = "4", default-features = false, features = ["alloc", "zeroize", "precomputed-tables"] }
|
|
||||||
|
|
||||||
# Used for the hash to curve, along with the more complicated proofs
|
|
||||||
group = { version = "0.13", default-features = false }
|
|
||||||
dalek-ff-group = { path = "../../crypto/dalek-ff-group", version = "0.4", default-features = false }
|
|
||||||
multiexp = { path = "../../crypto/multiexp", version = "0.4", default-features = false, features = ["batch"] }
|
|
||||||
|
|
||||||
# Needed for multisig
|
|
||||||
transcript = { package = "flexible-transcript", path = "../../crypto/transcript", version = "0.3", default-features = false, features = ["recommended"], optional = true }
|
|
||||||
dleq = { path = "../../crypto/dleq", version = "0.4", default-features = false, features = ["serialize"], optional = true }
|
|
||||||
frost = { package = "modular-frost", path = "../../crypto/frost", version = "0.8", default-features = false, features = ["ed25519"], optional = true }
|
|
||||||
|
|
||||||
monero-generators = { path = "generators", version = "0.4", default-features = false }
|
|
||||||
|
|
||||||
async-lock = { version = "3", default-features = false, optional = true }
|
|
||||||
|
|
||||||
hex-literal = "0.4"
|
|
||||||
hex = { version = "0.4", default-features = false, features = ["alloc"] }
|
|
||||||
serde = { version = "1", default-features = false, features = ["derive", "alloc"] }
|
|
||||||
serde_json = { version = "1", default-features = false, features = ["alloc"] }
|
|
||||||
|
|
||||||
base58-monero = { version = "2", default-features = false, features = ["check"] }
|
|
||||||
|
|
||||||
# Used for the provided HTTP RPC
|
|
||||||
digest_auth = { version = "0.3", default-features = false, optional = true }
|
|
||||||
simple-request = { path = "../../common/request", version = "0.1", default-features = false, features = ["tls"], optional = true }
|
|
||||||
tokio = { version = "1", default-features = false, optional = true }
|
|
||||||
|
|
||||||
[build-dependencies]
|
|
||||||
dalek-ff-group = { path = "../../crypto/dalek-ff-group", version = "0.4", default-features = false }
|
|
||||||
monero-generators = { path = "generators", version = "0.4", default-features = false }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
tokio = { version = "1", features = ["sync", "macros"] }
|
|
||||||
|
|
||||||
frost = { package = "modular-frost", path = "../../crypto/frost", features = ["tests"] }
|
|
||||||
|
|
||||||
[features]
|
|
||||||
std = [
|
|
||||||
"std-shims/std",
|
|
||||||
|
|
||||||
"thiserror",
|
|
||||||
|
|
||||||
"zeroize/std",
|
|
||||||
"subtle/std",
|
|
||||||
|
|
||||||
"rand_core/std",
|
|
||||||
"rand/std",
|
|
||||||
"rand_chacha/std",
|
|
||||||
"rand_distr/std",
|
|
||||||
|
|
||||||
"sha3/std",
|
|
||||||
"pbkdf2/std",
|
|
||||||
|
|
||||||
"multiexp/std",
|
|
||||||
|
|
||||||
"transcript/std",
|
|
||||||
"dleq/std",
|
|
||||||
|
|
||||||
"monero-generators/std",
|
|
||||||
|
|
||||||
"async-lock?/std",
|
|
||||||
|
|
||||||
"hex/std",
|
|
||||||
"serde/std",
|
|
||||||
"serde_json/std",
|
|
||||||
|
|
||||||
"base58-monero/std",
|
|
||||||
]
|
|
||||||
|
|
||||||
cache-distribution = ["async-lock"]
|
|
||||||
http-rpc = ["digest_auth", "simple-request", "tokio"]
|
|
||||||
multisig = ["transcript", "frost", "dleq", "std"]
|
|
||||||
binaries = ["tokio/rt-multi-thread", "tokio/macros", "http-rpc"]
|
|
||||||
experimental = []
|
|
||||||
|
|
||||||
default = ["std", "http-rpc"]
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
# monero-serai
|
|
||||||
|
|
||||||
A modern Monero transaction library intended for usage in wallets. It prides
|
|
||||||
itself on accuracy, correctness, and removing common pit falls developers may
|
|
||||||
face.
|
|
||||||
|
|
||||||
monero-serai also offers the following features:
|
|
||||||
|
|
||||||
- Featured Addresses
|
|
||||||
- A FROST-based multisig orders of magnitude more performant than Monero's
|
|
||||||
|
|
||||||
### Purpose and support
|
|
||||||
|
|
||||||
monero-serai was written for Serai, a decentralized exchange aiming to support
|
|
||||||
Monero. Despite this, monero-serai is intended to be a widely usable library,
|
|
||||||
accurate to Monero. monero-serai guarantees the functionality needed for Serai,
|
|
||||||
yet will not deprive functionality from other users.
|
|
||||||
|
|
||||||
Various legacy transaction formats are not currently implemented, yet we are
|
|
||||||
willing to add support for them. There aren't active development efforts around
|
|
||||||
them however.
|
|
||||||
|
|
||||||
### Caveats
|
|
||||||
|
|
||||||
This library DOES attempt to do the following:
|
|
||||||
|
|
||||||
- Create on-chain transactions identical to how wallet2 would (unless told not
|
|
||||||
to)
|
|
||||||
- Not be detectable as monero-serai when scanning outputs
|
|
||||||
- Not reveal spent outputs to the connected RPC node
|
|
||||||
|
|
||||||
This library DOES NOT attempt to do the following:
|
|
||||||
|
|
||||||
- Have identical RPC behavior when creating transactions
|
|
||||||
- Be a wallet
|
|
||||||
|
|
||||||
This means that monero-serai shouldn't be fingerprintable on-chain. It also
|
|
||||||
shouldn't be fingerprintable if a targeted attack occurs to detect if the
|
|
||||||
receiving wallet is monero-serai or wallet2. It also should be generally safe
|
|
||||||
for usage with remote nodes.
|
|
||||||
|
|
||||||
It won't hide from remote nodes it's monero-serai however, potentially
|
|
||||||
allowing a remote node to profile you. The implications of this are left to the
|
|
||||||
user to consider.
|
|
||||||
|
|
||||||
It also won't act as a wallet, just as a transaction library. wallet2 has
|
|
||||||
several *non-transaction-level* policies, such as always attempting to use two
|
|
||||||
inputs to create transactions. These are considered out of scope to
|
|
||||||
monero-serai.
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
use std::{
|
|
||||||
io::Write,
|
|
||||||
env,
|
|
||||||
path::Path,
|
|
||||||
fs::{File, remove_file},
|
|
||||||
};
|
|
||||||
|
|
||||||
use dalek_ff_group::EdwardsPoint;
|
|
||||||
|
|
||||||
use monero_generators::bulletproofs_generators;
|
|
||||||
|
|
||||||
fn serialize(generators_string: &mut String, points: &[EdwardsPoint]) {
|
|
||||||
for generator in points {
|
|
||||||
generators_string.extend(
|
|
||||||
format!(
|
|
||||||
"
|
|
||||||
dalek_ff_group::EdwardsPoint(
|
|
||||||
curve25519_dalek::edwards::CompressedEdwardsY({:?}).decompress().unwrap()
|
|
||||||
),
|
|
||||||
",
|
|
||||||
generator.compress().to_bytes()
|
|
||||||
)
|
|
||||||
.chars(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generators(prefix: &'static str, path: &str) {
|
|
||||||
let generators = bulletproofs_generators(prefix.as_bytes());
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let mut G_str = String::new();
|
|
||||||
serialize(&mut G_str, &generators.G);
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let mut H_str = String::new();
|
|
||||||
serialize(&mut H_str, &generators.H);
|
|
||||||
|
|
||||||
let path = Path::new(&env::var("OUT_DIR").unwrap()).join(path);
|
|
||||||
let _ = remove_file(&path);
|
|
||||||
File::create(&path)
|
|
||||||
.unwrap()
|
|
||||||
.write_all(
|
|
||||||
format!(
|
|
||||||
"
|
|
||||||
pub(crate) static GENERATORS_CELL: OnceLock<Generators> = OnceLock::new();
|
|
||||||
pub fn GENERATORS() -> &'static Generators {{
|
|
||||||
GENERATORS_CELL.get_or_init(|| Generators {{
|
|
||||||
G: vec![
|
|
||||||
{G_str}
|
|
||||||
],
|
|
||||||
H: vec![
|
|
||||||
{H_str}
|
|
||||||
],
|
|
||||||
}})
|
|
||||||
}}
|
|
||||||
",
|
|
||||||
)
|
|
||||||
.as_bytes(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
println!("cargo:rerun-if-changed=build.rs");
|
|
||||||
|
|
||||||
generators("bulletproof", "generators.rs");
|
|
||||||
generators("bulletproof_plus", "generators_plus.rs");
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "monero-generators"
|
|
||||||
version = "0.4.0"
|
|
||||||
description = "Monero's hash_to_point and generators"
|
|
||||||
license = "MIT"
|
|
||||||
repository = "https://github.com/serai-dex/serai/tree/develop/coins/monero/generators"
|
|
||||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
|
||||||
all-features = true
|
|
||||||
rustdoc-args = ["--cfg", "docsrs"]
|
|
||||||
|
|
||||||
[lints]
|
|
||||||
workspace = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
std-shims = { path = "../../../common/std-shims", version = "^0.1.1", default-features = false }
|
|
||||||
|
|
||||||
subtle = { version = "^2.4", default-features = false }
|
|
||||||
|
|
||||||
sha3 = { version = "0.10", default-features = false }
|
|
||||||
|
|
||||||
curve25519-dalek = { version = "4", default-features = false, features = ["alloc", "zeroize", "precomputed-tables"] }
|
|
||||||
|
|
||||||
group = { version = "0.13", default-features = false }
|
|
||||||
dalek-ff-group = { path = "../../../crypto/dalek-ff-group", version = "0.4", default-features = false }
|
|
||||||
|
|
||||||
[features]
|
|
||||||
std = ["std-shims/std", "subtle/std", "sha3/std", "dalek-ff-group/std"]
|
|
||||||
default = ["std"]
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
# Monero Generators
|
|
||||||
|
|
||||||
Generators used by Monero in both its Pedersen commitments and Bulletproofs(+).
|
|
||||||
An implementation of Monero's `ge_fromfe_frombytes_vartime`, simply called
|
|
||||||
`hash_to_point` here, is included, as needed to generate generators.
|
|
||||||
|
|
||||||
This library is usable under no-std when the `std` feature is disabled.
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
use subtle::ConditionallySelectable;
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::{EdwardsPoint, CompressedEdwardsY};
|
|
||||||
|
|
||||||
use group::ff::{Field, PrimeField};
|
|
||||||
use dalek_ff_group::FieldElement;
|
|
||||||
|
|
||||||
use crate::hash;
|
|
||||||
|
|
||||||
/// Monero's hash to point function, as named `ge_fromfe_frombytes_vartime`.
|
|
||||||
pub fn hash_to_point(bytes: [u8; 32]) -> EdwardsPoint {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let A = FieldElement::from(486662u64);
|
|
||||||
|
|
||||||
let v = FieldElement::from_square(hash(&bytes)).double();
|
|
||||||
let w = v + FieldElement::ONE;
|
|
||||||
let x = w.square() + (-A.square() * v);
|
|
||||||
|
|
||||||
// This isn't the complete X, yet its initial value
|
|
||||||
// We don't calculate the full X, and instead solely calculate Y, letting dalek reconstruct X
|
|
||||||
// While inefficient, it solves API boundaries and reduces the amount of work done here
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let X = {
|
|
||||||
let u = w;
|
|
||||||
let v = x;
|
|
||||||
let v3 = v * v * v;
|
|
||||||
let uv3 = u * v3;
|
|
||||||
let v7 = v3 * v3 * v;
|
|
||||||
let uv7 = u * v7;
|
|
||||||
uv3 * uv7.pow((-FieldElement::from(5u8)) * FieldElement::from(8u8).invert().unwrap())
|
|
||||||
};
|
|
||||||
let x = X.square() * x;
|
|
||||||
|
|
||||||
let y = w - x;
|
|
||||||
let non_zero_0 = !y.is_zero();
|
|
||||||
let y_if_non_zero_0 = w + x;
|
|
||||||
let sign = non_zero_0 & (!y_if_non_zero_0.is_zero());
|
|
||||||
|
|
||||||
let mut z = -A;
|
|
||||||
z *= FieldElement::conditional_select(&v, &FieldElement::from(1u8), sign);
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let Z = z + w;
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let mut Y = z - w;
|
|
||||||
|
|
||||||
Y *= Z.invert().unwrap();
|
|
||||||
let mut bytes = Y.to_repr();
|
|
||||||
bytes[31] |= sign.unwrap_u8() << 7;
|
|
||||||
|
|
||||||
CompressedEdwardsY(bytes).decompress().unwrap().mul_by_cofactor()
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
//! Generators used by Monero in both its Pedersen commitments and Bulletproofs(+).
|
|
||||||
//!
|
|
||||||
//! An implementation of Monero's `ge_fromfe_frombytes_vartime`, simply called
|
|
||||||
//! `hash_to_point` here, is included, as needed to generate generators.
|
|
||||||
|
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
|
||||||
|
|
||||||
use std_shims::{sync::OnceLock, vec::Vec};
|
|
||||||
|
|
||||||
use sha3::{Digest, Keccak256};
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::{EdwardsPoint as DalekPoint, CompressedEdwardsY};
|
|
||||||
|
|
||||||
use group::{Group, GroupEncoding};
|
|
||||||
use dalek_ff_group::EdwardsPoint;
|
|
||||||
|
|
||||||
mod varint;
|
|
||||||
use varint::write_varint;
|
|
||||||
|
|
||||||
mod hash_to_point;
|
|
||||||
pub use hash_to_point::hash_to_point;
|
|
||||||
|
|
||||||
fn hash(data: &[u8]) -> [u8; 32] {
|
|
||||||
Keccak256::digest(data).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
static H_CELL: OnceLock<DalekPoint> = OnceLock::new();
|
|
||||||
/// Monero's alternate generator `H`, used for amounts in Pedersen commitments.
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub fn H() -> DalekPoint {
|
|
||||||
*H_CELL.get_or_init(|| {
|
|
||||||
CompressedEdwardsY(hash(&EdwardsPoint::generator().to_bytes()))
|
|
||||||
.decompress()
|
|
||||||
.unwrap()
|
|
||||||
.mul_by_cofactor()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
static H_POW_2_CELL: OnceLock<[DalekPoint; 64]> = OnceLock::new();
|
|
||||||
/// Monero's alternate generator `H`, multiplied by 2**i for i in 1 ..= 64.
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub fn H_pow_2() -> &'static [DalekPoint; 64] {
|
|
||||||
H_POW_2_CELL.get_or_init(|| {
|
|
||||||
let mut res = [H(); 64];
|
|
||||||
for i in 1 .. 64 {
|
|
||||||
res[i] = res[i - 1] + res[i - 1];
|
|
||||||
}
|
|
||||||
res
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const MAX_M: usize = 16;
|
|
||||||
const N: usize = 64;
|
|
||||||
const MAX_MN: usize = MAX_M * N;
|
|
||||||
|
|
||||||
/// Container struct for Bulletproofs(+) generators.
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub struct Generators {
|
|
||||||
pub G: Vec<EdwardsPoint>,
|
|
||||||
pub H: Vec<EdwardsPoint>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate generators as needed for Bulletproofs(+), as Monero does.
|
|
||||||
pub fn bulletproofs_generators(dst: &'static [u8]) -> Generators {
|
|
||||||
let mut res = Generators { G: Vec::with_capacity(MAX_MN), H: Vec::with_capacity(MAX_MN) };
|
|
||||||
for i in 0 .. MAX_MN {
|
|
||||||
let i = 2 * i;
|
|
||||||
|
|
||||||
let mut even = H().compress().to_bytes().to_vec();
|
|
||||||
even.extend(dst);
|
|
||||||
let mut odd = even.clone();
|
|
||||||
|
|
||||||
write_varint(&i.try_into().unwrap(), &mut even).unwrap();
|
|
||||||
write_varint(&(i + 1).try_into().unwrap(), &mut odd).unwrap();
|
|
||||||
res.H.push(EdwardsPoint(hash_to_point(hash(&even))));
|
|
||||||
res.G.push(EdwardsPoint(hash_to_point(hash(&odd))));
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
use std_shims::io::{self, Write};
|
|
||||||
|
|
||||||
const VARINT_CONTINUATION_MASK: u8 = 0b1000_0000;
|
|
||||||
pub(crate) fn write_varint<W: Write>(varint: &u64, w: &mut W) -> io::Result<()> {
|
|
||||||
let mut varint = *varint;
|
|
||||||
while {
|
|
||||||
let mut b = u8::try_from(varint & u64::from(!VARINT_CONTINUATION_MASK)).unwrap();
|
|
||||||
varint >>= 7;
|
|
||||||
if varint != 0 {
|
|
||||||
b |= VARINT_CONTINUATION_MASK;
|
|
||||||
}
|
|
||||||
w.write_all(&[b])?;
|
|
||||||
varint != 0
|
|
||||||
} {}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,323 +0,0 @@
|
|||||||
#[cfg(feature = "binaries")]
|
|
||||||
mod binaries {
|
|
||||||
pub(crate) use std::sync::Arc;
|
|
||||||
|
|
||||||
pub(crate) use curve25519_dalek::{
|
|
||||||
scalar::Scalar,
|
|
||||||
edwards::{CompressedEdwardsY, EdwardsPoint},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub(crate) use multiexp::BatchVerifier;
|
|
||||||
|
|
||||||
pub(crate) use serde::Deserialize;
|
|
||||||
pub(crate) use serde_json::json;
|
|
||||||
|
|
||||||
pub(crate) use monero_serai::{
|
|
||||||
Commitment,
|
|
||||||
ringct::RctPrunable,
|
|
||||||
transaction::{Input, Transaction},
|
|
||||||
block::Block,
|
|
||||||
rpc::{RpcError, Rpc, HttpRpc},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub(crate) use tokio::task::JoinHandle;
|
|
||||||
|
|
||||||
pub(crate) async fn check_block(rpc: Arc<Rpc<HttpRpc>>, block_i: usize) {
|
|
||||||
let hash = loop {
|
|
||||||
match rpc.get_block_hash(block_i).await {
|
|
||||||
Ok(hash) => break hash,
|
|
||||||
Err(RpcError::ConnectionError(e)) => {
|
|
||||||
println!("get_block_hash ConnectionError: {e}");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Err(e) => panic!("couldn't get block {block_i}'s hash: {e:?}"),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Grab the JSON to also check it was deserialized correctly
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct BlockResponse {
|
|
||||||
blob: String,
|
|
||||||
}
|
|
||||||
let res: BlockResponse = loop {
|
|
||||||
match rpc.json_rpc_call("get_block", Some(json!({ "hash": hex::encode(hash) }))).await {
|
|
||||||
Ok(res) => break res,
|
|
||||||
Err(RpcError::ConnectionError(e)) => {
|
|
||||||
println!("get_block ConnectionError: {e}");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Err(e) => panic!("couldn't get block {block_i} via block.hash(): {e:?}"),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let blob = hex::decode(res.blob).expect("node returned non-hex block");
|
|
||||||
let block = Block::read(&mut blob.as_slice())
|
|
||||||
.unwrap_or_else(|e| panic!("couldn't deserialize block {block_i}: {e}"));
|
|
||||||
assert_eq!(block.hash(), hash, "hash differs");
|
|
||||||
assert_eq!(block.serialize(), blob, "serialization differs");
|
|
||||||
|
|
||||||
let txs_len = 1 + block.txs.len();
|
|
||||||
|
|
||||||
if !block.txs.is_empty() {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct TransactionResponse {
|
|
||||||
tx_hash: String,
|
|
||||||
as_hex: String,
|
|
||||||
}
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct TransactionsResponse {
|
|
||||||
#[serde(default)]
|
|
||||||
missed_tx: Vec<String>,
|
|
||||||
txs: Vec<TransactionResponse>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut hashes_hex = block.txs.iter().map(hex::encode).collect::<Vec<_>>();
|
|
||||||
let mut all_txs = vec![];
|
|
||||||
while !hashes_hex.is_empty() {
|
|
||||||
let txs: TransactionsResponse = loop {
|
|
||||||
match rpc
|
|
||||||
.rpc_call(
|
|
||||||
"get_transactions",
|
|
||||||
Some(json!({
|
|
||||||
"txs_hashes": hashes_hex.drain(.. hashes_hex.len().min(100)).collect::<Vec<_>>(),
|
|
||||||
})),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(txs) => break txs,
|
|
||||||
Err(RpcError::ConnectionError(e)) => {
|
|
||||||
println!("get_transactions ConnectionError: {e}");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Err(e) => panic!("couldn't call get_transactions: {e:?}"),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
assert!(txs.missed_tx.is_empty());
|
|
||||||
all_txs.extend(txs.txs);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut batch = BatchVerifier::new(block.txs.len());
|
|
||||||
for (tx_hash, tx_res) in block.txs.into_iter().zip(all_txs) {
|
|
||||||
assert_eq!(
|
|
||||||
tx_res.tx_hash,
|
|
||||||
hex::encode(tx_hash),
|
|
||||||
"node returned a transaction with different hash"
|
|
||||||
);
|
|
||||||
|
|
||||||
let tx = Transaction::read(
|
|
||||||
&mut hex::decode(&tx_res.as_hex).expect("node returned non-hex transaction").as_slice(),
|
|
||||||
)
|
|
||||||
.expect("couldn't deserialize transaction");
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
hex::encode(tx.serialize()),
|
|
||||||
tx_res.as_hex,
|
|
||||||
"Transaction serialization was different"
|
|
||||||
);
|
|
||||||
assert_eq!(tx.hash(), tx_hash, "Transaction hash was different");
|
|
||||||
|
|
||||||
if matches!(tx.rct_signatures.prunable, RctPrunable::Null) {
|
|
||||||
assert_eq!(tx.prefix.version, 1);
|
|
||||||
assert!(!tx.signatures.is_empty());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let sig_hash = tx.signature_hash();
|
|
||||||
// Verify all proofs we support proving for
|
|
||||||
// This is due to having debug_asserts calling verify within their proving, and CLSAG
|
|
||||||
// multisig explicitly calling verify as part of its signing process
|
|
||||||
// Accordingly, making sure our signature_hash algorithm is correct is great, and further
|
|
||||||
// making sure the verification functions are valid is appreciated
|
|
||||||
match tx.rct_signatures.prunable {
|
|
||||||
RctPrunable::Null |
|
|
||||||
RctPrunable::AggregateMlsagBorromean { .. } |
|
|
||||||
RctPrunable::MlsagBorromean { .. } => {}
|
|
||||||
RctPrunable::MlsagBulletproofs { bulletproofs, .. } => {
|
|
||||||
assert!(bulletproofs.batch_verify(
|
|
||||||
&mut rand_core::OsRng,
|
|
||||||
&mut batch,
|
|
||||||
(),
|
|
||||||
&tx.rct_signatures.base.commitments
|
|
||||||
));
|
|
||||||
}
|
|
||||||
RctPrunable::Clsag { bulletproofs, clsags, pseudo_outs } => {
|
|
||||||
assert!(bulletproofs.batch_verify(
|
|
||||||
&mut rand_core::OsRng,
|
|
||||||
&mut batch,
|
|
||||||
(),
|
|
||||||
&tx.rct_signatures.base.commitments
|
|
||||||
));
|
|
||||||
|
|
||||||
for (i, clsag) in clsags.into_iter().enumerate() {
|
|
||||||
let (amount, key_offsets, image) = match &tx.prefix.inputs[i] {
|
|
||||||
Input::Gen(_) => panic!("Input::Gen"),
|
|
||||||
Input::ToKey { amount, key_offsets, key_image } => (amount, key_offsets, key_image),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut running_sum = 0;
|
|
||||||
let mut actual_indexes = vec![];
|
|
||||||
for offset in key_offsets {
|
|
||||||
running_sum += offset;
|
|
||||||
actual_indexes.push(running_sum);
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_outs(
|
|
||||||
rpc: &Rpc<HttpRpc>,
|
|
||||||
amount: u64,
|
|
||||||
indexes: &[u64],
|
|
||||||
) -> Vec<[EdwardsPoint; 2]> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct Out {
|
|
||||||
key: String,
|
|
||||||
mask: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct Outs {
|
|
||||||
outs: Vec<Out>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let outs: Outs = loop {
|
|
||||||
match rpc
|
|
||||||
.rpc_call(
|
|
||||||
"get_outs",
|
|
||||||
Some(json!({
|
|
||||||
"get_txid": true,
|
|
||||||
"outputs": indexes.iter().map(|o| json!({
|
|
||||||
"amount": amount,
|
|
||||||
"index": o
|
|
||||||
})).collect::<Vec<_>>()
|
|
||||||
})),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(outs) => break outs,
|
|
||||||
Err(RpcError::ConnectionError(e)) => {
|
|
||||||
println!("get_outs ConnectionError: {e}");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Err(e) => panic!("couldn't connect to RPC to get outs: {e:?}"),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let rpc_point = |point: &str| {
|
|
||||||
CompressedEdwardsY(
|
|
||||||
hex::decode(point)
|
|
||||||
.expect("invalid hex for ring member")
|
|
||||||
.try_into()
|
|
||||||
.expect("invalid point len for ring member"),
|
|
||||||
)
|
|
||||||
.decompress()
|
|
||||||
.expect("invalid point for ring member")
|
|
||||||
};
|
|
||||||
|
|
||||||
outs
|
|
||||||
.outs
|
|
||||||
.iter()
|
|
||||||
.map(|out| {
|
|
||||||
let mask = rpc_point(&out.mask);
|
|
||||||
if amount != 0 {
|
|
||||||
assert_eq!(mask, Commitment::new(Scalar::from(1u8), amount).calculate());
|
|
||||||
}
|
|
||||||
[rpc_point(&out.key), mask]
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
clsag
|
|
||||||
.verify(
|
|
||||||
&get_outs(&rpc, amount.unwrap_or(0), &actual_indexes).await,
|
|
||||||
image,
|
|
||||||
&pseudo_outs[i],
|
|
||||||
&sig_hash,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert!(batch.verify_vartime());
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("Deserialized, hashed, and reserialized {block_i} with {txs_len} TXs");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "binaries")]
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() {
|
|
||||||
use binaries::*;
|
|
||||||
|
|
||||||
let args = std::env::args().collect::<Vec<String>>();
|
|
||||||
|
|
||||||
// Read start block as the first arg
|
|
||||||
let mut block_i = args[1].parse::<usize>().expect("invalid start block");
|
|
||||||
|
|
||||||
// How many blocks to work on at once
|
|
||||||
let async_parallelism: usize =
|
|
||||||
args.get(2).unwrap_or(&"8".to_string()).parse::<usize>().expect("invalid parallelism argument");
|
|
||||||
|
|
||||||
// Read further args as RPC URLs
|
|
||||||
let default_nodes = vec![
|
|
||||||
"http://xmr-node.cakewallet.com:18081".to_string(),
|
|
||||||
"https://node.sethforprivacy.com".to_string(),
|
|
||||||
];
|
|
||||||
let mut specified_nodes = vec![];
|
|
||||||
{
|
|
||||||
let mut i = 0;
|
|
||||||
loop {
|
|
||||||
let Some(node) = args.get(3 + i) else { break };
|
|
||||||
specified_nodes.push(node.clone());
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let nodes = if specified_nodes.is_empty() { default_nodes } else { specified_nodes };
|
|
||||||
|
|
||||||
let rpc = |url: String| async move {
|
|
||||||
HttpRpc::new(url.clone())
|
|
||||||
.await
|
|
||||||
.unwrap_or_else(|_| panic!("couldn't create HttpRpc connected to {url}"))
|
|
||||||
};
|
|
||||||
let main_rpc = rpc(nodes[0].clone()).await;
|
|
||||||
let mut rpcs = vec![];
|
|
||||||
for i in 0 .. async_parallelism {
|
|
||||||
rpcs.push(Arc::new(rpc(nodes[i % nodes.len()].clone()).await));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut rpc_i = 0;
|
|
||||||
let mut handles: Vec<JoinHandle<()>> = vec![];
|
|
||||||
let mut height = 0;
|
|
||||||
loop {
|
|
||||||
let new_height = main_rpc.get_height().await.expect("couldn't call get_height");
|
|
||||||
if new_height == height {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
height = new_height;
|
|
||||||
|
|
||||||
while block_i < height {
|
|
||||||
if handles.len() >= async_parallelism {
|
|
||||||
// Guarantee one handle is complete
|
|
||||||
handles.swap_remove(0).await.unwrap();
|
|
||||||
|
|
||||||
// Remove all of the finished handles
|
|
||||||
let mut i = 0;
|
|
||||||
while i < handles.len() {
|
|
||||||
if handles[i].is_finished() {
|
|
||||||
handles.swap_remove(i).await.unwrap();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
handles.push(tokio::spawn(check_block(rpcs[rpc_i].clone(), block_i)));
|
|
||||||
rpc_i = (rpc_i + 1) % rpcs.len();
|
|
||||||
block_i += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "binaries"))]
|
|
||||||
fn main() {
|
|
||||||
panic!("To run binaries, please build with `--feature binaries`.");
|
|
||||||
}
|
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
hash,
|
|
||||||
merkle::merkle_root,
|
|
||||||
serialize::*,
|
|
||||||
transaction::{Input, Transaction},
|
|
||||||
};
|
|
||||||
|
|
||||||
const CORRECT_BLOCK_HASH_202612: [u8; 32] =
|
|
||||||
hex_literal::hex!("426d16cff04c71f8b16340b722dc4010a2dd3831c22041431f772547ba6e331a");
|
|
||||||
const EXISTING_BLOCK_HASH_202612: [u8; 32] =
|
|
||||||
hex_literal::hex!("bbd604d2ba11ba27935e006ed39c9bfdd99b76bf4a50654bc1e1e61217962698");
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct BlockHeader {
|
|
||||||
pub major_version: u8,
|
|
||||||
pub minor_version: u8,
|
|
||||||
pub timestamp: u64,
|
|
||||||
pub previous: [u8; 32],
|
|
||||||
pub nonce: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BlockHeader {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
write_varint(&self.major_version, w)?;
|
|
||||||
write_varint(&self.minor_version, w)?;
|
|
||||||
write_varint(&self.timestamp, w)?;
|
|
||||||
w.write_all(&self.previous)?;
|
|
||||||
w.write_all(&self.nonce.to_le_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = vec![];
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<BlockHeader> {
|
|
||||||
Ok(BlockHeader {
|
|
||||||
major_version: read_varint(r)?,
|
|
||||||
minor_version: read_varint(r)?,
|
|
||||||
timestamp: read_varint(r)?,
|
|
||||||
previous: read_bytes(r)?,
|
|
||||||
nonce: read_bytes(r).map(u32::from_le_bytes)?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct Block {
|
|
||||||
pub header: BlockHeader,
|
|
||||||
pub miner_tx: Transaction,
|
|
||||||
pub txs: Vec<[u8; 32]>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Block {
|
|
||||||
pub fn number(&self) -> usize {
|
|
||||||
match self.miner_tx.prefix.inputs.first() {
|
|
||||||
Some(Input::Gen(number)) => (*number).try_into().unwrap(),
|
|
||||||
_ => panic!("invalid block, miner TX didn't have a Input::Gen"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
self.header.write(w)?;
|
|
||||||
self.miner_tx.write(w)?;
|
|
||||||
write_varint(&self.txs.len(), w)?;
|
|
||||||
for tx in &self.txs {
|
|
||||||
w.write_all(tx)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tx_merkle_root(&self) -> [u8; 32] {
|
|
||||||
merkle_root(self.miner_tx.hash(), &self.txs)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Serialize the block as required for the proof of work hash.
|
|
||||||
///
|
|
||||||
/// This is distinct from the serialization required for the block hash. To get the block hash,
|
|
||||||
/// use the [`Block::hash`] function.
|
|
||||||
pub fn serialize_hashable(&self) -> Vec<u8> {
|
|
||||||
let mut blob = self.header.serialize();
|
|
||||||
blob.extend_from_slice(&self.tx_merkle_root());
|
|
||||||
write_varint(&(1 + u64::try_from(self.txs.len()).unwrap()), &mut blob).unwrap();
|
|
||||||
|
|
||||||
blob
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hash(&self) -> [u8; 32] {
|
|
||||||
let mut hashable = self.serialize_hashable();
|
|
||||||
// Monero pre-appends a VarInt of the block hashing blobs length before getting the block hash
|
|
||||||
// but doesn't do this when getting the proof of work hash :)
|
|
||||||
let mut hashing_blob = Vec::with_capacity(8 + hashable.len());
|
|
||||||
write_varint(&u64::try_from(hashable.len()).unwrap(), &mut hashing_blob).unwrap();
|
|
||||||
hashing_blob.append(&mut hashable);
|
|
||||||
|
|
||||||
let hash = hash(&hashing_blob);
|
|
||||||
if hash == CORRECT_BLOCK_HASH_202612 {
|
|
||||||
return EXISTING_BLOCK_HASH_202612;
|
|
||||||
};
|
|
||||||
|
|
||||||
hash
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = vec![];
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<Block> {
|
|
||||||
Ok(Block {
|
|
||||||
header: BlockHeader::read(r)?,
|
|
||||||
miner_tx: Transaction::read(r)?,
|
|
||||||
txs: (0_usize .. read_varint(r)?).map(|_| read_bytes(r)).collect::<Result<_, _>>()?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,225 +0,0 @@
|
|||||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
||||||
#![doc = include_str!("../README.md")]
|
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
|
||||||
|
|
||||||
#[cfg(not(feature = "std"))]
|
|
||||||
#[macro_use]
|
|
||||||
extern crate alloc;
|
|
||||||
|
|
||||||
use std_shims::{sync::OnceLock, io};
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop};
|
|
||||||
|
|
||||||
use sha3::{Digest, Keccak256};
|
|
||||||
|
|
||||||
use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, scalar::Scalar, edwards::EdwardsPoint};
|
|
||||||
|
|
||||||
pub use monero_generators::H;
|
|
||||||
|
|
||||||
mod merkle;
|
|
||||||
|
|
||||||
mod serialize;
|
|
||||||
use serialize::{read_byte, read_u16};
|
|
||||||
|
|
||||||
/// UnreducedScalar struct with functionality for recovering incorrectly reduced scalars.
|
|
||||||
mod unreduced_scalar;
|
|
||||||
|
|
||||||
/// Ring Signature structs and functionality.
|
|
||||||
pub mod ring_signatures;
|
|
||||||
|
|
||||||
/// RingCT structs and functionality.
|
|
||||||
pub mod ringct;
|
|
||||||
use ringct::RctType;
|
|
||||||
|
|
||||||
/// Transaction structs.
|
|
||||||
pub mod transaction;
|
|
||||||
/// Block structs.
|
|
||||||
pub mod block;
|
|
||||||
|
|
||||||
/// Monero daemon RPC interface.
|
|
||||||
pub mod rpc;
|
|
||||||
/// Wallet functionality, enabling scanning and sending transactions.
|
|
||||||
pub mod wallet;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests;
|
|
||||||
|
|
||||||
static INV_EIGHT_CELL: OnceLock<Scalar> = OnceLock::new();
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub(crate) fn INV_EIGHT() -> Scalar {
|
|
||||||
*INV_EIGHT_CELL.get_or_init(|| Scalar::from(8u8).invert())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Monero protocol version.
|
|
||||||
///
|
|
||||||
/// v15 is omitted as v15 was simply v14 and v16 being active at the same time, with regards to the
|
|
||||||
/// transactions supported. Accordingly, v16 should be used during v15.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub enum Protocol {
|
|
||||||
v14,
|
|
||||||
v16,
|
|
||||||
Custom {
|
|
||||||
ring_len: usize,
|
|
||||||
bp_plus: bool,
|
|
||||||
optimal_rct_type: RctType,
|
|
||||||
view_tags: bool,
|
|
||||||
v16_fee: bool,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Protocol {
|
|
||||||
/// Amount of ring members under this protocol version.
|
|
||||||
pub fn ring_len(&self) -> usize {
|
|
||||||
match self {
|
|
||||||
Protocol::v14 => 11,
|
|
||||||
Protocol::v16 => 16,
|
|
||||||
Protocol::Custom { ring_len, .. } => *ring_len,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether or not the specified version uses Bulletproofs or Bulletproofs+.
|
|
||||||
///
|
|
||||||
/// This method will likely be reworked when versions not using Bulletproofs at all are added.
|
|
||||||
pub fn bp_plus(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Protocol::v14 => false,
|
|
||||||
Protocol::v16 => true,
|
|
||||||
Protocol::Custom { bp_plus, .. } => *bp_plus,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Make this an Option when we support pre-RCT protocols
|
|
||||||
pub fn optimal_rct_type(&self) -> RctType {
|
|
||||||
match self {
|
|
||||||
Protocol::v14 => RctType::Clsag,
|
|
||||||
Protocol::v16 => RctType::BulletproofsPlus,
|
|
||||||
Protocol::Custom { optimal_rct_type, .. } => *optimal_rct_type,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether or not the specified version uses view tags.
|
|
||||||
pub fn view_tags(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Protocol::v14 => false,
|
|
||||||
Protocol::v16 => true,
|
|
||||||
Protocol::Custom { view_tags, .. } => *view_tags,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether or not the specified version uses the fee algorithm from Monero
|
|
||||||
/// hard fork version 16 (released in v18 binaries).
|
|
||||||
pub fn v16_fee(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Protocol::v14 => false,
|
|
||||||
Protocol::v16 => true,
|
|
||||||
Protocol::Custom { v16_fee, .. } => *v16_fee,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
match self {
|
|
||||||
Protocol::v14 => w.write_all(&[0, 14]),
|
|
||||||
Protocol::v16 => w.write_all(&[0, 16]),
|
|
||||||
Protocol::Custom { ring_len, bp_plus, optimal_rct_type, view_tags, v16_fee } => {
|
|
||||||
// Custom, version 0
|
|
||||||
w.write_all(&[1, 0])?;
|
|
||||||
w.write_all(&u16::try_from(*ring_len).unwrap().to_le_bytes())?;
|
|
||||||
w.write_all(&[u8::from(*bp_plus)])?;
|
|
||||||
w.write_all(&[optimal_rct_type.to_byte()])?;
|
|
||||||
w.write_all(&[u8::from(*view_tags)])?;
|
|
||||||
w.write_all(&[u8::from(*v16_fee)])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read<R: io::Read>(r: &mut R) -> io::Result<Protocol> {
|
|
||||||
Ok(match read_byte(r)? {
|
|
||||||
// Monero protocol
|
|
||||||
0 => match read_byte(r)? {
|
|
||||||
14 => Protocol::v14,
|
|
||||||
16 => Protocol::v16,
|
|
||||||
_ => Err(io::Error::other("unrecognized monero protocol"))?,
|
|
||||||
},
|
|
||||||
// Custom
|
|
||||||
1 => match read_byte(r)? {
|
|
||||||
0 => Protocol::Custom {
|
|
||||||
ring_len: read_u16(r)?.into(),
|
|
||||||
bp_plus: match read_byte(r)? {
|
|
||||||
0 => false,
|
|
||||||
1 => true,
|
|
||||||
_ => Err(io::Error::other("invalid bool serialization"))?,
|
|
||||||
},
|
|
||||||
optimal_rct_type: RctType::from_byte(read_byte(r)?)
|
|
||||||
.ok_or_else(|| io::Error::other("invalid RctType serialization"))?,
|
|
||||||
view_tags: match read_byte(r)? {
|
|
||||||
0 => false,
|
|
||||||
1 => true,
|
|
||||||
_ => Err(io::Error::other("invalid bool serialization"))?,
|
|
||||||
},
|
|
||||||
v16_fee: match read_byte(r)? {
|
|
||||||
0 => false,
|
|
||||||
1 => true,
|
|
||||||
_ => Err(io::Error::other("invalid bool serialization"))?,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
_ => Err(io::Error::other("unrecognized custom protocol serialization"))?,
|
|
||||||
},
|
|
||||||
_ => Err(io::Error::other("unrecognized protocol serialization"))?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Transparent structure representing a Pedersen commitment's contents.
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct Commitment {
|
|
||||||
pub mask: Scalar,
|
|
||||||
pub amount: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl core::fmt::Debug for Commitment {
|
|
||||||
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
|
|
||||||
fmt.debug_struct("Commitment").field("amount", &self.amount).finish_non_exhaustive()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Commitment {
|
|
||||||
/// A commitment to zero, defined with a mask of 1 (as to not be the identity).
|
|
||||||
pub fn zero() -> Commitment {
|
|
||||||
Commitment { mask: Scalar::ONE, amount: 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(mask: Scalar, amount: u64) -> Commitment {
|
|
||||||
Commitment { mask, amount }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculate a Pedersen commitment, as a point, from the transparent structure.
|
|
||||||
pub fn calculate(&self) -> EdwardsPoint {
|
|
||||||
(&self.mask * ED25519_BASEPOINT_TABLE) + (Scalar::from(self.amount) * H())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Support generating a random scalar using a modern rand, as dalek's is notoriously dated.
|
|
||||||
pub fn random_scalar<R: RngCore + CryptoRng>(rng: &mut R) -> Scalar {
|
|
||||||
let mut r = [0; 64];
|
|
||||||
rng.fill_bytes(&mut r);
|
|
||||||
Scalar::from_bytes_mod_order_wide(&r)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn hash(data: &[u8]) -> [u8; 32] {
|
|
||||||
Keccak256::digest(data).into()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Hash the provided data to a scalar via keccak256(data) % l.
|
|
||||||
pub fn hash_to_scalar(data: &[u8]) -> Scalar {
|
|
||||||
let scalar = Scalar::from_bytes_mod_order(hash(data));
|
|
||||||
// Monero will explicitly error in this case
|
|
||||||
// This library acknowledges its practical impossibility of it occurring, and doesn't bother to
|
|
||||||
// code in logic to handle it. That said, if it ever occurs, something must happen in order to
|
|
||||||
// not generate/verify a proof we believe to be valid when it isn't
|
|
||||||
assert!(scalar != Scalar::ZERO, "ZERO HASH: {data:?}");
|
|
||||||
scalar
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
use std_shims::vec::Vec;
|
|
||||||
|
|
||||||
use crate::hash;
|
|
||||||
|
|
||||||
pub(crate) fn merkle_root(root: [u8; 32], leafs: &[[u8; 32]]) -> [u8; 32] {
|
|
||||||
match leafs.len() {
|
|
||||||
0 => root,
|
|
||||||
1 => hash(&[root, leafs[0]].concat()),
|
|
||||||
_ => {
|
|
||||||
let mut hashes = Vec::with_capacity(1 + leafs.len());
|
|
||||||
hashes.push(root);
|
|
||||||
hashes.extend(leafs);
|
|
||||||
|
|
||||||
// Monero preprocess this so the length is a power of 2
|
|
||||||
let mut high_pow_2 = 4; // 4 is the lowest value this can be
|
|
||||||
while high_pow_2 < hashes.len() {
|
|
||||||
high_pow_2 *= 2;
|
|
||||||
}
|
|
||||||
let low_pow_2 = high_pow_2 / 2;
|
|
||||||
|
|
||||||
// Merge right-most hashes until we're at the low_pow_2
|
|
||||||
{
|
|
||||||
let overage = hashes.len() - low_pow_2;
|
|
||||||
let mut rightmost = hashes.drain((low_pow_2 - overage) ..);
|
|
||||||
// This is true since we took overage from beneath and above low_pow_2, taking twice as
|
|
||||||
// many elements as overage
|
|
||||||
debug_assert_eq!(rightmost.len() % 2, 0);
|
|
||||||
|
|
||||||
let mut paired_hashes = Vec::with_capacity(overage);
|
|
||||||
while let Some(left) = rightmost.next() {
|
|
||||||
let right = rightmost.next().unwrap();
|
|
||||||
paired_hashes.push(hash(&[left.as_ref(), &right].concat()));
|
|
||||||
}
|
|
||||||
drop(rightmost);
|
|
||||||
|
|
||||||
hashes.extend(paired_hashes);
|
|
||||||
assert_eq!(hashes.len(), low_pow_2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do a traditional pairing off
|
|
||||||
let mut new_hashes = Vec::with_capacity(hashes.len() / 2);
|
|
||||||
while hashes.len() > 1 {
|
|
||||||
let mut i = 0;
|
|
||||||
while i < hashes.len() {
|
|
||||||
new_hashes.push(hash(&[hashes[i], hashes[i + 1]].concat()));
|
|
||||||
i += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
hashes = new_hashes;
|
|
||||||
new_hashes = Vec::with_capacity(hashes.len() / 2);
|
|
||||||
}
|
|
||||||
hashes[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
use std_shims::{
|
|
||||||
io::{self, *},
|
|
||||||
vec::Vec,
|
|
||||||
};
|
|
||||||
|
|
||||||
use zeroize::Zeroize;
|
|
||||||
|
|
||||||
use curve25519_dalek::{EdwardsPoint, Scalar};
|
|
||||||
|
|
||||||
use monero_generators::hash_to_point;
|
|
||||||
|
|
||||||
use crate::{serialize::*, hash_to_scalar};
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub struct Signature {
|
|
||||||
c: Scalar,
|
|
||||||
r: Scalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Signature {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
write_scalar(&self.c, w)?;
|
|
||||||
write_scalar(&self.r, w)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<Signature> {
|
|
||||||
Ok(Signature { c: read_scalar(r)?, r: read_scalar(r)? })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub struct RingSignature {
|
|
||||||
sigs: Vec<Signature>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RingSignature {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
for sig in &self.sigs {
|
|
||||||
sig.write(w)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(members: usize, r: &mut R) -> io::Result<RingSignature> {
|
|
||||||
Ok(RingSignature { sigs: read_raw_vec(Signature::read, members, r)? })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn verify(&self, msg: &[u8; 32], ring: &[EdwardsPoint], key_image: &EdwardsPoint) -> bool {
|
|
||||||
if ring.len() != self.sigs.len() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = Vec::with_capacity(32 + (32 * 2 * ring.len()));
|
|
||||||
buf.extend_from_slice(msg);
|
|
||||||
|
|
||||||
let mut sum = Scalar::ZERO;
|
|
||||||
|
|
||||||
for (ring_member, sig) in ring.iter().zip(&self.sigs) {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let Li = EdwardsPoint::vartime_double_scalar_mul_basepoint(&sig.c, ring_member, &sig.r);
|
|
||||||
buf.extend_from_slice(Li.compress().as_bytes());
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let Ri = (sig.r * hash_to_point(ring_member.compress().to_bytes())) + (sig.c * key_image);
|
|
||||||
buf.extend_from_slice(Ri.compress().as_bytes());
|
|
||||||
|
|
||||||
sum += sig.c;
|
|
||||||
}
|
|
||||||
|
|
||||||
sum == hash_to_scalar(&buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
use core::fmt::Debug;
|
|
||||||
use std_shims::io::{self, Read, Write};
|
|
||||||
|
|
||||||
use curve25519_dalek::{traits::Identity, Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use monero_generators::H_pow_2;
|
|
||||||
|
|
||||||
use crate::{hash_to_scalar, unreduced_scalar::UnreducedScalar, serialize::*};
|
|
||||||
|
|
||||||
/// 64 Borromean ring signatures.
|
|
||||||
///
|
|
||||||
/// s0 and s1 are stored as `UnreducedScalar`s due to Monero not requiring they were reduced.
|
|
||||||
/// `UnreducedScalar` preserves their original byte encoding and implements a custom reduction
|
|
||||||
/// algorithm which was in use.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct BorromeanSignatures {
|
|
||||||
pub s0: [UnreducedScalar; 64],
|
|
||||||
pub s1: [UnreducedScalar; 64],
|
|
||||||
pub ee: Scalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BorromeanSignatures {
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<BorromeanSignatures> {
|
|
||||||
Ok(BorromeanSignatures {
|
|
||||||
s0: read_array(UnreducedScalar::read, r)?,
|
|
||||||
s1: read_array(UnreducedScalar::read, r)?,
|
|
||||||
ee: read_scalar(r)?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
for s0 in &self.s0 {
|
|
||||||
s0.write(w)?;
|
|
||||||
}
|
|
||||||
for s1 in &self.s1 {
|
|
||||||
s1.write(w)?;
|
|
||||||
}
|
|
||||||
write_scalar(&self.ee, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn verify(&self, keys_a: &[EdwardsPoint], keys_b: &[EdwardsPoint]) -> bool {
|
|
||||||
let mut transcript = [0; 2048];
|
|
||||||
|
|
||||||
for i in 0 .. 64 {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let LL = EdwardsPoint::vartime_double_scalar_mul_basepoint(
|
|
||||||
&self.ee,
|
|
||||||
&keys_a[i],
|
|
||||||
&self.s0[i].recover_monero_slide_scalar(),
|
|
||||||
);
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let LV = EdwardsPoint::vartime_double_scalar_mul_basepoint(
|
|
||||||
&hash_to_scalar(LL.compress().as_bytes()),
|
|
||||||
&keys_b[i],
|
|
||||||
&self.s1[i].recover_monero_slide_scalar(),
|
|
||||||
);
|
|
||||||
transcript[(i * 32) .. ((i + 1) * 32)].copy_from_slice(LV.compress().as_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
hash_to_scalar(&transcript) == self.ee
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A range proof premised on Borromean ring signatures.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct BorromeanRange {
|
|
||||||
pub sigs: BorromeanSignatures,
|
|
||||||
pub bit_commitments: [EdwardsPoint; 64],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BorromeanRange {
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<BorromeanRange> {
|
|
||||||
Ok(BorromeanRange {
|
|
||||||
sigs: BorromeanSignatures::read(r)?,
|
|
||||||
bit_commitments: read_array(read_point, r)?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
self.sigs.write(w)?;
|
|
||||||
write_raw_vec(write_point, &self.bit_commitments, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn verify(&self, commitment: &EdwardsPoint) -> bool {
|
|
||||||
if &self.bit_commitments.iter().sum::<EdwardsPoint>() != commitment {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let H_pow_2 = H_pow_2();
|
|
||||||
let mut commitments_sub_one = [EdwardsPoint::identity(); 64];
|
|
||||||
for i in 0 .. 64 {
|
|
||||||
commitments_sub_one[i] = self.bit_commitments[i] - H_pow_2[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
self.sigs.verify(&self.bit_commitments, &commitments_sub_one)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,151 +0,0 @@
|
|||||||
use std_shims::{vec::Vec, sync::OnceLock};
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use subtle::{Choice, ConditionallySelectable};
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::EdwardsPoint as DalekPoint;
|
|
||||||
|
|
||||||
use group::{ff::Field, Group};
|
|
||||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use multiexp::multiexp as multiexp_const;
|
|
||||||
|
|
||||||
pub(crate) use monero_generators::Generators;
|
|
||||||
|
|
||||||
use crate::{INV_EIGHT as DALEK_INV_EIGHT, H as DALEK_H, Commitment, hash_to_scalar as dalek_hash};
|
|
||||||
pub(crate) use crate::ringct::bulletproofs::scalar_vector::*;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn INV_EIGHT() -> Scalar {
|
|
||||||
Scalar(DALEK_INV_EIGHT())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn H() -> EdwardsPoint {
|
|
||||||
EdwardsPoint(DALEK_H())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn hash_to_scalar(data: &[u8]) -> Scalar {
|
|
||||||
Scalar(dalek_hash(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Components common between variants
|
|
||||||
pub(crate) const MAX_M: usize = 16;
|
|
||||||
pub(crate) const LOG_N: usize = 6; // 2 << 6 == N
|
|
||||||
pub(crate) const N: usize = 64;
|
|
||||||
|
|
||||||
pub(crate) fn prove_multiexp(pairs: &[(Scalar, EdwardsPoint)]) -> EdwardsPoint {
|
|
||||||
multiexp_const(pairs) * INV_EIGHT()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn vector_exponent(
|
|
||||||
generators: &Generators,
|
|
||||||
a: &ScalarVector,
|
|
||||||
b: &ScalarVector,
|
|
||||||
) -> EdwardsPoint {
|
|
||||||
debug_assert_eq!(a.len(), b.len());
|
|
||||||
(a * &generators.G[.. a.len()]) + (b * &generators.H[.. b.len()])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn hash_cache(cache: &mut Scalar, mash: &[[u8; 32]]) -> Scalar {
|
|
||||||
let slice =
|
|
||||||
&[cache.to_bytes().as_ref(), mash.iter().copied().flatten().collect::<Vec<_>>().as_ref()]
|
|
||||||
.concat();
|
|
||||||
*cache = hash_to_scalar(slice);
|
|
||||||
*cache
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn MN(outputs: usize) -> (usize, usize, usize) {
|
|
||||||
let mut logM = 0;
|
|
||||||
let mut M;
|
|
||||||
while {
|
|
||||||
M = 1 << logM;
|
|
||||||
(M <= MAX_M) && (M < outputs)
|
|
||||||
} {
|
|
||||||
logM += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
(logM + LOG_N, M, M * N)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn bit_decompose(commitments: &[Commitment]) -> (ScalarVector, ScalarVector) {
|
|
||||||
let (_, M, MN) = MN(commitments.len());
|
|
||||||
|
|
||||||
let sv = commitments.iter().map(|c| Scalar::from(c.amount)).collect::<Vec<_>>();
|
|
||||||
let mut aL = ScalarVector::new(MN);
|
|
||||||
let mut aR = ScalarVector::new(MN);
|
|
||||||
|
|
||||||
for j in 0 .. M {
|
|
||||||
for i in (0 .. N).rev() {
|
|
||||||
let bit =
|
|
||||||
if j < sv.len() { Choice::from((sv[j][i / 8] >> (i % 8)) & 1) } else { Choice::from(0) };
|
|
||||||
aL.0[(j * N) + i] = Scalar::conditional_select(&Scalar::ZERO, &Scalar::ONE, bit);
|
|
||||||
aR.0[(j * N) + i] = Scalar::conditional_select(&-Scalar::ONE, &Scalar::ZERO, bit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
(aL, aR)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn hash_commitments<C: IntoIterator<Item = DalekPoint>>(
|
|
||||||
commitments: C,
|
|
||||||
) -> (Scalar, Vec<EdwardsPoint>) {
|
|
||||||
let V = commitments.into_iter().map(|c| EdwardsPoint(c) * INV_EIGHT()).collect::<Vec<_>>();
|
|
||||||
(hash_to_scalar(&V.iter().flat_map(|V| V.compress().to_bytes()).collect::<Vec<_>>()), V)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn alpha_rho<R: RngCore + CryptoRng>(
|
|
||||||
rng: &mut R,
|
|
||||||
generators: &Generators,
|
|
||||||
aL: &ScalarVector,
|
|
||||||
aR: &ScalarVector,
|
|
||||||
) -> (Scalar, EdwardsPoint) {
|
|
||||||
let ar = Scalar::random(rng);
|
|
||||||
(ar, (vector_exponent(generators, aL, aR) + (EdwardsPoint::generator() * ar)) * INV_EIGHT())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn LR_statements(
|
|
||||||
a: &ScalarVector,
|
|
||||||
G_i: &[EdwardsPoint],
|
|
||||||
b: &ScalarVector,
|
|
||||||
H_i: &[EdwardsPoint],
|
|
||||||
cL: Scalar,
|
|
||||||
U: EdwardsPoint,
|
|
||||||
) -> Vec<(Scalar, EdwardsPoint)> {
|
|
||||||
let mut res = a
|
|
||||||
.0
|
|
||||||
.iter()
|
|
||||||
.copied()
|
|
||||||
.zip(G_i.iter().copied())
|
|
||||||
.chain(b.0.iter().copied().zip(H_i.iter().copied()))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
res.push((cL, U));
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
static TWO_N_CELL: OnceLock<ScalarVector> = OnceLock::new();
|
|
||||||
pub(crate) fn TWO_N() -> &'static ScalarVector {
|
|
||||||
TWO_N_CELL.get_or_init(|| ScalarVector::powers(Scalar::from(2u8), N))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn challenge_products(w: &[Scalar], winv: &[Scalar]) -> Vec<Scalar> {
|
|
||||||
let mut products = vec![Scalar::ZERO; 1 << w.len()];
|
|
||||||
products[0] = winv[0];
|
|
||||||
products[1] = w[0];
|
|
||||||
for j in 1 .. w.len() {
|
|
||||||
let mut slots = (1 << (j + 1)) - 1;
|
|
||||||
while slots > 0 {
|
|
||||||
products[slots] = products[slots / 2] * w[j];
|
|
||||||
products[slots - 1] = products[slots / 2] * winv[j];
|
|
||||||
slots = slots.saturating_sub(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sanity check as if the above failed to populate, it'd be critical
|
|
||||||
for w in &products {
|
|
||||||
debug_assert!(!bool::from(w.is_zero()));
|
|
||||||
}
|
|
||||||
|
|
||||||
products
|
|
||||||
}
|
|
||||||
@@ -1,229 +0,0 @@
|
|||||||
#![allow(non_snake_case)]
|
|
||||||
|
|
||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, Zeroizing};
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::EdwardsPoint;
|
|
||||||
use multiexp::BatchVerifier;
|
|
||||||
|
|
||||||
use crate::{Commitment, wallet::TransactionError, serialize::*};
|
|
||||||
|
|
||||||
pub(crate) mod scalar_vector;
|
|
||||||
pub(crate) mod core;
|
|
||||||
use self::core::LOG_N;
|
|
||||||
|
|
||||||
pub(crate) mod original;
|
|
||||||
use self::original::OriginalStruct;
|
|
||||||
|
|
||||||
pub(crate) mod plus;
|
|
||||||
use self::plus::*;
|
|
||||||
|
|
||||||
pub(crate) const MAX_OUTPUTS: usize = self::core::MAX_M;
|
|
||||||
|
|
||||||
/// Bulletproofs enum, supporting the original and plus formulations.
|
|
||||||
#[allow(clippy::large_enum_variant)]
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub enum Bulletproofs {
|
|
||||||
Original(OriginalStruct),
|
|
||||||
Plus(AggregateRangeProof),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Bulletproofs {
|
|
||||||
fn bp_fields(plus: bool) -> usize {
|
|
||||||
if plus {
|
|
||||||
6
|
|
||||||
} else {
|
|
||||||
9
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/monero-project/monero/blob/94e67bf96bbc010241f29ada6abc89f49a81759c/
|
|
||||||
// src/cryptonote_basic/cryptonote_format_utils.cpp#L106-L124
|
|
||||||
pub(crate) fn calculate_bp_clawback(plus: bool, n_outputs: usize) -> (usize, usize) {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let mut LR_len = 0;
|
|
||||||
let mut n_padded_outputs = 1;
|
|
||||||
while n_padded_outputs < n_outputs {
|
|
||||||
LR_len += 1;
|
|
||||||
n_padded_outputs = 1 << LR_len;
|
|
||||||
}
|
|
||||||
LR_len += LOG_N;
|
|
||||||
|
|
||||||
let mut bp_clawback = 0;
|
|
||||||
if n_padded_outputs > 2 {
|
|
||||||
let fields = Bulletproofs::bp_fields(plus);
|
|
||||||
let base = ((fields + (2 * (LOG_N + 1))) * 32) / 2;
|
|
||||||
let size = (fields + (2 * LR_len)) * 32;
|
|
||||||
bp_clawback = ((base * n_padded_outputs) - size) * 4 / 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
(bp_clawback, LR_len)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn fee_weight(plus: bool, outputs: usize) -> usize {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let (bp_clawback, LR_len) = Bulletproofs::calculate_bp_clawback(plus, outputs);
|
|
||||||
32 * (Bulletproofs::bp_fields(plus) + (2 * LR_len)) + 2 + bp_clawback
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Prove the list of commitments are within [0 .. 2^64).
|
|
||||||
pub fn prove<R: RngCore + CryptoRng>(
|
|
||||||
rng: &mut R,
|
|
||||||
outputs: &[Commitment],
|
|
||||||
plus: bool,
|
|
||||||
) -> Result<Bulletproofs, TransactionError> {
|
|
||||||
if outputs.is_empty() {
|
|
||||||
Err(TransactionError::NoOutputs)?;
|
|
||||||
}
|
|
||||||
if outputs.len() > MAX_OUTPUTS {
|
|
||||||
Err(TransactionError::TooManyOutputs)?;
|
|
||||||
}
|
|
||||||
Ok(if !plus {
|
|
||||||
Bulletproofs::Original(OriginalStruct::prove(rng, outputs))
|
|
||||||
} else {
|
|
||||||
use dalek_ff_group::EdwardsPoint as DfgPoint;
|
|
||||||
Bulletproofs::Plus(
|
|
||||||
AggregateRangeStatement::new(outputs.iter().map(|com| DfgPoint(com.calculate())).collect())
|
|
||||||
.unwrap()
|
|
||||||
.prove(rng, &Zeroizing::new(AggregateRangeWitness::new(outputs).unwrap()))
|
|
||||||
.unwrap(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Verify the given Bulletproofs.
|
|
||||||
#[must_use]
|
|
||||||
pub fn verify<R: RngCore + CryptoRng>(&self, rng: &mut R, commitments: &[EdwardsPoint]) -> bool {
|
|
||||||
match self {
|
|
||||||
Bulletproofs::Original(bp) => bp.verify(rng, commitments),
|
|
||||||
Bulletproofs::Plus(bp) => {
|
|
||||||
let mut verifier = BatchVerifier::new(1);
|
|
||||||
// If this commitment is torsioned (which is allowed), this won't be a well-formed
|
|
||||||
// dfg::EdwardsPoint (expected to be of prime-order)
|
|
||||||
// The actual BP+ impl will perform a torsion clear though, making this safe
|
|
||||||
// TODO: Have AggregateRangeStatement take in dalek EdwardsPoint for clarity on this
|
|
||||||
let Some(statement) = AggregateRangeStatement::new(
|
|
||||||
commitments.iter().map(|c| dalek_ff_group::EdwardsPoint(*c)).collect(),
|
|
||||||
) else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
if !statement.verify(rng, &mut verifier, (), bp.clone()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
verifier.verify_vartime()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Accumulate the verification for the given Bulletproofs into the specified BatchVerifier.
|
|
||||||
/// Returns false if the Bulletproofs aren't sane, without mutating the BatchVerifier.
|
|
||||||
/// Returns true if the Bulletproofs are sane, regardless of their validity.
|
|
||||||
#[must_use]
|
|
||||||
pub fn batch_verify<ID: Copy + Zeroize, R: RngCore + CryptoRng>(
|
|
||||||
&self,
|
|
||||||
rng: &mut R,
|
|
||||||
verifier: &mut BatchVerifier<ID, dalek_ff_group::EdwardsPoint>,
|
|
||||||
id: ID,
|
|
||||||
commitments: &[EdwardsPoint],
|
|
||||||
) -> bool {
|
|
||||||
match self {
|
|
||||||
Bulletproofs::Original(bp) => bp.batch_verify(rng, verifier, id, commitments),
|
|
||||||
Bulletproofs::Plus(bp) => {
|
|
||||||
let Some(statement) = AggregateRangeStatement::new(
|
|
||||||
commitments.iter().map(|c| dalek_ff_group::EdwardsPoint(*c)).collect(),
|
|
||||||
) else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
statement.verify(rng, verifier, id, bp.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_core<W: Write, F: Fn(&[EdwardsPoint], &mut W) -> io::Result<()>>(
|
|
||||||
&self,
|
|
||||||
w: &mut W,
|
|
||||||
specific_write_vec: F,
|
|
||||||
) -> io::Result<()> {
|
|
||||||
match self {
|
|
||||||
Bulletproofs::Original(bp) => {
|
|
||||||
write_point(&bp.A, w)?;
|
|
||||||
write_point(&bp.S, w)?;
|
|
||||||
write_point(&bp.T1, w)?;
|
|
||||||
write_point(&bp.T2, w)?;
|
|
||||||
write_scalar(&bp.taux, w)?;
|
|
||||||
write_scalar(&bp.mu, w)?;
|
|
||||||
specific_write_vec(&bp.L, w)?;
|
|
||||||
specific_write_vec(&bp.R, w)?;
|
|
||||||
write_scalar(&bp.a, w)?;
|
|
||||||
write_scalar(&bp.b, w)?;
|
|
||||||
write_scalar(&bp.t, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
Bulletproofs::Plus(bp) => {
|
|
||||||
write_point(&bp.A.0, w)?;
|
|
||||||
write_point(&bp.wip.A.0, w)?;
|
|
||||||
write_point(&bp.wip.B.0, w)?;
|
|
||||||
write_scalar(&bp.wip.r_answer.0, w)?;
|
|
||||||
write_scalar(&bp.wip.s_answer.0, w)?;
|
|
||||||
write_scalar(&bp.wip.delta_answer.0, w)?;
|
|
||||||
specific_write_vec(&bp.wip.L.iter().copied().map(|L| L.0).collect::<Vec<_>>(), w)?;
|
|
||||||
specific_write_vec(&bp.wip.R.iter().copied().map(|R| R.0).collect::<Vec<_>>(), w)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn signature_write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
self.write_core(w, |points, w| write_raw_vec(write_point, points, w))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
self.write_core(w, |points, w| write_vec(write_point, points, w))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = vec![];
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Read Bulletproofs.
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<Bulletproofs> {
|
|
||||||
Ok(Bulletproofs::Original(OriginalStruct {
|
|
||||||
A: read_point(r)?,
|
|
||||||
S: read_point(r)?,
|
|
||||||
T1: read_point(r)?,
|
|
||||||
T2: read_point(r)?,
|
|
||||||
taux: read_scalar(r)?,
|
|
||||||
mu: read_scalar(r)?,
|
|
||||||
L: read_vec(read_point, r)?,
|
|
||||||
R: read_vec(read_point, r)?,
|
|
||||||
a: read_scalar(r)?,
|
|
||||||
b: read_scalar(r)?,
|
|
||||||
t: read_scalar(r)?,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Read Bulletproofs+.
|
|
||||||
pub fn read_plus<R: Read>(r: &mut R) -> io::Result<Bulletproofs> {
|
|
||||||
use dalek_ff_group::{Scalar as DfgScalar, EdwardsPoint as DfgPoint};
|
|
||||||
|
|
||||||
Ok(Bulletproofs::Plus(AggregateRangeProof {
|
|
||||||
A: DfgPoint(read_point(r)?),
|
|
||||||
wip: WipProof {
|
|
||||||
A: DfgPoint(read_point(r)?),
|
|
||||||
B: DfgPoint(read_point(r)?),
|
|
||||||
r_answer: DfgScalar(read_scalar(r)?),
|
|
||||||
s_answer: DfgScalar(read_scalar(r)?),
|
|
||||||
delta_answer: DfgScalar(read_scalar(r)?),
|
|
||||||
L: read_vec(read_point, r)?.into_iter().map(DfgPoint).collect(),
|
|
||||||
R: read_vec(read_point, r)?.into_iter().map(DfgPoint).collect(),
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,309 +0,0 @@
|
|||||||
use std_shims::{vec::Vec, sync::OnceLock};
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use zeroize::Zeroize;
|
|
||||||
|
|
||||||
use curve25519_dalek::{scalar::Scalar as DalekScalar, edwards::EdwardsPoint as DalekPoint};
|
|
||||||
|
|
||||||
use group::{ff::Field, Group};
|
|
||||||
use dalek_ff_group::{ED25519_BASEPOINT_POINT as G, Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use multiexp::BatchVerifier;
|
|
||||||
|
|
||||||
use crate::{Commitment, ringct::bulletproofs::core::*};
|
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/generators.rs"));
|
|
||||||
|
|
||||||
static IP12_CELL: OnceLock<Scalar> = OnceLock::new();
|
|
||||||
pub(crate) fn IP12() -> Scalar {
|
|
||||||
*IP12_CELL.get_or_init(|| inner_product(&ScalarVector(vec![Scalar::ONE; N]), TWO_N()))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct OriginalStruct {
|
|
||||||
pub(crate) A: DalekPoint,
|
|
||||||
pub(crate) S: DalekPoint,
|
|
||||||
pub(crate) T1: DalekPoint,
|
|
||||||
pub(crate) T2: DalekPoint,
|
|
||||||
pub(crate) taux: DalekScalar,
|
|
||||||
pub(crate) mu: DalekScalar,
|
|
||||||
pub(crate) L: Vec<DalekPoint>,
|
|
||||||
pub(crate) R: Vec<DalekPoint>,
|
|
||||||
pub(crate) a: DalekScalar,
|
|
||||||
pub(crate) b: DalekScalar,
|
|
||||||
pub(crate) t: DalekScalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OriginalStruct {
|
|
||||||
pub(crate) fn prove<R: RngCore + CryptoRng>(
|
|
||||||
rng: &mut R,
|
|
||||||
commitments: &[Commitment],
|
|
||||||
) -> OriginalStruct {
|
|
||||||
let (logMN, M, MN) = MN(commitments.len());
|
|
||||||
|
|
||||||
let (aL, aR) = bit_decompose(commitments);
|
|
||||||
let commitments_points = commitments.iter().map(Commitment::calculate).collect::<Vec<_>>();
|
|
||||||
let (mut cache, _) = hash_commitments(commitments_points.clone());
|
|
||||||
|
|
||||||
let (sL, sR) =
|
|
||||||
ScalarVector((0 .. (MN * 2)).map(|_| Scalar::random(&mut *rng)).collect::<Vec<_>>()).split();
|
|
||||||
|
|
||||||
let generators = GENERATORS();
|
|
||||||
let (mut alpha, A) = alpha_rho(&mut *rng, generators, &aL, &aR);
|
|
||||||
let (mut rho, S) = alpha_rho(&mut *rng, generators, &sL, &sR);
|
|
||||||
|
|
||||||
let y = hash_cache(&mut cache, &[A.compress().to_bytes(), S.compress().to_bytes()]);
|
|
||||||
let mut cache = hash_to_scalar(&y.to_bytes());
|
|
||||||
let z = cache;
|
|
||||||
|
|
||||||
let l0 = &aL - z;
|
|
||||||
let l1 = sL;
|
|
||||||
|
|
||||||
let mut zero_twos = Vec::with_capacity(MN);
|
|
||||||
let zpow = ScalarVector::powers(z, M + 2);
|
|
||||||
for j in 0 .. M {
|
|
||||||
for i in 0 .. N {
|
|
||||||
zero_twos.push(zpow[j + 2] * TWO_N()[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let yMN = ScalarVector::powers(y, MN);
|
|
||||||
let r0 = (&(aR + z) * &yMN) + ScalarVector(zero_twos);
|
|
||||||
let r1 = yMN * sR;
|
|
||||||
|
|
||||||
let (T1, T2, x, mut taux) = {
|
|
||||||
let t1 = inner_product(&l0, &r1) + inner_product(&l1, &r0);
|
|
||||||
let t2 = inner_product(&l1, &r1);
|
|
||||||
|
|
||||||
let mut tau1 = Scalar::random(&mut *rng);
|
|
||||||
let mut tau2 = Scalar::random(&mut *rng);
|
|
||||||
|
|
||||||
let T1 = prove_multiexp(&[(t1, H()), (tau1, EdwardsPoint::generator())]);
|
|
||||||
let T2 = prove_multiexp(&[(t2, H()), (tau2, EdwardsPoint::generator())]);
|
|
||||||
|
|
||||||
let x =
|
|
||||||
hash_cache(&mut cache, &[z.to_bytes(), T1.compress().to_bytes(), T2.compress().to_bytes()]);
|
|
||||||
|
|
||||||
let taux = (tau2 * (x * x)) + (tau1 * x);
|
|
||||||
|
|
||||||
tau1.zeroize();
|
|
||||||
tau2.zeroize();
|
|
||||||
(T1, T2, x, taux)
|
|
||||||
};
|
|
||||||
|
|
||||||
let mu = (x * rho) + alpha;
|
|
||||||
alpha.zeroize();
|
|
||||||
rho.zeroize();
|
|
||||||
|
|
||||||
for (i, gamma) in commitments.iter().map(|c| Scalar(c.mask)).enumerate() {
|
|
||||||
taux += zpow[i + 2] * gamma;
|
|
||||||
}
|
|
||||||
|
|
||||||
let l = &l0 + &(l1 * x);
|
|
||||||
let r = &r0 + &(r1 * x);
|
|
||||||
|
|
||||||
let t = inner_product(&l, &r);
|
|
||||||
|
|
||||||
let x_ip =
|
|
||||||
hash_cache(&mut cache, &[x.to_bytes(), taux.to_bytes(), mu.to_bytes(), t.to_bytes()]);
|
|
||||||
|
|
||||||
let mut a = l;
|
|
||||||
let mut b = r;
|
|
||||||
|
|
||||||
let yinv = y.invert().unwrap();
|
|
||||||
let yinvpow = ScalarVector::powers(yinv, MN);
|
|
||||||
|
|
||||||
let mut G_proof = generators.G[.. a.len()].to_vec();
|
|
||||||
let mut H_proof = generators.H[.. a.len()].to_vec();
|
|
||||||
H_proof.iter_mut().zip(yinvpow.0.iter()).for_each(|(this_H, yinvpow)| *this_H *= yinvpow);
|
|
||||||
let U = H() * x_ip;
|
|
||||||
|
|
||||||
let mut L = Vec::with_capacity(logMN);
|
|
||||||
let mut R = Vec::with_capacity(logMN);
|
|
||||||
|
|
||||||
while a.len() != 1 {
|
|
||||||
let (aL, aR) = a.split();
|
|
||||||
let (bL, bR) = b.split();
|
|
||||||
|
|
||||||
let cL = inner_product(&aL, &bR);
|
|
||||||
let cR = inner_product(&aR, &bL);
|
|
||||||
|
|
||||||
let (G_L, G_R) = G_proof.split_at(aL.len());
|
|
||||||
let (H_L, H_R) = H_proof.split_at(aL.len());
|
|
||||||
|
|
||||||
let L_i = prove_multiexp(&LR_statements(&aL, G_R, &bR, H_L, cL, U));
|
|
||||||
let R_i = prove_multiexp(&LR_statements(&aR, G_L, &bL, H_R, cR, U));
|
|
||||||
L.push(L_i);
|
|
||||||
R.push(R_i);
|
|
||||||
|
|
||||||
let w = hash_cache(&mut cache, &[L_i.compress().to_bytes(), R_i.compress().to_bytes()]);
|
|
||||||
let winv = w.invert().unwrap();
|
|
||||||
|
|
||||||
a = (aL * w) + (aR * winv);
|
|
||||||
b = (bL * winv) + (bR * w);
|
|
||||||
|
|
||||||
if a.len() != 1 {
|
|
||||||
G_proof = hadamard_fold(G_L, G_R, winv, w);
|
|
||||||
H_proof = hadamard_fold(H_L, H_R, w, winv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let res = OriginalStruct {
|
|
||||||
A: *A,
|
|
||||||
S: *S,
|
|
||||||
T1: *T1,
|
|
||||||
T2: *T2,
|
|
||||||
taux: *taux,
|
|
||||||
mu: *mu,
|
|
||||||
L: L.drain(..).map(|L| *L).collect(),
|
|
||||||
R: R.drain(..).map(|R| *R).collect(),
|
|
||||||
a: *a[0],
|
|
||||||
b: *b[0],
|
|
||||||
t: *t,
|
|
||||||
};
|
|
||||||
debug_assert!(res.verify(rng, &commitments_points));
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
fn verify_core<ID: Copy + Zeroize, R: RngCore + CryptoRng>(
|
|
||||||
&self,
|
|
||||||
rng: &mut R,
|
|
||||||
verifier: &mut BatchVerifier<ID, EdwardsPoint>,
|
|
||||||
id: ID,
|
|
||||||
commitments: &[DalekPoint],
|
|
||||||
) -> bool {
|
|
||||||
// Verify commitments are valid
|
|
||||||
if commitments.is_empty() || (commitments.len() > MAX_M) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify L and R are properly sized
|
|
||||||
if self.L.len() != self.R.len() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let (logMN, M, MN) = MN(commitments.len());
|
|
||||||
if self.L.len() != logMN {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rebuild all challenges
|
|
||||||
let (mut cache, commitments) = hash_commitments(commitments.iter().copied());
|
|
||||||
let y = hash_cache(&mut cache, &[self.A.compress().to_bytes(), self.S.compress().to_bytes()]);
|
|
||||||
|
|
||||||
let z = hash_to_scalar(&y.to_bytes());
|
|
||||||
cache = z;
|
|
||||||
|
|
||||||
let x = hash_cache(
|
|
||||||
&mut cache,
|
|
||||||
&[z.to_bytes(), self.T1.compress().to_bytes(), self.T2.compress().to_bytes()],
|
|
||||||
);
|
|
||||||
|
|
||||||
let x_ip = hash_cache(
|
|
||||||
&mut cache,
|
|
||||||
&[x.to_bytes(), self.taux.to_bytes(), self.mu.to_bytes(), self.t.to_bytes()],
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut w = Vec::with_capacity(logMN);
|
|
||||||
let mut winv = Vec::with_capacity(logMN);
|
|
||||||
for (L, R) in self.L.iter().zip(&self.R) {
|
|
||||||
w.push(hash_cache(&mut cache, &[L.compress().to_bytes(), R.compress().to_bytes()]));
|
|
||||||
winv.push(cache.invert().unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert the proof from * INV_EIGHT to its actual form
|
|
||||||
let normalize = |point: &DalekPoint| EdwardsPoint(point.mul_by_cofactor());
|
|
||||||
|
|
||||||
let L = self.L.iter().map(normalize).collect::<Vec<_>>();
|
|
||||||
let R = self.R.iter().map(normalize).collect::<Vec<_>>();
|
|
||||||
let T1 = normalize(&self.T1);
|
|
||||||
let T2 = normalize(&self.T2);
|
|
||||||
let A = normalize(&self.A);
|
|
||||||
let S = normalize(&self.S);
|
|
||||||
|
|
||||||
let commitments = commitments.iter().map(EdwardsPoint::mul_by_cofactor).collect::<Vec<_>>();
|
|
||||||
|
|
||||||
// Verify it
|
|
||||||
let mut proof = Vec::with_capacity(4 + commitments.len());
|
|
||||||
|
|
||||||
let zpow = ScalarVector::powers(z, M + 3);
|
|
||||||
let ip1y = ScalarVector::powers(y, M * N).sum();
|
|
||||||
let mut k = -(zpow[2] * ip1y);
|
|
||||||
for j in 1 ..= M {
|
|
||||||
k -= zpow[j + 2] * IP12();
|
|
||||||
}
|
|
||||||
let y1 = Scalar(self.t) - ((z * ip1y) + k);
|
|
||||||
proof.push((-y1, H()));
|
|
||||||
|
|
||||||
proof.push((-Scalar(self.taux), G));
|
|
||||||
|
|
||||||
for (j, commitment) in commitments.iter().enumerate() {
|
|
||||||
proof.push((zpow[j + 2], *commitment));
|
|
||||||
}
|
|
||||||
|
|
||||||
proof.push((x, T1));
|
|
||||||
proof.push((x * x, T2));
|
|
||||||
verifier.queue(&mut *rng, id, proof);
|
|
||||||
|
|
||||||
proof = Vec::with_capacity(4 + (2 * (MN + logMN)));
|
|
||||||
let z3 = (Scalar(self.t) - (Scalar(self.a) * Scalar(self.b))) * x_ip;
|
|
||||||
proof.push((z3, H()));
|
|
||||||
proof.push((-Scalar(self.mu), G));
|
|
||||||
|
|
||||||
proof.push((Scalar::ONE, A));
|
|
||||||
proof.push((x, S));
|
|
||||||
|
|
||||||
{
|
|
||||||
let ypow = ScalarVector::powers(y, MN);
|
|
||||||
let yinv = y.invert().unwrap();
|
|
||||||
let yinvpow = ScalarVector::powers(yinv, MN);
|
|
||||||
|
|
||||||
let w_cache = challenge_products(&w, &winv);
|
|
||||||
|
|
||||||
let generators = GENERATORS();
|
|
||||||
for i in 0 .. MN {
|
|
||||||
let g = (Scalar(self.a) * w_cache[i]) + z;
|
|
||||||
proof.push((-g, generators.G[i]));
|
|
||||||
|
|
||||||
let mut h = Scalar(self.b) * yinvpow[i] * w_cache[(!i) & (MN - 1)];
|
|
||||||
h -= ((zpow[(i / N) + 2] * TWO_N()[i % N]) + (z * ypow[i])) * yinvpow[i];
|
|
||||||
proof.push((-h, generators.H[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0 .. logMN {
|
|
||||||
proof.push((w[i] * w[i], L[i]));
|
|
||||||
proof.push((winv[i] * winv[i], R[i]));
|
|
||||||
}
|
|
||||||
verifier.queue(rng, id, proof);
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub(crate) fn verify<R: RngCore + CryptoRng>(
|
|
||||||
&self,
|
|
||||||
rng: &mut R,
|
|
||||||
commitments: &[DalekPoint],
|
|
||||||
) -> bool {
|
|
||||||
let mut verifier = BatchVerifier::new(1);
|
|
||||||
if self.verify_core(rng, &mut verifier, (), commitments) {
|
|
||||||
verifier.verify_vartime()
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub(crate) fn batch_verify<ID: Copy + Zeroize, R: RngCore + CryptoRng>(
|
|
||||||
&self,
|
|
||||||
rng: &mut R,
|
|
||||||
verifier: &mut BatchVerifier<ID, EdwardsPoint>,
|
|
||||||
id: ID,
|
|
||||||
commitments: &[DalekPoint],
|
|
||||||
) -> bool {
|
|
||||||
self.verify_core(rng, verifier, id, commitments)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,249 +0,0 @@
|
|||||||
use std_shims::vec::Vec;
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
|
|
||||||
|
|
||||||
use multiexp::{multiexp, multiexp_vartime, BatchVerifier};
|
|
||||||
use group::{
|
|
||||||
ff::{Field, PrimeField},
|
|
||||||
Group, GroupEncoding,
|
|
||||||
};
|
|
||||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Commitment,
|
|
||||||
ringct::{
|
|
||||||
bulletproofs::core::{MAX_M, N},
|
|
||||||
bulletproofs::plus::{
|
|
||||||
ScalarVector, PointVector, GeneratorsList, Generators,
|
|
||||||
transcript::*,
|
|
||||||
weighted_inner_product::{WipStatement, WipWitness, WipProof},
|
|
||||||
padded_pow_of_2, u64_decompose,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Figure 3
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub(crate) struct AggregateRangeStatement {
|
|
||||||
generators: Generators,
|
|
||||||
V: Vec<EdwardsPoint>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Zeroize for AggregateRangeStatement {
|
|
||||||
fn zeroize(&mut self) {
|
|
||||||
self.V.zeroize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub(crate) struct AggregateRangeWitness {
|
|
||||||
values: Vec<u64>,
|
|
||||||
gammas: Vec<Scalar>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AggregateRangeWitness {
|
|
||||||
pub(crate) fn new(commitments: &[Commitment]) -> Option<Self> {
|
|
||||||
if commitments.is_empty() || (commitments.len() > MAX_M) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut values = Vec::with_capacity(commitments.len());
|
|
||||||
let mut gammas = Vec::with_capacity(commitments.len());
|
|
||||||
for commitment in commitments {
|
|
||||||
values.push(commitment.amount);
|
|
||||||
gammas.push(Scalar(commitment.mask));
|
|
||||||
}
|
|
||||||
Some(AggregateRangeWitness { values, gammas })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub struct AggregateRangeProof {
|
|
||||||
pub(crate) A: EdwardsPoint,
|
|
||||||
pub(crate) wip: WipProof,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AggregateRangeStatement {
|
|
||||||
pub(crate) fn new(V: Vec<EdwardsPoint>) -> Option<Self> {
|
|
||||||
if V.is_empty() || (V.len() > MAX_M) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Self { generators: Generators::new(), V })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn transcript_A(transcript: &mut Scalar, A: EdwardsPoint) -> (Scalar, Scalar) {
|
|
||||||
let y = hash_to_scalar(&[transcript.to_repr().as_ref(), A.to_bytes().as_ref()].concat());
|
|
||||||
let z = hash_to_scalar(y.to_bytes().as_ref());
|
|
||||||
*transcript = z;
|
|
||||||
(y, z)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn d_j(j: usize, m: usize) -> ScalarVector {
|
|
||||||
let mut d_j = Vec::with_capacity(m * N);
|
|
||||||
for _ in 0 .. (j - 1) * N {
|
|
||||||
d_j.push(Scalar::ZERO);
|
|
||||||
}
|
|
||||||
d_j.append(&mut ScalarVector::powers(Scalar::from(2u8), N).0);
|
|
||||||
for _ in 0 .. (m - j) * N {
|
|
||||||
d_j.push(Scalar::ZERO);
|
|
||||||
}
|
|
||||||
ScalarVector(d_j)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compute_A_hat(
|
|
||||||
mut V: PointVector,
|
|
||||||
generators: &Generators,
|
|
||||||
transcript: &mut Scalar,
|
|
||||||
mut A: EdwardsPoint,
|
|
||||||
) -> (Scalar, ScalarVector, Scalar, Scalar, ScalarVector, EdwardsPoint) {
|
|
||||||
let (y, z) = Self::transcript_A(transcript, A);
|
|
||||||
A = A.mul_by_cofactor();
|
|
||||||
|
|
||||||
while V.len() < padded_pow_of_2(V.len()) {
|
|
||||||
V.0.push(EdwardsPoint::identity());
|
|
||||||
}
|
|
||||||
let mn = V.len() * N;
|
|
||||||
|
|
||||||
let mut z_pow = Vec::with_capacity(V.len());
|
|
||||||
|
|
||||||
let mut d = ScalarVector::new(mn);
|
|
||||||
for j in 1 ..= V.len() {
|
|
||||||
z_pow.push(z.pow(Scalar::from(2 * u64::try_from(j).unwrap()))); // TODO: Optimize this
|
|
||||||
d = d.add_vec(&Self::d_j(j, V.len()).mul(z_pow[j - 1]));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut ascending_y = ScalarVector(vec![y]);
|
|
||||||
for i in 1 .. d.len() {
|
|
||||||
ascending_y.0.push(ascending_y[i - 1] * y);
|
|
||||||
}
|
|
||||||
let y_pows = ascending_y.clone().sum();
|
|
||||||
|
|
||||||
let mut descending_y = ascending_y.clone();
|
|
||||||
descending_y.0.reverse();
|
|
||||||
|
|
||||||
let d_descending_y = d.mul_vec(&descending_y);
|
|
||||||
|
|
||||||
let y_mn_plus_one = descending_y[0] * y;
|
|
||||||
|
|
||||||
let mut commitment_accum = EdwardsPoint::identity();
|
|
||||||
for (j, commitment) in V.0.iter().enumerate() {
|
|
||||||
commitment_accum += *commitment * z_pow[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
let neg_z = -z;
|
|
||||||
let mut A_terms = Vec::with_capacity((generators.len() * 2) + 2);
|
|
||||||
for (i, d_y_z) in d_descending_y.add(z).0.drain(..).enumerate() {
|
|
||||||
A_terms.push((neg_z, generators.generator(GeneratorsList::GBold1, i)));
|
|
||||||
A_terms.push((d_y_z, generators.generator(GeneratorsList::HBold1, i)));
|
|
||||||
}
|
|
||||||
A_terms.push((y_mn_plus_one, commitment_accum));
|
|
||||||
A_terms.push((
|
|
||||||
((y_pows * z) - (d.sum() * y_mn_plus_one * z) - (y_pows * z.square())),
|
|
||||||
Generators::g(),
|
|
||||||
));
|
|
||||||
|
|
||||||
(y, d_descending_y, y_mn_plus_one, z, ScalarVector(z_pow), A + multiexp_vartime(&A_terms))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn prove<R: RngCore + CryptoRng>(
|
|
||||||
self,
|
|
||||||
rng: &mut R,
|
|
||||||
witness: &AggregateRangeWitness,
|
|
||||||
) -> Option<AggregateRangeProof> {
|
|
||||||
// Check for consistency with the witness
|
|
||||||
if self.V.len() != witness.values.len() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
for (commitment, (value, gamma)) in
|
|
||||||
self.V.iter().zip(witness.values.iter().zip(witness.gammas.iter()))
|
|
||||||
{
|
|
||||||
if Commitment::new(**gamma, *value).calculate() != **commitment {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let Self { generators, V } = self;
|
|
||||||
// Monero expects all of these points to be torsion-free
|
|
||||||
// Generally, for Bulletproofs, it sends points * INV_EIGHT and then performs a torsion clear
|
|
||||||
// by multiplying by 8
|
|
||||||
// This also restores the original value due to the preprocessing
|
|
||||||
// Commitments aren't transmitted INV_EIGHT though, so this multiplies by INV_EIGHT to enable
|
|
||||||
// clearing its cofactor without mutating the value
|
|
||||||
// For some reason, these values are transcripted * INV_EIGHT, not as transmitted
|
|
||||||
let mut V = V.into_iter().map(|V| EdwardsPoint(V.0 * crate::INV_EIGHT())).collect::<Vec<_>>();
|
|
||||||
let mut transcript = initial_transcript(V.iter());
|
|
||||||
V.iter_mut().for_each(|V| *V = V.mul_by_cofactor());
|
|
||||||
|
|
||||||
// Pad V
|
|
||||||
while V.len() < padded_pow_of_2(V.len()) {
|
|
||||||
V.push(EdwardsPoint::identity());
|
|
||||||
}
|
|
||||||
|
|
||||||
let generators = generators.reduce(V.len() * N);
|
|
||||||
|
|
||||||
let mut d_js = Vec::with_capacity(V.len());
|
|
||||||
let mut a_l = ScalarVector(Vec::with_capacity(V.len() * N));
|
|
||||||
for j in 1 ..= V.len() {
|
|
||||||
d_js.push(Self::d_j(j, V.len()));
|
|
||||||
a_l.0.append(&mut u64_decompose(*witness.values.get(j - 1).unwrap_or(&0)).0);
|
|
||||||
}
|
|
||||||
|
|
||||||
let a_r = a_l.sub(Scalar::ONE);
|
|
||||||
|
|
||||||
let alpha = Scalar::random(&mut *rng);
|
|
||||||
|
|
||||||
let mut A_terms = Vec::with_capacity((generators.len() * 2) + 1);
|
|
||||||
for (i, a_l) in a_l.0.iter().enumerate() {
|
|
||||||
A_terms.push((*a_l, generators.generator(GeneratorsList::GBold1, i)));
|
|
||||||
}
|
|
||||||
for (i, a_r) in a_r.0.iter().enumerate() {
|
|
||||||
A_terms.push((*a_r, generators.generator(GeneratorsList::HBold1, i)));
|
|
||||||
}
|
|
||||||
A_terms.push((alpha, Generators::h()));
|
|
||||||
let mut A = multiexp(&A_terms);
|
|
||||||
A_terms.zeroize();
|
|
||||||
|
|
||||||
// Multiply by INV_EIGHT per earlier commentary
|
|
||||||
A.0 *= crate::INV_EIGHT();
|
|
||||||
|
|
||||||
let (y, d_descending_y, y_mn_plus_one, z, z_pow, A_hat) =
|
|
||||||
Self::compute_A_hat(PointVector(V), &generators, &mut transcript, A);
|
|
||||||
|
|
||||||
let a_l = a_l.sub(z);
|
|
||||||
let a_r = a_r.add_vec(&d_descending_y).add(z);
|
|
||||||
let mut alpha = alpha;
|
|
||||||
for j in 1 ..= witness.gammas.len() {
|
|
||||||
alpha += z_pow[j - 1] * witness.gammas[j - 1] * y_mn_plus_one;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(AggregateRangeProof {
|
|
||||||
A,
|
|
||||||
wip: WipStatement::new(generators, A_hat, y)
|
|
||||||
.prove(rng, transcript, &Zeroizing::new(WipWitness::new(a_l, a_r, alpha).unwrap()))
|
|
||||||
.unwrap(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn verify<Id: Copy + Zeroize, R: RngCore + CryptoRng>(
|
|
||||||
self,
|
|
||||||
rng: &mut R,
|
|
||||||
verifier: &mut BatchVerifier<Id, EdwardsPoint>,
|
|
||||||
id: Id,
|
|
||||||
proof: AggregateRangeProof,
|
|
||||||
) -> bool {
|
|
||||||
let Self { generators, V } = self;
|
|
||||||
|
|
||||||
let mut V = V.into_iter().map(|V| EdwardsPoint(V.0 * crate::INV_EIGHT())).collect::<Vec<_>>();
|
|
||||||
let mut transcript = initial_transcript(V.iter());
|
|
||||||
V.iter_mut().for_each(|V| *V = V.mul_by_cofactor());
|
|
||||||
|
|
||||||
let generators = generators.reduce(V.len() * N);
|
|
||||||
|
|
||||||
let (y, _, _, _, _, A_hat) =
|
|
||||||
Self::compute_A_hat(PointVector(V), &generators, &mut transcript, proof.A);
|
|
||||||
WipStatement::new(generators, A_hat, y).verify(rng, verifier, id, transcript, proof.wip)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
#![allow(non_snake_case)]
|
|
||||||
|
|
||||||
use group::Group;
|
|
||||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
mod scalar_vector;
|
|
||||||
pub(crate) use scalar_vector::{ScalarVector, weighted_inner_product};
|
|
||||||
mod point_vector;
|
|
||||||
pub(crate) use point_vector::PointVector;
|
|
||||||
|
|
||||||
pub(crate) mod transcript;
|
|
||||||
pub(crate) mod weighted_inner_product;
|
|
||||||
pub(crate) use weighted_inner_product::*;
|
|
||||||
pub(crate) mod aggregate_range_proof;
|
|
||||||
pub(crate) use aggregate_range_proof::*;
|
|
||||||
|
|
||||||
pub(crate) fn padded_pow_of_2(i: usize) -> usize {
|
|
||||||
let mut next_pow_of_2 = 1;
|
|
||||||
while next_pow_of_2 < i {
|
|
||||||
next_pow_of_2 <<= 1;
|
|
||||||
}
|
|
||||||
next_pow_of_2
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
|
||||||
pub(crate) enum GeneratorsList {
|
|
||||||
GBold1,
|
|
||||||
HBold1,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Table these
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub(crate) struct Generators {
|
|
||||||
g_bold1: &'static [EdwardsPoint],
|
|
||||||
h_bold1: &'static [EdwardsPoint],
|
|
||||||
}
|
|
||||||
|
|
||||||
mod generators {
|
|
||||||
use std_shims::sync::OnceLock;
|
|
||||||
use monero_generators::Generators;
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/generators_plus.rs"));
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Generators {
|
|
||||||
#[allow(clippy::new_without_default)]
|
|
||||||
pub(crate) fn new() -> Self {
|
|
||||||
let gens = generators::GENERATORS();
|
|
||||||
Generators { g_bold1: &gens.G, h_bold1: &gens.H }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn len(&self) -> usize {
|
|
||||||
self.g_bold1.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn g() -> EdwardsPoint {
|
|
||||||
dalek_ff_group::EdwardsPoint(crate::H())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn h() -> EdwardsPoint {
|
|
||||||
EdwardsPoint::generator()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn generator(&self, list: GeneratorsList, i: usize) -> EdwardsPoint {
|
|
||||||
match list {
|
|
||||||
GeneratorsList::GBold1 => self.g_bold1[i],
|
|
||||||
GeneratorsList::HBold1 => self.h_bold1[i],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn reduce(&self, generators: usize) -> Self {
|
|
||||||
// Round to the nearest power of 2
|
|
||||||
let generators = padded_pow_of_2(generators);
|
|
||||||
assert!(generators <= self.g_bold1.len());
|
|
||||||
|
|
||||||
Generators { g_bold1: &self.g_bold1[.. generators], h_bold1: &self.h_bold1[.. generators] }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the little-endian decomposition.
|
|
||||||
fn u64_decompose(value: u64) -> ScalarVector {
|
|
||||||
let mut bits = ScalarVector::new(64);
|
|
||||||
for bit in 0 .. 64 {
|
|
||||||
bits[bit] = Scalar::from((value >> bit) & 1);
|
|
||||||
}
|
|
||||||
bits
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
use core::ops::{Index, IndexMut};
|
|
||||||
use std_shims::vec::Vec;
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop};
|
|
||||||
|
|
||||||
use dalek_ff_group::EdwardsPoint;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
use multiexp::multiexp;
|
|
||||||
#[cfg(test)]
|
|
||||||
use crate::ringct::bulletproofs::plus::ScalarVector;
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub(crate) struct PointVector(pub(crate) Vec<EdwardsPoint>);
|
|
||||||
|
|
||||||
impl Index<usize> for PointVector {
|
|
||||||
type Output = EdwardsPoint;
|
|
||||||
fn index(&self, index: usize) -> &EdwardsPoint {
|
|
||||||
&self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IndexMut<usize> for PointVector {
|
|
||||||
fn index_mut(&mut self, index: usize) -> &mut EdwardsPoint {
|
|
||||||
&mut self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PointVector {
|
|
||||||
#[cfg(test)]
|
|
||||||
pub(crate) fn multiexp(&self, vector: &ScalarVector) -> EdwardsPoint {
|
|
||||||
debug_assert_eq!(self.len(), vector.len());
|
|
||||||
let mut res = Vec::with_capacity(self.len());
|
|
||||||
for (point, scalar) in self.0.iter().copied().zip(vector.0.iter().copied()) {
|
|
||||||
res.push((scalar, point));
|
|
||||||
}
|
|
||||||
multiexp(&res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn len(&self) -> usize {
|
|
||||||
self.0.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn split(mut self) -> (Self, Self) {
|
|
||||||
debug_assert!(self.len() > 1);
|
|
||||||
let r = self.0.split_off(self.0.len() / 2);
|
|
||||||
debug_assert_eq!(self.len(), r.len());
|
|
||||||
(self, PointVector(r))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
use core::{
|
|
||||||
borrow::Borrow,
|
|
||||||
ops::{Index, IndexMut},
|
|
||||||
};
|
|
||||||
use std_shims::vec::Vec;
|
|
||||||
|
|
||||||
use zeroize::Zeroize;
|
|
||||||
|
|
||||||
use group::ff::Field;
|
|
||||||
use dalek_ff_group::Scalar;
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub(crate) struct ScalarVector(pub(crate) Vec<Scalar>);
|
|
||||||
|
|
||||||
impl Index<usize> for ScalarVector {
|
|
||||||
type Output = Scalar;
|
|
||||||
fn index(&self, index: usize) -> &Scalar {
|
|
||||||
&self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IndexMut<usize> for ScalarVector {
|
|
||||||
fn index_mut(&mut self, index: usize) -> &mut Scalar {
|
|
||||||
&mut self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ScalarVector {
|
|
||||||
pub(crate) fn new(len: usize) -> Self {
|
|
||||||
ScalarVector(vec![Scalar::ZERO; len])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add(&self, scalar: impl Borrow<Scalar>) -> Self {
|
|
||||||
let mut res = self.clone();
|
|
||||||
for val in &mut res.0 {
|
|
||||||
*val += scalar.borrow();
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn sub(&self, scalar: impl Borrow<Scalar>) -> Self {
|
|
||||||
let mut res = self.clone();
|
|
||||||
for val in &mut res.0 {
|
|
||||||
*val -= scalar.borrow();
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn mul(&self, scalar: impl Borrow<Scalar>) -> Self {
|
|
||||||
let mut res = self.clone();
|
|
||||||
for val in &mut res.0 {
|
|
||||||
*val *= scalar.borrow();
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add_vec(&self, vector: &Self) -> Self {
|
|
||||||
debug_assert_eq!(self.len(), vector.len());
|
|
||||||
let mut res = self.clone();
|
|
||||||
for (i, val) in res.0.iter_mut().enumerate() {
|
|
||||||
*val += vector.0[i];
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn mul_vec(&self, vector: &Self) -> Self {
|
|
||||||
debug_assert_eq!(self.len(), vector.len());
|
|
||||||
let mut res = self.clone();
|
|
||||||
for (i, val) in res.0.iter_mut().enumerate() {
|
|
||||||
*val *= vector.0[i];
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn inner_product(&self, vector: &Self) -> Scalar {
|
|
||||||
self.mul_vec(vector).sum()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn powers(x: Scalar, len: usize) -> Self {
|
|
||||||
debug_assert!(len != 0);
|
|
||||||
|
|
||||||
let mut res = Vec::with_capacity(len);
|
|
||||||
res.push(Scalar::ONE);
|
|
||||||
res.push(x);
|
|
||||||
for i in 2 .. len {
|
|
||||||
res.push(res[i - 1] * x);
|
|
||||||
}
|
|
||||||
res.truncate(len);
|
|
||||||
ScalarVector(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn sum(mut self) -> Scalar {
|
|
||||||
self.0.drain(..).sum()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn len(&self) -> usize {
|
|
||||||
self.0.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn split(mut self) -> (Self, Self) {
|
|
||||||
debug_assert!(self.len() > 1);
|
|
||||||
let r = self.0.split_off(self.0.len() / 2);
|
|
||||||
debug_assert_eq!(self.len(), r.len());
|
|
||||||
(self, ScalarVector(r))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn weighted_inner_product(
|
|
||||||
a: &ScalarVector,
|
|
||||||
b: &ScalarVector,
|
|
||||||
y: &ScalarVector,
|
|
||||||
) -> Scalar {
|
|
||||||
a.inner_product(&b.mul_vec(y))
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
use std_shims::{sync::OnceLock, vec::Vec};
|
|
||||||
|
|
||||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use monero_generators::{hash_to_point as raw_hash_to_point};
|
|
||||||
use crate::{hash, hash_to_scalar as dalek_hash};
|
|
||||||
|
|
||||||
// Monero starts BP+ transcripts with the following constant.
|
|
||||||
static TRANSCRIPT_CELL: OnceLock<[u8; 32]> = OnceLock::new();
|
|
||||||
pub(crate) fn TRANSCRIPT() -> [u8; 32] {
|
|
||||||
// Why this uses a hash_to_point is completely unknown.
|
|
||||||
*TRANSCRIPT_CELL
|
|
||||||
.get_or_init(|| raw_hash_to_point(hash(b"bulletproof_plus_transcript")).compress().to_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn hash_to_scalar(data: &[u8]) -> Scalar {
|
|
||||||
Scalar(dalek_hash(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn initial_transcript(commitments: core::slice::Iter<'_, EdwardsPoint>) -> Scalar {
|
|
||||||
let commitments_hash =
|
|
||||||
hash_to_scalar(&commitments.flat_map(|V| V.compress().to_bytes()).collect::<Vec<_>>());
|
|
||||||
hash_to_scalar(&[TRANSCRIPT().as_ref(), &commitments_hash.to_bytes()].concat())
|
|
||||||
}
|
|
||||||
@@ -1,447 +0,0 @@
|
|||||||
use std_shims::vec::Vec;
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop};
|
|
||||||
|
|
||||||
use multiexp::{multiexp, multiexp_vartime, BatchVerifier};
|
|
||||||
use group::{
|
|
||||||
ff::{Field, PrimeField},
|
|
||||||
GroupEncoding,
|
|
||||||
};
|
|
||||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use crate::ringct::bulletproofs::plus::{
|
|
||||||
ScalarVector, PointVector, GeneratorsList, Generators, padded_pow_of_2, weighted_inner_product,
|
|
||||||
transcript::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Figure 1
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub(crate) struct WipStatement {
|
|
||||||
generators: Generators,
|
|
||||||
P: EdwardsPoint,
|
|
||||||
y: ScalarVector,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Zeroize for WipStatement {
|
|
||||||
fn zeroize(&mut self) {
|
|
||||||
self.P.zeroize();
|
|
||||||
self.y.zeroize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub(crate) struct WipWitness {
|
|
||||||
a: ScalarVector,
|
|
||||||
b: ScalarVector,
|
|
||||||
alpha: Scalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WipWitness {
|
|
||||||
pub(crate) fn new(mut a: ScalarVector, mut b: ScalarVector, alpha: Scalar) -> Option<Self> {
|
|
||||||
if a.0.is_empty() || (a.len() != b.len()) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pad to the nearest power of 2
|
|
||||||
let missing = padded_pow_of_2(a.len()) - a.len();
|
|
||||||
a.0.reserve(missing);
|
|
||||||
b.0.reserve(missing);
|
|
||||||
for _ in 0 .. missing {
|
|
||||||
a.0.push(Scalar::ZERO);
|
|
||||||
b.0.push(Scalar::ZERO);
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Self { a, b, alpha })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub(crate) struct WipProof {
|
|
||||||
pub(crate) L: Vec<EdwardsPoint>,
|
|
||||||
pub(crate) R: Vec<EdwardsPoint>,
|
|
||||||
pub(crate) A: EdwardsPoint,
|
|
||||||
pub(crate) B: EdwardsPoint,
|
|
||||||
pub(crate) r_answer: Scalar,
|
|
||||||
pub(crate) s_answer: Scalar,
|
|
||||||
pub(crate) delta_answer: Scalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WipStatement {
|
|
||||||
pub(crate) fn new(generators: Generators, P: EdwardsPoint, y: Scalar) -> Self {
|
|
||||||
debug_assert_eq!(generators.len(), padded_pow_of_2(generators.len()));
|
|
||||||
|
|
||||||
// y ** n
|
|
||||||
let mut y_vec = ScalarVector::new(generators.len());
|
|
||||||
y_vec[0] = y;
|
|
||||||
for i in 1 .. y_vec.len() {
|
|
||||||
y_vec[i] = y_vec[i - 1] * y;
|
|
||||||
}
|
|
||||||
|
|
||||||
Self { generators, P, y: y_vec }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn transcript_L_R(transcript: &mut Scalar, L: EdwardsPoint, R: EdwardsPoint) -> Scalar {
|
|
||||||
let e = hash_to_scalar(
|
|
||||||
&[transcript.to_repr().as_ref(), L.to_bytes().as_ref(), R.to_bytes().as_ref()].concat(),
|
|
||||||
);
|
|
||||||
*transcript = e;
|
|
||||||
e
|
|
||||||
}
|
|
||||||
|
|
||||||
fn transcript_A_B(transcript: &mut Scalar, A: EdwardsPoint, B: EdwardsPoint) -> Scalar {
|
|
||||||
let e = hash_to_scalar(
|
|
||||||
&[transcript.to_repr().as_ref(), A.to_bytes().as_ref(), B.to_bytes().as_ref()].concat(),
|
|
||||||
);
|
|
||||||
*transcript = e;
|
|
||||||
e
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prover's variant of the shared code block to calculate G/H/P when n > 1
|
|
||||||
// Returns each permutation of G/H since the prover needs to do operation on each permutation
|
|
||||||
// P is dropped as it's unused in the prover's path
|
|
||||||
// TODO: It'd still probably be faster to keep in terms of the original generators, both between
|
|
||||||
// the reduced amount of group operations and the potential tabling of the generators under
|
|
||||||
// multiexp
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
fn next_G_H(
|
|
||||||
transcript: &mut Scalar,
|
|
||||||
mut g_bold1: PointVector,
|
|
||||||
mut g_bold2: PointVector,
|
|
||||||
mut h_bold1: PointVector,
|
|
||||||
mut h_bold2: PointVector,
|
|
||||||
L: EdwardsPoint,
|
|
||||||
R: EdwardsPoint,
|
|
||||||
y_inv_n_hat: Scalar,
|
|
||||||
) -> (Scalar, Scalar, Scalar, Scalar, PointVector, PointVector) {
|
|
||||||
debug_assert_eq!(g_bold1.len(), g_bold2.len());
|
|
||||||
debug_assert_eq!(h_bold1.len(), h_bold2.len());
|
|
||||||
debug_assert_eq!(g_bold1.len(), h_bold1.len());
|
|
||||||
|
|
||||||
let e = Self::transcript_L_R(transcript, L, R);
|
|
||||||
let inv_e = e.invert().unwrap();
|
|
||||||
|
|
||||||
// This vartime is safe as all of these arguments are public
|
|
||||||
let mut new_g_bold = Vec::with_capacity(g_bold1.len());
|
|
||||||
let e_y_inv = e * y_inv_n_hat;
|
|
||||||
for g_bold in g_bold1.0.drain(..).zip(g_bold2.0.drain(..)) {
|
|
||||||
new_g_bold.push(multiexp_vartime(&[(inv_e, g_bold.0), (e_y_inv, g_bold.1)]));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut new_h_bold = Vec::with_capacity(h_bold1.len());
|
|
||||||
for h_bold in h_bold1.0.drain(..).zip(h_bold2.0.drain(..)) {
|
|
||||||
new_h_bold.push(multiexp_vartime(&[(e, h_bold.0), (inv_e, h_bold.1)]));
|
|
||||||
}
|
|
||||||
|
|
||||||
let e_square = e.square();
|
|
||||||
let inv_e_square = inv_e.square();
|
|
||||||
|
|
||||||
(e, inv_e, e_square, inv_e_square, PointVector(new_g_bold), PointVector(new_h_bold))
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
This has room for optimization worth investigating further. It currently takes
|
|
||||||
an iterative approach. It can be optimized further via divide and conquer.
|
|
||||||
|
|
||||||
Assume there are 4 challenges.
|
|
||||||
|
|
||||||
Iterative approach (current):
|
|
||||||
1. Do the optimal multiplications across challenge column 0 and 1.
|
|
||||||
2. Do the optimal multiplications across that result and column 2.
|
|
||||||
3. Do the optimal multiplications across that result and column 3.
|
|
||||||
|
|
||||||
Divide and conquer (worth investigating further):
|
|
||||||
1. Do the optimal multiplications across challenge column 0 and 1.
|
|
||||||
2. Do the optimal multiplications across challenge column 2 and 3.
|
|
||||||
3. Multiply both results together.
|
|
||||||
|
|
||||||
When there are 4 challenges (n=16), the iterative approach does 28 multiplications
|
|
||||||
versus divide and conquer's 24.
|
|
||||||
*/
|
|
||||||
fn challenge_products(challenges: &[(Scalar, Scalar)]) -> Vec<Scalar> {
|
|
||||||
let mut products = vec![Scalar::ONE; 1 << challenges.len()];
|
|
||||||
|
|
||||||
if !challenges.is_empty() {
|
|
||||||
products[0] = challenges[0].1;
|
|
||||||
products[1] = challenges[0].0;
|
|
||||||
|
|
||||||
for (j, challenge) in challenges.iter().enumerate().skip(1) {
|
|
||||||
let mut slots = (1 << (j + 1)) - 1;
|
|
||||||
while slots > 0 {
|
|
||||||
products[slots] = products[slots / 2] * challenge.0;
|
|
||||||
products[slots - 1] = products[slots / 2] * challenge.1;
|
|
||||||
|
|
||||||
slots = slots.saturating_sub(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sanity check since if the above failed to populate, it'd be critical
|
|
||||||
for product in &products {
|
|
||||||
debug_assert!(!bool::from(product.is_zero()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
products
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn prove<R: RngCore + CryptoRng>(
|
|
||||||
self,
|
|
||||||
rng: &mut R,
|
|
||||||
mut transcript: Scalar,
|
|
||||||
witness: &WipWitness,
|
|
||||||
) -> Option<WipProof> {
|
|
||||||
let WipStatement { generators, P, mut y } = self;
|
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
let _ = P;
|
|
||||||
|
|
||||||
if generators.len() != witness.a.len() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let (g, h) = (Generators::g(), Generators::h());
|
|
||||||
let mut g_bold = vec![];
|
|
||||||
let mut h_bold = vec![];
|
|
||||||
for i in 0 .. generators.len() {
|
|
||||||
g_bold.push(generators.generator(GeneratorsList::GBold1, i));
|
|
||||||
h_bold.push(generators.generator(GeneratorsList::HBold1, i));
|
|
||||||
}
|
|
||||||
let mut g_bold = PointVector(g_bold);
|
|
||||||
let mut h_bold = PointVector(h_bold);
|
|
||||||
|
|
||||||
// Check P has the expected relationship
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
{
|
|
||||||
let mut P_terms = witness
|
|
||||||
.a
|
|
||||||
.0
|
|
||||||
.iter()
|
|
||||||
.copied()
|
|
||||||
.zip(g_bold.0.iter().copied())
|
|
||||||
.chain(witness.b.0.iter().copied().zip(h_bold.0.iter().copied()))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
P_terms.push((weighted_inner_product(&witness.a, &witness.b, &y), g));
|
|
||||||
P_terms.push((witness.alpha, h));
|
|
||||||
debug_assert_eq!(multiexp(&P_terms), P);
|
|
||||||
P_terms.zeroize();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut a = witness.a.clone();
|
|
||||||
let mut b = witness.b.clone();
|
|
||||||
let mut alpha = witness.alpha;
|
|
||||||
|
|
||||||
// From here on, g_bold.len() is used as n
|
|
||||||
debug_assert_eq!(g_bold.len(), a.len());
|
|
||||||
|
|
||||||
let mut L_vec = vec![];
|
|
||||||
let mut R_vec = vec![];
|
|
||||||
|
|
||||||
// else n > 1 case from figure 1
|
|
||||||
while g_bold.len() > 1 {
|
|
||||||
let (a1, a2) = a.clone().split();
|
|
||||||
let (b1, b2) = b.clone().split();
|
|
||||||
let (g_bold1, g_bold2) = g_bold.split();
|
|
||||||
let (h_bold1, h_bold2) = h_bold.split();
|
|
||||||
|
|
||||||
let n_hat = g_bold1.len();
|
|
||||||
debug_assert_eq!(a1.len(), n_hat);
|
|
||||||
debug_assert_eq!(a2.len(), n_hat);
|
|
||||||
debug_assert_eq!(b1.len(), n_hat);
|
|
||||||
debug_assert_eq!(b2.len(), n_hat);
|
|
||||||
debug_assert_eq!(g_bold1.len(), n_hat);
|
|
||||||
debug_assert_eq!(g_bold2.len(), n_hat);
|
|
||||||
debug_assert_eq!(h_bold1.len(), n_hat);
|
|
||||||
debug_assert_eq!(h_bold2.len(), n_hat);
|
|
||||||
|
|
||||||
let y_n_hat = y[n_hat - 1];
|
|
||||||
y.0.truncate(n_hat);
|
|
||||||
|
|
||||||
let d_l = Scalar::random(&mut *rng);
|
|
||||||
let d_r = Scalar::random(&mut *rng);
|
|
||||||
|
|
||||||
let c_l = weighted_inner_product(&a1, &b2, &y);
|
|
||||||
let c_r = weighted_inner_product(&(a2.mul(y_n_hat)), &b1, &y);
|
|
||||||
|
|
||||||
// TODO: Calculate these with a batch inversion
|
|
||||||
let y_inv_n_hat = y_n_hat.invert().unwrap();
|
|
||||||
|
|
||||||
let mut L_terms = a1
|
|
||||||
.mul(y_inv_n_hat)
|
|
||||||
.0
|
|
||||||
.drain(..)
|
|
||||||
.zip(g_bold2.0.iter().copied())
|
|
||||||
.chain(b2.0.iter().copied().zip(h_bold1.0.iter().copied()))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
L_terms.push((c_l, g));
|
|
||||||
L_terms.push((d_l, h));
|
|
||||||
let L = multiexp(&L_terms) * Scalar(crate::INV_EIGHT());
|
|
||||||
L_vec.push(L);
|
|
||||||
L_terms.zeroize();
|
|
||||||
|
|
||||||
let mut R_terms = a2
|
|
||||||
.mul(y_n_hat)
|
|
||||||
.0
|
|
||||||
.drain(..)
|
|
||||||
.zip(g_bold1.0.iter().copied())
|
|
||||||
.chain(b1.0.iter().copied().zip(h_bold2.0.iter().copied()))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
R_terms.push((c_r, g));
|
|
||||||
R_terms.push((d_r, h));
|
|
||||||
let R = multiexp(&R_terms) * Scalar(crate::INV_EIGHT());
|
|
||||||
R_vec.push(R);
|
|
||||||
R_terms.zeroize();
|
|
||||||
|
|
||||||
let (e, inv_e, e_square, inv_e_square);
|
|
||||||
(e, inv_e, e_square, inv_e_square, g_bold, h_bold) =
|
|
||||||
Self::next_G_H(&mut transcript, g_bold1, g_bold2, h_bold1, h_bold2, L, R, y_inv_n_hat);
|
|
||||||
|
|
||||||
a = a1.mul(e).add_vec(&a2.mul(y_n_hat * inv_e));
|
|
||||||
b = b1.mul(inv_e).add_vec(&b2.mul(e));
|
|
||||||
alpha += (d_l * e_square) + (d_r * inv_e_square);
|
|
||||||
|
|
||||||
debug_assert_eq!(g_bold.len(), a.len());
|
|
||||||
debug_assert_eq!(g_bold.len(), h_bold.len());
|
|
||||||
debug_assert_eq!(g_bold.len(), b.len());
|
|
||||||
}
|
|
||||||
|
|
||||||
// n == 1 case from figure 1
|
|
||||||
debug_assert_eq!(g_bold.len(), 1);
|
|
||||||
debug_assert_eq!(h_bold.len(), 1);
|
|
||||||
|
|
||||||
debug_assert_eq!(a.len(), 1);
|
|
||||||
debug_assert_eq!(b.len(), 1);
|
|
||||||
|
|
||||||
let r = Scalar::random(&mut *rng);
|
|
||||||
let s = Scalar::random(&mut *rng);
|
|
||||||
let delta = Scalar::random(&mut *rng);
|
|
||||||
let eta = Scalar::random(&mut *rng);
|
|
||||||
|
|
||||||
let ry = r * y[0];
|
|
||||||
|
|
||||||
let mut A_terms =
|
|
||||||
vec![(r, g_bold[0]), (s, h_bold[0]), ((ry * b[0]) + (s * y[0] * a[0]), g), (delta, h)];
|
|
||||||
let A = multiexp(&A_terms) * Scalar(crate::INV_EIGHT());
|
|
||||||
A_terms.zeroize();
|
|
||||||
|
|
||||||
let mut B_terms = vec![(ry * s, g), (eta, h)];
|
|
||||||
let B = multiexp(&B_terms) * Scalar(crate::INV_EIGHT());
|
|
||||||
B_terms.zeroize();
|
|
||||||
|
|
||||||
let e = Self::transcript_A_B(&mut transcript, A, B);
|
|
||||||
|
|
||||||
let r_answer = r + (a[0] * e);
|
|
||||||
let s_answer = s + (b[0] * e);
|
|
||||||
let delta_answer = eta + (delta * e) + (alpha * e.square());
|
|
||||||
|
|
||||||
Some(WipProof { L: L_vec, R: R_vec, A, B, r_answer, s_answer, delta_answer })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn verify<Id: Copy + Zeroize, R: RngCore + CryptoRng>(
|
|
||||||
self,
|
|
||||||
rng: &mut R,
|
|
||||||
verifier: &mut BatchVerifier<Id, EdwardsPoint>,
|
|
||||||
id: Id,
|
|
||||||
mut transcript: Scalar,
|
|
||||||
mut proof: WipProof,
|
|
||||||
) -> bool {
|
|
||||||
let WipStatement { generators, P, y } = self;
|
|
||||||
|
|
||||||
let (g, h) = (Generators::g(), Generators::h());
|
|
||||||
|
|
||||||
// Verify the L/R lengths
|
|
||||||
{
|
|
||||||
let mut lr_len = 0;
|
|
||||||
while (1 << lr_len) < generators.len() {
|
|
||||||
lr_len += 1;
|
|
||||||
}
|
|
||||||
if (proof.L.len() != lr_len) ||
|
|
||||||
(proof.R.len() != lr_len) ||
|
|
||||||
(generators.len() != (1 << lr_len))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let inv_y = {
|
|
||||||
let inv_y = y[0].invert().unwrap();
|
|
||||||
let mut res = Vec::with_capacity(y.len());
|
|
||||||
res.push(inv_y);
|
|
||||||
while res.len() < y.len() {
|
|
||||||
res.push(inv_y * res.last().unwrap());
|
|
||||||
}
|
|
||||||
res
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut P_terms = vec![(Scalar::ONE, P)];
|
|
||||||
P_terms.reserve(6 + (2 * generators.len()) + proof.L.len());
|
|
||||||
|
|
||||||
let mut challenges = Vec::with_capacity(proof.L.len());
|
|
||||||
let product_cache = {
|
|
||||||
let mut es = Vec::with_capacity(proof.L.len());
|
|
||||||
for (L, R) in proof.L.iter_mut().zip(proof.R.iter_mut()) {
|
|
||||||
es.push(Self::transcript_L_R(&mut transcript, *L, *R));
|
|
||||||
*L = L.mul_by_cofactor();
|
|
||||||
*R = R.mul_by_cofactor();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut inv_es = es.clone();
|
|
||||||
let mut scratch = vec![Scalar::ZERO; es.len()];
|
|
||||||
group::ff::BatchInverter::invert_with_external_scratch(&mut inv_es, &mut scratch);
|
|
||||||
drop(scratch);
|
|
||||||
|
|
||||||
debug_assert_eq!(es.len(), inv_es.len());
|
|
||||||
debug_assert_eq!(es.len(), proof.L.len());
|
|
||||||
debug_assert_eq!(es.len(), proof.R.len());
|
|
||||||
for ((e, inv_e), (L, R)) in
|
|
||||||
es.drain(..).zip(inv_es.drain(..)).zip(proof.L.iter().zip(proof.R.iter()))
|
|
||||||
{
|
|
||||||
debug_assert_eq!(e.invert().unwrap(), inv_e);
|
|
||||||
|
|
||||||
challenges.push((e, inv_e));
|
|
||||||
|
|
||||||
let e_square = e.square();
|
|
||||||
let inv_e_square = inv_e.square();
|
|
||||||
P_terms.push((e_square, *L));
|
|
||||||
P_terms.push((inv_e_square, *R));
|
|
||||||
}
|
|
||||||
|
|
||||||
Self::challenge_products(&challenges)
|
|
||||||
};
|
|
||||||
|
|
||||||
let e = Self::transcript_A_B(&mut transcript, proof.A, proof.B);
|
|
||||||
proof.A = proof.A.mul_by_cofactor();
|
|
||||||
proof.B = proof.B.mul_by_cofactor();
|
|
||||||
let neg_e_square = -e.square();
|
|
||||||
|
|
||||||
let mut multiexp = P_terms;
|
|
||||||
multiexp.reserve(4 + (2 * generators.len()));
|
|
||||||
for (scalar, _) in &mut multiexp {
|
|
||||||
*scalar *= neg_e_square;
|
|
||||||
}
|
|
||||||
|
|
||||||
let re = proof.r_answer * e;
|
|
||||||
for i in 0 .. generators.len() {
|
|
||||||
let mut scalar = product_cache[i] * re;
|
|
||||||
if i > 0 {
|
|
||||||
scalar *= inv_y[i - 1];
|
|
||||||
}
|
|
||||||
multiexp.push((scalar, generators.generator(GeneratorsList::GBold1, i)));
|
|
||||||
}
|
|
||||||
|
|
||||||
let se = proof.s_answer * e;
|
|
||||||
for i in 0 .. generators.len() {
|
|
||||||
multiexp.push((
|
|
||||||
se * product_cache[product_cache.len() - 1 - i],
|
|
||||||
generators.generator(GeneratorsList::HBold1, i),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
multiexp.push((-e, proof.A));
|
|
||||||
multiexp.push((proof.r_answer * y[0] * proof.s_answer, g));
|
|
||||||
multiexp.push((proof.delta_answer, h));
|
|
||||||
multiexp.push((-Scalar::ONE, proof.B));
|
|
||||||
|
|
||||||
verifier.queue(rng, id, multiexp);
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
use core::ops::{Add, Sub, Mul, Index};
|
|
||||||
use std_shims::vec::Vec;
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop};
|
|
||||||
|
|
||||||
use group::ff::Field;
|
|
||||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use multiexp::multiexp;
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub(crate) struct ScalarVector(pub(crate) Vec<Scalar>);
|
|
||||||
macro_rules! math_op {
|
|
||||||
($Op: ident, $op: ident, $f: expr) => {
|
|
||||||
#[allow(clippy::redundant_closure_call)]
|
|
||||||
impl $Op<Scalar> for ScalarVector {
|
|
||||||
type Output = ScalarVector;
|
|
||||||
fn $op(self, b: Scalar) -> ScalarVector {
|
|
||||||
ScalarVector(self.0.iter().map(|a| $f((a, &b))).collect())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::redundant_closure_call)]
|
|
||||||
impl $Op<Scalar> for &ScalarVector {
|
|
||||||
type Output = ScalarVector;
|
|
||||||
fn $op(self, b: Scalar) -> ScalarVector {
|
|
||||||
ScalarVector(self.0.iter().map(|a| $f((a, &b))).collect())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::redundant_closure_call)]
|
|
||||||
impl $Op<ScalarVector> for ScalarVector {
|
|
||||||
type Output = ScalarVector;
|
|
||||||
fn $op(self, b: ScalarVector) -> ScalarVector {
|
|
||||||
debug_assert_eq!(self.len(), b.len());
|
|
||||||
ScalarVector(self.0.iter().zip(b.0.iter()).map($f).collect())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::redundant_closure_call)]
|
|
||||||
impl $Op<&ScalarVector> for &ScalarVector {
|
|
||||||
type Output = ScalarVector;
|
|
||||||
fn $op(self, b: &ScalarVector) -> ScalarVector {
|
|
||||||
debug_assert_eq!(self.len(), b.len());
|
|
||||||
ScalarVector(self.0.iter().zip(b.0.iter()).map($f).collect())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
math_op!(Add, add, |(a, b): (&Scalar, &Scalar)| *a + *b);
|
|
||||||
math_op!(Sub, sub, |(a, b): (&Scalar, &Scalar)| *a - *b);
|
|
||||||
math_op!(Mul, mul, |(a, b): (&Scalar, &Scalar)| *a * *b);
|
|
||||||
|
|
||||||
impl ScalarVector {
|
|
||||||
pub(crate) fn new(len: usize) -> ScalarVector {
|
|
||||||
ScalarVector(vec![Scalar::ZERO; len])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn powers(x: Scalar, len: usize) -> ScalarVector {
|
|
||||||
debug_assert!(len != 0);
|
|
||||||
|
|
||||||
let mut res = Vec::with_capacity(len);
|
|
||||||
res.push(Scalar::ONE);
|
|
||||||
for i in 1 .. len {
|
|
||||||
res.push(res[i - 1] * x);
|
|
||||||
}
|
|
||||||
ScalarVector(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn sum(mut self) -> Scalar {
|
|
||||||
self.0.drain(..).sum()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn len(&self) -> usize {
|
|
||||||
self.0.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn split(self) -> (ScalarVector, ScalarVector) {
|
|
||||||
let (l, r) = self.0.split_at(self.0.len() / 2);
|
|
||||||
(ScalarVector(l.to_vec()), ScalarVector(r.to_vec()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<usize> for ScalarVector {
|
|
||||||
type Output = Scalar;
|
|
||||||
fn index(&self, index: usize) -> &Scalar {
|
|
||||||
&self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn inner_product(a: &ScalarVector, b: &ScalarVector) -> Scalar {
|
|
||||||
(a * b).sum()
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mul<&[EdwardsPoint]> for &ScalarVector {
|
|
||||||
type Output = EdwardsPoint;
|
|
||||||
fn mul(self, b: &[EdwardsPoint]) -> EdwardsPoint {
|
|
||||||
debug_assert_eq!(self.len(), b.len());
|
|
||||||
multiexp(&self.0.iter().copied().zip(b.iter().copied()).collect::<Vec<_>>())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn hadamard_fold(
|
|
||||||
l: &[EdwardsPoint],
|
|
||||||
r: &[EdwardsPoint],
|
|
||||||
a: Scalar,
|
|
||||||
b: Scalar,
|
|
||||||
) -> Vec<EdwardsPoint> {
|
|
||||||
let mut res = Vec::with_capacity(l.len() / 2);
|
|
||||||
for i in 0 .. l.len() {
|
|
||||||
res.push(multiexp(&[(a, l[i]), (b, r[i])]));
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
@@ -1,324 +0,0 @@
|
|||||||
#![allow(non_snake_case)]
|
|
||||||
|
|
||||||
use core::ops::Deref;
|
|
||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
|
|
||||||
use subtle::{ConstantTimeEq, Choice, CtOption};
|
|
||||||
|
|
||||||
use curve25519_dalek::{
|
|
||||||
constants::ED25519_BASEPOINT_TABLE,
|
|
||||||
scalar::Scalar,
|
|
||||||
traits::{IsIdentity, VartimePrecomputedMultiscalarMul},
|
|
||||||
edwards::{EdwardsPoint, VartimeEdwardsPrecomputation},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
INV_EIGHT, Commitment, random_scalar, hash_to_scalar, wallet::decoys::Decoys,
|
|
||||||
ringct::hash_to_point, serialize::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
mod multisig;
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
pub use multisig::{ClsagDetails, ClsagAddendum, ClsagMultisig};
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
pub(crate) use multisig::add_key_image_share;
|
|
||||||
|
|
||||||
/// Errors returned when CLSAG signing fails.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
#[cfg_attr(feature = "std", derive(thiserror::Error))]
|
|
||||||
pub enum ClsagError {
|
|
||||||
#[cfg_attr(feature = "std", error("internal error ({0})"))]
|
|
||||||
InternalError(&'static str),
|
|
||||||
#[cfg_attr(feature = "std", error("invalid ring"))]
|
|
||||||
InvalidRing,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid ring member (member {0}, ring size {1})"))]
|
|
||||||
InvalidRingMember(u8, u8),
|
|
||||||
#[cfg_attr(feature = "std", error("invalid commitment"))]
|
|
||||||
InvalidCommitment,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid key image"))]
|
|
||||||
InvalidImage,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid D"))]
|
|
||||||
InvalidD,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid s"))]
|
|
||||||
InvalidS,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid c1"))]
|
|
||||||
InvalidC1,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Input being signed for.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct ClsagInput {
|
|
||||||
// The actual commitment for the true spend
|
|
||||||
pub(crate) commitment: Commitment,
|
|
||||||
// True spend index, offsets, and ring
|
|
||||||
pub(crate) decoys: Decoys,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ClsagInput {
|
|
||||||
pub fn new(commitment: Commitment, decoys: Decoys) -> Result<ClsagInput, ClsagError> {
|
|
||||||
let n = decoys.len();
|
|
||||||
if n > u8::MAX.into() {
|
|
||||||
Err(ClsagError::InternalError("max ring size in this library is u8 max"))?;
|
|
||||||
}
|
|
||||||
let n = u8::try_from(n).unwrap();
|
|
||||||
if decoys.i >= n {
|
|
||||||
Err(ClsagError::InvalidRingMember(decoys.i, n))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate the commitment matches
|
|
||||||
if decoys.ring[usize::from(decoys.i)][1] != commitment.calculate() {
|
|
||||||
Err(ClsagError::InvalidCommitment)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(ClsagInput { commitment, decoys })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::large_enum_variant)]
|
|
||||||
enum Mode {
|
|
||||||
Sign(usize, EdwardsPoint, EdwardsPoint),
|
|
||||||
Verify(Scalar),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Core of the CLSAG algorithm, applicable to both sign and verify with minimal differences
|
|
||||||
// Said differences are covered via the above Mode
|
|
||||||
fn core(
|
|
||||||
ring: &[[EdwardsPoint; 2]],
|
|
||||||
I: &EdwardsPoint,
|
|
||||||
pseudo_out: &EdwardsPoint,
|
|
||||||
msg: &[u8; 32],
|
|
||||||
D: &EdwardsPoint,
|
|
||||||
s: &[Scalar],
|
|
||||||
A_c1: &Mode,
|
|
||||||
) -> ((EdwardsPoint, Scalar, Scalar), Scalar) {
|
|
||||||
let n = ring.len();
|
|
||||||
|
|
||||||
let images_precomp = VartimeEdwardsPrecomputation::new([I, D]);
|
|
||||||
let D = D * INV_EIGHT();
|
|
||||||
|
|
||||||
// Generate the transcript
|
|
||||||
// Instead of generating multiple, a single transcript is created and then edited as needed
|
|
||||||
const PREFIX: &[u8] = b"CLSAG_";
|
|
||||||
#[rustfmt::skip]
|
|
||||||
const AGG_0: &[u8] = b"agg_0";
|
|
||||||
#[rustfmt::skip]
|
|
||||||
const ROUND: &[u8] = b"round";
|
|
||||||
const PREFIX_AGG_0_LEN: usize = PREFIX.len() + AGG_0.len();
|
|
||||||
|
|
||||||
let mut to_hash = Vec::with_capacity(((2 * n) + 5) * 32);
|
|
||||||
to_hash.extend(PREFIX);
|
|
||||||
to_hash.extend(AGG_0);
|
|
||||||
to_hash.extend([0; 32 - PREFIX_AGG_0_LEN]);
|
|
||||||
|
|
||||||
let mut P = Vec::with_capacity(n);
|
|
||||||
for member in ring {
|
|
||||||
P.push(member[0]);
|
|
||||||
to_hash.extend(member[0].compress().to_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut C = Vec::with_capacity(n);
|
|
||||||
for member in ring {
|
|
||||||
C.push(member[1] - pseudo_out);
|
|
||||||
to_hash.extend(member[1].compress().to_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
to_hash.extend(I.compress().to_bytes());
|
|
||||||
to_hash.extend(D.compress().to_bytes());
|
|
||||||
to_hash.extend(pseudo_out.compress().to_bytes());
|
|
||||||
// mu_P with agg_0
|
|
||||||
let mu_P = hash_to_scalar(&to_hash);
|
|
||||||
// mu_C with agg_1
|
|
||||||
to_hash[PREFIX_AGG_0_LEN - 1] = b'1';
|
|
||||||
let mu_C = hash_to_scalar(&to_hash);
|
|
||||||
|
|
||||||
// Truncate it for the round transcript, altering the DST as needed
|
|
||||||
to_hash.truncate(((2 * n) + 1) * 32);
|
|
||||||
for i in 0 .. ROUND.len() {
|
|
||||||
to_hash[PREFIX.len() + i] = ROUND[i];
|
|
||||||
}
|
|
||||||
// Unfortunately, it's I D pseudo_out instead of pseudo_out I D, meaning this needs to be
|
|
||||||
// truncated just to add it back
|
|
||||||
to_hash.extend(pseudo_out.compress().to_bytes());
|
|
||||||
to_hash.extend(msg);
|
|
||||||
|
|
||||||
// Configure the loop based on if we're signing or verifying
|
|
||||||
let start;
|
|
||||||
let end;
|
|
||||||
let mut c;
|
|
||||||
match A_c1 {
|
|
||||||
Mode::Sign(r, A, AH) => {
|
|
||||||
start = r + 1;
|
|
||||||
end = r + n;
|
|
||||||
to_hash.extend(A.compress().to_bytes());
|
|
||||||
to_hash.extend(AH.compress().to_bytes());
|
|
||||||
c = hash_to_scalar(&to_hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
Mode::Verify(c1) => {
|
|
||||||
start = 0;
|
|
||||||
end = n;
|
|
||||||
c = *c1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform the core loop
|
|
||||||
let mut c1 = CtOption::new(Scalar::ZERO, Choice::from(0));
|
|
||||||
for i in (start .. end).map(|i| i % n) {
|
|
||||||
// This will only execute once and shouldn't need to be constant time. Making it constant time
|
|
||||||
// removes the risk of branch prediction creating timing differences depending on ring index
|
|
||||||
// however
|
|
||||||
c1 = c1.or_else(|| CtOption::new(c, i.ct_eq(&0)));
|
|
||||||
|
|
||||||
let c_p = mu_P * c;
|
|
||||||
let c_c = mu_C * c;
|
|
||||||
|
|
||||||
let L = (&s[i] * ED25519_BASEPOINT_TABLE) + (c_p * P[i]) + (c_c * C[i]);
|
|
||||||
let PH = hash_to_point(&P[i]);
|
|
||||||
// Shouldn't be an issue as all of the variables in this vartime statement are public
|
|
||||||
let R = (s[i] * PH) + images_precomp.vartime_multiscalar_mul([c_p, c_c]);
|
|
||||||
|
|
||||||
to_hash.truncate(((2 * n) + 3) * 32);
|
|
||||||
to_hash.extend(L.compress().to_bytes());
|
|
||||||
to_hash.extend(R.compress().to_bytes());
|
|
||||||
c = hash_to_scalar(&to_hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
// This first tuple is needed to continue signing, the latter is the c to be tested/worked with
|
|
||||||
((D, c * mu_P, c * mu_C), c1.unwrap_or(c))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// CLSAG signature, as used in Monero.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct Clsag {
|
|
||||||
pub D: EdwardsPoint,
|
|
||||||
pub s: Vec<Scalar>,
|
|
||||||
pub c1: Scalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clsag {
|
|
||||||
// Sign core is the extension of core as needed for signing, yet is shared between single signer
|
|
||||||
// and multisig, hence why it's still core
|
|
||||||
pub(crate) fn sign_core<R: RngCore + CryptoRng>(
|
|
||||||
rng: &mut R,
|
|
||||||
I: &EdwardsPoint,
|
|
||||||
input: &ClsagInput,
|
|
||||||
mask: Scalar,
|
|
||||||
msg: &[u8; 32],
|
|
||||||
A: EdwardsPoint,
|
|
||||||
AH: EdwardsPoint,
|
|
||||||
) -> (Clsag, EdwardsPoint, Scalar, Scalar) {
|
|
||||||
let r: usize = input.decoys.i.into();
|
|
||||||
|
|
||||||
let pseudo_out = Commitment::new(mask, input.commitment.amount).calculate();
|
|
||||||
let z = input.commitment.mask - mask;
|
|
||||||
|
|
||||||
let H = hash_to_point(&input.decoys.ring[r][0]);
|
|
||||||
let D = H * z;
|
|
||||||
let mut s = Vec::with_capacity(input.decoys.ring.len());
|
|
||||||
for _ in 0 .. input.decoys.ring.len() {
|
|
||||||
s.push(random_scalar(rng));
|
|
||||||
}
|
|
||||||
let ((D, p, c), c1) =
|
|
||||||
core(&input.decoys.ring, I, &pseudo_out, msg, &D, &s, &Mode::Sign(r, A, AH));
|
|
||||||
|
|
||||||
(Clsag { D, s, c1 }, pseudo_out, p, c * z)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate CLSAG signatures for the given inputs.
|
|
||||||
/// inputs is of the form (private key, key image, input).
|
|
||||||
/// sum_outputs is for the sum of the outputs' commitment masks.
|
|
||||||
pub fn sign<R: RngCore + CryptoRng>(
|
|
||||||
rng: &mut R,
|
|
||||||
mut inputs: Vec<(Zeroizing<Scalar>, EdwardsPoint, ClsagInput)>,
|
|
||||||
sum_outputs: Scalar,
|
|
||||||
msg: [u8; 32],
|
|
||||||
) -> Vec<(Clsag, EdwardsPoint)> {
|
|
||||||
let mut res = Vec::with_capacity(inputs.len());
|
|
||||||
let mut sum_pseudo_outs = Scalar::ZERO;
|
|
||||||
for i in 0 .. inputs.len() {
|
|
||||||
let mut mask = random_scalar(rng);
|
|
||||||
if i == (inputs.len() - 1) {
|
|
||||||
mask = sum_outputs - sum_pseudo_outs;
|
|
||||||
} else {
|
|
||||||
sum_pseudo_outs += mask;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut nonce = Zeroizing::new(random_scalar(rng));
|
|
||||||
let (mut clsag, pseudo_out, p, c) = Clsag::sign_core(
|
|
||||||
rng,
|
|
||||||
&inputs[i].1,
|
|
||||||
&inputs[i].2,
|
|
||||||
mask,
|
|
||||||
&msg,
|
|
||||||
nonce.deref() * ED25519_BASEPOINT_TABLE,
|
|
||||||
nonce.deref() *
|
|
||||||
hash_to_point(&inputs[i].2.decoys.ring[usize::from(inputs[i].2.decoys.i)][0]),
|
|
||||||
);
|
|
||||||
clsag.s[usize::from(inputs[i].2.decoys.i)] =
|
|
||||||
(-((p * inputs[i].0.deref()) + c)) + nonce.deref();
|
|
||||||
inputs[i].0.zeroize();
|
|
||||||
nonce.zeroize();
|
|
||||||
|
|
||||||
debug_assert!(clsag
|
|
||||||
.verify(&inputs[i].2.decoys.ring, &inputs[i].1, &pseudo_out, &msg)
|
|
||||||
.is_ok());
|
|
||||||
|
|
||||||
res.push((clsag, pseudo_out));
|
|
||||||
}
|
|
||||||
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Verify the CLSAG signature against the given Transaction data.
|
|
||||||
pub fn verify(
|
|
||||||
&self,
|
|
||||||
ring: &[[EdwardsPoint; 2]],
|
|
||||||
I: &EdwardsPoint,
|
|
||||||
pseudo_out: &EdwardsPoint,
|
|
||||||
msg: &[u8; 32],
|
|
||||||
) -> Result<(), ClsagError> {
|
|
||||||
// Preliminary checks. s, c1, and points must also be encoded canonically, which isn't checked
|
|
||||||
// here
|
|
||||||
if ring.is_empty() {
|
|
||||||
Err(ClsagError::InvalidRing)?;
|
|
||||||
}
|
|
||||||
if ring.len() != self.s.len() {
|
|
||||||
Err(ClsagError::InvalidS)?;
|
|
||||||
}
|
|
||||||
if I.is_identity() {
|
|
||||||
Err(ClsagError::InvalidImage)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let D = self.D.mul_by_cofactor();
|
|
||||||
if D.is_identity() {
|
|
||||||
Err(ClsagError::InvalidD)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let (_, c1) = core(ring, I, pseudo_out, msg, &D, &self.s, &Mode::Verify(self.c1));
|
|
||||||
if c1 != self.c1 {
|
|
||||||
Err(ClsagError::InvalidC1)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn fee_weight(ring_len: usize) -> usize {
|
|
||||||
(ring_len * 32) + 32 + 32
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
write_raw_vec(write_scalar, &self.s, w)?;
|
|
||||||
w.write_all(&self.c1.to_bytes())?;
|
|
||||||
write_point(&self.D, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(decoys: usize, r: &mut R) -> io::Result<Clsag> {
|
|
||||||
Ok(Clsag { s: read_raw_vec(read_scalar, decoys, r)?, c1: read_scalar(r)?, D: read_point(r)? })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,304 +0,0 @@
|
|||||||
use core::{ops::Deref, fmt::Debug};
|
|
||||||
use std_shims::io::{self, Read, Write};
|
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng, SeedableRng};
|
|
||||||
use rand_chacha::ChaCha20Rng;
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
|
|
||||||
|
|
||||||
use curve25519_dalek::{scalar::Scalar, edwards::EdwardsPoint};
|
|
||||||
|
|
||||||
use group::{ff::Field, Group, GroupEncoding};
|
|
||||||
|
|
||||||
use transcript::{Transcript, RecommendedTranscript};
|
|
||||||
use dalek_ff_group as dfg;
|
|
||||||
use dleq::DLEqProof;
|
|
||||||
use frost::{
|
|
||||||
dkg::lagrange,
|
|
||||||
curve::Ed25519,
|
|
||||||
Participant, FrostError, ThresholdKeys, ThresholdView,
|
|
||||||
algorithm::{WriteAddendum, Algorithm},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::ringct::{
|
|
||||||
hash_to_point,
|
|
||||||
clsag::{ClsagInput, Clsag},
|
|
||||||
};
|
|
||||||
|
|
||||||
fn dleq_transcript() -> RecommendedTranscript {
|
|
||||||
RecommendedTranscript::new(b"monero_key_image_dleq")
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ClsagInput {
|
|
||||||
fn transcript<T: Transcript>(&self, transcript: &mut T) {
|
|
||||||
// Doesn't domain separate as this is considered part of the larger CLSAG proof
|
|
||||||
|
|
||||||
// Ring index
|
|
||||||
transcript.append_message(b"real_spend", [self.decoys.i]);
|
|
||||||
|
|
||||||
// Ring
|
|
||||||
for (i, pair) in self.decoys.ring.iter().enumerate() {
|
|
||||||
// Doesn't include global output indexes as CLSAG doesn't care and won't be affected by it
|
|
||||||
// They're just a unreliable reference to this data which will be included in the message
|
|
||||||
// if in use
|
|
||||||
transcript.append_message(b"member", [u8::try_from(i).expect("ring size exceeded 255")]);
|
|
||||||
transcript.append_message(b"key", pair[0].compress().to_bytes());
|
|
||||||
transcript.append_message(b"commitment", pair[1].compress().to_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Doesn't include the commitment's parts as the above ring + index includes the commitment
|
|
||||||
// The only potential malleability would be if the G/H relationship is known breaking the
|
|
||||||
// discrete log problem, which breaks everything already
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// CLSAG input and the mask to use for it.
|
|
||||||
#[derive(Clone, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct ClsagDetails {
|
|
||||||
input: ClsagInput,
|
|
||||||
mask: Scalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ClsagDetails {
|
|
||||||
pub fn new(input: ClsagInput, mask: Scalar) -> ClsagDetails {
|
|
||||||
ClsagDetails { input, mask }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Addendum produced during the FROST signing process with relevant data.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Zeroize, Debug)]
|
|
||||||
pub struct ClsagAddendum {
|
|
||||||
pub(crate) key_image: dfg::EdwardsPoint,
|
|
||||||
dleq: DLEqProof<dfg::EdwardsPoint>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WriteAddendum for ClsagAddendum {
|
|
||||||
fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
|
|
||||||
writer.write_all(self.key_image.compress().to_bytes().as_ref())?;
|
|
||||||
self.dleq.write(writer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
struct Interim {
|
|
||||||
p: Scalar,
|
|
||||||
c: Scalar,
|
|
||||||
|
|
||||||
clsag: Clsag,
|
|
||||||
pseudo_out: EdwardsPoint,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// FROST algorithm for producing a CLSAG signature.
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct ClsagMultisig {
|
|
||||||
transcript: RecommendedTranscript,
|
|
||||||
|
|
||||||
pub(crate) H: EdwardsPoint,
|
|
||||||
// Merged here as CLSAG needs it, passing it would be a mess, yet having it beforehand requires
|
|
||||||
// an extra round
|
|
||||||
image: EdwardsPoint,
|
|
||||||
|
|
||||||
details: Arc<RwLock<Option<ClsagDetails>>>,
|
|
||||||
|
|
||||||
msg: Option<[u8; 32]>,
|
|
||||||
interim: Option<Interim>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ClsagMultisig {
|
|
||||||
pub fn new(
|
|
||||||
transcript: RecommendedTranscript,
|
|
||||||
output_key: EdwardsPoint,
|
|
||||||
details: Arc<RwLock<Option<ClsagDetails>>>,
|
|
||||||
) -> ClsagMultisig {
|
|
||||||
ClsagMultisig {
|
|
||||||
transcript,
|
|
||||||
|
|
||||||
H: hash_to_point(&output_key),
|
|
||||||
image: EdwardsPoint::identity(),
|
|
||||||
|
|
||||||
details,
|
|
||||||
|
|
||||||
msg: None,
|
|
||||||
interim: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn input(&self) -> ClsagInput {
|
|
||||||
(*self.details.read().unwrap()).as_ref().unwrap().input.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mask(&self) -> Scalar {
|
|
||||||
(*self.details.read().unwrap()).as_ref().unwrap().mask
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn add_key_image_share(
|
|
||||||
image: &mut EdwardsPoint,
|
|
||||||
generator: EdwardsPoint,
|
|
||||||
offset: Scalar,
|
|
||||||
included: &[Participant],
|
|
||||||
participant: Participant,
|
|
||||||
share: EdwardsPoint,
|
|
||||||
) {
|
|
||||||
if image.is_identity().into() {
|
|
||||||
*image = generator * offset;
|
|
||||||
}
|
|
||||||
*image += share * lagrange::<dfg::Scalar>(participant, included).0;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Algorithm<Ed25519> for ClsagMultisig {
|
|
||||||
type Transcript = RecommendedTranscript;
|
|
||||||
type Addendum = ClsagAddendum;
|
|
||||||
type Signature = (Clsag, EdwardsPoint);
|
|
||||||
|
|
||||||
fn nonces(&self) -> Vec<Vec<dfg::EdwardsPoint>> {
|
|
||||||
vec![vec![dfg::EdwardsPoint::generator(), dfg::EdwardsPoint(self.H)]]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn preprocess_addendum<R: RngCore + CryptoRng>(
|
|
||||||
&mut self,
|
|
||||||
rng: &mut R,
|
|
||||||
keys: &ThresholdKeys<Ed25519>,
|
|
||||||
) -> ClsagAddendum {
|
|
||||||
ClsagAddendum {
|
|
||||||
key_image: dfg::EdwardsPoint(self.H) * keys.secret_share().deref(),
|
|
||||||
dleq: DLEqProof::prove(
|
|
||||||
rng,
|
|
||||||
// Doesn't take in a larger transcript object due to the usage of this
|
|
||||||
// Every prover would immediately write their own DLEq proof, when they can only do so in
|
|
||||||
// the proper order if they want to reach consensus
|
|
||||||
// It'd be a poor API to have CLSAG define a new transcript solely to pass here, just to
|
|
||||||
// try to merge later in some form, when it should instead just merge xH (as it does)
|
|
||||||
&mut dleq_transcript(),
|
|
||||||
&[dfg::EdwardsPoint::generator(), dfg::EdwardsPoint(self.H)],
|
|
||||||
keys.secret_share(),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_addendum<R: Read>(&self, reader: &mut R) -> io::Result<ClsagAddendum> {
|
|
||||||
let mut bytes = [0; 32];
|
|
||||||
reader.read_exact(&mut bytes)?;
|
|
||||||
// dfg ensures the point is torsion free
|
|
||||||
let xH = Option::<dfg::EdwardsPoint>::from(dfg::EdwardsPoint::from_bytes(&bytes))
|
|
||||||
.ok_or_else(|| io::Error::other("invalid key image"))?;
|
|
||||||
// Ensure this is a canonical point
|
|
||||||
if xH.to_bytes() != bytes {
|
|
||||||
Err(io::Error::other("non-canonical key image"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(ClsagAddendum { key_image: xH, dleq: DLEqProof::<dfg::EdwardsPoint>::read(reader)? })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process_addendum(
|
|
||||||
&mut self,
|
|
||||||
view: &ThresholdView<Ed25519>,
|
|
||||||
l: Participant,
|
|
||||||
addendum: ClsagAddendum,
|
|
||||||
) -> Result<(), FrostError> {
|
|
||||||
if self.image.is_identity().into() {
|
|
||||||
self.transcript.domain_separate(b"CLSAG");
|
|
||||||
self.input().transcript(&mut self.transcript);
|
|
||||||
self.transcript.append_message(b"mask", self.mask().to_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
self.transcript.append_message(b"participant", l.to_bytes());
|
|
||||||
|
|
||||||
addendum
|
|
||||||
.dleq
|
|
||||||
.verify(
|
|
||||||
&mut dleq_transcript(),
|
|
||||||
&[dfg::EdwardsPoint::generator(), dfg::EdwardsPoint(self.H)],
|
|
||||||
&[view.original_verification_share(l), addendum.key_image],
|
|
||||||
)
|
|
||||||
.map_err(|_| FrostError::InvalidPreprocess(l))?;
|
|
||||||
|
|
||||||
self.transcript.append_message(b"key_image_share", addendum.key_image.compress().to_bytes());
|
|
||||||
add_key_image_share(
|
|
||||||
&mut self.image,
|
|
||||||
self.H,
|
|
||||||
view.offset().0,
|
|
||||||
view.included(),
|
|
||||||
l,
|
|
||||||
addendum.key_image.0,
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn transcript(&mut self) -> &mut Self::Transcript {
|
|
||||||
&mut self.transcript
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sign_share(
|
|
||||||
&mut self,
|
|
||||||
view: &ThresholdView<Ed25519>,
|
|
||||||
nonce_sums: &[Vec<dfg::EdwardsPoint>],
|
|
||||||
nonces: Vec<Zeroizing<dfg::Scalar>>,
|
|
||||||
msg: &[u8],
|
|
||||||
) -> dfg::Scalar {
|
|
||||||
// Use the transcript to get a seeded random number generator
|
|
||||||
// The transcript contains private data, preventing passive adversaries from recreating this
|
|
||||||
// process even if they have access to commitments (specifically, the ring index being signed
|
|
||||||
// for, along with the mask which should not only require knowing the shared keys yet also the
|
|
||||||
// input commitment masks)
|
|
||||||
let mut rng = ChaCha20Rng::from_seed(self.transcript.rng_seed(b"decoy_responses"));
|
|
||||||
|
|
||||||
self.msg = Some(msg.try_into().expect("CLSAG message should be 32-bytes"));
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let (clsag, pseudo_out, p, c) = Clsag::sign_core(
|
|
||||||
&mut rng,
|
|
||||||
&self.image,
|
|
||||||
&self.input(),
|
|
||||||
self.mask(),
|
|
||||||
self.msg.as_ref().unwrap(),
|
|
||||||
nonce_sums[0][0].0,
|
|
||||||
nonce_sums[0][1].0,
|
|
||||||
);
|
|
||||||
self.interim = Some(Interim { p, c, clsag, pseudo_out });
|
|
||||||
|
|
||||||
(-(dfg::Scalar(p) * view.secret_share().deref())) + nonces[0].deref()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
fn verify(
|
|
||||||
&self,
|
|
||||||
_: dfg::EdwardsPoint,
|
|
||||||
_: &[Vec<dfg::EdwardsPoint>],
|
|
||||||
sum: dfg::Scalar,
|
|
||||||
) -> Option<Self::Signature> {
|
|
||||||
let interim = self.interim.as_ref().unwrap();
|
|
||||||
let mut clsag = interim.clsag.clone();
|
|
||||||
clsag.s[usize::from(self.input().decoys.i)] = sum.0 - interim.c;
|
|
||||||
if clsag
|
|
||||||
.verify(
|
|
||||||
&self.input().decoys.ring,
|
|
||||||
&self.image,
|
|
||||||
&interim.pseudo_out,
|
|
||||||
self.msg.as_ref().unwrap(),
|
|
||||||
)
|
|
||||||
.is_ok()
|
|
||||||
{
|
|
||||||
return Some((clsag, interim.pseudo_out));
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn verify_share(
|
|
||||||
&self,
|
|
||||||
verification_share: dfg::EdwardsPoint,
|
|
||||||
nonces: &[Vec<dfg::EdwardsPoint>],
|
|
||||||
share: dfg::Scalar,
|
|
||||||
) -> Result<Vec<(dfg::Scalar, dfg::EdwardsPoint)>, ()> {
|
|
||||||
let interim = self.interim.as_ref().unwrap();
|
|
||||||
Ok(vec![
|
|
||||||
(share, dfg::EdwardsPoint::generator()),
|
|
||||||
(dfg::Scalar(interim.p), verification_share),
|
|
||||||
(-dfg::Scalar::ONE, nonces[0][0]),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
use curve25519_dalek::edwards::EdwardsPoint;
|
|
||||||
|
|
||||||
pub use monero_generators::{hash_to_point as raw_hash_to_point};
|
|
||||||
|
|
||||||
/// Monero's hash to point function, as named `ge_fromfe_frombytes_vartime`.
|
|
||||||
pub fn hash_to_point(key: &EdwardsPoint) -> EdwardsPoint {
|
|
||||||
raw_hash_to_point(key.compress().to_bytes())
|
|
||||||
}
|
|
||||||
@@ -1,213 +0,0 @@
|
|||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use zeroize::Zeroize;
|
|
||||||
|
|
||||||
use curve25519_dalek::{traits::IsIdentity, Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use monero_generators::H;
|
|
||||||
|
|
||||||
use crate::{hash_to_scalar, ringct::hash_to_point, serialize::*};
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
#[cfg_attr(feature = "std", derive(thiserror::Error))]
|
|
||||||
pub enum MlsagError {
|
|
||||||
#[cfg_attr(feature = "std", error("invalid ring"))]
|
|
||||||
InvalidRing,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid amount of key images"))]
|
|
||||||
InvalidAmountOfKeyImages,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid ss"))]
|
|
||||||
InvalidSs,
|
|
||||||
#[cfg_attr(feature = "std", error("key image was identity"))]
|
|
||||||
IdentityKeyImage,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid ci"))]
|
|
||||||
InvalidCi,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub struct RingMatrix {
|
|
||||||
matrix: Vec<Vec<EdwardsPoint>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RingMatrix {
|
|
||||||
pub fn new(matrix: Vec<Vec<EdwardsPoint>>) -> Result<Self, MlsagError> {
|
|
||||||
if matrix.is_empty() {
|
|
||||||
Err(MlsagError::InvalidRing)?;
|
|
||||||
}
|
|
||||||
for member in &matrix {
|
|
||||||
if member.is_empty() || (member.len() != matrix[0].len()) {
|
|
||||||
Err(MlsagError::InvalidRing)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(RingMatrix { matrix })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct a ring matrix for an individual output.
|
|
||||||
pub fn individual(
|
|
||||||
ring: &[[EdwardsPoint; 2]],
|
|
||||||
pseudo_out: EdwardsPoint,
|
|
||||||
) -> Result<Self, MlsagError> {
|
|
||||||
let mut matrix = Vec::with_capacity(ring.len());
|
|
||||||
for ring_member in ring {
|
|
||||||
matrix.push(vec![ring_member[0], ring_member[1] - pseudo_out]);
|
|
||||||
}
|
|
||||||
RingMatrix::new(matrix)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn iter(&self) -> impl Iterator<Item = &[EdwardsPoint]> {
|
|
||||||
self.matrix.iter().map(AsRef::as_ref)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the amount of members in the ring.
|
|
||||||
pub fn members(&self) -> usize {
|
|
||||||
self.matrix.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the length of a ring member.
|
|
||||||
///
|
|
||||||
/// A ring member is a vector of points for which the signer knows all of the discrete logarithms
|
|
||||||
/// of.
|
|
||||||
pub fn member_len(&self) -> usize {
|
|
||||||
// this is safe to do as the constructors don't allow empty rings
|
|
||||||
self.matrix[0].len()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub struct Mlsag {
|
|
||||||
pub ss: Vec<Vec<Scalar>>,
|
|
||||||
pub cc: Scalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mlsag {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
for ss in &self.ss {
|
|
||||||
write_raw_vec(write_scalar, ss, w)?;
|
|
||||||
}
|
|
||||||
write_scalar(&self.cc, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(mixins: usize, ss_2_elements: usize, r: &mut R) -> io::Result<Mlsag> {
|
|
||||||
Ok(Mlsag {
|
|
||||||
ss: (0 .. mixins)
|
|
||||||
.map(|_| read_raw_vec(read_scalar, ss_2_elements, r))
|
|
||||||
.collect::<Result<_, _>>()?,
|
|
||||||
cc: read_scalar(r)?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn verify(
|
|
||||||
&self,
|
|
||||||
msg: &[u8; 32],
|
|
||||||
ring: &RingMatrix,
|
|
||||||
key_images: &[EdwardsPoint],
|
|
||||||
) -> Result<(), MlsagError> {
|
|
||||||
// Mlsag allows for layers to not need linkability, hence they don't need key images
|
|
||||||
// Monero requires that there is always only 1 non-linkable layer - the amount commitments.
|
|
||||||
if ring.member_len() != (key_images.len() + 1) {
|
|
||||||
Err(MlsagError::InvalidAmountOfKeyImages)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = Vec::with_capacity(6 * 32);
|
|
||||||
buf.extend_from_slice(msg);
|
|
||||||
|
|
||||||
let mut ci = self.cc;
|
|
||||||
|
|
||||||
// This is an iterator over the key images as options with an added entry of `None` at the
|
|
||||||
// end for the non-linkable layer
|
|
||||||
let key_images_iter = key_images.iter().map(|ki| Some(*ki)).chain(core::iter::once(None));
|
|
||||||
|
|
||||||
if ring.matrix.len() != self.ss.len() {
|
|
||||||
Err(MlsagError::InvalidSs)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ring_member, ss) in ring.iter().zip(&self.ss) {
|
|
||||||
if ring_member.len() != ss.len() {
|
|
||||||
Err(MlsagError::InvalidSs)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
for ((ring_member_entry, s), ki) in ring_member.iter().zip(ss).zip(key_images_iter.clone()) {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let L = EdwardsPoint::vartime_double_scalar_mul_basepoint(&ci, ring_member_entry, s);
|
|
||||||
|
|
||||||
buf.extend_from_slice(ring_member_entry.compress().as_bytes());
|
|
||||||
buf.extend_from_slice(L.compress().as_bytes());
|
|
||||||
|
|
||||||
// Not all dimensions need to be linkable, e.g. commitments, and only linkable layers need
|
|
||||||
// to have key images.
|
|
||||||
if let Some(ki) = ki {
|
|
||||||
if ki.is_identity() {
|
|
||||||
Err(MlsagError::IdentityKeyImage)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let R = (s * hash_to_point(ring_member_entry)) + (ci * ki);
|
|
||||||
buf.extend_from_slice(R.compress().as_bytes());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ci = hash_to_scalar(&buf);
|
|
||||||
// keep the msg in the buffer.
|
|
||||||
buf.drain(msg.len() ..);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ci != self.cc {
|
|
||||||
Err(MlsagError::InvalidCi)?
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An aggregate ring matrix builder, usable to set up the ring matrix to prove/verify an aggregate
|
|
||||||
/// MLSAG signature.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub struct AggregateRingMatrixBuilder {
|
|
||||||
key_ring: Vec<Vec<EdwardsPoint>>,
|
|
||||||
amounts_ring: Vec<EdwardsPoint>,
|
|
||||||
sum_out: EdwardsPoint,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AggregateRingMatrixBuilder {
|
|
||||||
/// Create a new AggregateRingMatrixBuilder.
|
|
||||||
///
|
|
||||||
/// Takes in the transaction's outputs; commitments and fee.
|
|
||||||
pub fn new(commitments: &[EdwardsPoint], fee: u64) -> Self {
|
|
||||||
AggregateRingMatrixBuilder {
|
|
||||||
key_ring: vec![],
|
|
||||||
amounts_ring: vec![],
|
|
||||||
sum_out: commitments.iter().sum::<EdwardsPoint>() + (H() * Scalar::from(fee)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Push a ring of [output key, commitment] to the matrix.
|
|
||||||
pub fn push_ring(&mut self, ring: &[[EdwardsPoint; 2]]) -> Result<(), MlsagError> {
|
|
||||||
if self.key_ring.is_empty() {
|
|
||||||
self.key_ring = vec![vec![]; ring.len()];
|
|
||||||
// Now that we know the length of the ring, fill the `amounts_ring`.
|
|
||||||
self.amounts_ring = vec![-self.sum_out; ring.len()];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self.amounts_ring.len() != ring.len()) || ring.is_empty() {
|
|
||||||
// All the rings in an aggregate matrix must be the same length.
|
|
||||||
return Err(MlsagError::InvalidRing);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i, ring_member) in ring.iter().enumerate() {
|
|
||||||
self.key_ring[i].push(ring_member[0]);
|
|
||||||
self.amounts_ring[i] += ring_member[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Build and return the [`RingMatrix`]
|
|
||||||
pub fn build(mut self) -> Result<RingMatrix, MlsagError> {
|
|
||||||
for (i, amount_commitment) in self.amounts_ring.drain(..).enumerate() {
|
|
||||||
self.key_ring[i].push(amount_commitment);
|
|
||||||
}
|
|
||||||
RingMatrix::new(self.key_ring)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,389 +0,0 @@
|
|||||||
use core::ops::Deref;
|
|
||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, Zeroizing};
|
|
||||||
|
|
||||||
use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, scalar::Scalar, edwards::EdwardsPoint};
|
|
||||||
|
|
||||||
pub(crate) mod hash_to_point;
|
|
||||||
pub use hash_to_point::{raw_hash_to_point, hash_to_point};
|
|
||||||
|
|
||||||
/// MLSAG struct, along with verifying functionality.
|
|
||||||
pub mod mlsag;
|
|
||||||
/// CLSAG struct, along with signing and verifying functionality.
|
|
||||||
pub mod clsag;
|
|
||||||
/// BorromeanRange struct, along with verifying functionality.
|
|
||||||
pub mod borromean;
|
|
||||||
/// Bulletproofs(+) structs, along with proving and verifying functionality.
|
|
||||||
pub mod bulletproofs;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Protocol,
|
|
||||||
serialize::*,
|
|
||||||
ringct::{mlsag::Mlsag, clsag::Clsag, borromean::BorromeanRange, bulletproofs::Bulletproofs},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Generate a key image for a given key. Defined as `x * hash_to_point(xG)`.
|
|
||||||
pub fn generate_key_image(secret: &Zeroizing<Scalar>) -> EdwardsPoint {
|
|
||||||
hash_to_point(&(ED25519_BASEPOINT_TABLE * secret.deref())) * secret.deref()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub enum EncryptedAmount {
|
|
||||||
Original { mask: [u8; 32], amount: [u8; 32] },
|
|
||||||
Compact { amount: [u8; 8] },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EncryptedAmount {
|
|
||||||
pub fn read<R: Read>(compact: bool, r: &mut R) -> io::Result<EncryptedAmount> {
|
|
||||||
Ok(if !compact {
|
|
||||||
EncryptedAmount::Original { mask: read_bytes(r)?, amount: read_bytes(r)? }
|
|
||||||
} else {
|
|
||||||
EncryptedAmount::Compact { amount: read_bytes(r)? }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
match self {
|
|
||||||
EncryptedAmount::Original { mask, amount } => {
|
|
||||||
w.write_all(mask)?;
|
|
||||||
w.write_all(amount)
|
|
||||||
}
|
|
||||||
EncryptedAmount::Compact { amount } => w.write_all(amount),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub enum RctType {
|
|
||||||
/// No RCT proofs.
|
|
||||||
Null,
|
|
||||||
/// One MLSAG for multiple inputs and Borromean range proofs (RCTTypeFull).
|
|
||||||
MlsagAggregate,
|
|
||||||
// One MLSAG for each input and a Borromean range proof (RCTTypeSimple).
|
|
||||||
MlsagIndividual,
|
|
||||||
// One MLSAG for each input and a Bulletproof (RCTTypeBulletproof).
|
|
||||||
Bulletproofs,
|
|
||||||
/// One MLSAG for each input and a Bulletproof, yet starting to use EncryptedAmount::Compact
|
|
||||||
/// (RCTTypeBulletproof2).
|
|
||||||
BulletproofsCompactAmount,
|
|
||||||
/// One CLSAG for each input and a Bulletproof (RCTTypeCLSAG).
|
|
||||||
Clsag,
|
|
||||||
/// One CLSAG for each input and a Bulletproof+ (RCTTypeBulletproofPlus).
|
|
||||||
BulletproofsPlus,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RctType {
|
|
||||||
pub fn to_byte(self) -> u8 {
|
|
||||||
match self {
|
|
||||||
RctType::Null => 0,
|
|
||||||
RctType::MlsagAggregate => 1,
|
|
||||||
RctType::MlsagIndividual => 2,
|
|
||||||
RctType::Bulletproofs => 3,
|
|
||||||
RctType::BulletproofsCompactAmount => 4,
|
|
||||||
RctType::Clsag => 5,
|
|
||||||
RctType::BulletproofsPlus => 6,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_byte(byte: u8) -> Option<Self> {
|
|
||||||
Some(match byte {
|
|
||||||
0 => RctType::Null,
|
|
||||||
1 => RctType::MlsagAggregate,
|
|
||||||
2 => RctType::MlsagIndividual,
|
|
||||||
3 => RctType::Bulletproofs,
|
|
||||||
4 => RctType::BulletproofsCompactAmount,
|
|
||||||
5 => RctType::Clsag,
|
|
||||||
6 => RctType::BulletproofsPlus,
|
|
||||||
_ => None?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn compact_encrypted_amounts(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
RctType::Null |
|
|
||||||
RctType::MlsagAggregate |
|
|
||||||
RctType::MlsagIndividual |
|
|
||||||
RctType::Bulletproofs => false,
|
|
||||||
RctType::BulletproofsCompactAmount | RctType::Clsag | RctType::BulletproofsPlus => true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct RctBase {
|
|
||||||
pub fee: u64,
|
|
||||||
pub pseudo_outs: Vec<EdwardsPoint>,
|
|
||||||
pub encrypted_amounts: Vec<EncryptedAmount>,
|
|
||||||
pub commitments: Vec<EdwardsPoint>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RctBase {
|
|
||||||
pub(crate) fn fee_weight(outputs: usize, fee: u64) -> usize {
|
|
||||||
// 1 byte for the RCT signature type
|
|
||||||
1 + (outputs * (8 + 32)) + varint_len(fee)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W, rct_type: RctType) -> io::Result<()> {
|
|
||||||
w.write_all(&[rct_type.to_byte()])?;
|
|
||||||
match rct_type {
|
|
||||||
RctType::Null => Ok(()),
|
|
||||||
_ => {
|
|
||||||
write_varint(&self.fee, w)?;
|
|
||||||
if rct_type == RctType::MlsagIndividual {
|
|
||||||
write_raw_vec(write_point, &self.pseudo_outs, w)?;
|
|
||||||
}
|
|
||||||
for encrypted_amount in &self.encrypted_amounts {
|
|
||||||
encrypted_amount.write(w)?;
|
|
||||||
}
|
|
||||||
write_raw_vec(write_point, &self.commitments, w)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(inputs: usize, outputs: usize, r: &mut R) -> io::Result<(RctBase, RctType)> {
|
|
||||||
let rct_type =
|
|
||||||
RctType::from_byte(read_byte(r)?).ok_or_else(|| io::Error::other("invalid RCT type"))?;
|
|
||||||
|
|
||||||
match rct_type {
|
|
||||||
RctType::Null | RctType::MlsagAggregate | RctType::MlsagIndividual => {}
|
|
||||||
RctType::Bulletproofs |
|
|
||||||
RctType::BulletproofsCompactAmount |
|
|
||||||
RctType::Clsag |
|
|
||||||
RctType::BulletproofsPlus => {
|
|
||||||
if outputs == 0 {
|
|
||||||
// Because the Bulletproofs(+) layout must be canonical, there must be 1 Bulletproof if
|
|
||||||
// Bulletproofs are in use
|
|
||||||
// If there are Bulletproofs, there must be a matching amount of outputs, implicitly
|
|
||||||
// banning 0 outputs
|
|
||||||
// Since HF 12 (CLSAG being 13), a 2-output minimum has also been enforced
|
|
||||||
Err(io::Error::other("RCT with Bulletproofs(+) had 0 outputs"))?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((
|
|
||||||
if rct_type == RctType::Null {
|
|
||||||
RctBase { fee: 0, pseudo_outs: vec![], encrypted_amounts: vec![], commitments: vec![] }
|
|
||||||
} else {
|
|
||||||
RctBase {
|
|
||||||
fee: read_varint(r)?,
|
|
||||||
pseudo_outs: if rct_type == RctType::MlsagIndividual {
|
|
||||||
read_raw_vec(read_point, inputs, r)?
|
|
||||||
} else {
|
|
||||||
vec![]
|
|
||||||
},
|
|
||||||
encrypted_amounts: (0 .. outputs)
|
|
||||||
.map(|_| EncryptedAmount::read(rct_type.compact_encrypted_amounts(), r))
|
|
||||||
.collect::<Result<_, _>>()?,
|
|
||||||
commitments: read_raw_vec(read_point, outputs, r)?,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
rct_type,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub enum RctPrunable {
|
|
||||||
Null,
|
|
||||||
AggregateMlsagBorromean {
|
|
||||||
borromean: Vec<BorromeanRange>,
|
|
||||||
mlsag: Mlsag,
|
|
||||||
},
|
|
||||||
MlsagBorromean {
|
|
||||||
borromean: Vec<BorromeanRange>,
|
|
||||||
mlsags: Vec<Mlsag>,
|
|
||||||
},
|
|
||||||
MlsagBulletproofs {
|
|
||||||
bulletproofs: Bulletproofs,
|
|
||||||
mlsags: Vec<Mlsag>,
|
|
||||||
pseudo_outs: Vec<EdwardsPoint>,
|
|
||||||
},
|
|
||||||
Clsag {
|
|
||||||
bulletproofs: Bulletproofs,
|
|
||||||
clsags: Vec<Clsag>,
|
|
||||||
pseudo_outs: Vec<EdwardsPoint>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RctPrunable {
|
|
||||||
pub(crate) fn fee_weight(protocol: Protocol, inputs: usize, outputs: usize) -> usize {
|
|
||||||
// 1 byte for number of BPs (technically a VarInt, yet there's always just zero or one)
|
|
||||||
1 + Bulletproofs::fee_weight(protocol.bp_plus(), outputs) +
|
|
||||||
(inputs * (Clsag::fee_weight(protocol.ring_len()) + 32))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W, rct_type: RctType) -> io::Result<()> {
|
|
||||||
match self {
|
|
||||||
RctPrunable::Null => Ok(()),
|
|
||||||
RctPrunable::AggregateMlsagBorromean { borromean, mlsag } => {
|
|
||||||
write_raw_vec(BorromeanRange::write, borromean, w)?;
|
|
||||||
mlsag.write(w)
|
|
||||||
}
|
|
||||||
RctPrunable::MlsagBorromean { borromean, mlsags } => {
|
|
||||||
write_raw_vec(BorromeanRange::write, borromean, w)?;
|
|
||||||
write_raw_vec(Mlsag::write, mlsags, w)
|
|
||||||
}
|
|
||||||
RctPrunable::MlsagBulletproofs { bulletproofs, mlsags, pseudo_outs } => {
|
|
||||||
if rct_type == RctType::Bulletproofs {
|
|
||||||
w.write_all(&1u32.to_le_bytes())?;
|
|
||||||
} else {
|
|
||||||
w.write_all(&[1])?;
|
|
||||||
}
|
|
||||||
bulletproofs.write(w)?;
|
|
||||||
|
|
||||||
write_raw_vec(Mlsag::write, mlsags, w)?;
|
|
||||||
write_raw_vec(write_point, pseudo_outs, w)
|
|
||||||
}
|
|
||||||
RctPrunable::Clsag { bulletproofs, clsags, pseudo_outs } => {
|
|
||||||
w.write_all(&[1])?;
|
|
||||||
bulletproofs.write(w)?;
|
|
||||||
|
|
||||||
write_raw_vec(Clsag::write, clsags, w)?;
|
|
||||||
write_raw_vec(write_point, pseudo_outs, w)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self, rct_type: RctType) -> Vec<u8> {
|
|
||||||
let mut serialized = vec![];
|
|
||||||
self.write(&mut serialized, rct_type).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(
|
|
||||||
rct_type: RctType,
|
|
||||||
decoys: &[usize],
|
|
||||||
outputs: usize,
|
|
||||||
r: &mut R,
|
|
||||||
) -> io::Result<RctPrunable> {
|
|
||||||
// While we generally don't bother with misc consensus checks, this affects the safety of
|
|
||||||
// the below defined rct_type function
|
|
||||||
// The exact line preventing zero-input transactions is:
|
|
||||||
// https://github.com/monero-project/monero/blob/00fd416a99686f0956361d1cd0337fe56e58d4a7/
|
|
||||||
// src/ringct/rctSigs.cpp#L609
|
|
||||||
// And then for RctNull, that's only allowed for miner TXs which require one input of
|
|
||||||
// Input::Gen
|
|
||||||
if decoys.is_empty() {
|
|
||||||
Err(io::Error::other("transaction had no inputs"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(match rct_type {
|
|
||||||
RctType::Null => RctPrunable::Null,
|
|
||||||
RctType::MlsagAggregate => RctPrunable::AggregateMlsagBorromean {
|
|
||||||
borromean: read_raw_vec(BorromeanRange::read, outputs, r)?,
|
|
||||||
mlsag: Mlsag::read(decoys[0], decoys.len() + 1, r)?,
|
|
||||||
},
|
|
||||||
RctType::MlsagIndividual => RctPrunable::MlsagBorromean {
|
|
||||||
borromean: read_raw_vec(BorromeanRange::read, outputs, r)?,
|
|
||||||
mlsags: decoys.iter().map(|d| Mlsag::read(*d, 2, r)).collect::<Result<_, _>>()?,
|
|
||||||
},
|
|
||||||
RctType::Bulletproofs | RctType::BulletproofsCompactAmount => {
|
|
||||||
RctPrunable::MlsagBulletproofs {
|
|
||||||
bulletproofs: {
|
|
||||||
if (if rct_type == RctType::Bulletproofs {
|
|
||||||
u64::from(read_u32(r)?)
|
|
||||||
} else {
|
|
||||||
read_varint(r)?
|
|
||||||
}) != 1
|
|
||||||
{
|
|
||||||
Err(io::Error::other("n bulletproofs instead of one"))?;
|
|
||||||
}
|
|
||||||
Bulletproofs::read(r)?
|
|
||||||
},
|
|
||||||
mlsags: decoys.iter().map(|d| Mlsag::read(*d, 2, r)).collect::<Result<_, _>>()?,
|
|
||||||
pseudo_outs: read_raw_vec(read_point, decoys.len(), r)?,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RctType::Clsag | RctType::BulletproofsPlus => RctPrunable::Clsag {
|
|
||||||
bulletproofs: {
|
|
||||||
if read_varint::<_, u64>(r)? != 1 {
|
|
||||||
Err(io::Error::other("n bulletproofs instead of one"))?;
|
|
||||||
}
|
|
||||||
(if rct_type == RctType::Clsag { Bulletproofs::read } else { Bulletproofs::read_plus })(
|
|
||||||
r,
|
|
||||||
)?
|
|
||||||
},
|
|
||||||
clsags: (0 .. decoys.len()).map(|o| Clsag::read(decoys[o], r)).collect::<Result<_, _>>()?,
|
|
||||||
pseudo_outs: read_raw_vec(read_point, decoys.len(), r)?,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn signature_write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
match self {
|
|
||||||
RctPrunable::Null => panic!("Serializing RctPrunable::Null for a signature"),
|
|
||||||
RctPrunable::AggregateMlsagBorromean { borromean, .. } |
|
|
||||||
RctPrunable::MlsagBorromean { borromean, .. } => {
|
|
||||||
borromean.iter().try_for_each(|rs| rs.write(w))
|
|
||||||
}
|
|
||||||
RctPrunable::MlsagBulletproofs { bulletproofs, .. } |
|
|
||||||
RctPrunable::Clsag { bulletproofs, .. } => bulletproofs.signature_write(w),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct RctSignatures {
|
|
||||||
pub base: RctBase,
|
|
||||||
pub prunable: RctPrunable,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RctSignatures {
|
|
||||||
/// RctType for a given RctSignatures struct.
|
|
||||||
pub fn rct_type(&self) -> RctType {
|
|
||||||
match &self.prunable {
|
|
||||||
RctPrunable::Null => RctType::Null,
|
|
||||||
RctPrunable::AggregateMlsagBorromean { .. } => RctType::MlsagAggregate,
|
|
||||||
RctPrunable::MlsagBorromean { .. } => RctType::MlsagIndividual,
|
|
||||||
// RctBase ensures there's at least one output, making the following
|
|
||||||
// inferences guaranteed/expects impossible on any valid RctSignatures
|
|
||||||
RctPrunable::MlsagBulletproofs { .. } => {
|
|
||||||
if matches!(
|
|
||||||
self
|
|
||||||
.base
|
|
||||||
.encrypted_amounts
|
|
||||||
.first()
|
|
||||||
.expect("MLSAG with Bulletproofs didn't have any outputs"),
|
|
||||||
EncryptedAmount::Original { .. }
|
|
||||||
) {
|
|
||||||
RctType::Bulletproofs
|
|
||||||
} else {
|
|
||||||
RctType::BulletproofsCompactAmount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RctPrunable::Clsag { bulletproofs, .. } => {
|
|
||||||
if matches!(bulletproofs, Bulletproofs::Original { .. }) {
|
|
||||||
RctType::Clsag
|
|
||||||
} else {
|
|
||||||
RctType::BulletproofsPlus
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn fee_weight(protocol: Protocol, inputs: usize, outputs: usize, fee: u64) -> usize {
|
|
||||||
RctBase::fee_weight(outputs, fee) + RctPrunable::fee_weight(protocol, inputs, outputs)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
let rct_type = self.rct_type();
|
|
||||||
self.base.write(w, rct_type)?;
|
|
||||||
self.prunable.write(w, rct_type)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = vec![];
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(decoys: &[usize], outputs: usize, r: &mut R) -> io::Result<RctSignatures> {
|
|
||||||
let base = RctBase::read(decoys.len(), outputs, r)?;
|
|
||||||
Ok(RctSignatures { base: base.0, prunable: RctPrunable::read(base.1, decoys, outputs, r)? })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,286 +0,0 @@
|
|||||||
use std::{sync::Arc, io::Read, time::Duration};
|
|
||||||
|
|
||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
use tokio::sync::Mutex;
|
|
||||||
|
|
||||||
use digest_auth::{WwwAuthenticateHeader, AuthContext};
|
|
||||||
use simple_request::{
|
|
||||||
hyper::{StatusCode, header::HeaderValue, Request},
|
|
||||||
Response, Client,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::rpc::{RpcError, RpcConnection, Rpc};
|
|
||||||
|
|
||||||
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
enum Authentication {
|
|
||||||
// If unauthenticated, use a single client
|
|
||||||
Unauthenticated(Client),
|
|
||||||
// If authenticated, use a single client which supports being locked and tracks its nonce
|
|
||||||
// This ensures that if a nonce is requested, another caller doesn't make a request invalidating
|
|
||||||
// it
|
|
||||||
Authenticated {
|
|
||||||
username: String,
|
|
||||||
password: String,
|
|
||||||
#[allow(clippy::type_complexity)]
|
|
||||||
connection: Arc<Mutex<(Option<(WwwAuthenticateHeader, u64)>, Client)>>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An HTTP(S) transport for the RPC.
|
|
||||||
///
|
|
||||||
/// Requires tokio.
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct HttpRpc {
|
|
||||||
authentication: Authentication,
|
|
||||||
url: String,
|
|
||||||
request_timeout: Duration,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HttpRpc {
|
|
||||||
fn digest_auth_challenge(
|
|
||||||
response: &Response,
|
|
||||||
) -> Result<Option<(WwwAuthenticateHeader, u64)>, RpcError> {
|
|
||||||
Ok(if let Some(header) = response.headers().get("www-authenticate") {
|
|
||||||
Some((
|
|
||||||
digest_auth::parse(header.to_str().map_err(|_| {
|
|
||||||
RpcError::InvalidNode("www-authenticate header wasn't a string".to_string())
|
|
||||||
})?)
|
|
||||||
.map_err(|_| RpcError::InvalidNode("invalid digest-auth response".to_string()))?,
|
|
||||||
0,
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new HTTP(S) RPC connection.
|
|
||||||
///
|
|
||||||
/// A daemon requiring authentication can be used via including the username and password in the
|
|
||||||
/// URL.
|
|
||||||
pub async fn new(url: String) -> Result<Rpc<HttpRpc>, RpcError> {
|
|
||||||
Self::with_custom_timeout(url, DEFAULT_TIMEOUT).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new HTTP(S) RPC connection with a custom timeout.
|
|
||||||
///
|
|
||||||
/// A daemon requiring authentication can be used via including the username and password in the
|
|
||||||
/// URL.
|
|
||||||
pub async fn with_custom_timeout(
|
|
||||||
mut url: String,
|
|
||||||
request_timeout: Duration,
|
|
||||||
) -> Result<Rpc<HttpRpc>, RpcError> {
|
|
||||||
let authentication = if url.contains('@') {
|
|
||||||
// Parse out the username and password
|
|
||||||
let url_clone = url;
|
|
||||||
let split_url = url_clone.split('@').collect::<Vec<_>>();
|
|
||||||
if split_url.len() != 2 {
|
|
||||||
Err(RpcError::ConnectionError("invalid amount of login specifications".to_string()))?;
|
|
||||||
}
|
|
||||||
let mut userpass = split_url[0];
|
|
||||||
url = split_url[1].to_string();
|
|
||||||
|
|
||||||
// If there was additionally a protocol string, restore that to the daemon URL
|
|
||||||
if userpass.contains("://") {
|
|
||||||
let split_userpass = userpass.split("://").collect::<Vec<_>>();
|
|
||||||
if split_userpass.len() != 2 {
|
|
||||||
Err(RpcError::ConnectionError("invalid amount of protocol specifications".to_string()))?;
|
|
||||||
}
|
|
||||||
url = split_userpass[0].to_string() + "://" + &url;
|
|
||||||
userpass = split_userpass[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
let split_userpass = userpass.split(':').collect::<Vec<_>>();
|
|
||||||
if split_userpass.len() > 2 {
|
|
||||||
Err(RpcError::ConnectionError("invalid amount of passwords".to_string()))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let client = Client::without_connection_pool(&url)
|
|
||||||
.map_err(|_| RpcError::ConnectionError("invalid URL".to_string()))?;
|
|
||||||
// Obtain the initial challenge, which also somewhat validates this connection
|
|
||||||
let challenge = Self::digest_auth_challenge(
|
|
||||||
&client
|
|
||||||
.request(
|
|
||||||
Request::post(url.clone())
|
|
||||||
.body(vec![].into())
|
|
||||||
.map_err(|e| RpcError::ConnectionError(format!("couldn't make request: {e:?}")))?,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?,
|
|
||||||
)?;
|
|
||||||
Authentication::Authenticated {
|
|
||||||
username: split_userpass[0].to_string(),
|
|
||||||
password: (*split_userpass.get(1).unwrap_or(&"")).to_string(),
|
|
||||||
connection: Arc::new(Mutex::new((challenge, client))),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Authentication::Unauthenticated(Client::with_connection_pool())
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Rpc(HttpRpc { authentication, url, request_timeout }))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HttpRpc {
|
|
||||||
async fn inner_post(&self, route: &str, body: Vec<u8>) -> Result<Vec<u8>, RpcError> {
|
|
||||||
let request_fn = |uri| {
|
|
||||||
Request::post(uri)
|
|
||||||
.body(body.clone().into())
|
|
||||||
.map_err(|e| RpcError::ConnectionError(format!("couldn't make request: {e:?}")))
|
|
||||||
};
|
|
||||||
|
|
||||||
async fn body_from_response(response: Response<'_>) -> Result<Vec<u8>, RpcError> {
|
|
||||||
/*
|
|
||||||
let length = usize::try_from(
|
|
||||||
response
|
|
||||||
.headers()
|
|
||||||
.get("content-length")
|
|
||||||
.ok_or(RpcError::InvalidNode("no content-length header"))?
|
|
||||||
.to_str()
|
|
||||||
.map_err(|_| RpcError::InvalidNode("non-ascii content-length value"))?
|
|
||||||
.parse::<u32>()
|
|
||||||
.map_err(|_| RpcError::InvalidNode("non-u32 content-length value"))?,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
// Only pre-allocate 1 MB so a malicious node which claims a content-length of 1 GB actually
|
|
||||||
// has to send 1 GB of data to cause a 1 GB allocation
|
|
||||||
let mut res = Vec::with_capacity(length.max(1024 * 1024));
|
|
||||||
let mut body = response.into_body();
|
|
||||||
while res.len() < length {
|
|
||||||
let Some(data) = body.data().await else { break };
|
|
||||||
res.extend(data.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?.as_ref());
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
let mut res = Vec::with_capacity(128);
|
|
||||||
response
|
|
||||||
.body()
|
|
||||||
.await
|
|
||||||
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?
|
|
||||||
.read_to_end(&mut res)
|
|
||||||
.unwrap();
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
for attempt in 0 .. 2 {
|
|
||||||
return Ok(match &self.authentication {
|
|
||||||
Authentication::Unauthenticated(client) => {
|
|
||||||
body_from_response(
|
|
||||||
client
|
|
||||||
.request(request_fn(self.url.clone() + "/" + route)?)
|
|
||||||
.await
|
|
||||||
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
}
|
|
||||||
Authentication::Authenticated { username, password, connection } => {
|
|
||||||
let mut connection_lock = connection.lock().await;
|
|
||||||
|
|
||||||
let mut request = request_fn("/".to_string() + route)?;
|
|
||||||
|
|
||||||
// If we don't have an auth challenge, obtain one
|
|
||||||
if connection_lock.0.is_none() {
|
|
||||||
connection_lock.0 = Self::digest_auth_challenge(
|
|
||||||
&connection_lock
|
|
||||||
.1
|
|
||||||
.request(request)
|
|
||||||
.await
|
|
||||||
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?,
|
|
||||||
)?;
|
|
||||||
request = request_fn("/".to_string() + route)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert the challenge response, if we have a challenge
|
|
||||||
if let Some((challenge, cnonce)) = connection_lock.0.as_mut() {
|
|
||||||
// Update the cnonce
|
|
||||||
// Overflow isn't a concern as this is a u64
|
|
||||||
*cnonce += 1;
|
|
||||||
|
|
||||||
let mut context = AuthContext::new_post::<_, _, _, &[u8]>(
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
"/".to_string() + route,
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
context.set_custom_cnonce(hex::encode(cnonce.to_le_bytes()));
|
|
||||||
|
|
||||||
request.headers_mut().insert(
|
|
||||||
"Authorization",
|
|
||||||
HeaderValue::from_str(
|
|
||||||
&challenge
|
|
||||||
.respond(&context)
|
|
||||||
.map_err(|_| {
|
|
||||||
RpcError::InvalidNode("couldn't respond to digest-auth challenge".to_string())
|
|
||||||
})?
|
|
||||||
.to_header_string(),
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let response = connection_lock
|
|
||||||
.1
|
|
||||||
.request(request)
|
|
||||||
.await
|
|
||||||
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")));
|
|
||||||
|
|
||||||
let (error, is_stale) = match &response {
|
|
||||||
Err(e) => (Some(e.clone()), false),
|
|
||||||
Ok(response) => (
|
|
||||||
None,
|
|
||||||
if response.status() == StatusCode::UNAUTHORIZED {
|
|
||||||
if let Some(header) = response.headers().get("www-authenticate") {
|
|
||||||
header
|
|
||||||
.to_str()
|
|
||||||
.map_err(|_| {
|
|
||||||
RpcError::InvalidNode("www-authenticate header wasn't a string".to_string())
|
|
||||||
})?
|
|
||||||
.contains("stale")
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
},
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
// If the connection entered an error state, drop the cached challenge as challenges are
|
|
||||||
// per-connection
|
|
||||||
// We don't need to create a new connection as simple-request will for us
|
|
||||||
if error.is_some() || is_stale {
|
|
||||||
connection_lock.0 = None;
|
|
||||||
// If we're not already on our second attempt, move to the next loop iteration
|
|
||||||
// (retrying all of this once)
|
|
||||||
if attempt == 0 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if let Some(e) = error {
|
|
||||||
Err(e)?
|
|
||||||
} else {
|
|
||||||
debug_assert!(is_stale);
|
|
||||||
Err(RpcError::InvalidNode(
|
|
||||||
"node claimed fresh connection had stale authentication".to_string(),
|
|
||||||
))?
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
body_from_response(response.unwrap()).await?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
unreachable!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl RpcConnection for HttpRpc {
|
|
||||||
async fn post(&self, route: &str, body: Vec<u8>) -> Result<Vec<u8>, RpcError> {
|
|
||||||
tokio::time::timeout(self.request_timeout, self.inner_post(route, body))
|
|
||||||
.await
|
|
||||||
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,739 +0,0 @@
|
|||||||
use core::fmt::Debug;
|
|
||||||
#[cfg(not(feature = "std"))]
|
|
||||||
use alloc::boxed::Box;
|
|
||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io,
|
|
||||||
string::{String, ToString},
|
|
||||||
};
|
|
||||||
|
|
||||||
use async_trait::async_trait;
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::{EdwardsPoint, CompressedEdwardsY};
|
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize, de::DeserializeOwned};
|
|
||||||
use serde_json::{Value, json};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Protocol,
|
|
||||||
serialize::*,
|
|
||||||
transaction::{Input, Timelock, Transaction},
|
|
||||||
block::Block,
|
|
||||||
wallet::{FeePriority, Fee},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "http-rpc")]
|
|
||||||
mod http;
|
|
||||||
#[cfg(feature = "http-rpc")]
|
|
||||||
pub use http::*;
|
|
||||||
|
|
||||||
// Number of blocks the fee estimate will be valid for
|
|
||||||
// https://github.com/monero-project/monero/blob/94e67bf96bbc010241f29ada6abc89f49a81759c/
|
|
||||||
// src/wallet/wallet2.cpp#L121
|
|
||||||
const GRACE_BLOCKS_FOR_FEE_ESTIMATE: u64 = 10;
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
pub struct EmptyResponse {}
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
pub struct JsonRpcResponse<T> {
|
|
||||||
result: T,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct TransactionResponse {
|
|
||||||
tx_hash: String,
|
|
||||||
as_hex: String,
|
|
||||||
pruned_as_hex: String,
|
|
||||||
}
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct TransactionsResponse {
|
|
||||||
#[serde(default)]
|
|
||||||
missed_tx: Vec<String>,
|
|
||||||
txs: Vec<TransactionResponse>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
#[cfg_attr(feature = "std", derive(thiserror::Error))]
|
|
||||||
pub enum RpcError {
|
|
||||||
#[cfg_attr(feature = "std", error("internal error ({0})"))]
|
|
||||||
InternalError(&'static str),
|
|
||||||
#[cfg_attr(feature = "std", error("connection error ({0})"))]
|
|
||||||
ConnectionError(String),
|
|
||||||
#[cfg_attr(feature = "std", error("invalid node ({0})"))]
|
|
||||||
InvalidNode(String),
|
|
||||||
#[cfg_attr(feature = "std", error("unsupported protocol version ({0})"))]
|
|
||||||
UnsupportedProtocol(usize),
|
|
||||||
#[cfg_attr(feature = "std", error("transactions not found"))]
|
|
||||||
TransactionsNotFound(Vec<[u8; 32]>),
|
|
||||||
#[cfg_attr(feature = "std", error("invalid point ({0})"))]
|
|
||||||
InvalidPoint(String),
|
|
||||||
#[cfg_attr(feature = "std", error("pruned transaction"))]
|
|
||||||
PrunedTransaction,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid transaction ({0:?})"))]
|
|
||||||
InvalidTransaction([u8; 32]),
|
|
||||||
#[cfg_attr(feature = "std", error("unexpected fee response"))]
|
|
||||||
InvalidFee,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid priority"))]
|
|
||||||
InvalidPriority,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rpc_hex(value: &str) -> Result<Vec<u8>, RpcError> {
|
|
||||||
hex::decode(value).map_err(|_| RpcError::InvalidNode("expected hex wasn't hex".to_string()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hash_hex(hash: &str) -> Result<[u8; 32], RpcError> {
|
|
||||||
rpc_hex(hash)?.try_into().map_err(|_| RpcError::InvalidNode("hash wasn't 32-bytes".to_string()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rpc_point(point: &str) -> Result<EdwardsPoint, RpcError> {
|
|
||||||
CompressedEdwardsY(
|
|
||||||
rpc_hex(point)?.try_into().map_err(|_| RpcError::InvalidPoint(point.to_string()))?,
|
|
||||||
)
|
|
||||||
.decompress()
|
|
||||||
.ok_or_else(|| RpcError::InvalidPoint(point.to_string()))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read an EPEE VarInt, distinct from the VarInts used throughout the rest of the protocol
|
|
||||||
fn read_epee_vi<R: io::Read>(reader: &mut R) -> io::Result<u64> {
|
|
||||||
let vi_start = read_byte(reader)?;
|
|
||||||
let len = match vi_start & 0b11 {
|
|
||||||
0 => 1,
|
|
||||||
1 => 2,
|
|
||||||
2 => 4,
|
|
||||||
3 => 8,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
let mut vi = u64::from(vi_start >> 2);
|
|
||||||
for i in 1 .. len {
|
|
||||||
vi |= u64::from(read_byte(reader)?) << (((i - 1) * 8) + 6);
|
|
||||||
}
|
|
||||||
Ok(vi)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
pub trait RpcConnection: Clone + Debug {
|
|
||||||
/// Perform a POST request to the specified route with the specified body.
|
|
||||||
///
|
|
||||||
/// The implementor is left to handle anything such as authentication.
|
|
||||||
async fn post(&self, route: &str, body: Vec<u8>) -> Result<Vec<u8>, RpcError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Make this provided methods for RpcConnection?
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct Rpc<R: RpcConnection>(R);
|
|
||||||
impl<R: RpcConnection> Rpc<R> {
|
|
||||||
/// Perform a RPC call to the specified route with the provided parameters.
|
|
||||||
///
|
|
||||||
/// This is NOT a JSON-RPC call. They use a route of "json_rpc" and are available via
|
|
||||||
/// `json_rpc_call`.
|
|
||||||
pub async fn rpc_call<Params: Serialize + Debug, Response: DeserializeOwned + Debug>(
|
|
||||||
&self,
|
|
||||||
route: &str,
|
|
||||||
params: Option<Params>,
|
|
||||||
) -> Result<Response, RpcError> {
|
|
||||||
let res = self
|
|
||||||
.0
|
|
||||||
.post(
|
|
||||||
route,
|
|
||||||
if let Some(params) = params {
|
|
||||||
serde_json::to_string(¶ms).unwrap().into_bytes()
|
|
||||||
} else {
|
|
||||||
vec![]
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let res_str = std_shims::str::from_utf8(&res)
|
|
||||||
.map_err(|_| RpcError::InvalidNode("response wasn't utf-8".to_string()))?;
|
|
||||||
serde_json::from_str(res_str)
|
|
||||||
.map_err(|_| RpcError::InvalidNode(format!("response wasn't json: {res_str}")))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Perform a JSON-RPC call with the specified method with the provided parameters
|
|
||||||
pub async fn json_rpc_call<Response: DeserializeOwned + Debug>(
|
|
||||||
&self,
|
|
||||||
method: &str,
|
|
||||||
params: Option<Value>,
|
|
||||||
) -> Result<Response, RpcError> {
|
|
||||||
let mut req = json!({ "method": method });
|
|
||||||
if let Some(params) = params {
|
|
||||||
req.as_object_mut().unwrap().insert("params".into(), params);
|
|
||||||
}
|
|
||||||
Ok(self.rpc_call::<_, JsonRpcResponse<Response>>("json_rpc", Some(req)).await?.result)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Perform a binary call to the specified route with the provided parameters.
|
|
||||||
pub async fn bin_call(&self, route: &str, params: Vec<u8>) -> Result<Vec<u8>, RpcError> {
|
|
||||||
self.0.post(route, params).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the active blockchain protocol version.
|
|
||||||
pub async fn get_protocol(&self) -> Result<Protocol, RpcError> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct ProtocolResponse {
|
|
||||||
major_version: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct LastHeaderResponse {
|
|
||||||
block_header: ProtocolResponse,
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(
|
|
||||||
match self
|
|
||||||
.json_rpc_call::<LastHeaderResponse>("get_last_block_header", None)
|
|
||||||
.await?
|
|
||||||
.block_header
|
|
||||||
.major_version
|
|
||||||
{
|
|
||||||
13 | 14 => Protocol::v14,
|
|
||||||
15 | 16 => Protocol::v16,
|
|
||||||
protocol => Err(RpcError::UnsupportedProtocol(protocol))?,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_height(&self) -> Result<usize, RpcError> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct HeightResponse {
|
|
||||||
height: usize,
|
|
||||||
}
|
|
||||||
Ok(self.rpc_call::<Option<()>, HeightResponse>("get_height", None).await?.height)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_transactions(&self, hashes: &[[u8; 32]]) -> Result<Vec<Transaction>, RpcError> {
|
|
||||||
if hashes.is_empty() {
|
|
||||||
return Ok(vec![]);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut hashes_hex = hashes.iter().map(hex::encode).collect::<Vec<_>>();
|
|
||||||
let mut all_txs = Vec::with_capacity(hashes.len());
|
|
||||||
while !hashes_hex.is_empty() {
|
|
||||||
// Monero errors if more than 100 is requested unless using a non-restricted RPC
|
|
||||||
const TXS_PER_REQUEST: usize = 100;
|
|
||||||
let this_count = TXS_PER_REQUEST.min(hashes_hex.len());
|
|
||||||
|
|
||||||
let txs: TransactionsResponse = self
|
|
||||||
.rpc_call(
|
|
||||||
"get_transactions",
|
|
||||||
Some(json!({
|
|
||||||
"txs_hashes": hashes_hex.drain(.. this_count).collect::<Vec<_>>(),
|
|
||||||
})),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if !txs.missed_tx.is_empty() {
|
|
||||||
Err(RpcError::TransactionsNotFound(
|
|
||||||
txs.missed_tx.iter().map(|hash| hash_hex(hash)).collect::<Result<_, _>>()?,
|
|
||||||
))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
all_txs.extend(txs.txs);
|
|
||||||
}
|
|
||||||
|
|
||||||
all_txs
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, res)| {
|
|
||||||
let tx = Transaction::read::<&[u8]>(
|
|
||||||
&mut rpc_hex(if !res.as_hex.is_empty() { &res.as_hex } else { &res.pruned_as_hex })?
|
|
||||||
.as_ref(),
|
|
||||||
)
|
|
||||||
.map_err(|_| match hash_hex(&res.tx_hash) {
|
|
||||||
Ok(hash) => RpcError::InvalidTransaction(hash),
|
|
||||||
Err(err) => err,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// https://github.com/monero-project/monero/issues/8311
|
|
||||||
if res.as_hex.is_empty() {
|
|
||||||
match tx.prefix.inputs.first() {
|
|
||||||
Some(Input::Gen { .. }) => (),
|
|
||||||
_ => Err(RpcError::PrunedTransaction)?,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This does run a few keccak256 hashes, which is pointless if the node is trusted
|
|
||||||
// In exchange, this provides resilience against invalid/malicious nodes
|
|
||||||
if tx.hash() != hashes[i] {
|
|
||||||
Err(RpcError::InvalidNode(
|
|
||||||
"replied with transaction wasn't the requested transaction".to_string(),
|
|
||||||
))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(tx)
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_transaction(&self, tx: [u8; 32]) -> Result<Transaction, RpcError> {
|
|
||||||
self.get_transactions(&[tx]).await.map(|mut txs| txs.swap_remove(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the hash of a block from the node by the block's numbers.
|
|
||||||
/// This function does not verify the returned block hash is actually for the number in question.
|
|
||||||
pub async fn get_block_hash(&self, number: usize) -> Result<[u8; 32], RpcError> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct BlockHeaderResponse {
|
|
||||||
hash: String,
|
|
||||||
}
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct BlockHeaderByHeightResponse {
|
|
||||||
block_header: BlockHeaderResponse,
|
|
||||||
}
|
|
||||||
|
|
||||||
let header: BlockHeaderByHeightResponse =
|
|
||||||
self.json_rpc_call("get_block_header_by_height", Some(json!({ "height": number }))).await?;
|
|
||||||
hash_hex(&header.block_header.hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get a block from the node by its hash.
|
|
||||||
/// This function does not verify the returned block actually has the hash in question.
|
|
||||||
pub async fn get_block(&self, hash: [u8; 32]) -> Result<Block, RpcError> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct BlockResponse {
|
|
||||||
blob: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
let res: BlockResponse =
|
|
||||||
self.json_rpc_call("get_block", Some(json!({ "hash": hex::encode(hash) }))).await?;
|
|
||||||
|
|
||||||
let block = Block::read::<&[u8]>(&mut rpc_hex(&res.blob)?.as_ref())
|
|
||||||
.map_err(|_| RpcError::InvalidNode("invalid block".to_string()))?;
|
|
||||||
if block.hash() != hash {
|
|
||||||
Err(RpcError::InvalidNode("different block than requested (hash)".to_string()))?;
|
|
||||||
}
|
|
||||||
Ok(block)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_block_by_number(&self, number: usize) -> Result<Block, RpcError> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct BlockResponse {
|
|
||||||
blob: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
let res: BlockResponse =
|
|
||||||
self.json_rpc_call("get_block", Some(json!({ "height": number }))).await?;
|
|
||||||
|
|
||||||
let block = Block::read::<&[u8]>(&mut rpc_hex(&res.blob)?.as_ref())
|
|
||||||
.map_err(|_| RpcError::InvalidNode("invalid block".to_string()))?;
|
|
||||||
|
|
||||||
// Make sure this is actually the block for this number
|
|
||||||
match block.miner_tx.prefix.inputs.first() {
|
|
||||||
Some(Input::Gen(actual)) => {
|
|
||||||
if usize::try_from(*actual).unwrap() == number {
|
|
||||||
Ok(block)
|
|
||||||
} else {
|
|
||||||
Err(RpcError::InvalidNode("different block than requested (number)".to_string()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => Err(RpcError::InvalidNode(
|
|
||||||
"block's miner_tx didn't have an input of kind Input::Gen".to_string(),
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_block_transactions(&self, hash: [u8; 32]) -> Result<Vec<Transaction>, RpcError> {
|
|
||||||
let block = self.get_block(hash).await?;
|
|
||||||
let mut res = vec![block.miner_tx];
|
|
||||||
res.extend(self.get_transactions(&block.txs).await?);
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_block_transactions_by_number(
|
|
||||||
&self,
|
|
||||||
number: usize,
|
|
||||||
) -> Result<Vec<Transaction>, RpcError> {
|
|
||||||
self.get_block_transactions(self.get_block_hash(number).await?).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the output indexes of the specified transaction.
|
|
||||||
pub async fn get_o_indexes(&self, hash: [u8; 32]) -> Result<Vec<u64>, RpcError> {
|
|
||||||
/*
|
|
||||||
TODO: Use these when a suitable epee serde lib exists
|
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
|
||||||
struct Request {
|
|
||||||
txid: [u8; 32],
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct OIndexes {
|
|
||||||
o_indexes: Vec<u64>,
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Given the immaturity of Rust epee libraries, this is a homegrown one which is only validated
|
|
||||||
// to work against this specific function
|
|
||||||
|
|
||||||
// Header for EPEE, an 8-byte magic and a version
|
|
||||||
const EPEE_HEADER: &[u8] = b"\x01\x11\x01\x01\x01\x01\x02\x01\x01";
|
|
||||||
|
|
||||||
let mut request = EPEE_HEADER.to_vec();
|
|
||||||
// Number of fields (shifted over 2 bits as the 2 LSBs are reserved for metadata)
|
|
||||||
request.push(1 << 2);
|
|
||||||
// Length of field name
|
|
||||||
request.push(4);
|
|
||||||
// Field name
|
|
||||||
request.extend(b"txid");
|
|
||||||
// Type of field
|
|
||||||
request.push(10);
|
|
||||||
// Length of string, since this byte array is technically a string
|
|
||||||
request.push(32 << 2);
|
|
||||||
// The "string"
|
|
||||||
request.extend(hash);
|
|
||||||
|
|
||||||
let indexes_buf = self.bin_call("get_o_indexes.bin", request).await?;
|
|
||||||
let mut indexes: &[u8] = indexes_buf.as_ref();
|
|
||||||
|
|
||||||
(|| {
|
|
||||||
let mut res = None;
|
|
||||||
let mut is_okay = false;
|
|
||||||
|
|
||||||
if read_bytes::<_, { EPEE_HEADER.len() }>(&mut indexes)? != EPEE_HEADER {
|
|
||||||
Err(io::Error::other("invalid header"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let read_object = |reader: &mut &[u8]| -> io::Result<Vec<u64>> {
|
|
||||||
let fields = read_byte(reader)? >> 2;
|
|
||||||
|
|
||||||
for _ in 0 .. fields {
|
|
||||||
let name_len = read_byte(reader)?;
|
|
||||||
let name = read_raw_vec(read_byte, name_len.into(), reader)?;
|
|
||||||
|
|
||||||
let type_with_array_flag = read_byte(reader)?;
|
|
||||||
let kind = type_with_array_flag & (!0x80);
|
|
||||||
|
|
||||||
let iters = if type_with_array_flag != kind { read_epee_vi(reader)? } else { 1 };
|
|
||||||
|
|
||||||
if (&name == b"o_indexes") && (kind != 5) {
|
|
||||||
Err(io::Error::other("o_indexes weren't u64s"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let f = match kind {
|
|
||||||
// i64
|
|
||||||
1 => |reader: &mut &[u8]| read_raw_vec(read_byte, 8, reader),
|
|
||||||
// i32
|
|
||||||
2 => |reader: &mut &[u8]| read_raw_vec(read_byte, 4, reader),
|
|
||||||
// i16
|
|
||||||
3 => |reader: &mut &[u8]| read_raw_vec(read_byte, 2, reader),
|
|
||||||
// i8
|
|
||||||
4 => |reader: &mut &[u8]| read_raw_vec(read_byte, 1, reader),
|
|
||||||
// u64
|
|
||||||
5 => |reader: &mut &[u8]| read_raw_vec(read_byte, 8, reader),
|
|
||||||
// u32
|
|
||||||
6 => |reader: &mut &[u8]| read_raw_vec(read_byte, 4, reader),
|
|
||||||
// u16
|
|
||||||
7 => |reader: &mut &[u8]| read_raw_vec(read_byte, 2, reader),
|
|
||||||
// u8
|
|
||||||
8 => |reader: &mut &[u8]| read_raw_vec(read_byte, 1, reader),
|
|
||||||
// double
|
|
||||||
9 => |reader: &mut &[u8]| read_raw_vec(read_byte, 8, reader),
|
|
||||||
// string, or any collection of bytes
|
|
||||||
10 => |reader: &mut &[u8]| {
|
|
||||||
let len = read_epee_vi(reader)?;
|
|
||||||
read_raw_vec(
|
|
||||||
read_byte,
|
|
||||||
len.try_into().map_err(|_| io::Error::other("u64 length exceeded usize"))?,
|
|
||||||
reader,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
// bool
|
|
||||||
11 => |reader: &mut &[u8]| read_raw_vec(read_byte, 1, reader),
|
|
||||||
// object, errors here as it shouldn't be used on this call
|
|
||||||
12 => {
|
|
||||||
|_: &mut &[u8]| Err(io::Error::other("node used object in reply to get_o_indexes"))
|
|
||||||
}
|
|
||||||
// array, so far unused
|
|
||||||
13 => |_: &mut &[u8]| Err(io::Error::other("node used the unused array type")),
|
|
||||||
_ => |_: &mut &[u8]| Err(io::Error::other("node used an invalid type")),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut bytes_res = vec![];
|
|
||||||
for _ in 0 .. iters {
|
|
||||||
bytes_res.push(f(reader)?);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut actual_res = Vec::with_capacity(bytes_res.len());
|
|
||||||
match name.as_slice() {
|
|
||||||
b"o_indexes" => {
|
|
||||||
for o_index in bytes_res {
|
|
||||||
actual_res.push(u64::from_le_bytes(
|
|
||||||
o_index
|
|
||||||
.try_into()
|
|
||||||
.map_err(|_| io::Error::other("node didn't provide 8 bytes for a u64"))?,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
res = Some(actual_res);
|
|
||||||
}
|
|
||||||
b"status" => {
|
|
||||||
if bytes_res
|
|
||||||
.first()
|
|
||||||
.ok_or_else(|| io::Error::other("status wasn't a string"))?
|
|
||||||
.as_slice() !=
|
|
||||||
b"OK"
|
|
||||||
{
|
|
||||||
// TODO: Better handle non-OK responses
|
|
||||||
Err(io::Error::other("response wasn't OK"))?;
|
|
||||||
}
|
|
||||||
is_okay = true;
|
|
||||||
}
|
|
||||||
_ => continue,
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_okay && res.is_some() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Didn't return a response with a status
|
|
||||||
// (if the status wasn't okay, we would've already errored)
|
|
||||||
if !is_okay {
|
|
||||||
Err(io::Error::other("response didn't contain a status"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the Vec was empty, it would've been omitted, hence the unwrap_or
|
|
||||||
// TODO: Test against a 0-output TX, such as the ones found in block 202612
|
|
||||||
Ok(res.unwrap_or(vec![]))
|
|
||||||
};
|
|
||||||
|
|
||||||
read_object(&mut indexes)
|
|
||||||
})()
|
|
||||||
.map_err(|_| RpcError::InvalidNode("invalid binary response".to_string()))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the output distribution, from the specified height to the specified height (both
|
|
||||||
/// inclusive).
|
|
||||||
pub async fn get_output_distribution(
|
|
||||||
&self,
|
|
||||||
from: usize,
|
|
||||||
to: usize,
|
|
||||||
) -> Result<Vec<u64>, RpcError> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct Distribution {
|
|
||||||
distribution: Vec<u64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct Distributions {
|
|
||||||
distributions: Vec<Distribution>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut distributions: Distributions = self
|
|
||||||
.json_rpc_call(
|
|
||||||
"get_output_distribution",
|
|
||||||
Some(json!({
|
|
||||||
"binary": false,
|
|
||||||
"amounts": [0],
|
|
||||||
"cumulative": true,
|
|
||||||
"from_height": from,
|
|
||||||
"to_height": to,
|
|
||||||
})),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(distributions.distributions.swap_remove(0).distribution)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the specified outputs from the RingCT (zero-amount) pool, but only return them if their
|
|
||||||
/// timelock has been satisfied.
|
|
||||||
///
|
|
||||||
/// The timelock being satisfied is distinct from being free of the 10-block lock applied to all
|
|
||||||
/// Monero transactions.
|
|
||||||
pub async fn get_unlocked_outputs(
|
|
||||||
&self,
|
|
||||||
indexes: &[u64],
|
|
||||||
height: usize,
|
|
||||||
) -> Result<Vec<Option<[EdwardsPoint; 2]>>, RpcError> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct Out {
|
|
||||||
key: String,
|
|
||||||
mask: String,
|
|
||||||
txid: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct Outs {
|
|
||||||
outs: Vec<Out>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let outs: Outs = self
|
|
||||||
.rpc_call(
|
|
||||||
"get_outs",
|
|
||||||
Some(json!({
|
|
||||||
"get_txid": true,
|
|
||||||
"outputs": indexes.iter().map(|o| json!({
|
|
||||||
"amount": 0,
|
|
||||||
"index": o
|
|
||||||
})).collect::<Vec<_>>()
|
|
||||||
})),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let txs = self
|
|
||||||
.get_transactions(
|
|
||||||
&outs.outs.iter().map(|out| hash_hex(&out.txid)).collect::<Result<Vec<_>, _>>()?,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// TODO: https://github.com/serai-dex/serai/issues/104
|
|
||||||
outs
|
|
||||||
.outs
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, out)| {
|
|
||||||
// Allow keys to be invalid, though if they are, return None to trigger selection of a new
|
|
||||||
// decoy
|
|
||||||
// Only valid keys can be used in CLSAG proofs, hence the need for re-selection, yet
|
|
||||||
// invalid keys may honestly exist on the blockchain
|
|
||||||
// Only a recent hard fork checked output keys were valid points
|
|
||||||
let Some(key) = CompressedEdwardsY(
|
|
||||||
rpc_hex(&out.key)?
|
|
||||||
.try_into()
|
|
||||||
.map_err(|_| RpcError::InvalidNode("non-32-byte point".to_string()))?,
|
|
||||||
)
|
|
||||||
.decompress() else {
|
|
||||||
return Ok(None);
|
|
||||||
};
|
|
||||||
Ok(
|
|
||||||
Some([key, rpc_point(&out.mask)?])
|
|
||||||
.filter(|_| Timelock::Block(height) >= txs[i].prefix.timelock),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_fee_v14(&self, priority: FeePriority) -> Result<Fee, RpcError> {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct FeeResponseV14 {
|
|
||||||
status: String,
|
|
||||||
fee: u64,
|
|
||||||
quantization_mask: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/monero-project/monero/blob/94e67bf96bbc010241f29ada6abc89f49a81759c/
|
|
||||||
// src/wallet/wallet2.cpp#L7569-L7584
|
|
||||||
// https://github.com/monero-project/monero/blob/94e67bf96bbc010241f29ada6abc89f49a81759c/
|
|
||||||
// src/wallet/wallet2.cpp#L7660-L7661
|
|
||||||
let priority_idx =
|
|
||||||
usize::try_from(if priority.fee_priority() == 0 { 1 } else { priority.fee_priority() - 1 })
|
|
||||||
.map_err(|_| RpcError::InvalidPriority)?;
|
|
||||||
let multipliers = [1, 5, 25, 1000];
|
|
||||||
if priority_idx >= multipliers.len() {
|
|
||||||
// though not an RPC error, it seems sensible to treat as such
|
|
||||||
Err(RpcError::InvalidPriority)?;
|
|
||||||
}
|
|
||||||
let fee_multiplier = multipliers[priority_idx];
|
|
||||||
|
|
||||||
let res: FeeResponseV14 = self
|
|
||||||
.json_rpc_call(
|
|
||||||
"get_fee_estimate",
|
|
||||||
Some(json!({ "grace_blocks": GRACE_BLOCKS_FOR_FEE_ESTIMATE })),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if res.status != "OK" {
|
|
||||||
Err(RpcError::InvalidFee)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Fee { per_weight: res.fee * fee_multiplier, mask: res.quantization_mask })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the currently estimated fee from the node.
|
|
||||||
///
|
|
||||||
/// This may be manipulated to unsafe levels and MUST be sanity checked.
|
|
||||||
// TODO: Take a sanity check argument
|
|
||||||
pub async fn get_fee(&self, protocol: Protocol, priority: FeePriority) -> Result<Fee, RpcError> {
|
|
||||||
// TODO: Implement wallet2's adjust_priority which by default automatically uses a lower
|
|
||||||
// priority than provided depending on the backlog in the pool
|
|
||||||
if protocol.v16_fee() {
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct FeeResponse {
|
|
||||||
status: String,
|
|
||||||
fees: Vec<u64>,
|
|
||||||
quantization_mask: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
let res: FeeResponse = self
|
|
||||||
.json_rpc_call(
|
|
||||||
"get_fee_estimate",
|
|
||||||
Some(json!({ "grace_blocks": GRACE_BLOCKS_FOR_FEE_ESTIMATE })),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// https://github.com/monero-project/monero/blob/94e67bf96bbc010241f29ada6abc89f49a81759c/
|
|
||||||
// src/wallet/wallet2.cpp#L7615-L7620
|
|
||||||
let priority_idx = usize::try_from(if priority.fee_priority() >= 4 {
|
|
||||||
3
|
|
||||||
} else {
|
|
||||||
priority.fee_priority().saturating_sub(1)
|
|
||||||
})
|
|
||||||
.map_err(|_| RpcError::InvalidPriority)?;
|
|
||||||
|
|
||||||
if res.status != "OK" {
|
|
||||||
Err(RpcError::InvalidFee)
|
|
||||||
} else if priority_idx >= res.fees.len() {
|
|
||||||
Err(RpcError::InvalidPriority)
|
|
||||||
} else {
|
|
||||||
Ok(Fee { per_weight: res.fees[priority_idx], mask: res.quantization_mask })
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.get_fee_v14(priority).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn publish_transaction(&self, tx: &Transaction) -> Result<(), RpcError> {
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Deserialize, Debug)]
|
|
||||||
struct SendRawResponse {
|
|
||||||
status: String,
|
|
||||||
double_spend: bool,
|
|
||||||
fee_too_low: bool,
|
|
||||||
invalid_input: bool,
|
|
||||||
invalid_output: bool,
|
|
||||||
low_mixin: bool,
|
|
||||||
not_relayed: bool,
|
|
||||||
overspend: bool,
|
|
||||||
too_big: bool,
|
|
||||||
too_few_outputs: bool,
|
|
||||||
reason: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
let res: SendRawResponse = self
|
|
||||||
.rpc_call("send_raw_transaction", Some(json!({ "tx_as_hex": hex::encode(tx.serialize()) })))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if res.status != "OK" {
|
|
||||||
Err(RpcError::InvalidTransaction(tx.hash()))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Take &Address, not &str?
|
|
||||||
pub async fn generate_blocks(
|
|
||||||
&self,
|
|
||||||
address: &str,
|
|
||||||
block_count: usize,
|
|
||||||
) -> Result<Vec<[u8; 32]>, RpcError> {
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct BlocksResponse {
|
|
||||||
blocks: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let block_strs = self
|
|
||||||
.json_rpc_call::<BlocksResponse>(
|
|
||||||
"generateblocks",
|
|
||||||
Some(json!({
|
|
||||||
"wallet_address": address,
|
|
||||||
"amount_of_blocks": block_count
|
|
||||||
})),
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
.blocks;
|
|
||||||
|
|
||||||
let mut blocks = Vec::with_capacity(block_strs.len());
|
|
||||||
for block in block_strs {
|
|
||||||
blocks.push(hash_hex(&block)?);
|
|
||||||
}
|
|
||||||
Ok(blocks)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,167 +0,0 @@
|
|||||||
use core::fmt::Debug;
|
|
||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use curve25519_dalek::{
|
|
||||||
scalar::Scalar,
|
|
||||||
edwards::{EdwardsPoint, CompressedEdwardsY},
|
|
||||||
};
|
|
||||||
|
|
||||||
const VARINT_CONTINUATION_MASK: u8 = 0b1000_0000;
|
|
||||||
|
|
||||||
mod sealed {
|
|
||||||
pub trait VarInt: TryInto<u64> + TryFrom<u64> + Copy {}
|
|
||||||
impl VarInt for u8 {}
|
|
||||||
impl VarInt for u32 {}
|
|
||||||
impl VarInt for u64 {}
|
|
||||||
impl VarInt for usize {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This will panic if the VarInt exceeds u64::MAX
|
|
||||||
pub(crate) fn varint_len<U: sealed::VarInt>(varint: U) -> usize {
|
|
||||||
let varint_u64: u64 = varint.try_into().map_err(|_| "varint exceeded u64").unwrap();
|
|
||||||
((usize::try_from(u64::BITS - varint_u64.leading_zeros()).unwrap().saturating_sub(1)) / 7) + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write_byte<W: Write>(byte: &u8, w: &mut W) -> io::Result<()> {
|
|
||||||
w.write_all(&[*byte])
|
|
||||||
}
|
|
||||||
|
|
||||||
// This will panic if the VarInt exceeds u64::MAX
|
|
||||||
pub(crate) fn write_varint<W: Write, U: sealed::VarInt>(varint: &U, w: &mut W) -> io::Result<()> {
|
|
||||||
let mut varint: u64 = (*varint).try_into().map_err(|_| "varint exceeded u64").unwrap();
|
|
||||||
while {
|
|
||||||
let mut b = u8::try_from(varint & u64::from(!VARINT_CONTINUATION_MASK)).unwrap();
|
|
||||||
varint >>= 7;
|
|
||||||
if varint != 0 {
|
|
||||||
b |= VARINT_CONTINUATION_MASK;
|
|
||||||
}
|
|
||||||
write_byte(&b, w)?;
|
|
||||||
varint != 0
|
|
||||||
} {}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write_scalar<W: Write>(scalar: &Scalar, w: &mut W) -> io::Result<()> {
|
|
||||||
w.write_all(&scalar.to_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write_point<W: Write>(point: &EdwardsPoint, w: &mut W) -> io::Result<()> {
|
|
||||||
w.write_all(&point.compress().to_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write_raw_vec<T, W: Write, F: Fn(&T, &mut W) -> io::Result<()>>(
|
|
||||||
f: F,
|
|
||||||
values: &[T],
|
|
||||||
w: &mut W,
|
|
||||||
) -> io::Result<()> {
|
|
||||||
for value in values {
|
|
||||||
f(value, w)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write_vec<T, W: Write, F: Fn(&T, &mut W) -> io::Result<()>>(
|
|
||||||
f: F,
|
|
||||||
values: &[T],
|
|
||||||
w: &mut W,
|
|
||||||
) -> io::Result<()> {
|
|
||||||
write_varint(&values.len(), w)?;
|
|
||||||
write_raw_vec(f, values, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_bytes<R: Read, const N: usize>(r: &mut R) -> io::Result<[u8; N]> {
|
|
||||||
let mut res = [0; N];
|
|
||||||
r.read_exact(&mut res)?;
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_byte<R: Read>(r: &mut R) -> io::Result<u8> {
|
|
||||||
Ok(read_bytes::<_, 1>(r)?[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_u16<R: Read>(r: &mut R) -> io::Result<u16> {
|
|
||||||
read_bytes(r).map(u16::from_le_bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_u32<R: Read>(r: &mut R) -> io::Result<u32> {
|
|
||||||
read_bytes(r).map(u32::from_le_bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_u64<R: Read>(r: &mut R) -> io::Result<u64> {
|
|
||||||
read_bytes(r).map(u64::from_le_bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_varint<R: Read, U: sealed::VarInt>(r: &mut R) -> io::Result<U> {
|
|
||||||
let mut bits = 0;
|
|
||||||
let mut res = 0;
|
|
||||||
while {
|
|
||||||
let b = read_byte(r)?;
|
|
||||||
if (bits != 0) && (b == 0) {
|
|
||||||
Err(io::Error::other("non-canonical varint"))?;
|
|
||||||
}
|
|
||||||
if ((bits + 7) > 64) && (b >= (1 << (64 - bits))) {
|
|
||||||
Err(io::Error::other("varint overflow"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
res += u64::from(b & (!VARINT_CONTINUATION_MASK)) << bits;
|
|
||||||
bits += 7;
|
|
||||||
b & VARINT_CONTINUATION_MASK == VARINT_CONTINUATION_MASK
|
|
||||||
} {}
|
|
||||||
res.try_into().map_err(|_| io::Error::other("VarInt does not fit into integer type"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// All scalar fields supported by monero-serai are checked to be canonical for valid transactions
|
|
||||||
// While from_bytes_mod_order would be more flexible, it's not currently needed and would be
|
|
||||||
// inaccurate to include now. While casting a wide net may be preferable, it'd also be inaccurate
|
|
||||||
// for now. There's also further edge cases as noted by
|
|
||||||
// https://github.com/monero-project/monero/issues/8438, where some scalars had an archaic
|
|
||||||
// reduction applied
|
|
||||||
pub(crate) fn read_scalar<R: Read>(r: &mut R) -> io::Result<Scalar> {
|
|
||||||
Option::from(Scalar::from_canonical_bytes(read_bytes(r)?))
|
|
||||||
.ok_or_else(|| io::Error::other("unreduced scalar"))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_point<R: Read>(r: &mut R) -> io::Result<EdwardsPoint> {
|
|
||||||
let bytes = read_bytes(r)?;
|
|
||||||
CompressedEdwardsY(bytes)
|
|
||||||
.decompress()
|
|
||||||
// Ban points which are either unreduced or -0
|
|
||||||
.filter(|point| point.compress().to_bytes() == bytes)
|
|
||||||
.ok_or_else(|| io::Error::other("invalid point"))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_torsion_free_point<R: Read>(r: &mut R) -> io::Result<EdwardsPoint> {
|
|
||||||
read_point(r)
|
|
||||||
.ok()
|
|
||||||
.filter(EdwardsPoint::is_torsion_free)
|
|
||||||
.ok_or_else(|| io::Error::other("invalid point"))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_raw_vec<R: Read, T, F: Fn(&mut R) -> io::Result<T>>(
|
|
||||||
f: F,
|
|
||||||
len: usize,
|
|
||||||
r: &mut R,
|
|
||||||
) -> io::Result<Vec<T>> {
|
|
||||||
let mut res = vec![];
|
|
||||||
for _ in 0 .. len {
|
|
||||||
res.push(f(r)?);
|
|
||||||
}
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_array<R: Read, T: Debug, F: Fn(&mut R) -> io::Result<T>, const N: usize>(
|
|
||||||
f: F,
|
|
||||||
r: &mut R,
|
|
||||||
) -> io::Result<[T; N]> {
|
|
||||||
read_raw_vec(f, N, r).map(|vec| vec.try_into().unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn read_vec<R: Read, T, F: Fn(&mut R) -> io::Result<T>>(
|
|
||||||
f: F,
|
|
||||||
r: &mut R,
|
|
||||||
) -> io::Result<Vec<T>> {
|
|
||||||
read_raw_vec(f, read_varint(r)?, r)
|
|
||||||
}
|
|
||||||
@@ -1,180 +0,0 @@
|
|||||||
use hex_literal::hex;
|
|
||||||
|
|
||||||
use rand_core::{RngCore, OsRng};
|
|
||||||
|
|
||||||
use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, edwards::CompressedEdwardsY};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
random_scalar,
|
|
||||||
wallet::address::{Network, AddressType, AddressMeta, MoneroAddress},
|
|
||||||
};
|
|
||||||
|
|
||||||
const SPEND: [u8; 32] = hex!("f8631661f6ab4e6fda310c797330d86e23a682f20d5bc8cc27b18051191f16d7");
|
|
||||||
const VIEW: [u8; 32] = hex!("4a1535063ad1fee2dabbf909d4fd9a873e29541b401f0944754e17c9a41820ce");
|
|
||||||
|
|
||||||
const STANDARD: &str =
|
|
||||||
"4B33mFPMq6mKi7Eiyd5XuyKRVMGVZz1Rqb9ZTyGApXW5d1aT7UBDZ89ewmnWFkzJ5wPd2SFbn313vCT8a4E2Qf4KQH4pNey";
|
|
||||||
|
|
||||||
const PAYMENT_ID: [u8; 8] = hex!("b8963a57855cf73f");
|
|
||||||
const INTEGRATED: &str =
|
|
||||||
"4Ljin4CrSNHKi7Eiyd5XuyKRVMGVZz1Rqb9ZTyGApXW5d1aT7UBDZ89ewmnWFkzJ5wPd2SFbn313vCT8a4E2Qf4KbaTH6Mn\
|
|
||||||
pXSn88oBX35";
|
|
||||||
|
|
||||||
const SUB_SPEND: [u8; 32] =
|
|
||||||
hex!("fe358188b528335ad1cfdc24a22a23988d742c882b6f19a602892eaab3c1b62b");
|
|
||||||
const SUB_VIEW: [u8; 32] = hex!("9bc2b464de90d058468522098d5610c5019c45fd1711a9517db1eea7794f5470");
|
|
||||||
const SUBADDRESS: &str =
|
|
||||||
"8C5zHM5ud8nGC4hC2ULiBLSWx9infi8JUUmWEat4fcTf8J4H38iWYVdFmPCA9UmfLTZxD43RsyKnGEdZkoGij6csDeUnbEB";
|
|
||||||
|
|
||||||
const FEATURED_JSON: &str = include_str!("vectors/featured_addresses.json");
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn standard_address() {
|
|
||||||
let addr = MoneroAddress::from_str(Network::Mainnet, STANDARD).unwrap();
|
|
||||||
assert_eq!(addr.meta.network, Network::Mainnet);
|
|
||||||
assert_eq!(addr.meta.kind, AddressType::Standard);
|
|
||||||
assert!(!addr.meta.kind.is_subaddress());
|
|
||||||
assert_eq!(addr.meta.kind.payment_id(), None);
|
|
||||||
assert!(!addr.meta.kind.is_guaranteed());
|
|
||||||
assert_eq!(addr.spend.compress().to_bytes(), SPEND);
|
|
||||||
assert_eq!(addr.view.compress().to_bytes(), VIEW);
|
|
||||||
assert_eq!(addr.to_string(), STANDARD);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn integrated_address() {
|
|
||||||
let addr = MoneroAddress::from_str(Network::Mainnet, INTEGRATED).unwrap();
|
|
||||||
assert_eq!(addr.meta.network, Network::Mainnet);
|
|
||||||
assert_eq!(addr.meta.kind, AddressType::Integrated(PAYMENT_ID));
|
|
||||||
assert!(!addr.meta.kind.is_subaddress());
|
|
||||||
assert_eq!(addr.meta.kind.payment_id(), Some(PAYMENT_ID));
|
|
||||||
assert!(!addr.meta.kind.is_guaranteed());
|
|
||||||
assert_eq!(addr.spend.compress().to_bytes(), SPEND);
|
|
||||||
assert_eq!(addr.view.compress().to_bytes(), VIEW);
|
|
||||||
assert_eq!(addr.to_string(), INTEGRATED);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn subaddress() {
|
|
||||||
let addr = MoneroAddress::from_str(Network::Mainnet, SUBADDRESS).unwrap();
|
|
||||||
assert_eq!(addr.meta.network, Network::Mainnet);
|
|
||||||
assert_eq!(addr.meta.kind, AddressType::Subaddress);
|
|
||||||
assert!(addr.meta.kind.is_subaddress());
|
|
||||||
assert_eq!(addr.meta.kind.payment_id(), None);
|
|
||||||
assert!(!addr.meta.kind.is_guaranteed());
|
|
||||||
assert_eq!(addr.spend.compress().to_bytes(), SUB_SPEND);
|
|
||||||
assert_eq!(addr.view.compress().to_bytes(), SUB_VIEW);
|
|
||||||
assert_eq!(addr.to_string(), SUBADDRESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn featured() {
|
|
||||||
for (network, first) in
|
|
||||||
[(Network::Mainnet, 'C'), (Network::Testnet, 'K'), (Network::Stagenet, 'F')]
|
|
||||||
{
|
|
||||||
for _ in 0 .. 100 {
|
|
||||||
let spend = &random_scalar(&mut OsRng) * ED25519_BASEPOINT_TABLE;
|
|
||||||
let view = &random_scalar(&mut OsRng) * ED25519_BASEPOINT_TABLE;
|
|
||||||
|
|
||||||
for features in 0 .. (1 << 3) {
|
|
||||||
const SUBADDRESS_FEATURE_BIT: u8 = 1;
|
|
||||||
const INTEGRATED_FEATURE_BIT: u8 = 1 << 1;
|
|
||||||
const GUARANTEED_FEATURE_BIT: u8 = 1 << 2;
|
|
||||||
|
|
||||||
let subaddress = (features & SUBADDRESS_FEATURE_BIT) == SUBADDRESS_FEATURE_BIT;
|
|
||||||
|
|
||||||
let mut payment_id = [0; 8];
|
|
||||||
OsRng.fill_bytes(&mut payment_id);
|
|
||||||
let payment_id = Some(payment_id)
|
|
||||||
.filter(|_| (features & INTEGRATED_FEATURE_BIT) == INTEGRATED_FEATURE_BIT);
|
|
||||||
|
|
||||||
let guaranteed = (features & GUARANTEED_FEATURE_BIT) == GUARANTEED_FEATURE_BIT;
|
|
||||||
|
|
||||||
let kind = AddressType::Featured { subaddress, payment_id, guaranteed };
|
|
||||||
let meta = AddressMeta::new(network, kind);
|
|
||||||
let addr = MoneroAddress::new(meta, spend, view);
|
|
||||||
|
|
||||||
assert_eq!(addr.to_string().chars().next().unwrap(), first);
|
|
||||||
assert_eq!(MoneroAddress::from_str(network, &addr.to_string()).unwrap(), addr);
|
|
||||||
|
|
||||||
assert_eq!(addr.spend, spend);
|
|
||||||
assert_eq!(addr.view, view);
|
|
||||||
|
|
||||||
assert_eq!(addr.is_subaddress(), subaddress);
|
|
||||||
assert_eq!(addr.payment_id(), payment_id);
|
|
||||||
assert_eq!(addr.is_guaranteed(), guaranteed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn featured_vectors() {
|
|
||||||
#[derive(serde::Deserialize)]
|
|
||||||
struct Vector {
|
|
||||||
address: String,
|
|
||||||
|
|
||||||
network: String,
|
|
||||||
spend: String,
|
|
||||||
view: String,
|
|
||||||
|
|
||||||
subaddress: bool,
|
|
||||||
integrated: bool,
|
|
||||||
payment_id: Option<[u8; 8]>,
|
|
||||||
guaranteed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
let vectors = serde_json::from_str::<Vec<Vector>>(FEATURED_JSON).unwrap();
|
|
||||||
for vector in vectors {
|
|
||||||
let first = vector.address.chars().next().unwrap();
|
|
||||||
let network = match vector.network.as_str() {
|
|
||||||
"Mainnet" => {
|
|
||||||
assert_eq!(first, 'C');
|
|
||||||
Network::Mainnet
|
|
||||||
}
|
|
||||||
"Testnet" => {
|
|
||||||
assert_eq!(first, 'K');
|
|
||||||
Network::Testnet
|
|
||||||
}
|
|
||||||
"Stagenet" => {
|
|
||||||
assert_eq!(first, 'F');
|
|
||||||
Network::Stagenet
|
|
||||||
}
|
|
||||||
_ => panic!("Unknown network"),
|
|
||||||
};
|
|
||||||
let spend = CompressedEdwardsY::from_slice(&hex::decode(vector.spend).unwrap())
|
|
||||||
.unwrap()
|
|
||||||
.decompress()
|
|
||||||
.unwrap();
|
|
||||||
let view = CompressedEdwardsY::from_slice(&hex::decode(vector.view).unwrap())
|
|
||||||
.unwrap()
|
|
||||||
.decompress()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let addr = MoneroAddress::from_str(network, &vector.address).unwrap();
|
|
||||||
assert_eq!(addr.spend, spend);
|
|
||||||
assert_eq!(addr.view, view);
|
|
||||||
|
|
||||||
assert_eq!(addr.is_subaddress(), vector.subaddress);
|
|
||||||
assert_eq!(vector.integrated, vector.payment_id.is_some());
|
|
||||||
assert_eq!(addr.payment_id(), vector.payment_id);
|
|
||||||
assert_eq!(addr.is_guaranteed(), vector.guaranteed);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
MoneroAddress::new(
|
|
||||||
AddressMeta::new(
|
|
||||||
network,
|
|
||||||
AddressType::Featured {
|
|
||||||
subaddress: vector.subaddress,
|
|
||||||
payment_id: vector.payment_id,
|
|
||||||
guaranteed: vector.guaranteed
|
|
||||||
}
|
|
||||||
),
|
|
||||||
spend,
|
|
||||||
view
|
|
||||||
)
|
|
||||||
.to_string(),
|
|
||||||
vector.address
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
use hex_literal::hex;
|
|
||||||
use rand_core::OsRng;
|
|
||||||
|
|
||||||
use curve25519_dalek::{scalar::Scalar, edwards::CompressedEdwardsY};
|
|
||||||
use multiexp::BatchVerifier;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Commitment, random_scalar,
|
|
||||||
ringct::bulletproofs::{Bulletproofs, original::OriginalStruct},
|
|
||||||
};
|
|
||||||
|
|
||||||
mod plus;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn bulletproofs_vector() {
|
|
||||||
let scalar = |scalar| Scalar::from_canonical_bytes(scalar).unwrap();
|
|
||||||
let point = |point| CompressedEdwardsY(point).decompress().unwrap();
|
|
||||||
|
|
||||||
// Generated from Monero
|
|
||||||
assert!(Bulletproofs::Original(OriginalStruct {
|
|
||||||
A: point(hex!("ef32c0b9551b804decdcb107eb22aa715b7ce259bf3c5cac20e24dfa6b28ac71")),
|
|
||||||
S: point(hex!("e1285960861783574ee2b689ae53622834eb0b035d6943103f960cd23e063fa0")),
|
|
||||||
T1: point(hex!("4ea07735f184ba159d0e0eb662bac8cde3eb7d39f31e567b0fbda3aa23fe5620")),
|
|
||||||
T2: point(hex!("b8390aa4b60b255630d40e592f55ec6b7ab5e3a96bfcdcd6f1cd1d2fc95f441e")),
|
|
||||||
taux: scalar(hex!("5957dba8ea9afb23d6e81cc048a92f2d502c10c749dc1b2bd148ae8d41ec7107")),
|
|
||||||
mu: scalar(hex!("923023b234c2e64774b820b4961f7181f6c1dc152c438643e5a25b0bf271bc02")),
|
|
||||||
L: vec![
|
|
||||||
point(hex!("c45f656316b9ebf9d357fb6a9f85b5f09e0b991dd50a6e0ae9b02de3946c9d99")),
|
|
||||||
point(hex!("9304d2bf0f27183a2acc58cc755a0348da11bd345485fda41b872fee89e72aac")),
|
|
||||||
point(hex!("1bb8b71925d155dd9569f64129ea049d6149fdc4e7a42a86d9478801d922129b")),
|
|
||||||
point(hex!("5756a7bf887aa72b9a952f92f47182122e7b19d89e5dd434c747492b00e1c6b7")),
|
|
||||||
point(hex!("6e497c910d102592830555356af5ff8340e8d141e3fb60ea24cfa587e964f07d")),
|
|
||||||
point(hex!("f4fa3898e7b08e039183d444f3d55040f3c790ed806cb314de49f3068bdbb218")),
|
|
||||||
point(hex!("0bbc37597c3ead517a3841e159c8b7b79a5ceaee24b2a9a20350127aab428713")),
|
|
||||||
],
|
|
||||||
R: vec![
|
|
||||||
point(hex!("609420ba1702781692e84accfd225adb3d077aedc3cf8125563400466b52dbd9")),
|
|
||||||
point(hex!("fb4e1d079e7a2b0ec14f7e2a3943bf50b6d60bc346a54fcf562fb234b342abf8")),
|
|
||||||
point(hex!("6ae3ac97289c48ce95b9c557289e82a34932055f7f5e32720139824fe81b12e5")),
|
|
||||||
point(hex!("d071cc2ffbdab2d840326ad15f68c01da6482271cae3cf644670d1632f29a15c")),
|
|
||||||
point(hex!("e52a1754b95e1060589ba7ce0c43d0060820ebfc0d49dc52884bc3c65ad18af5")),
|
|
||||||
point(hex!("41573b06140108539957df71aceb4b1816d2409ce896659aa5c86f037ca5e851")),
|
|
||||||
point(hex!("a65970b2cc3c7b08b2b5b739dbc8e71e646783c41c625e2a5b1535e3d2e0f742")),
|
|
||||||
],
|
|
||||||
a: scalar(hex!("0077c5383dea44d3cd1bc74849376bd60679612dc4b945255822457fa0c0a209")),
|
|
||||||
b: scalar(hex!("fe80cf5756473482581e1d38644007793ddc66fdeb9404ec1689a907e4863302")),
|
|
||||||
t: scalar(hex!("40dfb08e09249040df997851db311bd6827c26e87d6f0f332c55be8eef10e603"))
|
|
||||||
})
|
|
||||||
.verify(
|
|
||||||
&mut OsRng,
|
|
||||||
&[
|
|
||||||
// For some reason, these vectors are * INV_EIGHT
|
|
||||||
point(hex!("8e8f23f315edae4f6c2f948d9a861e0ae32d356b933cd11d2f0e031ac744c41f"))
|
|
||||||
.mul_by_cofactor(),
|
|
||||||
point(hex!("2829cbd025aa54cd6e1b59a032564f22f0b2e5627f7f2c4297f90da438b5510f"))
|
|
||||||
.mul_by_cofactor(),
|
|
||||||
]
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! bulletproofs_tests {
|
|
||||||
($name: ident, $max: ident, $plus: literal) => {
|
|
||||||
#[test]
|
|
||||||
fn $name() {
|
|
||||||
// Create Bulletproofs for all possible output quantities
|
|
||||||
let mut verifier = BatchVerifier::new(16);
|
|
||||||
for i in 1 ..= 16 {
|
|
||||||
let commitments = (1 ..= i)
|
|
||||||
.map(|i| Commitment::new(random_scalar(&mut OsRng), u64::try_from(i).unwrap()))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let bp = Bulletproofs::prove(&mut OsRng, &commitments, $plus).unwrap();
|
|
||||||
|
|
||||||
let commitments = commitments.iter().map(Commitment::calculate).collect::<Vec<_>>();
|
|
||||||
assert!(bp.verify(&mut OsRng, &commitments));
|
|
||||||
assert!(bp.batch_verify(&mut OsRng, &mut verifier, i, &commitments));
|
|
||||||
}
|
|
||||||
assert!(verifier.verify_vartime());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn $max() {
|
|
||||||
// Check Bulletproofs errors if we try to prove for too many outputs
|
|
||||||
let mut commitments = vec![];
|
|
||||||
for _ in 0 .. 17 {
|
|
||||||
commitments.push(Commitment::new(Scalar::ZERO, 0));
|
|
||||||
}
|
|
||||||
assert!(Bulletproofs::prove(&mut OsRng, &commitments, $plus).is_err());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
bulletproofs_tests!(bulletproofs, bulletproofs_max, false);
|
|
||||||
bulletproofs_tests!(bulletproofs_plus, bulletproofs_plus_max, true);
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
use rand_core::{RngCore, OsRng};
|
|
||||||
|
|
||||||
use multiexp::BatchVerifier;
|
|
||||||
use group::ff::Field;
|
|
||||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Commitment,
|
|
||||||
ringct::bulletproofs::plus::aggregate_range_proof::{
|
|
||||||
AggregateRangeStatement, AggregateRangeWitness,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_aggregate_range_proof() {
|
|
||||||
let mut verifier = BatchVerifier::new(16);
|
|
||||||
for m in 1 ..= 16 {
|
|
||||||
let mut commitments = vec![];
|
|
||||||
for _ in 0 .. m {
|
|
||||||
commitments.push(Commitment::new(*Scalar::random(&mut OsRng), OsRng.next_u64()));
|
|
||||||
}
|
|
||||||
let commitment_points = commitments.iter().map(|com| EdwardsPoint(com.calculate())).collect();
|
|
||||||
let statement = AggregateRangeStatement::new(commitment_points).unwrap();
|
|
||||||
let witness = AggregateRangeWitness::new(&commitments).unwrap();
|
|
||||||
|
|
||||||
let proof = statement.clone().prove(&mut OsRng, &witness).unwrap();
|
|
||||||
statement.verify(&mut OsRng, &mut verifier, (), proof);
|
|
||||||
}
|
|
||||||
assert!(verifier.verify_vartime());
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
#[cfg(test)]
|
|
||||||
mod weighted_inner_product;
|
|
||||||
#[cfg(test)]
|
|
||||||
mod aggregate_range_proof;
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
// The inner product relation is P = sum(g_bold * a, h_bold * b, g * (a * y * b), h * alpha)
|
|
||||||
|
|
||||||
use rand_core::OsRng;
|
|
||||||
|
|
||||||
use multiexp::BatchVerifier;
|
|
||||||
use group::{ff::Field, Group};
|
|
||||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
|
||||||
|
|
||||||
use crate::ringct::bulletproofs::plus::{
|
|
||||||
ScalarVector, PointVector, GeneratorsList, Generators,
|
|
||||||
weighted_inner_product::{WipStatement, WipWitness},
|
|
||||||
weighted_inner_product,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_zero_weighted_inner_product() {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let P = EdwardsPoint::identity();
|
|
||||||
let y = Scalar::random(&mut OsRng);
|
|
||||||
|
|
||||||
let generators = Generators::new().reduce(1);
|
|
||||||
let statement = WipStatement::new(generators, P, y);
|
|
||||||
let witness = WipWitness::new(ScalarVector::new(1), ScalarVector::new(1), Scalar::ZERO).unwrap();
|
|
||||||
|
|
||||||
let transcript = Scalar::random(&mut OsRng);
|
|
||||||
let proof = statement.clone().prove(&mut OsRng, transcript, &witness).unwrap();
|
|
||||||
|
|
||||||
let mut verifier = BatchVerifier::new(1);
|
|
||||||
statement.verify(&mut OsRng, &mut verifier, (), transcript, proof);
|
|
||||||
assert!(verifier.verify_vartime());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_weighted_inner_product() {
|
|
||||||
// P = sum(g_bold * a, h_bold * b, g * (a * y * b), h * alpha)
|
|
||||||
let mut verifier = BatchVerifier::new(6);
|
|
||||||
let generators = Generators::new();
|
|
||||||
for i in [1, 2, 4, 8, 16, 32] {
|
|
||||||
let generators = generators.reduce(i);
|
|
||||||
let g = Generators::g();
|
|
||||||
let h = Generators::h();
|
|
||||||
assert_eq!(generators.len(), i);
|
|
||||||
let mut g_bold = vec![];
|
|
||||||
let mut h_bold = vec![];
|
|
||||||
for i in 0 .. i {
|
|
||||||
g_bold.push(generators.generator(GeneratorsList::GBold1, i));
|
|
||||||
h_bold.push(generators.generator(GeneratorsList::HBold1, i));
|
|
||||||
}
|
|
||||||
let g_bold = PointVector(g_bold);
|
|
||||||
let h_bold = PointVector(h_bold);
|
|
||||||
|
|
||||||
let mut a = ScalarVector::new(i);
|
|
||||||
let mut b = ScalarVector::new(i);
|
|
||||||
let alpha = Scalar::random(&mut OsRng);
|
|
||||||
|
|
||||||
let y = Scalar::random(&mut OsRng);
|
|
||||||
let mut y_vec = ScalarVector::new(g_bold.len());
|
|
||||||
y_vec[0] = y;
|
|
||||||
for i in 1 .. y_vec.len() {
|
|
||||||
y_vec[i] = y_vec[i - 1] * y;
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0 .. i {
|
|
||||||
a[i] = Scalar::random(&mut OsRng);
|
|
||||||
b[i] = Scalar::random(&mut OsRng);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let P = g_bold.multiexp(&a) +
|
|
||||||
h_bold.multiexp(&b) +
|
|
||||||
(g * weighted_inner_product(&a, &b, &y_vec)) +
|
|
||||||
(h * alpha);
|
|
||||||
|
|
||||||
let statement = WipStatement::new(generators, P, y);
|
|
||||||
let witness = WipWitness::new(a, b, alpha).unwrap();
|
|
||||||
|
|
||||||
let transcript = Scalar::random(&mut OsRng);
|
|
||||||
let proof = statement.clone().prove(&mut OsRng, transcript, &witness).unwrap();
|
|
||||||
statement.verify(&mut OsRng, &mut verifier, (), transcript, proof);
|
|
||||||
}
|
|
||||||
assert!(verifier.verify_vartime());
|
|
||||||
}
|
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
use core::ops::Deref;
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
|
|
||||||
use zeroize::Zeroizing;
|
|
||||||
use rand_core::{RngCore, OsRng};
|
|
||||||
|
|
||||||
use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, scalar::Scalar};
|
|
||||||
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
use transcript::{Transcript, RecommendedTranscript};
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
use frost::curve::Ed25519;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Commitment, random_scalar,
|
|
||||||
wallet::Decoys,
|
|
||||||
ringct::{
|
|
||||||
generate_key_image,
|
|
||||||
clsag::{ClsagInput, Clsag},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
use crate::ringct::clsag::{ClsagDetails, ClsagMultisig};
|
|
||||||
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
use frost::{
|
|
||||||
Participant,
|
|
||||||
tests::{key_gen, algorithm_machines, sign},
|
|
||||||
};
|
|
||||||
|
|
||||||
const RING_LEN: u64 = 11;
|
|
||||||
const AMOUNT: u64 = 1337;
|
|
||||||
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
const RING_INDEX: u8 = 3;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clsag() {
|
|
||||||
for real in 0 .. RING_LEN {
|
|
||||||
let msg = [1; 32];
|
|
||||||
|
|
||||||
let mut secrets = (Zeroizing::new(Scalar::ZERO), Scalar::ZERO);
|
|
||||||
let mut ring = vec![];
|
|
||||||
for i in 0 .. RING_LEN {
|
|
||||||
let dest = Zeroizing::new(random_scalar(&mut OsRng));
|
|
||||||
let mask = random_scalar(&mut OsRng);
|
|
||||||
let amount;
|
|
||||||
if i == real {
|
|
||||||
secrets = (dest.clone(), mask);
|
|
||||||
amount = AMOUNT;
|
|
||||||
} else {
|
|
||||||
amount = OsRng.next_u64();
|
|
||||||
}
|
|
||||||
ring
|
|
||||||
.push([dest.deref() * ED25519_BASEPOINT_TABLE, Commitment::new(mask, amount).calculate()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
let image = generate_key_image(&secrets.0);
|
|
||||||
let (clsag, pseudo_out) = Clsag::sign(
|
|
||||||
&mut OsRng,
|
|
||||||
vec![(
|
|
||||||
secrets.0,
|
|
||||||
image,
|
|
||||||
ClsagInput::new(
|
|
||||||
Commitment::new(secrets.1, AMOUNT),
|
|
||||||
Decoys {
|
|
||||||
i: u8::try_from(real).unwrap(),
|
|
||||||
offsets: (1 ..= RING_LEN).collect(),
|
|
||||||
ring: ring.clone(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
)],
|
|
||||||
random_scalar(&mut OsRng),
|
|
||||||
msg,
|
|
||||||
)
|
|
||||||
.swap_remove(0);
|
|
||||||
clsag.verify(&ring, &image, &pseudo_out, &msg).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
#[test]
|
|
||||||
fn clsag_multisig() {
|
|
||||||
let keys = key_gen::<_, Ed25519>(&mut OsRng);
|
|
||||||
|
|
||||||
let randomness = random_scalar(&mut OsRng);
|
|
||||||
let mut ring = vec![];
|
|
||||||
for i in 0 .. RING_LEN {
|
|
||||||
let dest;
|
|
||||||
let mask;
|
|
||||||
let amount;
|
|
||||||
if i != u64::from(RING_INDEX) {
|
|
||||||
dest = &random_scalar(&mut OsRng) * ED25519_BASEPOINT_TABLE;
|
|
||||||
mask = random_scalar(&mut OsRng);
|
|
||||||
amount = OsRng.next_u64();
|
|
||||||
} else {
|
|
||||||
dest = keys[&Participant::new(1).unwrap()].group_key().0;
|
|
||||||
mask = randomness;
|
|
||||||
amount = AMOUNT;
|
|
||||||
}
|
|
||||||
ring.push([dest, Commitment::new(mask, amount).calculate()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mask_sum = random_scalar(&mut OsRng);
|
|
||||||
let algorithm = ClsagMultisig::new(
|
|
||||||
RecommendedTranscript::new(b"Monero Serai CLSAG Test"),
|
|
||||||
keys[&Participant::new(1).unwrap()].group_key().0,
|
|
||||||
Arc::new(RwLock::new(Some(ClsagDetails::new(
|
|
||||||
ClsagInput::new(
|
|
||||||
Commitment::new(randomness, AMOUNT),
|
|
||||||
Decoys { i: RING_INDEX, offsets: (1 ..= RING_LEN).collect(), ring: ring.clone() },
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
mask_sum,
|
|
||||||
)))),
|
|
||||||
);
|
|
||||||
|
|
||||||
sign(
|
|
||||||
&mut OsRng,
|
|
||||||
&algorithm,
|
|
||||||
keys.clone(),
|
|
||||||
algorithm_machines(&mut OsRng, &algorithm, &keys),
|
|
||||||
&[1; 32],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
mod unreduced_scalar;
|
|
||||||
mod clsag;
|
|
||||||
mod bulletproofs;
|
|
||||||
mod address;
|
|
||||||
mod seed;
|
|
||||||
@@ -1,407 +0,0 @@
|
|||||||
use zeroize::Zeroizing;
|
|
||||||
|
|
||||||
use rand_core::OsRng;
|
|
||||||
|
|
||||||
use curve25519_dalek::scalar::Scalar;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
hash,
|
|
||||||
wallet::seed::{
|
|
||||||
Seed, SeedType,
|
|
||||||
classic::{self, trim_by_lang},
|
|
||||||
polyseed,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_classic_seed() {
|
|
||||||
struct Vector {
|
|
||||||
language: classic::Language,
|
|
||||||
seed: String,
|
|
||||||
spend: String,
|
|
||||||
view: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
let vectors = [
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Chinese,
|
|
||||||
seed: "摇 曲 艺 武 滴 然 效 似 赏 式 祥 歌 买 疑 小 碧 堆 博 键 房 鲜 悲 付 喷 武".into(),
|
|
||||||
spend: "a5e4fff1706ef9212993a69f246f5c95ad6d84371692d63e9bb0ea112a58340d".into(),
|
|
||||||
view: "1176c43ce541477ea2f3ef0b49b25112b084e26b8a843e1304ac4677b74cdf02".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::English,
|
|
||||||
seed: "washing thirsty occur lectures tuesday fainted toxic adapt \
|
|
||||||
abnormal memoir nylon mostly building shrugged online ember northern \
|
|
||||||
ruby woes dauntless boil family illness inroads northern"
|
|
||||||
.into(),
|
|
||||||
spend: "c0af65c0dd837e666b9d0dfed62745f4df35aed7ea619b2798a709f0fe545403".into(),
|
|
||||||
view: "513ba91c538a5a9069e0094de90e927c0cd147fa10428ce3ac1afd49f63e3b01".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Dutch,
|
|
||||||
seed: "setwinst riphagen vimmetje extase blief tuitelig fuiven meifeest \
|
|
||||||
ponywagen zesmaal ripdeal matverf codetaal leut ivoor rotten \
|
|
||||||
wisgerhof winzucht typograaf atrium rein zilt traktaat verzaagd setwinst"
|
|
||||||
.into(),
|
|
||||||
spend: "e2d2873085c447c2bc7664222ac8f7d240df3aeac137f5ff2022eaa629e5b10a".into(),
|
|
||||||
view: "eac30b69477e3f68093d131c7fd961564458401b07f8c87ff8f6030c1a0c7301".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::French,
|
|
||||||
seed: "poids vaseux tarte bazar poivre effet entier nuance \
|
|
||||||
sensuel ennui pacte osselet poudre battre alibi mouton \
|
|
||||||
stade paquet pliage gibier type question position projet pliage"
|
|
||||||
.into(),
|
|
||||||
spend: "2dd39ff1a4628a94b5c2ec3e42fb3dfe15c2b2f010154dc3b3de6791e805b904".into(),
|
|
||||||
view: "6725b32230400a1032f31d622b44c3a227f88258939b14a7c72e00939e7bdf0e".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Spanish,
|
|
||||||
seed: "minero ocupar mirar evadir octubre cal logro miope \
|
|
||||||
opaco disco ancla litio clase cuello nasal clase \
|
|
||||||
fiar avance deseo mente grumo negro cordón croqueta clase"
|
|
||||||
.into(),
|
|
||||||
spend: "ae2c9bebdddac067d73ec0180147fc92bdf9ac7337f1bcafbbe57dd13558eb02".into(),
|
|
||||||
view: "18deafb34d55b7a43cae2c1c1c206a3c80c12cc9d1f84640b484b95b7fec3e05".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::German,
|
|
||||||
seed: "Kaliber Gabelung Tapir Liveband Favorit Specht Enklave Nabel \
|
|
||||||
Jupiter Foliant Chronik nisten löten Vase Aussage Rekord \
|
|
||||||
Yeti Gesetz Eleganz Alraune Künstler Almweide Jahr Kastanie Almweide"
|
|
||||||
.into(),
|
|
||||||
spend: "79801b7a1b9796856e2397d862a113862e1fdc289a205e79d8d70995b276db06".into(),
|
|
||||||
view: "99f0ec556643bd9c038a4ed86edcb9c6c16032c4622ed2e000299d527a792701".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Italian,
|
|
||||||
seed: "cavo pancetta auto fulmine alleanza filmato diavolo prato \
|
|
||||||
forzare meritare litigare lezione segreto evasione votare buio \
|
|
||||||
licenza cliente dorso natale crescere vento tutelare vetta evasione"
|
|
||||||
.into(),
|
|
||||||
spend: "5e7fd774eb00fa5877e2a8b4dc9c7ffe111008a3891220b56a6e49ac816d650a".into(),
|
|
||||||
view: "698a1dce6018aef5516e82ca0cb3e3ec7778d17dfb41a137567bfa2e55e63a03".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Portuguese,
|
|
||||||
seed: "agito eventualidade onus itrio holograma sodomizar objetos dobro \
|
|
||||||
iugoslavo bcrepuscular odalisca abjeto iuane darwinista eczema acetona \
|
|
||||||
cibernetico hoquei gleba driver buffer azoto megera nogueira agito"
|
|
||||||
.into(),
|
|
||||||
spend: "13b3115f37e35c6aa1db97428b897e584698670c1b27854568d678e729200c0f".into(),
|
|
||||||
view: "ad1b4fd35270f5f36c4da7166672b347e75c3f4d41346ec2a06d1d0193632801".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Japanese,
|
|
||||||
seed: "ぜんぶ どうぐ おたがい せんきょ おうじ そんちょう じゅしん いろえんぴつ \
|
|
||||||
かほう つかれる えらぶ にちじょう くのう にちようび ぬまえび さんきゃく \
|
|
||||||
おおや ちぬき うすめる いがく せつでん さうな すいえい せつだん おおや"
|
|
||||||
.into(),
|
|
||||||
spend: "c56e895cdb13007eda8399222974cdbab493640663804b93cbef3d8c3df80b0b".into(),
|
|
||||||
view: "6c3634a313ec2ee979d565c33888fd7c3502d696ce0134a8bc1a2698c7f2c508".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Russian,
|
|
||||||
seed: "шатер икра нация ехать получать инерция доза реальный \
|
|
||||||
рыжий таможня лопата душа веселый клетка атлас лекция \
|
|
||||||
обгонять паек наивный лыжный дурак стать ежик задача паек"
|
|
||||||
.into(),
|
|
||||||
spend: "7cb5492df5eb2db4c84af20766391cd3e3662ab1a241c70fc881f3d02c381f05".into(),
|
|
||||||
view: "fcd53e41ec0df995ab43927f7c44bc3359c93523d5009fb3f5ba87431d545a03".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Esperanto,
|
|
||||||
seed: "ukazo klini peco etikedo fabriko imitado onklino urino \
|
|
||||||
pudro incidento kumuluso ikono smirgi hirundo uretro krii \
|
|
||||||
sparkado super speciala pupo alpinisto cvana vokegi zombio fabriko"
|
|
||||||
.into(),
|
|
||||||
spend: "82ebf0336d3b152701964ed41df6b6e9a035e57fc98b84039ed0bd4611c58904".into(),
|
|
||||||
view: "cd4d120e1ea34360af528f6a3e6156063312d9cefc9aa6b5218d366c0ed6a201".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::Lojban,
|
|
||||||
seed: "jetnu vensa julne xrotu xamsi julne cutci dakli \
|
|
||||||
mlatu xedja muvgau palpi xindo sfubu ciste cinri \
|
|
||||||
blabi darno dembi janli blabi fenki bukpu burcu blabi"
|
|
||||||
.into(),
|
|
||||||
spend: "e4f8c6819ab6cf792cebb858caabac9307fd646901d72123e0367ebc0a79c200".into(),
|
|
||||||
view: "c806ce62bafaa7b2d597f1a1e2dbe4a2f96bfd804bf6f8420fc7f4a6bd700c00".into(),
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: classic::Language::EnglishOld,
|
|
||||||
seed: "glorious especially puff son moment add youth nowhere \
|
|
||||||
throw glide grip wrong rhythm consume very swear \
|
|
||||||
bitter heavy eventually begin reason flirt type unable"
|
|
||||||
.into(),
|
|
||||||
spend: "647f4765b66b636ff07170ab6280a9a6804dfbaf19db2ad37d23be024a18730b".into(),
|
|
||||||
view: "045da65316a906a8c30046053119c18020b07a7a3a6ef5c01ab2a8755416bd02".into(),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
for vector in vectors {
|
|
||||||
let trim_seed = |seed: &str| {
|
|
||||||
seed
|
|
||||||
.split_whitespace()
|
|
||||||
.map(|word| trim_by_lang(word, vector.language))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(" ")
|
|
||||||
};
|
|
||||||
|
|
||||||
// Test against Monero
|
|
||||||
{
|
|
||||||
let seed = Seed::from_string(Zeroizing::new(vector.seed.clone())).unwrap();
|
|
||||||
let trim = trim_seed(&vector.seed);
|
|
||||||
println!(
|
|
||||||
"{}. seed: {}, entropy: {:?}, trim: {trim}",
|
|
||||||
line!(),
|
|
||||||
*seed.to_string(),
|
|
||||||
*seed.entropy()
|
|
||||||
);
|
|
||||||
assert_eq!(seed, Seed::from_string(Zeroizing::new(trim)).unwrap());
|
|
||||||
|
|
||||||
let spend: [u8; 32] = hex::decode(vector.spend).unwrap().try_into().unwrap();
|
|
||||||
// For classical seeds, Monero directly uses the entropy as a spend key
|
|
||||||
assert_eq!(
|
|
||||||
Option::<Scalar>::from(Scalar::from_canonical_bytes(*seed.entropy())),
|
|
||||||
Option::<Scalar>::from(Scalar::from_canonical_bytes(spend)),
|
|
||||||
);
|
|
||||||
|
|
||||||
let view: [u8; 32] = hex::decode(vector.view).unwrap().try_into().unwrap();
|
|
||||||
// Monero then derives the view key as H(spend)
|
|
||||||
assert_eq!(
|
|
||||||
Scalar::from_bytes_mod_order(hash(&spend)),
|
|
||||||
Scalar::from_canonical_bytes(view).unwrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Seed::from_entropy(SeedType::Classic(vector.language), Zeroizing::new(spend), None)
|
|
||||||
.unwrap(),
|
|
||||||
seed
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test against ourselves
|
|
||||||
{
|
|
||||||
let seed = Seed::new(&mut OsRng, SeedType::Classic(vector.language));
|
|
||||||
let trim = trim_seed(&seed.to_string());
|
|
||||||
println!(
|
|
||||||
"{}. seed: {}, entropy: {:?}, trim: {trim}",
|
|
||||||
line!(),
|
|
||||||
*seed.to_string(),
|
|
||||||
*seed.entropy()
|
|
||||||
);
|
|
||||||
assert_eq!(seed, Seed::from_string(Zeroizing::new(trim)).unwrap());
|
|
||||||
assert_eq!(
|
|
||||||
seed,
|
|
||||||
Seed::from_entropy(SeedType::Classic(vector.language), seed.entropy(), None).unwrap()
|
|
||||||
);
|
|
||||||
assert_eq!(seed, Seed::from_string(seed.to_string()).unwrap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_polyseed() {
|
|
||||||
struct Vector {
|
|
||||||
language: polyseed::Language,
|
|
||||||
seed: String,
|
|
||||||
entropy: String,
|
|
||||||
birthday: u64,
|
|
||||||
has_prefix: bool,
|
|
||||||
has_accent: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
let vectors = [
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::English,
|
|
||||||
seed: "raven tail swear infant grief assist regular lamp \
|
|
||||||
duck valid someone little harsh puppy airport language"
|
|
||||||
.into(),
|
|
||||||
entropy: "dd76e7359a0ded37cd0ff0f3c829a5ae01673300000000000000000000000000".into(),
|
|
||||||
birthday: 1638446400,
|
|
||||||
has_prefix: true,
|
|
||||||
has_accent: false,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::Spanish,
|
|
||||||
seed: "eje fin parte célebre tabú pestaña lienzo puma \
|
|
||||||
prisión hora regalo lengua existir lápiz lote sonoro"
|
|
||||||
.into(),
|
|
||||||
entropy: "5a2b02df7db21fcbe6ec6df137d54c7b20fd2b00000000000000000000000000".into(),
|
|
||||||
birthday: 3118651200,
|
|
||||||
has_prefix: true,
|
|
||||||
has_accent: true,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::French,
|
|
||||||
seed: "valable arracher décaler jeudi amusant dresser mener épaissir risible \
|
|
||||||
prouesse réserve ampleur ajuster muter caméra enchère"
|
|
||||||
.into(),
|
|
||||||
entropy: "11cfd870324b26657342c37360c424a14a050b00000000000000000000000000".into(),
|
|
||||||
birthday: 1679314966,
|
|
||||||
has_prefix: true,
|
|
||||||
has_accent: true,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::Italian,
|
|
||||||
seed: "caduco midollo copione meninge isotopo illogico riflesso tartaruga fermento \
|
|
||||||
olandese normale tristezza episodio voragine forbito achille"
|
|
||||||
.into(),
|
|
||||||
entropy: "7ecc57c9b4652d4e31428f62bec91cfd55500600000000000000000000000000".into(),
|
|
||||||
birthday: 1679316358,
|
|
||||||
has_prefix: true,
|
|
||||||
has_accent: false,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::Portuguese,
|
|
||||||
seed: "caverna custear azedo adeus senador apertada sedoso omitir \
|
|
||||||
sujeito aurora videira molho cartaz gesso dentista tapar"
|
|
||||||
.into(),
|
|
||||||
entropy: "45473063711376cae38f1b3eba18c874124e1d00000000000000000000000000".into(),
|
|
||||||
birthday: 1679316657,
|
|
||||||
has_prefix: true,
|
|
||||||
has_accent: false,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::Czech,
|
|
||||||
seed: "usmrtit nora dotaz komunita zavalit funkce mzda sotva akce \
|
|
||||||
vesta kabel herna stodola uvolnit ustrnout email"
|
|
||||||
.into(),
|
|
||||||
entropy: "7ac8a4efd62d9c3c4c02e350d32326df37821c00000000000000000000000000".into(),
|
|
||||||
birthday: 1679316898,
|
|
||||||
has_prefix: true,
|
|
||||||
has_accent: false,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::Korean,
|
|
||||||
seed: "전망 선풍기 국제 무궁화 설사 기름 이론적 해안 절망 예선 \
|
|
||||||
지우개 보관 절망 말기 시각 귀신"
|
|
||||||
.into(),
|
|
||||||
entropy: "684663fda420298f42ed94b2c512ed38ddf12b00000000000000000000000000".into(),
|
|
||||||
birthday: 1679317073,
|
|
||||||
has_prefix: false,
|
|
||||||
has_accent: false,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::Japanese,
|
|
||||||
seed: "うちあわせ ちつじょ つごう しはい けんこう とおる てみやげ はんとし たんとう \
|
|
||||||
といれ おさない おさえる むかう ぬぐう なふだ せまる"
|
|
||||||
.into(),
|
|
||||||
entropy: "94e6665518a6286c6e3ba508a2279eb62b771f00000000000000000000000000".into(),
|
|
||||||
birthday: 1679318722,
|
|
||||||
has_prefix: false,
|
|
||||||
has_accent: false,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::ChineseTraditional,
|
|
||||||
seed: "亂 挖 斤 柄 代 圈 枝 轄 魯 論 函 開 勘 番 榮 壁".into(),
|
|
||||||
entropy: "b1594f585987ab0fd5a31da1f0d377dae5283f00000000000000000000000000".into(),
|
|
||||||
birthday: 1679426433,
|
|
||||||
has_prefix: false,
|
|
||||||
has_accent: false,
|
|
||||||
},
|
|
||||||
Vector {
|
|
||||||
language: polyseed::Language::ChineseSimplified,
|
|
||||||
seed: "啊 百 族 府 票 划 伪 仓 叶 虾 借 溜 晨 左 等 鬼".into(),
|
|
||||||
entropy: "21cdd366f337b89b8d1bc1df9fe73047c22b0300000000000000000000000000".into(),
|
|
||||||
birthday: 1679426817,
|
|
||||||
has_prefix: false,
|
|
||||||
has_accent: false,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
for vector in vectors {
|
|
||||||
let add_whitespace = |mut seed: String| {
|
|
||||||
seed.push(' ');
|
|
||||||
seed
|
|
||||||
};
|
|
||||||
|
|
||||||
let seed_without_accents = |seed: &str| {
|
|
||||||
seed
|
|
||||||
.split_whitespace()
|
|
||||||
.map(|w| w.chars().filter(char::is_ascii).collect::<String>())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(" ")
|
|
||||||
};
|
|
||||||
|
|
||||||
let trim_seed = |seed: &str| {
|
|
||||||
let seed_to_trim =
|
|
||||||
if vector.has_accent { seed_without_accents(seed) } else { seed.to_string() };
|
|
||||||
seed_to_trim
|
|
||||||
.split_whitespace()
|
|
||||||
.map(|w| {
|
|
||||||
let mut ascii = 0;
|
|
||||||
let mut to_take = w.len();
|
|
||||||
for (i, char) in w.chars().enumerate() {
|
|
||||||
if char.is_ascii() {
|
|
||||||
ascii += 1;
|
|
||||||
}
|
|
||||||
if ascii == polyseed::PREFIX_LEN {
|
|
||||||
// +1 to include this character, which put us at the prefix length
|
|
||||||
to_take = i + 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
w.chars().take(to_take).collect::<String>()
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(" ")
|
|
||||||
};
|
|
||||||
|
|
||||||
// String -> Seed
|
|
||||||
let seed = Seed::from_string(Zeroizing::new(vector.seed.clone())).unwrap();
|
|
||||||
let trim = trim_seed(&vector.seed);
|
|
||||||
let add_whitespace = add_whitespace(vector.seed.clone());
|
|
||||||
let seed_without_accents = seed_without_accents(&vector.seed);
|
|
||||||
println!(
|
|
||||||
"{}. seed: {}, entropy: {:?}, trim: {}, add_whitespace: {}, seed_without_accents: {}",
|
|
||||||
line!(),
|
|
||||||
*seed.to_string(),
|
|
||||||
*seed.entropy(),
|
|
||||||
trim,
|
|
||||||
add_whitespace,
|
|
||||||
seed_without_accents,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Make sure a version with added whitespace still works
|
|
||||||
let whitespaced_seed = Seed::from_string(Zeroizing::new(add_whitespace)).unwrap();
|
|
||||||
assert_eq!(seed, whitespaced_seed);
|
|
||||||
// Check trimmed versions works
|
|
||||||
if vector.has_prefix {
|
|
||||||
let trimmed_seed = Seed::from_string(Zeroizing::new(trim)).unwrap();
|
|
||||||
assert_eq!(seed, trimmed_seed);
|
|
||||||
}
|
|
||||||
// Check versions without accents work
|
|
||||||
if vector.has_accent {
|
|
||||||
let seed_without_accents = Seed::from_string(Zeroizing::new(seed_without_accents)).unwrap();
|
|
||||||
assert_eq!(seed, seed_without_accents);
|
|
||||||
}
|
|
||||||
|
|
||||||
let entropy = Zeroizing::new(hex::decode(vector.entropy).unwrap().try_into().unwrap());
|
|
||||||
assert_eq!(seed.entropy(), entropy);
|
|
||||||
assert!(seed.birthday().abs_diff(vector.birthday) < polyseed::TIME_STEP);
|
|
||||||
|
|
||||||
// Entropy -> Seed
|
|
||||||
let from_entropy =
|
|
||||||
Seed::from_entropy(SeedType::Polyseed(vector.language), entropy, Some(seed.birthday()))
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(seed.to_string(), from_entropy.to_string());
|
|
||||||
|
|
||||||
// Check against ourselves
|
|
||||||
{
|
|
||||||
let seed = Seed::new(&mut OsRng, SeedType::Polyseed(vector.language));
|
|
||||||
println!("{}. seed: {}, key: {:?}", line!(), *seed.to_string(), *seed.key());
|
|
||||||
assert_eq!(seed, Seed::from_string(seed.to_string()).unwrap());
|
|
||||||
assert_eq!(
|
|
||||||
seed,
|
|
||||||
Seed::from_entropy(
|
|
||||||
SeedType::Polyseed(vector.language),
|
|
||||||
seed.entropy(),
|
|
||||||
Some(seed.birthday())
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
use curve25519_dalek::scalar::Scalar;
|
|
||||||
|
|
||||||
use crate::unreduced_scalar::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn recover_scalars() {
|
|
||||||
let test_recover = |stored: &str, recovered: &str| {
|
|
||||||
let stored = UnreducedScalar(hex::decode(stored).unwrap().try_into().unwrap());
|
|
||||||
let recovered =
|
|
||||||
Scalar::from_canonical_bytes(hex::decode(recovered).unwrap().try_into().unwrap()).unwrap();
|
|
||||||
assert_eq!(stored.recover_monero_slide_scalar(), recovered);
|
|
||||||
};
|
|
||||||
|
|
||||||
// https://www.moneroinflation.com/static/data_py/report_scalars_df.pdf
|
|
||||||
// Table 4.
|
|
||||||
test_recover(
|
|
||||||
"cb2be144948166d0a9edb831ea586da0c376efa217871505ad77f6ff80f203f8",
|
|
||||||
"b8ffd6a1aee47828808ab0d4c8524cb5c376efa217871505ad77f6ff80f20308",
|
|
||||||
);
|
|
||||||
test_recover(
|
|
||||||
"343d3df8a1051c15a400649c423dc4ed58bef49c50caef6ca4a618b80dee22f4",
|
|
||||||
"21113355bc682e6d7a9d5b3f2137a30259bef49c50caef6ca4a618b80dee2204",
|
|
||||||
);
|
|
||||||
test_recover(
|
|
||||||
"c14f75d612800ca2c1dcfa387a42c9cc086c005bc94b18d204dd61342418eba7",
|
|
||||||
"4f473804b1d27ab2c789c80ab21d034a096c005bc94b18d204dd61342418eb07",
|
|
||||||
);
|
|
||||||
test_recover(
|
|
||||||
"000102030405060708090a0b0c0d0e0f826c4f6e2329a31bc5bc320af0b2bcbb",
|
|
||||||
"a124cfd387f461bf3719e03965ee6877826c4f6e2329a31bc5bc320af0b2bc0b",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,230 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"address": "CjWdTpuDaZ69nTGxzm9YarR82YDYFECi1WaaREZTMy5yDsjaRX5bC3cbC3JpcrBPd7YYpjoWKuBMidgGaKBK5Jye2v3pYyUDn",
|
|
||||||
"network": "Mainnet",
|
|
||||||
"spend": "258dfe7eef9be934839f3b8e0d40e79035fe85879c0a9eb0d7372ae2deb0004c",
|
|
||||||
"view": "f91382373045f3cc69233254ab0406bc9e008707569ff9db4718654812d839df",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "CjWdTpuDaZ69nTGxzm9YarR82YDYFECi1WaaREZTMy5yDsjaRX5bC3cbC3JpcrBPd7YYpjoWKuBMidgGaKBK5Jye2v3wfMHCy",
|
|
||||||
"network": "Mainnet",
|
|
||||||
"spend": "258dfe7eef9be934839f3b8e0d40e79035fe85879c0a9eb0d7372ae2deb0004c",
|
|
||||||
"view": "f91382373045f3cc69233254ab0406bc9e008707569ff9db4718654812d839df",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "CjWdTpuDaZ69nTGxzm9YarR82YDYFECi1WaaREZTMy5yDsjaRX5bC3cbC3JpcrBPd7YYpjoWKuBMidgGaKBK5JyeeJTo4p5ayvj36PStM5AX",
|
|
||||||
"network": "Mainnet",
|
|
||||||
"spend": "258dfe7eef9be934839f3b8e0d40e79035fe85879c0a9eb0d7372ae2deb0004c",
|
|
||||||
"view": "f91382373045f3cc69233254ab0406bc9e008707569ff9db4718654812d839df",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [46, 48, 134, 34, 245, 148, 243, 195],
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "CjWdTpuDaZ69nTGxzm9YarR82YDYFECi1WaaREZTMy5yDsjaRX5bC3cbC3JpcrBPd7YYpjoWKuBMidgGaKBK5JyeeJWv5WqMCNE2hRs9rJfy",
|
|
||||||
"network": "Mainnet",
|
|
||||||
"spend": "258dfe7eef9be934839f3b8e0d40e79035fe85879c0a9eb0d7372ae2deb0004c",
|
|
||||||
"view": "f91382373045f3cc69233254ab0406bc9e008707569ff9db4718654812d839df",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [153, 176, 98, 204, 151, 27, 197, 168],
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "CjWdTpuDaZ69nTGxzm9YarR82YDYFECi1WaaREZTMy5yDsjaRX5bC3cbC3JpcrBPd7YYpjoWKuBMidgGaKBK5Jye2v4DwqwH1",
|
|
||||||
"network": "Mainnet",
|
|
||||||
"spend": "258dfe7eef9be934839f3b8e0d40e79035fe85879c0a9eb0d7372ae2deb0004c",
|
|
||||||
"view": "f91382373045f3cc69233254ab0406bc9e008707569ff9db4718654812d839df",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "CjWdTpuDaZ69nTGxzm9YarR82YDYFECi1WaaREZTMy5yDsjaRX5bC3cbC3JpcrBPd7YYpjoWKuBMidgGaKBK5Jye2v4Pyz8bD",
|
|
||||||
"network": "Mainnet",
|
|
||||||
"spend": "258dfe7eef9be934839f3b8e0d40e79035fe85879c0a9eb0d7372ae2deb0004c",
|
|
||||||
"view": "f91382373045f3cc69233254ab0406bc9e008707569ff9db4718654812d839df",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "CjWdTpuDaZ69nTGxzm9YarR82YDYFECi1WaaREZTMy5yDsjaRX5bC3cbC3JpcrBPd7YYpjoWKuBMidgGaKBK5JyeeJcwt7hykou237MqZZDA",
|
|
||||||
"network": "Mainnet",
|
|
||||||
"spend": "258dfe7eef9be934839f3b8e0d40e79035fe85879c0a9eb0d7372ae2deb0004c",
|
|
||||||
"view": "f91382373045f3cc69233254ab0406bc9e008707569ff9db4718654812d839df",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [88, 37, 149, 111, 171, 108, 120, 181],
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "CjWdTpuDaZ69nTGxzm9YarR82YDYFECi1WaaREZTMy5yDsjaRX5bC3cbC3JpcrBPd7YYpjoWKuBMidgGaKBK5JyeeJfTrFAp69u2MYbf5YeN",
|
|
||||||
"network": "Mainnet",
|
|
||||||
"spend": "258dfe7eef9be934839f3b8e0d40e79035fe85879c0a9eb0d7372ae2deb0004c",
|
|
||||||
"view": "f91382373045f3cc69233254ab0406bc9e008707569ff9db4718654812d839df",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [125, 69, 155, 152, 140, 160, 157, 186],
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "Kgx5uCVsMSEVm7seL8tjyRGmmVXjWfEowKpKjgaXUGVyMViBYMh13VQ4mfqpB7zEVVcJx3E8FFgAuQ8cq6mg5x712U9w7ScYA",
|
|
||||||
"network": "Testnet",
|
|
||||||
"spend": "bba3a8a5bb47f7abf2e2dffeaf43385e4b308fd63a9ff6707e355f3b0a6c247a",
|
|
||||||
"view": "881713a4fa9777168a54bbdcb75290d319fb92fdf1026a8a4b125a8e341de8ab",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "Kgx5uCVsMSEVm7seL8tjyRGmmVXjWfEowKpKjgaXUGVyMViBYMh13VQ4mfqpB7zEVVcJx3E8FFgAuQ8cq6mg5x712UA2gCrT1",
|
|
||||||
"network": "Testnet",
|
|
||||||
"spend": "bba3a8a5bb47f7abf2e2dffeaf43385e4b308fd63a9ff6707e355f3b0a6c247a",
|
|
||||||
"view": "881713a4fa9777168a54bbdcb75290d319fb92fdf1026a8a4b125a8e341de8ab",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "Kgx5uCVsMSEVm7seL8tjyRGmmVXjWfEowKpKjgaXUGVyMViBYMh13VQ4mfqpB7zEVVcJx3E8FFgAuQ8cq6mg5x71Vc1DbPKwJu81cxJjqBkS",
|
|
||||||
"network": "Testnet",
|
|
||||||
"spend": "bba3a8a5bb47f7abf2e2dffeaf43385e4b308fd63a9ff6707e355f3b0a6c247a",
|
|
||||||
"view": "881713a4fa9777168a54bbdcb75290d319fb92fdf1026a8a4b125a8e341de8ab",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [92, 225, 118, 220, 39, 3, 72, 51],
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "Kgx5uCVsMSEVm7seL8tjyRGmmVXjWfEowKpKjgaXUGVyMViBYMh13VQ4mfqpB7zEVVcJx3E8FFgAuQ8cq6mg5x71Vc2o1rPMaXN31Fe5J6dn",
|
|
||||||
"network": "Testnet",
|
|
||||||
"spend": "bba3a8a5bb47f7abf2e2dffeaf43385e4b308fd63a9ff6707e355f3b0a6c247a",
|
|
||||||
"view": "881713a4fa9777168a54bbdcb75290d319fb92fdf1026a8a4b125a8e341de8ab",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [20, 120, 47, 89, 72, 165, 233, 115],
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "Kgx5uCVsMSEVm7seL8tjyRGmmVXjWfEowKpKjgaXUGVyMViBYMh13VQ4mfqpB7zEVVcJx3E8FFgAuQ8cq6mg5x712UAQHCRZ4",
|
|
||||||
"network": "Testnet",
|
|
||||||
"spend": "bba3a8a5bb47f7abf2e2dffeaf43385e4b308fd63a9ff6707e355f3b0a6c247a",
|
|
||||||
"view": "881713a4fa9777168a54bbdcb75290d319fb92fdf1026a8a4b125a8e341de8ab",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "Kgx5uCVsMSEVm7seL8tjyRGmmVXjWfEowKpKjgaXUGVyMViBYMh13VQ4mfqpB7zEVVcJx3E8FFgAuQ8cq6mg5x712UAUzqaii",
|
|
||||||
"network": "Testnet",
|
|
||||||
"spend": "bba3a8a5bb47f7abf2e2dffeaf43385e4b308fd63a9ff6707e355f3b0a6c247a",
|
|
||||||
"view": "881713a4fa9777168a54bbdcb75290d319fb92fdf1026a8a4b125a8e341de8ab",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "Kgx5uCVsMSEVm7seL8tjyRGmmVXjWfEowKpKjgaXUGVyMViBYMh13VQ4mfqpB7zEVVcJx3E8FFgAuQ8cq6mg5x71VcAsfQc3gJQ2gHLd5DiQ",
|
|
||||||
"network": "Testnet",
|
|
||||||
"spend": "bba3a8a5bb47f7abf2e2dffeaf43385e4b308fd63a9ff6707e355f3b0a6c247a",
|
|
||||||
"view": "881713a4fa9777168a54bbdcb75290d319fb92fdf1026a8a4b125a8e341de8ab",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [193, 149, 123, 214, 180, 205, 195, 91],
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "Kgx5uCVsMSEVm7seL8tjyRGmmVXjWfEowKpKjgaXUGVyMViBYMh13VQ4mfqpB7zEVVcJx3E8FFgAuQ8cq6mg5x71VcDBAD5jbZQ3AMHFyvQB",
|
|
||||||
"network": "Testnet",
|
|
||||||
"spend": "bba3a8a5bb47f7abf2e2dffeaf43385e4b308fd63a9ff6707e355f3b0a6c247a",
|
|
||||||
"view": "881713a4fa9777168a54bbdcb75290d319fb92fdf1026a8a4b125a8e341de8ab",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [205, 170, 65, 0, 51, 175, 251, 184],
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "FSDinqdKK54PbjF73GgW3nUpf7bF8QbyxFCUurENmUyeEfSxSLL2hxwANBLzq1A8gTSAzzEn65hKjetA8o5BvjV61VPJnBtTP",
|
|
||||||
"network": "Stagenet",
|
|
||||||
"spend": "4cd503040f5e43871bf37d8ca7177da655bda410859af754e24e7b44437f3151",
|
|
||||||
"view": "af60d42b6c6e4437fd93eb32657a14967efa393630d7aee27b5973c8e1c5ad39",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "FSDinqdKK54PbjF73GgW3nUpf7bF8QbyxFCUurENmUyeEfSxSLL2hxwANBLzq1A8gTSAzzEn65hKjetA8o5BvjV61VPUrwMvP",
|
|
||||||
"network": "Stagenet",
|
|
||||||
"spend": "4cd503040f5e43871bf37d8ca7177da655bda410859af754e24e7b44437f3151",
|
|
||||||
"view": "af60d42b6c6e4437fd93eb32657a14967efa393630d7aee27b5973c8e1c5ad39",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "FSDinqdKK54PbjF73GgW3nUpf7bF8QbyxFCUurENmUyeEfSxSLL2hxwANBLzq1A8gTSAzzEn65hKjetA8o5BvjV6AY5ECEhP5Nr1aCRPXdxk",
|
|
||||||
"network": "Stagenet",
|
|
||||||
"spend": "4cd503040f5e43871bf37d8ca7177da655bda410859af754e24e7b44437f3151",
|
|
||||||
"view": "af60d42b6c6e4437fd93eb32657a14967efa393630d7aee27b5973c8e1c5ad39",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [173, 149, 78, 64, 215, 211, 66, 170],
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "FSDinqdKK54PbjF73GgW3nUpf7bF8QbyxFCUurENmUyeEfSxSLL2hxwANBLzq1A8gTSAzzEn65hKjetA8o5BvjV6AY882kTUS1D2LttnPvTR",
|
|
||||||
"network": "Stagenet",
|
|
||||||
"spend": "4cd503040f5e43871bf37d8ca7177da655bda410859af754e24e7b44437f3151",
|
|
||||||
"view": "af60d42b6c6e4437fd93eb32657a14967efa393630d7aee27b5973c8e1c5ad39",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [254, 159, 186, 162, 1, 8, 156, 108],
|
|
||||||
"guaranteed": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "FSDinqdKK54PbjF73GgW3nUpf7bF8QbyxFCUurENmUyeEfSxSLL2hxwANBLzq1A8gTSAzzEn65hKjetA8o5BvjV61VPpBBo8F",
|
|
||||||
"network": "Stagenet",
|
|
||||||
"spend": "4cd503040f5e43871bf37d8ca7177da655bda410859af754e24e7b44437f3151",
|
|
||||||
"view": "af60d42b6c6e4437fd93eb32657a14967efa393630d7aee27b5973c8e1c5ad39",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "FSDinqdKK54PbjF73GgW3nUpf7bF8QbyxFCUurENmUyeEfSxSLL2hxwANBLzq1A8gTSAzzEn65hKjetA8o5BvjV61VPuUJX3b",
|
|
||||||
"network": "Stagenet",
|
|
||||||
"spend": "4cd503040f5e43871bf37d8ca7177da655bda410859af754e24e7b44437f3151",
|
|
||||||
"view": "af60d42b6c6e4437fd93eb32657a14967efa393630d7aee27b5973c8e1c5ad39",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": false,
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "FSDinqdKK54PbjF73GgW3nUpf7bF8QbyxFCUurENmUyeEfSxSLL2hxwANBLzq1A8gTSAzzEn65hKjetA8o5BvjV6AYCZPxVAoDu21DryMoto",
|
|
||||||
"network": "Stagenet",
|
|
||||||
"spend": "4cd503040f5e43871bf37d8ca7177da655bda410859af754e24e7b44437f3151",
|
|
||||||
"view": "af60d42b6c6e4437fd93eb32657a14967efa393630d7aee27b5973c8e1c5ad39",
|
|
||||||
"subaddress": false,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [3, 115, 230, 129, 172, 108, 116, 235],
|
|
||||||
"guaranteed": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"address": "FSDinqdKK54PbjF73GgW3nUpf7bF8QbyxFCUurENmUyeEfSxSLL2hxwANBLzq1A8gTSAzzEn65hKjetA8o5BvjV6AYFYCqKQAWL18KkpBQ8R",
|
|
||||||
"network": "Stagenet",
|
|
||||||
"spend": "4cd503040f5e43871bf37d8ca7177da655bda410859af754e24e7b44437f3151",
|
|
||||||
"view": "af60d42b6c6e4437fd93eb32657a14967efa393630d7aee27b5973c8e1c5ad39",
|
|
||||||
"subaddress": true,
|
|
||||||
"integrated": true,
|
|
||||||
"payment_id": [94, 122, 63, 167, 209, 225, 14, 180],
|
|
||||||
"guaranteed": true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
@@ -1,433 +0,0 @@
|
|||||||
use core::cmp::Ordering;
|
|
||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use zeroize::Zeroize;
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::{EdwardsPoint, CompressedEdwardsY};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Protocol, hash,
|
|
||||||
serialize::*,
|
|
||||||
ring_signatures::RingSignature,
|
|
||||||
ringct::{bulletproofs::Bulletproofs, RctType, RctBase, RctPrunable, RctSignatures},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub enum Input {
|
|
||||||
Gen(u64),
|
|
||||||
ToKey { amount: Option<u64>, key_offsets: Vec<u64>, key_image: EdwardsPoint },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Input {
|
|
||||||
pub(crate) fn fee_weight(offsets_weight: usize) -> usize {
|
|
||||||
// Uses 1 byte for the input type
|
|
||||||
// Uses 1 byte for the VarInt amount due to amount being 0
|
|
||||||
1 + 1 + offsets_weight + 32
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
match self {
|
|
||||||
Input::Gen(height) => {
|
|
||||||
w.write_all(&[255])?;
|
|
||||||
write_varint(height, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
Input::ToKey { amount, key_offsets, key_image } => {
|
|
||||||
w.write_all(&[2])?;
|
|
||||||
write_varint(&amount.unwrap_or(0), w)?;
|
|
||||||
write_vec(write_varint, key_offsets, w)?;
|
|
||||||
write_point(key_image, w)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut res = vec![];
|
|
||||||
self.write(&mut res).unwrap();
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<Input> {
|
|
||||||
Ok(match read_byte(r)? {
|
|
||||||
255 => Input::Gen(read_varint(r)?),
|
|
||||||
2 => {
|
|
||||||
let amount = read_varint(r)?;
|
|
||||||
// https://github.com/monero-project/monero/
|
|
||||||
// blob/00fd416a99686f0956361d1cd0337fe56e58d4a7/
|
|
||||||
// src/cryptonote_basic/cryptonote_format_utils.cpp#L860-L863
|
|
||||||
// A non-RCT 0-amount input can't exist because only RCT TXs can have a 0-amount output
|
|
||||||
// That's why collapsing to None if the amount is 0 is safe, even without knowing if RCT
|
|
||||||
let amount = if amount == 0 { None } else { Some(amount) };
|
|
||||||
Input::ToKey {
|
|
||||||
amount,
|
|
||||||
key_offsets: read_vec(read_varint, r)?,
|
|
||||||
key_image: read_torsion_free_point(r)?,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => Err(io::Error::other("Tried to deserialize unknown/unused input type"))?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Doesn't bother moving to an enum for the unused Script classes
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct Output {
|
|
||||||
pub amount: Option<u64>,
|
|
||||||
pub key: CompressedEdwardsY,
|
|
||||||
pub view_tag: Option<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Output {
|
|
||||||
pub(crate) fn fee_weight(view_tags: bool) -> usize {
|
|
||||||
// Uses 1 byte for the output type
|
|
||||||
// Uses 1 byte for the VarInt amount due to amount being 0
|
|
||||||
1 + 1 + 32 + if view_tags { 1 } else { 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
write_varint(&self.amount.unwrap_or(0), w)?;
|
|
||||||
w.write_all(&[2 + u8::from(self.view_tag.is_some())])?;
|
|
||||||
w.write_all(&self.key.to_bytes())?;
|
|
||||||
if let Some(view_tag) = self.view_tag {
|
|
||||||
w.write_all(&[view_tag])?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut res = Vec::with_capacity(8 + 1 + 32);
|
|
||||||
self.write(&mut res).unwrap();
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(rct: bool, r: &mut R) -> io::Result<Output> {
|
|
||||||
let amount = read_varint(r)?;
|
|
||||||
let amount = if rct {
|
|
||||||
if amount != 0 {
|
|
||||||
Err(io::Error::other("RCT TX output wasn't 0"))?;
|
|
||||||
}
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(amount)
|
|
||||||
};
|
|
||||||
|
|
||||||
let view_tag = match read_byte(r)? {
|
|
||||||
2 => false,
|
|
||||||
3 => true,
|
|
||||||
_ => Err(io::Error::other("Tried to deserialize unknown/unused output type"))?,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Output {
|
|
||||||
amount,
|
|
||||||
key: CompressedEdwardsY(read_bytes(r)?),
|
|
||||||
view_tag: if view_tag { Some(read_byte(r)?) } else { None },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub enum Timelock {
|
|
||||||
None,
|
|
||||||
Block(usize),
|
|
||||||
Time(u64),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Timelock {
|
|
||||||
fn from_raw(raw: u64) -> Timelock {
|
|
||||||
if raw == 0 {
|
|
||||||
Timelock::None
|
|
||||||
} else if raw < 500_000_000 {
|
|
||||||
Timelock::Block(usize::try_from(raw).unwrap())
|
|
||||||
} else {
|
|
||||||
Timelock::Time(raw)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
write_varint(
|
|
||||||
&match self {
|
|
||||||
Timelock::None => 0,
|
|
||||||
Timelock::Block(block) => (*block).try_into().unwrap(),
|
|
||||||
Timelock::Time(time) => *time,
|
|
||||||
},
|
|
||||||
w,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for Timelock {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
match (self, other) {
|
|
||||||
(Timelock::None, _) => Some(Ordering::Less),
|
|
||||||
(Timelock::Block(a), Timelock::Block(b)) => a.partial_cmp(b),
|
|
||||||
(Timelock::Time(a), Timelock::Time(b)) => a.partial_cmp(b),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct TransactionPrefix {
|
|
||||||
pub version: u64,
|
|
||||||
pub timelock: Timelock,
|
|
||||||
pub inputs: Vec<Input>,
|
|
||||||
pub outputs: Vec<Output>,
|
|
||||||
pub extra: Vec<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TransactionPrefix {
|
|
||||||
pub(crate) fn fee_weight(
|
|
||||||
decoy_weights: &[usize],
|
|
||||||
outputs: usize,
|
|
||||||
view_tags: bool,
|
|
||||||
extra: usize,
|
|
||||||
) -> usize {
|
|
||||||
// Assumes Timelock::None since this library won't let you create a TX with a timelock
|
|
||||||
// 1 input for every decoy weight
|
|
||||||
1 + 1 +
|
|
||||||
varint_len(decoy_weights.len()) +
|
|
||||||
decoy_weights.iter().map(|&offsets_weight| Input::fee_weight(offsets_weight)).sum::<usize>() +
|
|
||||||
varint_len(outputs) +
|
|
||||||
(outputs * Output::fee_weight(view_tags)) +
|
|
||||||
varint_len(extra) +
|
|
||||||
extra
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
write_varint(&self.version, w)?;
|
|
||||||
self.timelock.write(w)?;
|
|
||||||
write_vec(Input::write, &self.inputs, w)?;
|
|
||||||
write_vec(Output::write, &self.outputs, w)?;
|
|
||||||
write_varint(&self.extra.len(), w)?;
|
|
||||||
w.write_all(&self.extra)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut res = vec![];
|
|
||||||
self.write(&mut res).unwrap();
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<TransactionPrefix> {
|
|
||||||
let version = read_varint(r)?;
|
|
||||||
// TODO: Create an enum out of version
|
|
||||||
if (version == 0) || (version > 2) {
|
|
||||||
Err(io::Error::other("unrecognized transaction version"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let timelock = Timelock::from_raw(read_varint(r)?);
|
|
||||||
|
|
||||||
let inputs = read_vec(|r| Input::read(r), r)?;
|
|
||||||
if inputs.is_empty() {
|
|
||||||
Err(io::Error::other("transaction had no inputs"))?;
|
|
||||||
}
|
|
||||||
let is_miner_tx = matches!(inputs[0], Input::Gen { .. });
|
|
||||||
|
|
||||||
let mut prefix = TransactionPrefix {
|
|
||||||
version,
|
|
||||||
timelock,
|
|
||||||
inputs,
|
|
||||||
outputs: read_vec(|r| Output::read((!is_miner_tx) && (version == 2), r), r)?,
|
|
||||||
extra: vec![],
|
|
||||||
};
|
|
||||||
prefix.extra = read_vec(read_byte, r)?;
|
|
||||||
Ok(prefix)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hash(&self) -> [u8; 32] {
|
|
||||||
hash(&self.serialize())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Monero transaction. For version 1, rct_signatures still contains an accurate fee value.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct Transaction {
|
|
||||||
pub prefix: TransactionPrefix,
|
|
||||||
pub signatures: Vec<RingSignature>,
|
|
||||||
pub rct_signatures: RctSignatures,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Transaction {
|
|
||||||
pub(crate) fn fee_weight(
|
|
||||||
protocol: Protocol,
|
|
||||||
decoy_weights: &[usize],
|
|
||||||
outputs: usize,
|
|
||||||
extra: usize,
|
|
||||||
fee: u64,
|
|
||||||
) -> usize {
|
|
||||||
TransactionPrefix::fee_weight(decoy_weights, outputs, protocol.view_tags(), extra) +
|
|
||||||
RctSignatures::fee_weight(protocol, decoy_weights.len(), outputs, fee)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
self.prefix.write(w)?;
|
|
||||||
if self.prefix.version == 1 {
|
|
||||||
for ring_sig in &self.signatures {
|
|
||||||
ring_sig.write(w)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
} else if self.prefix.version == 2 {
|
|
||||||
self.rct_signatures.write(w)
|
|
||||||
} else {
|
|
||||||
panic!("Serializing a transaction with an unknown version");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut res = Vec::with_capacity(2048);
|
|
||||||
self.write(&mut res).unwrap();
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<Transaction> {
|
|
||||||
let prefix = TransactionPrefix::read(r)?;
|
|
||||||
let mut signatures = vec![];
|
|
||||||
let mut rct_signatures = RctSignatures {
|
|
||||||
base: RctBase { fee: 0, encrypted_amounts: vec![], pseudo_outs: vec![], commitments: vec![] },
|
|
||||||
prunable: RctPrunable::Null,
|
|
||||||
};
|
|
||||||
|
|
||||||
if prefix.version == 1 {
|
|
||||||
signatures = prefix
|
|
||||||
.inputs
|
|
||||||
.iter()
|
|
||||||
.filter_map(|input| match input {
|
|
||||||
Input::ToKey { key_offsets, .. } => Some(RingSignature::read(key_offsets.len(), r)),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.collect::<Result<_, _>>()?;
|
|
||||||
|
|
||||||
if !matches!(prefix.inputs[0], Input::Gen(..)) {
|
|
||||||
let in_amount = prefix
|
|
||||||
.inputs
|
|
||||||
.iter()
|
|
||||||
.map(|input| match input {
|
|
||||||
Input::Gen(..) => Err(io::Error::other("Input::Gen present in non-coinbase v1 TX"))?,
|
|
||||||
// v1 TXs can burn v2 outputs
|
|
||||||
// dcff3fe4f914d6b6bd4a5b800cc4cca8f2fdd1bd73352f0700d463d36812f328 is one such TX
|
|
||||||
// It includes a pre-RCT signature for a RCT output, yet if you interpret the RCT
|
|
||||||
// output as being worth 0, it passes a sum check (guaranteed since no outputs are RCT)
|
|
||||||
Input::ToKey { amount, .. } => Ok(amount.unwrap_or(0)),
|
|
||||||
})
|
|
||||||
.collect::<io::Result<Vec<_>>>()?
|
|
||||||
.into_iter()
|
|
||||||
.sum::<u64>();
|
|
||||||
|
|
||||||
let mut out = 0;
|
|
||||||
for output in &prefix.outputs {
|
|
||||||
if output.amount.is_none() {
|
|
||||||
Err(io::Error::other("v1 transaction had a 0-amount output"))?;
|
|
||||||
}
|
|
||||||
out += output.amount.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
if in_amount < out {
|
|
||||||
Err(io::Error::other("transaction spent more than it had as inputs"))?;
|
|
||||||
}
|
|
||||||
rct_signatures.base.fee = in_amount - out;
|
|
||||||
}
|
|
||||||
} else if prefix.version == 2 {
|
|
||||||
rct_signatures = RctSignatures::read(
|
|
||||||
&prefix
|
|
||||||
.inputs
|
|
||||||
.iter()
|
|
||||||
.map(|input| match input {
|
|
||||||
Input::Gen(_) => 0,
|
|
||||||
Input::ToKey { key_offsets, .. } => key_offsets.len(),
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
prefix.outputs.len(),
|
|
||||||
r,
|
|
||||||
)?;
|
|
||||||
} else {
|
|
||||||
Err(io::Error::other("Tried to deserialize unknown version"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Transaction { prefix, signatures, rct_signatures })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hash(&self) -> [u8; 32] {
|
|
||||||
let mut buf = Vec::with_capacity(2048);
|
|
||||||
if self.prefix.version == 1 {
|
|
||||||
self.write(&mut buf).unwrap();
|
|
||||||
hash(&buf)
|
|
||||||
} else {
|
|
||||||
let mut hashes = Vec::with_capacity(96);
|
|
||||||
|
|
||||||
hashes.extend(self.prefix.hash());
|
|
||||||
|
|
||||||
self.rct_signatures.base.write(&mut buf, self.rct_signatures.rct_type()).unwrap();
|
|
||||||
hashes.extend(hash(&buf));
|
|
||||||
buf.clear();
|
|
||||||
|
|
||||||
hashes.extend(&match self.rct_signatures.prunable {
|
|
||||||
RctPrunable::Null => [0; 32],
|
|
||||||
_ => {
|
|
||||||
self.rct_signatures.prunable.write(&mut buf, self.rct_signatures.rct_type()).unwrap();
|
|
||||||
hash(&buf)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
hash(&hashes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculate the hash of this transaction as needed for signing it.
|
|
||||||
pub fn signature_hash(&self) -> [u8; 32] {
|
|
||||||
if self.prefix.version == 1 {
|
|
||||||
return self.prefix.hash();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = Vec::with_capacity(2048);
|
|
||||||
let mut sig_hash = Vec::with_capacity(96);
|
|
||||||
|
|
||||||
sig_hash.extend(self.prefix.hash());
|
|
||||||
|
|
||||||
self.rct_signatures.base.write(&mut buf, self.rct_signatures.rct_type()).unwrap();
|
|
||||||
sig_hash.extend(hash(&buf));
|
|
||||||
buf.clear();
|
|
||||||
|
|
||||||
self.rct_signatures.prunable.signature_write(&mut buf).unwrap();
|
|
||||||
sig_hash.extend(hash(&buf));
|
|
||||||
|
|
||||||
hash(&sig_hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_rct_bulletproof(&self) -> bool {
|
|
||||||
match &self.rct_signatures.rct_type() {
|
|
||||||
RctType::Bulletproofs | RctType::BulletproofsCompactAmount | RctType::Clsag => true,
|
|
||||||
RctType::Null |
|
|
||||||
RctType::MlsagAggregate |
|
|
||||||
RctType::MlsagIndividual |
|
|
||||||
RctType::BulletproofsPlus => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_rct_bulletproof_plus(&self) -> bool {
|
|
||||||
match &self.rct_signatures.rct_type() {
|
|
||||||
RctType::BulletproofsPlus => true,
|
|
||||||
RctType::Null |
|
|
||||||
RctType::MlsagAggregate |
|
|
||||||
RctType::MlsagIndividual |
|
|
||||||
RctType::Bulletproofs |
|
|
||||||
RctType::BulletproofsCompactAmount |
|
|
||||||
RctType::Clsag => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculate the transaction's weight.
|
|
||||||
pub fn weight(&self) -> usize {
|
|
||||||
let blob_size = self.serialize().len();
|
|
||||||
|
|
||||||
let bp = self.is_rct_bulletproof();
|
|
||||||
let bp_plus = self.is_rct_bulletproof_plus();
|
|
||||||
if !(bp || bp_plus) {
|
|
||||||
blob_size
|
|
||||||
} else {
|
|
||||||
blob_size + Bulletproofs::calculate_bp_clawback(bp_plus, self.prefix.outputs.len()).0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,137 +0,0 @@
|
|||||||
use core::cmp::Ordering;
|
|
||||||
|
|
||||||
use std_shims::{
|
|
||||||
sync::OnceLock,
|
|
||||||
io::{self, *},
|
|
||||||
};
|
|
||||||
|
|
||||||
use curve25519_dalek::scalar::Scalar;
|
|
||||||
|
|
||||||
use crate::serialize::*;
|
|
||||||
|
|
||||||
static PRECOMPUTED_SCALARS_CELL: OnceLock<[Scalar; 8]> = OnceLock::new();
|
|
||||||
/// Precomputed scalars used to recover an incorrectly reduced scalar.
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub(crate) fn PRECOMPUTED_SCALARS() -> [Scalar; 8] {
|
|
||||||
*PRECOMPUTED_SCALARS_CELL.get_or_init(|| {
|
|
||||||
let mut precomputed_scalars = [Scalar::ONE; 8];
|
|
||||||
for (i, scalar) in precomputed_scalars.iter_mut().enumerate().skip(1) {
|
|
||||||
*scalar = Scalar::from(u8::try_from((i * 2) + 1).unwrap());
|
|
||||||
}
|
|
||||||
precomputed_scalars
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
||||||
pub struct UnreducedScalar(pub [u8; 32]);
|
|
||||||
|
|
||||||
impl UnreducedScalar {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
w.write_all(&self.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<UnreducedScalar> {
|
|
||||||
Ok(UnreducedScalar(read_bytes(r)?))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_bytes(&self) -> &[u8; 32] {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_bits(&self) -> [u8; 256] {
|
|
||||||
let mut bits = [0; 256];
|
|
||||||
for (i, bit) in bits.iter_mut().enumerate() {
|
|
||||||
*bit = core::hint::black_box(1 & (self.0[i / 8] >> (i % 8)))
|
|
||||||
}
|
|
||||||
|
|
||||||
bits
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Computes the non-adjacent form of this scalar with width 5.
|
|
||||||
///
|
|
||||||
/// This matches Monero's `slide` function and intentionally gives incorrect outputs under
|
|
||||||
/// certain conditions in order to match Monero.
|
|
||||||
///
|
|
||||||
/// This function does not execute in constant time.
|
|
||||||
fn non_adjacent_form(&self) -> [i8; 256] {
|
|
||||||
let bits = self.as_bits();
|
|
||||||
let mut naf = [0i8; 256];
|
|
||||||
for (b, bit) in bits.into_iter().enumerate() {
|
|
||||||
naf[b] = i8::try_from(bit).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0 .. 256 {
|
|
||||||
if naf[i] != 0 {
|
|
||||||
// if the bit is a one, work our way up through the window
|
|
||||||
// combining the bits with this bit.
|
|
||||||
for b in 1 .. 6 {
|
|
||||||
if (i + b) >= 256 {
|
|
||||||
// if we are at the length of the array then break out
|
|
||||||
// the loop.
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// potential_carry - the value of the bit at i+b compared to the bit at i
|
|
||||||
let potential_carry = naf[i + b] << b;
|
|
||||||
|
|
||||||
if potential_carry != 0 {
|
|
||||||
if (naf[i] + potential_carry) <= 15 {
|
|
||||||
// if our current "bit" plus the potential carry is less than 16
|
|
||||||
// add it to our current "bit" and set the potential carry bit to 0.
|
|
||||||
naf[i] += potential_carry;
|
|
||||||
naf[i + b] = 0;
|
|
||||||
} else if (naf[i] - potential_carry) >= -15 {
|
|
||||||
// else if our current "bit" minus the potential carry is more than -16
|
|
||||||
// take it away from our current "bit".
|
|
||||||
// we then work our way up through the bits setting ones to zero, when
|
|
||||||
// we hit the first zero we change it to one then stop, this is to factor
|
|
||||||
// in the minus.
|
|
||||||
naf[i] -= potential_carry;
|
|
||||||
#[allow(clippy::needless_range_loop)]
|
|
||||||
for k in (i + b) .. 256 {
|
|
||||||
if naf[k] == 0 {
|
|
||||||
naf[k] = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
naf[k] = 0;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
naf
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Recover the scalar that an array of bytes was incorrectly interpreted as by Monero's `slide`
|
|
||||||
/// function.
|
|
||||||
///
|
|
||||||
/// In Borromean range proofs Monero was not checking that the scalars used were
|
|
||||||
/// reduced. This lead to the scalar stored being interpreted as a different scalar,
|
|
||||||
/// this function recovers that scalar.
|
|
||||||
///
|
|
||||||
/// See: https://github.com/monero-project/monero/issues/8438
|
|
||||||
pub fn recover_monero_slide_scalar(&self) -> Scalar {
|
|
||||||
if self.0[31] & 128 == 0 {
|
|
||||||
// Computing the w-NAF of a number can only give an output with 1 more bit than
|
|
||||||
// the number, so even if the number isn't reduced, the `slide` function will be
|
|
||||||
// correct when the last bit isn't set.
|
|
||||||
return Scalar::from_bytes_mod_order(self.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
let precomputed_scalars = PRECOMPUTED_SCALARS();
|
|
||||||
|
|
||||||
let mut recovered = Scalar::ZERO;
|
|
||||||
for &numb in self.non_adjacent_form().iter().rev() {
|
|
||||||
recovered += recovered;
|
|
||||||
match numb.cmp(&0) {
|
|
||||||
Ordering::Greater => recovered += precomputed_scalars[usize::try_from(numb).unwrap() / 2],
|
|
||||||
Ordering::Less => recovered -= precomputed_scalars[usize::try_from(-numb).unwrap() / 2],
|
|
||||||
Ordering::Equal => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
recovered
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,325 +0,0 @@
|
|||||||
use core::{marker::PhantomData, fmt::Debug};
|
|
||||||
use std_shims::string::{String, ToString};
|
|
||||||
|
|
||||||
use zeroize::Zeroize;
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::{EdwardsPoint, CompressedEdwardsY};
|
|
||||||
|
|
||||||
use base58_monero::base58::{encode_check, decode_check};
|
|
||||||
|
|
||||||
/// The network this address is for.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub enum Network {
|
|
||||||
Mainnet,
|
|
||||||
Testnet,
|
|
||||||
Stagenet,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The address type, supporting the officially documented addresses, along with
|
|
||||||
/// [Featured Addresses](https://gist.github.com/kayabaNerve/01c50bbc35441e0bbdcee63a9d823789).
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub enum AddressType {
|
|
||||||
Standard,
|
|
||||||
Integrated([u8; 8]),
|
|
||||||
Subaddress,
|
|
||||||
Featured { subaddress: bool, payment_id: Option<[u8; 8]>, guaranteed: bool },
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub struct SubaddressIndex {
|
|
||||||
pub(crate) account: u32,
|
|
||||||
pub(crate) address: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SubaddressIndex {
|
|
||||||
pub const fn new(account: u32, address: u32) -> Option<SubaddressIndex> {
|
|
||||||
if (account == 0) && (address == 0) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
Some(SubaddressIndex { account, address })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn account(&self) -> u32 {
|
|
||||||
self.account
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn address(&self) -> u32 {
|
|
||||||
self.address
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Address specification. Used internally to create addresses.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub enum AddressSpec {
|
|
||||||
Standard,
|
|
||||||
Integrated([u8; 8]),
|
|
||||||
Subaddress(SubaddressIndex),
|
|
||||||
Featured { subaddress: Option<SubaddressIndex>, payment_id: Option<[u8; 8]>, guaranteed: bool },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AddressType {
|
|
||||||
pub fn is_subaddress(&self) -> bool {
|
|
||||||
matches!(self, AddressType::Subaddress) ||
|
|
||||||
matches!(self, AddressType::Featured { subaddress: true, .. })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn payment_id(&self) -> Option<[u8; 8]> {
|
|
||||||
if let AddressType::Integrated(id) = self {
|
|
||||||
Some(*id)
|
|
||||||
} else if let AddressType::Featured { payment_id, .. } = self {
|
|
||||||
*payment_id
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_guaranteed(&self) -> bool {
|
|
||||||
matches!(self, AddressType::Featured { guaranteed: true, .. })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A type which returns the byte for a given address.
|
|
||||||
pub trait AddressBytes: Clone + Copy + PartialEq + Eq + Debug {
|
|
||||||
fn network_bytes(network: Network) -> (u8, u8, u8, u8);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Address bytes for Monero.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
pub struct MoneroAddressBytes;
|
|
||||||
impl AddressBytes for MoneroAddressBytes {
|
|
||||||
fn network_bytes(network: Network) -> (u8, u8, u8, u8) {
|
|
||||||
match network {
|
|
||||||
Network::Mainnet => (18, 19, 42, 70),
|
|
||||||
Network::Testnet => (53, 54, 63, 111),
|
|
||||||
Network::Stagenet => (24, 25, 36, 86),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Address metadata.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
pub struct AddressMeta<B: AddressBytes> {
|
|
||||||
_bytes: PhantomData<B>,
|
|
||||||
pub network: Network,
|
|
||||||
pub kind: AddressType,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B: AddressBytes> Zeroize for AddressMeta<B> {
|
|
||||||
fn zeroize(&mut self) {
|
|
||||||
self.network.zeroize();
|
|
||||||
self.kind.zeroize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Error when decoding an address.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
#[cfg_attr(feature = "std", derive(thiserror::Error))]
|
|
||||||
pub enum AddressError {
|
|
||||||
#[cfg_attr(feature = "std", error("invalid address byte"))]
|
|
||||||
InvalidByte,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid address encoding"))]
|
|
||||||
InvalidEncoding,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid length"))]
|
|
||||||
InvalidLength,
|
|
||||||
#[cfg_attr(feature = "std", error("invalid key"))]
|
|
||||||
InvalidKey,
|
|
||||||
#[cfg_attr(feature = "std", error("unknown features"))]
|
|
||||||
UnknownFeatures,
|
|
||||||
#[cfg_attr(feature = "std", error("different network than expected"))]
|
|
||||||
DifferentNetwork,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B: AddressBytes> AddressMeta<B> {
|
|
||||||
#[allow(clippy::wrong_self_convention)]
|
|
||||||
fn to_byte(&self) -> u8 {
|
|
||||||
let bytes = B::network_bytes(self.network);
|
|
||||||
match self.kind {
|
|
||||||
AddressType::Standard => bytes.0,
|
|
||||||
AddressType::Integrated(_) => bytes.1,
|
|
||||||
AddressType::Subaddress => bytes.2,
|
|
||||||
AddressType::Featured { .. } => bytes.3,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create an address's metadata.
|
|
||||||
pub fn new(network: Network, kind: AddressType) -> Self {
|
|
||||||
AddressMeta { _bytes: PhantomData, network, kind }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns an incomplete instantiation in the case of Integrated/Featured addresses
|
|
||||||
fn from_byte(byte: u8) -> Result<Self, AddressError> {
|
|
||||||
let mut meta = None;
|
|
||||||
for network in [Network::Mainnet, Network::Testnet, Network::Stagenet] {
|
|
||||||
let (standard, integrated, subaddress, featured) = B::network_bytes(network);
|
|
||||||
if let Some(kind) = match byte {
|
|
||||||
_ if byte == standard => Some(AddressType::Standard),
|
|
||||||
_ if byte == integrated => Some(AddressType::Integrated([0; 8])),
|
|
||||||
_ if byte == subaddress => Some(AddressType::Subaddress),
|
|
||||||
_ if byte == featured => {
|
|
||||||
Some(AddressType::Featured { subaddress: false, payment_id: None, guaranteed: false })
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
} {
|
|
||||||
meta = Some(AddressMeta::new(network, kind));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
meta.ok_or(AddressError::InvalidByte)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_subaddress(&self) -> bool {
|
|
||||||
self.kind.is_subaddress()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn payment_id(&self) -> Option<[u8; 8]> {
|
|
||||||
self.kind.payment_id()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_guaranteed(&self) -> bool {
|
|
||||||
self.kind.is_guaranteed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A Monero address, composed of metadata and a spend/view key.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub struct Address<B: AddressBytes> {
|
|
||||||
pub meta: AddressMeta<B>,
|
|
||||||
pub spend: EdwardsPoint,
|
|
||||||
pub view: EdwardsPoint,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B: AddressBytes> core::fmt::Debug for Address<B> {
|
|
||||||
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
|
|
||||||
fmt
|
|
||||||
.debug_struct("Address")
|
|
||||||
.field("meta", &self.meta)
|
|
||||||
.field("spend", &hex::encode(self.spend.compress().0))
|
|
||||||
.field("view", &hex::encode(self.view.compress().0))
|
|
||||||
// This is not a real field yet is the most valuable thing to know when debugging
|
|
||||||
.field("(address)", &self.to_string())
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B: AddressBytes> Zeroize for Address<B> {
|
|
||||||
fn zeroize(&mut self) {
|
|
||||||
self.meta.zeroize();
|
|
||||||
self.spend.zeroize();
|
|
||||||
self.view.zeroize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B: AddressBytes> ToString for Address<B> {
|
|
||||||
fn to_string(&self) -> String {
|
|
||||||
let mut data = vec![self.meta.to_byte()];
|
|
||||||
data.extend(self.spend.compress().to_bytes());
|
|
||||||
data.extend(self.view.compress().to_bytes());
|
|
||||||
if let AddressType::Featured { subaddress, payment_id, guaranteed } = self.meta.kind {
|
|
||||||
// Technically should be a VarInt, yet we don't have enough features it's needed
|
|
||||||
data.push(
|
|
||||||
u8::from(subaddress) + (u8::from(payment_id.is_some()) << 1) + (u8::from(guaranteed) << 2),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if let Some(id) = self.meta.kind.payment_id() {
|
|
||||||
data.extend(id);
|
|
||||||
}
|
|
||||||
encode_check(&data).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B: AddressBytes> Address<B> {
|
|
||||||
pub fn new(meta: AddressMeta<B>, spend: EdwardsPoint, view: EdwardsPoint) -> Self {
|
|
||||||
Address { meta, spend, view }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_str_raw(s: &str) -> Result<Self, AddressError> {
|
|
||||||
let raw = decode_check(s).map_err(|_| AddressError::InvalidEncoding)?;
|
|
||||||
if raw.len() < (1 + 32 + 32) {
|
|
||||||
Err(AddressError::InvalidLength)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut meta = AddressMeta::from_byte(raw[0])?;
|
|
||||||
let spend = CompressedEdwardsY(raw[1 .. 33].try_into().unwrap())
|
|
||||||
.decompress()
|
|
||||||
.ok_or(AddressError::InvalidKey)?;
|
|
||||||
let view = CompressedEdwardsY(raw[33 .. 65].try_into().unwrap())
|
|
||||||
.decompress()
|
|
||||||
.ok_or(AddressError::InvalidKey)?;
|
|
||||||
let mut read = 65;
|
|
||||||
|
|
||||||
if matches!(meta.kind, AddressType::Featured { .. }) {
|
|
||||||
if raw[read] >= (2 << 3) {
|
|
||||||
Err(AddressError::UnknownFeatures)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let subaddress = (raw[read] & 1) == 1;
|
|
||||||
let integrated = ((raw[read] >> 1) & 1) == 1;
|
|
||||||
let guaranteed = ((raw[read] >> 2) & 1) == 1;
|
|
||||||
|
|
||||||
meta.kind = AddressType::Featured {
|
|
||||||
subaddress,
|
|
||||||
payment_id: Some([0; 8]).filter(|_| integrated),
|
|
||||||
guaranteed,
|
|
||||||
};
|
|
||||||
read += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update read early so we can verify the length
|
|
||||||
if meta.kind.payment_id().is_some() {
|
|
||||||
read += 8;
|
|
||||||
}
|
|
||||||
if raw.len() != read {
|
|
||||||
Err(AddressError::InvalidLength)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let AddressType::Integrated(ref mut id) = meta.kind {
|
|
||||||
id.copy_from_slice(&raw[(read - 8) .. read]);
|
|
||||||
}
|
|
||||||
if let AddressType::Featured { payment_id: Some(ref mut id), .. } = meta.kind {
|
|
||||||
id.copy_from_slice(&raw[(read - 8) .. read]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Address { meta, spend, view })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_str(network: Network, s: &str) -> Result<Self, AddressError> {
|
|
||||||
Self::from_str_raw(s).and_then(|addr| {
|
|
||||||
if addr.meta.network == network {
|
|
||||||
Ok(addr)
|
|
||||||
} else {
|
|
||||||
Err(AddressError::DifferentNetwork)?
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn network(&self) -> Network {
|
|
||||||
self.meta.network
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_subaddress(&self) -> bool {
|
|
||||||
self.meta.is_subaddress()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn payment_id(&self) -> Option<[u8; 8]> {
|
|
||||||
self.meta.payment_id()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_guaranteed(&self) -> bool {
|
|
||||||
self.meta.is_guaranteed()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Instantiation of the Address type with Monero's network bytes.
|
|
||||||
pub type MoneroAddress = Address<MoneroAddressBytes>;
|
|
||||||
// Allow re-interpreting of an arbitrary address as a monero address so it can be used with the
|
|
||||||
// rest of this library. Doesn't use From as it was conflicting with From<T> for T.
|
|
||||||
impl MoneroAddress {
|
|
||||||
pub fn from<B: AddressBytes>(address: Address<B>) -> MoneroAddress {
|
|
||||||
MoneroAddress::new(
|
|
||||||
AddressMeta::new(address.meta.network, address.meta.kind),
|
|
||||||
address.spend,
|
|
||||||
address.view,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,298 +0,0 @@
|
|||||||
use std_shims::{vec::Vec, collections::HashSet};
|
|
||||||
|
|
||||||
#[cfg(feature = "cache-distribution")]
|
|
||||||
use std_shims::sync::OnceLock;
|
|
||||||
|
|
||||||
#[cfg(all(feature = "cache-distribution", not(feature = "std")))]
|
|
||||||
use std_shims::sync::Mutex;
|
|
||||||
#[cfg(all(feature = "cache-distribution", feature = "std"))]
|
|
||||||
use async_lock::Mutex;
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop};
|
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
use rand_distr::{Distribution, Gamma};
|
|
||||||
#[cfg(not(feature = "std"))]
|
|
||||||
use rand_distr::num_traits::Float;
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::EdwardsPoint;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
serialize::varint_len,
|
|
||||||
wallet::SpendableOutput,
|
|
||||||
rpc::{RpcError, RpcConnection, Rpc},
|
|
||||||
};
|
|
||||||
|
|
||||||
const LOCK_WINDOW: usize = 10;
|
|
||||||
const MATURITY: u64 = 60;
|
|
||||||
const RECENT_WINDOW: usize = 15;
|
|
||||||
const BLOCK_TIME: usize = 120;
|
|
||||||
const BLOCKS_PER_YEAR: usize = 365 * 24 * 60 * 60 / BLOCK_TIME;
|
|
||||||
#[allow(clippy::cast_precision_loss)]
|
|
||||||
const TIP_APPLICATION: f64 = (LOCK_WINDOW * BLOCK_TIME) as f64;
|
|
||||||
|
|
||||||
// TODO: Resolve safety of this in case a reorg occurs/the network changes
|
|
||||||
// TODO: Update this when scanning a block, as possible
|
|
||||||
#[cfg(feature = "cache-distribution")]
|
|
||||||
static DISTRIBUTION_CELL: OnceLock<Mutex<Vec<u64>>> = OnceLock::new();
|
|
||||||
#[cfg(feature = "cache-distribution")]
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
fn DISTRIBUTION() -> &'static Mutex<Vec<u64>> {
|
|
||||||
DISTRIBUTION_CELL.get_or_init(|| Mutex::new(Vec::with_capacity(3000000)))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
async fn select_n<'a, R: RngCore + CryptoRng, RPC: RpcConnection>(
|
|
||||||
rng: &mut R,
|
|
||||||
rpc: &Rpc<RPC>,
|
|
||||||
distribution: &[u64],
|
|
||||||
height: usize,
|
|
||||||
high: u64,
|
|
||||||
per_second: f64,
|
|
||||||
real: &[u64],
|
|
||||||
used: &mut HashSet<u64>,
|
|
||||||
count: usize,
|
|
||||||
) -> Result<Vec<(u64, [EdwardsPoint; 2])>, RpcError> {
|
|
||||||
if height >= rpc.get_height().await? {
|
|
||||||
// TODO: Don't use InternalError for the caller's failure
|
|
||||||
Err(RpcError::InternalError("decoys being requested from too young blocks"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
let mut iters = 0;
|
|
||||||
let mut confirmed = Vec::with_capacity(count);
|
|
||||||
// Retries on failure. Retries are obvious as decoys, yet should be minimal
|
|
||||||
while confirmed.len() != count {
|
|
||||||
let remaining = count - confirmed.len();
|
|
||||||
let mut candidates = Vec::with_capacity(remaining);
|
|
||||||
while candidates.len() != remaining {
|
|
||||||
#[cfg(test)]
|
|
||||||
{
|
|
||||||
iters += 1;
|
|
||||||
// This is cheap and on fresh chains, a lot of rounds may be needed
|
|
||||||
if iters == 100 {
|
|
||||||
Err(RpcError::InternalError("hit decoy selection round limit"))?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use a gamma distribution
|
|
||||||
let mut age = Gamma::<f64>::new(19.28, 1.0 / 1.61).unwrap().sample(rng).exp();
|
|
||||||
#[allow(clippy::cast_precision_loss)]
|
|
||||||
if age > TIP_APPLICATION {
|
|
||||||
age -= TIP_APPLICATION;
|
|
||||||
} else {
|
|
||||||
// f64 does not have try_from available, which is why these are written with `as`
|
|
||||||
age = (rng.next_u64() % u64::try_from(RECENT_WINDOW * BLOCK_TIME).unwrap()) as f64;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
|
|
||||||
let o = (age * per_second) as u64;
|
|
||||||
if o < high {
|
|
||||||
let i = distribution.partition_point(|s| *s < (high - 1 - o));
|
|
||||||
let prev = i.saturating_sub(1);
|
|
||||||
let n = distribution[i] - distribution[prev];
|
|
||||||
if n != 0 {
|
|
||||||
let o = distribution[prev] + (rng.next_u64() % n);
|
|
||||||
if !used.contains(&o) {
|
|
||||||
// It will either actually be used, or is unusable and this prevents trying it again
|
|
||||||
used.insert(o);
|
|
||||||
candidates.push(o);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If this is the first time we're requesting these outputs, include the real one as well
|
|
||||||
// Prevents the node we're connected to from having a list of known decoys and then seeing a
|
|
||||||
// TX which uses all of them, with one additional output (the true spend)
|
|
||||||
let mut real_indexes = HashSet::with_capacity(real.len());
|
|
||||||
if confirmed.is_empty() {
|
|
||||||
for real in real {
|
|
||||||
candidates.push(*real);
|
|
||||||
}
|
|
||||||
// Sort candidates so the real spends aren't the ones at the end
|
|
||||||
candidates.sort();
|
|
||||||
for real in real {
|
|
||||||
real_indexes.insert(candidates.binary_search(real).unwrap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i, output) in rpc.get_unlocked_outputs(&candidates, height).await?.iter_mut().enumerate() {
|
|
||||||
// Don't include the real spend as a decoy, despite requesting it
|
|
||||||
if real_indexes.contains(&i) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(output) = output.take() {
|
|
||||||
confirmed.push((candidates[i], output));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(confirmed)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn offset(ring: &[u64]) -> Vec<u64> {
|
|
||||||
let mut res = vec![ring[0]];
|
|
||||||
res.resize(ring.len(), 0);
|
|
||||||
for m in (1 .. ring.len()).rev() {
|
|
||||||
res[m] = ring[m] - ring[m - 1];
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Decoy data, containing the actual member as well (at index `i`).
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct Decoys {
|
|
||||||
pub(crate) i: u8,
|
|
||||||
pub(crate) offsets: Vec<u64>,
|
|
||||||
pub(crate) ring: Vec<[EdwardsPoint; 2]>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::len_without_is_empty)]
|
|
||||||
impl Decoys {
|
|
||||||
pub fn fee_weight(offsets: &[u64]) -> usize {
|
|
||||||
varint_len(offsets.len()) + offsets.iter().map(|offset| varint_len(*offset)).sum::<usize>()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.offsets.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Select decoys using the same distribution as Monero.
|
|
||||||
pub async fn select<R: RngCore + CryptoRng, RPC: RpcConnection>(
|
|
||||||
rng: &mut R,
|
|
||||||
rpc: &Rpc<RPC>,
|
|
||||||
ring_len: usize,
|
|
||||||
height: usize,
|
|
||||||
inputs: &[SpendableOutput],
|
|
||||||
) -> Result<Vec<Decoys>, RpcError> {
|
|
||||||
#[cfg(feature = "cache-distribution")]
|
|
||||||
#[cfg(not(feature = "std"))]
|
|
||||||
let mut distribution = DISTRIBUTION().lock();
|
|
||||||
#[cfg(feature = "cache-distribution")]
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
let mut distribution = DISTRIBUTION().lock().await;
|
|
||||||
|
|
||||||
#[cfg(not(feature = "cache-distribution"))]
|
|
||||||
let mut distribution = vec![];
|
|
||||||
|
|
||||||
let decoy_count = ring_len - 1;
|
|
||||||
|
|
||||||
// Convert the inputs in question to the raw output data
|
|
||||||
let mut real = Vec::with_capacity(inputs.len());
|
|
||||||
let mut outputs = Vec::with_capacity(inputs.len());
|
|
||||||
for input in inputs {
|
|
||||||
real.push(input.global_index);
|
|
||||||
outputs.push((real[real.len() - 1], [input.key(), input.commitment().calculate()]));
|
|
||||||
}
|
|
||||||
|
|
||||||
if distribution.len() <= height {
|
|
||||||
let extension = rpc.get_output_distribution(distribution.len(), height).await?;
|
|
||||||
distribution.extend(extension);
|
|
||||||
}
|
|
||||||
// If asked to use an older height than previously asked, truncate to ensure accuracy
|
|
||||||
// Should never happen, yet risks desyncing if it did
|
|
||||||
distribution.truncate(height + 1); // height is inclusive, and 0 is a valid height
|
|
||||||
|
|
||||||
let high = distribution[distribution.len() - 1];
|
|
||||||
#[allow(clippy::cast_precision_loss)]
|
|
||||||
let per_second = {
|
|
||||||
let blocks = distribution.len().min(BLOCKS_PER_YEAR);
|
|
||||||
let outputs = high - distribution[distribution.len().saturating_sub(blocks + 1)];
|
|
||||||
(outputs as f64) / ((blocks * BLOCK_TIME) as f64)
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut used = HashSet::<u64>::new();
|
|
||||||
for o in &outputs {
|
|
||||||
used.insert(o.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Create a TX with less than the target amount, as allowed by the protocol
|
|
||||||
if (high - MATURITY) < u64::try_from(inputs.len() * ring_len).unwrap() {
|
|
||||||
Err(RpcError::InternalError("not enough decoy candidates"))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select all decoys for this transaction, assuming we generate a sane transaction
|
|
||||||
// We should almost never naturally generate an insane transaction, hence why this doesn't
|
|
||||||
// bother with an overage
|
|
||||||
let mut decoys = select_n(
|
|
||||||
rng,
|
|
||||||
rpc,
|
|
||||||
&distribution,
|
|
||||||
height,
|
|
||||||
high,
|
|
||||||
per_second,
|
|
||||||
&real,
|
|
||||||
&mut used,
|
|
||||||
inputs.len() * decoy_count,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
real.zeroize();
|
|
||||||
|
|
||||||
let mut res = Vec::with_capacity(inputs.len());
|
|
||||||
for o in outputs {
|
|
||||||
// Grab the decoys for this specific output
|
|
||||||
let mut ring = decoys.drain((decoys.len() - decoy_count) ..).collect::<Vec<_>>();
|
|
||||||
ring.push(o);
|
|
||||||
ring.sort_by(|a, b| a.0.cmp(&b.0));
|
|
||||||
|
|
||||||
// Sanity checks are only run when 1000 outputs are available in Monero
|
|
||||||
// We run this check whenever the highest output index, which we acknowledge, is > 500
|
|
||||||
// This means we assume (for presumably test blockchains) the height being used has not had
|
|
||||||
// 500 outputs since while itself not being a sufficiently mature blockchain
|
|
||||||
// Considering Monero's p2p layer doesn't actually check transaction sanity, it should be
|
|
||||||
// fine for us to not have perfectly matching rules, especially since this code will infinite
|
|
||||||
// loop if it can't determine sanity, which is possible with sufficient inputs on
|
|
||||||
// sufficiently small chains
|
|
||||||
if high > 500 {
|
|
||||||
// Make sure the TX passes the sanity check that the median output is within the last 40%
|
|
||||||
let target_median = high * 3 / 5;
|
|
||||||
while ring[ring_len / 2].0 < target_median {
|
|
||||||
// If it's not, update the bottom half with new values to ensure the median only moves up
|
|
||||||
for removed in ring.drain(0 .. (ring_len / 2)).collect::<Vec<_>>() {
|
|
||||||
// If we removed the real spend, add it back
|
|
||||||
if removed.0 == o.0 {
|
|
||||||
ring.push(o);
|
|
||||||
} else {
|
|
||||||
// We could not remove this, saving CPU time and removing low values as
|
|
||||||
// possibilities, yet it'd increase the amount of decoys required to create this
|
|
||||||
// transaction and some removed outputs may be the best option (as we drop the first
|
|
||||||
// half, not just the bottom n)
|
|
||||||
used.remove(&removed.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select new outputs until we have a full sized ring again
|
|
||||||
ring.extend(
|
|
||||||
select_n(
|
|
||||||
rng,
|
|
||||||
rpc,
|
|
||||||
&distribution,
|
|
||||||
height,
|
|
||||||
high,
|
|
||||||
per_second,
|
|
||||||
&[],
|
|
||||||
&mut used,
|
|
||||||
ring_len - ring.len(),
|
|
||||||
)
|
|
||||||
.await?,
|
|
||||||
);
|
|
||||||
ring.sort_by(|a, b| a.0.cmp(&b.0));
|
|
||||||
}
|
|
||||||
|
|
||||||
// The other sanity check rule is about duplicates, yet we already enforce unique ring
|
|
||||||
// members
|
|
||||||
}
|
|
||||||
|
|
||||||
res.push(Decoys {
|
|
||||||
// Binary searches for the real spend since we don't know where it sorted to
|
|
||||||
i: u8::try_from(ring.partition_point(|x| x.0 < o.0)).unwrap(),
|
|
||||||
offsets: offset(&ring.iter().map(|output| output.0).collect::<Vec<_>>()),
|
|
||||||
ring: ring.iter().map(|output| output.1).collect(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,214 +0,0 @@
|
|||||||
use core::ops::BitXor;
|
|
||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use zeroize::Zeroize;
|
|
||||||
|
|
||||||
use curve25519_dalek::edwards::EdwardsPoint;
|
|
||||||
|
|
||||||
use crate::serialize::{
|
|
||||||
varint_len, read_byte, read_bytes, read_varint, read_point, read_vec, write_byte, write_varint,
|
|
||||||
write_point, write_vec,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const MAX_TX_EXTRA_NONCE_SIZE: usize = 255;
|
|
||||||
|
|
||||||
pub const PAYMENT_ID_MARKER: u8 = 0;
|
|
||||||
pub const ENCRYPTED_PAYMENT_ID_MARKER: u8 = 1;
|
|
||||||
// Used as it's the highest value not interpretable as a continued VarInt
|
|
||||||
pub const ARBITRARY_DATA_MARKER: u8 = 127;
|
|
||||||
|
|
||||||
// 1 byte is used for the marker
|
|
||||||
pub const MAX_ARBITRARY_DATA_SIZE: usize = MAX_TX_EXTRA_NONCE_SIZE - 1;
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub enum PaymentId {
|
|
||||||
Unencrypted([u8; 32]),
|
|
||||||
Encrypted([u8; 8]),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitXor<[u8; 8]> for PaymentId {
|
|
||||||
type Output = PaymentId;
|
|
||||||
|
|
||||||
fn bitxor(self, bytes: [u8; 8]) -> PaymentId {
|
|
||||||
match self {
|
|
||||||
// Don't perform the xor since this isn't intended to be encrypted with xor
|
|
||||||
PaymentId::Unencrypted(_) => self,
|
|
||||||
PaymentId::Encrypted(id) => {
|
|
||||||
PaymentId::Encrypted((u64::from_le_bytes(id) ^ u64::from_le_bytes(bytes)).to_le_bytes())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PaymentId {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
match self {
|
|
||||||
PaymentId::Unencrypted(id) => {
|
|
||||||
w.write_all(&[PAYMENT_ID_MARKER])?;
|
|
||||||
w.write_all(id)?;
|
|
||||||
}
|
|
||||||
PaymentId::Encrypted(id) => {
|
|
||||||
w.write_all(&[ENCRYPTED_PAYMENT_ID_MARKER])?;
|
|
||||||
w.write_all(id)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<PaymentId> {
|
|
||||||
Ok(match read_byte(r)? {
|
|
||||||
0 => PaymentId::Unencrypted(read_bytes(r)?),
|
|
||||||
1 => PaymentId::Encrypted(read_bytes(r)?),
|
|
||||||
_ => Err(io::Error::other("unknown payment ID type"))?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Doesn't bother with padding nor MinerGate
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub enum ExtraField {
|
|
||||||
PublicKey(EdwardsPoint),
|
|
||||||
Nonce(Vec<u8>),
|
|
||||||
MergeMining(usize, [u8; 32]),
|
|
||||||
PublicKeys(Vec<EdwardsPoint>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ExtraField {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
match self {
|
|
||||||
ExtraField::PublicKey(key) => {
|
|
||||||
w.write_all(&[1])?;
|
|
||||||
w.write_all(&key.compress().to_bytes())?;
|
|
||||||
}
|
|
||||||
ExtraField::Nonce(data) => {
|
|
||||||
w.write_all(&[2])?;
|
|
||||||
write_vec(write_byte, data, w)?;
|
|
||||||
}
|
|
||||||
ExtraField::MergeMining(height, merkle) => {
|
|
||||||
w.write_all(&[3])?;
|
|
||||||
write_varint(&u64::try_from(*height).unwrap(), w)?;
|
|
||||||
w.write_all(merkle)?;
|
|
||||||
}
|
|
||||||
ExtraField::PublicKeys(keys) => {
|
|
||||||
w.write_all(&[4])?;
|
|
||||||
write_vec(write_point, keys, w)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<ExtraField> {
|
|
||||||
Ok(match read_byte(r)? {
|
|
||||||
1 => ExtraField::PublicKey(read_point(r)?),
|
|
||||||
2 => ExtraField::Nonce({
|
|
||||||
let nonce = read_vec(read_byte, r)?;
|
|
||||||
if nonce.len() > MAX_TX_EXTRA_NONCE_SIZE {
|
|
||||||
Err(io::Error::other("too long nonce"))?;
|
|
||||||
}
|
|
||||||
nonce
|
|
||||||
}),
|
|
||||||
3 => ExtraField::MergeMining(read_varint(r)?, read_bytes(r)?),
|
|
||||||
4 => ExtraField::PublicKeys(read_vec(read_point, r)?),
|
|
||||||
_ => Err(io::Error::other("unknown extra field"))?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
||||||
pub struct Extra(Vec<ExtraField>);
|
|
||||||
impl Extra {
|
|
||||||
pub fn keys(&self) -> Option<(EdwardsPoint, Option<Vec<EdwardsPoint>>)> {
|
|
||||||
let mut key = None;
|
|
||||||
let mut additional = None;
|
|
||||||
for field in &self.0 {
|
|
||||||
match field.clone() {
|
|
||||||
ExtraField::PublicKey(this_key) => key = key.or(Some(this_key)),
|
|
||||||
ExtraField::PublicKeys(these_additional) => {
|
|
||||||
additional = additional.or(Some(these_additional))
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Don't return any keys if this was non-standard and didn't include the primary key
|
|
||||||
key.map(|key| (key, additional))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn payment_id(&self) -> Option<PaymentId> {
|
|
||||||
for field in &self.0 {
|
|
||||||
if let ExtraField::Nonce(data) = field {
|
|
||||||
return PaymentId::read::<&[u8]>(&mut data.as_ref()).ok();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn data(&self) -> Vec<Vec<u8>> {
|
|
||||||
let mut res = vec![];
|
|
||||||
for field in &self.0 {
|
|
||||||
if let ExtraField::Nonce(data) = field {
|
|
||||||
if data[0] == ARBITRARY_DATA_MARKER {
|
|
||||||
res.push(data[1 ..].to_vec());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn new(key: EdwardsPoint, additional: Vec<EdwardsPoint>) -> Extra {
|
|
||||||
let mut res = Extra(Vec::with_capacity(3));
|
|
||||||
res.push(ExtraField::PublicKey(key));
|
|
||||||
if !additional.is_empty() {
|
|
||||||
res.push(ExtraField::PublicKeys(additional));
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn push(&mut self, field: ExtraField) {
|
|
||||||
self.0.push(field);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rustfmt::skip]
|
|
||||||
pub(crate) fn fee_weight(
|
|
||||||
outputs: usize,
|
|
||||||
additional: bool,
|
|
||||||
payment_id: bool,
|
|
||||||
data: &[Vec<u8>]
|
|
||||||
) -> usize {
|
|
||||||
// PublicKey, key
|
|
||||||
(1 + 32) +
|
|
||||||
// PublicKeys, length, additional keys
|
|
||||||
(if additional { 1 + 1 + (outputs * 32) } else { 0 }) +
|
|
||||||
// PaymentId (Nonce), length, encrypted, ID
|
|
||||||
(if payment_id { 1 + 1 + 1 + 8 } else { 0 }) +
|
|
||||||
// Nonce, length, ARBITRARY_DATA_MARKER, data
|
|
||||||
data.iter().map(|v| 1 + varint_len(1 + v.len()) + 1 + v.len()).sum::<usize>()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
for field in &self.0 {
|
|
||||||
field.write(w)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut buf = vec![];
|
|
||||||
self.write(&mut buf).unwrap();
|
|
||||||
buf
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<Extra> {
|
|
||||||
let mut res = Extra(vec![]);
|
|
||||||
let mut field;
|
|
||||||
while {
|
|
||||||
field = ExtraField::read(r);
|
|
||||||
field.is_ok()
|
|
||||||
} {
|
|
||||||
res.0.push(field.unwrap());
|
|
||||||
}
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,268 +0,0 @@
|
|||||||
use core::ops::Deref;
|
|
||||||
use std_shims::collections::{HashSet, HashMap};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
|
|
||||||
|
|
||||||
use curve25519_dalek::{
|
|
||||||
constants::ED25519_BASEPOINT_TABLE,
|
|
||||||
scalar::Scalar,
|
|
||||||
edwards::{EdwardsPoint, CompressedEdwardsY},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
hash, hash_to_scalar, serialize::write_varint, ringct::EncryptedAmount, transaction::Input,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod extra;
|
|
||||||
pub(crate) use extra::{PaymentId, ExtraField, Extra};
|
|
||||||
|
|
||||||
/// Seed creation and parsing functionality.
|
|
||||||
pub mod seed;
|
|
||||||
|
|
||||||
/// Address encoding and decoding functionality.
|
|
||||||
pub mod address;
|
|
||||||
use address::{Network, AddressType, SubaddressIndex, AddressSpec, AddressMeta, MoneroAddress};
|
|
||||||
|
|
||||||
mod scan;
|
|
||||||
pub use scan::{ReceivedOutput, SpendableOutput, Timelocked};
|
|
||||||
|
|
||||||
pub mod decoys;
|
|
||||||
pub use decoys::Decoys;
|
|
||||||
|
|
||||||
mod send;
|
|
||||||
pub use send::{FeePriority, Fee, TransactionError, Change, SignableTransaction, Eventuality};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use send::SignableTransactionBuilder;
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
pub(crate) use send::InternalPayment;
|
|
||||||
#[cfg(feature = "multisig")]
|
|
||||||
pub use send::TransactionMachine;
|
|
||||||
|
|
||||||
fn key_image_sort(x: &EdwardsPoint, y: &EdwardsPoint) -> core::cmp::Ordering {
|
|
||||||
x.compress().to_bytes().cmp(&y.compress().to_bytes()).reverse()
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://gist.github.com/kayabaNerve/8066c13f1fe1573286ba7a2fd79f6100
|
|
||||||
pub(crate) fn uniqueness(inputs: &[Input]) -> [u8; 32] {
|
|
||||||
let mut u = b"uniqueness".to_vec();
|
|
||||||
for input in inputs {
|
|
||||||
match input {
|
|
||||||
// If Gen, this should be the only input, making this loop somewhat pointless
|
|
||||||
// This works and even if there were somehow multiple inputs, it'd be a false negative
|
|
||||||
Input::Gen(height) => {
|
|
||||||
write_varint(height, &mut u).unwrap();
|
|
||||||
}
|
|
||||||
Input::ToKey { key_image, .. } => u.extend(key_image.compress().to_bytes()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hash(&u)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hs("view_tag" || 8Ra || o), Hs(8Ra || o), and H(8Ra || 0x8d) with uniqueness inclusion in the
|
|
||||||
// Scalar as an option
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub(crate) fn shared_key(
|
|
||||||
uniqueness: Option<[u8; 32]>,
|
|
||||||
ecdh: EdwardsPoint,
|
|
||||||
o: usize,
|
|
||||||
) -> (u8, Scalar, [u8; 8]) {
|
|
||||||
// 8Ra
|
|
||||||
let mut output_derivation = ecdh.mul_by_cofactor().compress().to_bytes().to_vec();
|
|
||||||
|
|
||||||
let mut payment_id_xor = [0; 8];
|
|
||||||
payment_id_xor
|
|
||||||
.copy_from_slice(&hash(&[output_derivation.as_ref(), [0x8d].as_ref()].concat())[.. 8]);
|
|
||||||
|
|
||||||
// || o
|
|
||||||
write_varint(&o, &mut output_derivation).unwrap();
|
|
||||||
|
|
||||||
let view_tag = hash(&[b"view_tag".as_ref(), &output_derivation].concat())[0];
|
|
||||||
|
|
||||||
// uniqueness ||
|
|
||||||
let shared_key = if let Some(uniqueness) = uniqueness {
|
|
||||||
[uniqueness.as_ref(), &output_derivation].concat()
|
|
||||||
} else {
|
|
||||||
output_derivation
|
|
||||||
};
|
|
||||||
|
|
||||||
(view_tag, hash_to_scalar(&shared_key), payment_id_xor)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn commitment_mask(shared_key: Scalar) -> Scalar {
|
|
||||||
let mut mask = b"commitment_mask".to_vec();
|
|
||||||
mask.extend(shared_key.to_bytes());
|
|
||||||
hash_to_scalar(&mask)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn amount_encryption(amount: u64, key: Scalar) -> [u8; 8] {
|
|
||||||
let mut amount_mask = b"amount".to_vec();
|
|
||||||
amount_mask.extend(key.to_bytes());
|
|
||||||
(amount ^ u64::from_le_bytes(hash(&amount_mask)[.. 8].try_into().unwrap())).to_le_bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Move this under EncryptedAmount?
|
|
||||||
fn amount_decryption(amount: &EncryptedAmount, key: Scalar) -> (Scalar, u64) {
|
|
||||||
match amount {
|
|
||||||
EncryptedAmount::Original { mask, amount } => {
|
|
||||||
#[cfg(feature = "experimental")]
|
|
||||||
{
|
|
||||||
let mask_shared_sec = hash(key.as_bytes());
|
|
||||||
let mask =
|
|
||||||
Scalar::from_bytes_mod_order(*mask) - Scalar::from_bytes_mod_order(mask_shared_sec);
|
|
||||||
|
|
||||||
let amount_shared_sec = hash(&mask_shared_sec);
|
|
||||||
let amount_scalar =
|
|
||||||
Scalar::from_bytes_mod_order(*amount) - Scalar::from_bytes_mod_order(amount_shared_sec);
|
|
||||||
// d2b from rctTypes.cpp
|
|
||||||
let amount = u64::from_le_bytes(amount_scalar.to_bytes()[0 .. 8].try_into().unwrap());
|
|
||||||
|
|
||||||
(mask, amount)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "experimental"))]
|
|
||||||
{
|
|
||||||
let _ = mask;
|
|
||||||
let _ = amount;
|
|
||||||
todo!("decrypting a legacy monero transaction's amount")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EncryptedAmount::Compact { amount } => (
|
|
||||||
commitment_mask(key),
|
|
||||||
u64::from_le_bytes(amount_encryption(u64::from_le_bytes(*amount), key)),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The private view key and public spend key, enabling scanning transactions.
|
|
||||||
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct ViewPair {
|
|
||||||
spend: EdwardsPoint,
|
|
||||||
view: Zeroizing<Scalar>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ViewPair {
|
|
||||||
pub fn new(spend: EdwardsPoint, view: Zeroizing<Scalar>) -> ViewPair {
|
|
||||||
ViewPair { spend, view }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spend(&self) -> EdwardsPoint {
|
|
||||||
self.spend
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn view(&self) -> EdwardsPoint {
|
|
||||||
self.view.deref() * ED25519_BASEPOINT_TABLE
|
|
||||||
}
|
|
||||||
|
|
||||||
fn subaddress_derivation(&self, index: SubaddressIndex) -> Scalar {
|
|
||||||
hash_to_scalar(&Zeroizing::new(
|
|
||||||
[
|
|
||||||
b"SubAddr\0".as_ref(),
|
|
||||||
Zeroizing::new(self.view.to_bytes()).as_ref(),
|
|
||||||
&index.account().to_le_bytes(),
|
|
||||||
&index.address().to_le_bytes(),
|
|
||||||
]
|
|
||||||
.concat(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn subaddress_keys(&self, index: SubaddressIndex) -> (EdwardsPoint, EdwardsPoint) {
|
|
||||||
let scalar = self.subaddress_derivation(index);
|
|
||||||
let spend = self.spend + (&scalar * ED25519_BASEPOINT_TABLE);
|
|
||||||
let view = self.view.deref() * spend;
|
|
||||||
(spend, view)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an address with the provided specification.
|
|
||||||
pub fn address(&self, network: Network, spec: AddressSpec) -> MoneroAddress {
|
|
||||||
let mut spend = self.spend;
|
|
||||||
let mut view: EdwardsPoint = self.view.deref() * ED25519_BASEPOINT_TABLE;
|
|
||||||
|
|
||||||
// construct the address meta
|
|
||||||
let meta = match spec {
|
|
||||||
AddressSpec::Standard => AddressMeta::new(network, AddressType::Standard),
|
|
||||||
AddressSpec::Integrated(payment_id) => {
|
|
||||||
AddressMeta::new(network, AddressType::Integrated(payment_id))
|
|
||||||
}
|
|
||||||
AddressSpec::Subaddress(index) => {
|
|
||||||
(spend, view) = self.subaddress_keys(index);
|
|
||||||
AddressMeta::new(network, AddressType::Subaddress)
|
|
||||||
}
|
|
||||||
AddressSpec::Featured { subaddress, payment_id, guaranteed } => {
|
|
||||||
if let Some(index) = subaddress {
|
|
||||||
(spend, view) = self.subaddress_keys(index);
|
|
||||||
}
|
|
||||||
AddressMeta::new(
|
|
||||||
network,
|
|
||||||
AddressType::Featured { subaddress: subaddress.is_some(), payment_id, guaranteed },
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
MoneroAddress::new(meta, spend, view)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Transaction scanner.
|
|
||||||
/// This scanner is capable of generating subaddresses, additionally scanning for them once they've
|
|
||||||
/// been explicitly generated. If the burning bug is attempted, any secondary outputs will be
|
|
||||||
/// ignored.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Scanner {
|
|
||||||
pair: ViewPair,
|
|
||||||
// Also contains the spend key as None
|
|
||||||
pub(crate) subaddresses: HashMap<CompressedEdwardsY, Option<SubaddressIndex>>,
|
|
||||||
pub(crate) burning_bug: Option<HashSet<CompressedEdwardsY>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Zeroize for Scanner {
|
|
||||||
fn zeroize(&mut self) {
|
|
||||||
self.pair.zeroize();
|
|
||||||
|
|
||||||
// These may not be effective, unfortunately
|
|
||||||
for (mut key, mut value) in self.subaddresses.drain() {
|
|
||||||
key.zeroize();
|
|
||||||
value.zeroize();
|
|
||||||
}
|
|
||||||
if let Some(ref mut burning_bug) = self.burning_bug.take() {
|
|
||||||
for mut output in burning_bug.drain() {
|
|
||||||
output.zeroize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for Scanner {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.zeroize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ZeroizeOnDrop for Scanner {}
|
|
||||||
|
|
||||||
impl Scanner {
|
|
||||||
/// Create a Scanner from a ViewPair.
|
|
||||||
///
|
|
||||||
/// burning_bug is a HashSet of used keys, intended to prevent key reuse which would burn funds.
|
|
||||||
///
|
|
||||||
/// When an output is successfully scanned, the output key MUST be saved to disk.
|
|
||||||
///
|
|
||||||
/// When a new scanner is created, ALL saved output keys must be passed in to be secure.
|
|
||||||
///
|
|
||||||
/// If None is passed, a modified shared key derivation is used which is immune to the burning
|
|
||||||
/// bug (specifically the Guaranteed feature from Featured Addresses).
|
|
||||||
pub fn from_view(pair: ViewPair, burning_bug: Option<HashSet<CompressedEdwardsY>>) -> Scanner {
|
|
||||||
let mut subaddresses = HashMap::new();
|
|
||||||
subaddresses.insert(pair.spend.compress(), None);
|
|
||||||
Scanner { pair, subaddresses, burning_bug }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Register a subaddress.
|
|
||||||
// There used to be an address function here, yet it wasn't safe. It could generate addresses
|
|
||||||
// incompatible with the Scanner. While we could return None for that, then we have the issue
|
|
||||||
// of runtime failures to generate an address.
|
|
||||||
// Removing that API was the simplest option.
|
|
||||||
pub fn register_subaddress(&mut self, subaddress: SubaddressIndex) {
|
|
||||||
let (spend, _) = self.pair.subaddress_keys(subaddress);
|
|
||||||
self.subaddresses.insert(spend.compress(), Some(subaddress));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,509 +0,0 @@
|
|||||||
use core::ops::Deref;
|
|
||||||
use std_shims::{
|
|
||||||
vec::Vec,
|
|
||||||
string::ToString,
|
|
||||||
io::{self, Read, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, ZeroizeOnDrop};
|
|
||||||
|
|
||||||
use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, scalar::Scalar, edwards::EdwardsPoint};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
Commitment,
|
|
||||||
serialize::{read_byte, read_u32, read_u64, read_bytes, read_scalar, read_point, read_raw_vec},
|
|
||||||
transaction::{Input, Timelock, Transaction},
|
|
||||||
block::Block,
|
|
||||||
rpc::{RpcError, RpcConnection, Rpc},
|
|
||||||
wallet::{
|
|
||||||
PaymentId, Extra, address::SubaddressIndex, Scanner, uniqueness, shared_key, amount_decryption,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// An absolute output ID, defined as its transaction hash and output index.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct AbsoluteId {
|
|
||||||
pub tx: [u8; 32],
|
|
||||||
pub o: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl core::fmt::Debug for AbsoluteId {
|
|
||||||
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
|
|
||||||
fmt.debug_struct("AbsoluteId").field("tx", &hex::encode(self.tx)).field("o", &self.o).finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AbsoluteId {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
w.write_all(&self.tx)?;
|
|
||||||
w.write_all(&[self.o])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = Vec::with_capacity(32 + 1);
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<AbsoluteId> {
|
|
||||||
Ok(AbsoluteId { tx: read_bytes(r)?, o: read_byte(r)? })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The data contained with an output.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct OutputData {
|
|
||||||
pub key: EdwardsPoint,
|
|
||||||
/// Absolute difference between the spend key and the key in this output
|
|
||||||
pub key_offset: Scalar,
|
|
||||||
pub commitment: Commitment,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl core::fmt::Debug for OutputData {
|
|
||||||
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
|
|
||||||
fmt
|
|
||||||
.debug_struct("OutputData")
|
|
||||||
.field("key", &hex::encode(self.key.compress().0))
|
|
||||||
.field("key_offset", &hex::encode(self.key_offset.to_bytes()))
|
|
||||||
.field("commitment", &self.commitment)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OutputData {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
w.write_all(&self.key.compress().to_bytes())?;
|
|
||||||
w.write_all(&self.key_offset.to_bytes())?;
|
|
||||||
w.write_all(&self.commitment.mask.to_bytes())?;
|
|
||||||
w.write_all(&self.commitment.amount.to_le_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = Vec::with_capacity(32 + 32 + 32 + 8);
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<OutputData> {
|
|
||||||
Ok(OutputData {
|
|
||||||
key: read_point(r)?,
|
|
||||||
key_offset: read_scalar(r)?,
|
|
||||||
commitment: Commitment::new(read_scalar(r)?, read_u64(r)?),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The metadata for an output.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct Metadata {
|
|
||||||
/// The subaddress this output was sent to.
|
|
||||||
pub subaddress: Option<SubaddressIndex>,
|
|
||||||
/// The payment ID included with this output.
|
|
||||||
/// This will be gibberish if the payment ID wasn't intended for the recipient or wasn't included.
|
|
||||||
// Could be an Option, as extra doesn't necessarily have a payment ID, yet all Monero TXs should
|
|
||||||
// have this making it simplest for it to be as-is.
|
|
||||||
pub payment_id: [u8; 8],
|
|
||||||
/// Arbitrary data encoded in TX extra.
|
|
||||||
pub arbitrary_data: Vec<Vec<u8>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl core::fmt::Debug for Metadata {
|
|
||||||
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
|
|
||||||
fmt
|
|
||||||
.debug_struct("Metadata")
|
|
||||||
.field("subaddress", &self.subaddress)
|
|
||||||
.field("payment_id", &hex::encode(self.payment_id))
|
|
||||||
.field("arbitrary_data", &self.arbitrary_data.iter().map(hex::encode).collect::<Vec<_>>())
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Metadata {
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
if let Some(subaddress) = self.subaddress {
|
|
||||||
w.write_all(&[1])?;
|
|
||||||
w.write_all(&subaddress.account().to_le_bytes())?;
|
|
||||||
w.write_all(&subaddress.address().to_le_bytes())?;
|
|
||||||
} else {
|
|
||||||
w.write_all(&[0])?;
|
|
||||||
}
|
|
||||||
w.write_all(&self.payment_id)?;
|
|
||||||
|
|
||||||
w.write_all(&u32::try_from(self.arbitrary_data.len()).unwrap().to_le_bytes())?;
|
|
||||||
for part in &self.arbitrary_data {
|
|
||||||
w.write_all(&[u8::try_from(part.len()).unwrap()])?;
|
|
||||||
w.write_all(part)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = Vec::with_capacity(1 + 8 + 1);
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<Metadata> {
|
|
||||||
let subaddress = if read_byte(r)? == 1 {
|
|
||||||
Some(
|
|
||||||
SubaddressIndex::new(read_u32(r)?, read_u32(r)?)
|
|
||||||
.ok_or_else(|| io::Error::other("invalid subaddress in metadata"))?,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Metadata {
|
|
||||||
subaddress,
|
|
||||||
payment_id: read_bytes(r)?,
|
|
||||||
arbitrary_data: {
|
|
||||||
let mut data = vec![];
|
|
||||||
for _ in 0 .. read_u32(r)? {
|
|
||||||
let len = read_byte(r)?;
|
|
||||||
data.push(read_raw_vec(read_byte, usize::from(len), r)?);
|
|
||||||
}
|
|
||||||
data
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A received output, defined as its absolute ID, data, and metadara.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct ReceivedOutput {
|
|
||||||
pub absolute: AbsoluteId,
|
|
||||||
pub data: OutputData,
|
|
||||||
pub metadata: Metadata,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ReceivedOutput {
|
|
||||||
pub fn key(&self) -> EdwardsPoint {
|
|
||||||
self.data.key
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn key_offset(&self) -> Scalar {
|
|
||||||
self.data.key_offset
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn commitment(&self) -> Commitment {
|
|
||||||
self.data.commitment.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn arbitrary_data(&self) -> &[Vec<u8>] {
|
|
||||||
&self.metadata.arbitrary_data
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
self.absolute.write(w)?;
|
|
||||||
self.data.write(w)?;
|
|
||||||
self.metadata.write(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = vec![];
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<ReceivedOutput> {
|
|
||||||
Ok(ReceivedOutput {
|
|
||||||
absolute: AbsoluteId::read(r)?,
|
|
||||||
data: OutputData::read(r)?,
|
|
||||||
metadata: Metadata::read(r)?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A spendable output, defined as a received output and its index on the Monero blockchain.
|
|
||||||
/// This index is dependent on the Monero blockchain and will only be known once the output is
|
|
||||||
/// included within a block. This may change if there's a reorganization.
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
|
|
||||||
pub struct SpendableOutput {
|
|
||||||
pub output: ReceivedOutput,
|
|
||||||
pub global_index: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SpendableOutput {
|
|
||||||
/// Update the spendable output's global index. This is intended to be called if a
|
|
||||||
/// re-organization occurred.
|
|
||||||
pub async fn refresh_global_index<RPC: RpcConnection>(
|
|
||||||
&mut self,
|
|
||||||
rpc: &Rpc<RPC>,
|
|
||||||
) -> Result<(), RpcError> {
|
|
||||||
self.global_index = *rpc
|
|
||||||
.get_o_indexes(self.output.absolute.tx)
|
|
||||||
.await?
|
|
||||||
.get(usize::from(self.output.absolute.o))
|
|
||||||
.ok_or(RpcError::InvalidNode(
|
|
||||||
"node returned output indexes didn't include an index for this output".to_string(),
|
|
||||||
))?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn from<RPC: RpcConnection>(
|
|
||||||
rpc: &Rpc<RPC>,
|
|
||||||
output: ReceivedOutput,
|
|
||||||
) -> Result<SpendableOutput, RpcError> {
|
|
||||||
let mut output = SpendableOutput { output, global_index: 0 };
|
|
||||||
output.refresh_global_index(rpc).await?;
|
|
||||||
Ok(output)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn key(&self) -> EdwardsPoint {
|
|
||||||
self.output.key()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn key_offset(&self) -> Scalar {
|
|
||||||
self.output.key_offset()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn commitment(&self) -> Commitment {
|
|
||||||
self.output.commitment()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn arbitrary_data(&self) -> &[Vec<u8>] {
|
|
||||||
self.output.arbitrary_data()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
|
||||||
self.output.write(w)?;
|
|
||||||
w.write_all(&self.global_index.to_le_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
|
||||||
let mut serialized = vec![];
|
|
||||||
self.write(&mut serialized).unwrap();
|
|
||||||
serialized
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read<R: Read>(r: &mut R) -> io::Result<SpendableOutput> {
|
|
||||||
Ok(SpendableOutput { output: ReceivedOutput::read(r)?, global_index: read_u64(r)? })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A collection of timelocked outputs, either received or spendable.
|
|
||||||
#[derive(Zeroize)]
|
|
||||||
pub struct Timelocked<O: Clone + Zeroize>(Timelock, Vec<O>);
|
|
||||||
impl<O: Clone + Zeroize> Drop for Timelocked<O> {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.zeroize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<O: Clone + Zeroize> ZeroizeOnDrop for Timelocked<O> {}
|
|
||||||
|
|
||||||
impl<O: Clone + Zeroize> Timelocked<O> {
|
|
||||||
pub fn timelock(&self) -> Timelock {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return the outputs if they're not timelocked, or an empty vector if they are.
|
|
||||||
#[must_use]
|
|
||||||
pub fn not_locked(&self) -> Vec<O> {
|
|
||||||
if self.0 == Timelock::None {
|
|
||||||
return self.1.clone();
|
|
||||||
}
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns None if the Timelocks aren't comparable. Returns Some(vec![]) if none are unlocked.
|
|
||||||
#[must_use]
|
|
||||||
pub fn unlocked(&self, timelock: Timelock) -> Option<Vec<O>> {
|
|
||||||
// If the Timelocks are comparable, return the outputs if they're now unlocked
|
|
||||||
if self.0 <= timelock {
|
|
||||||
Some(self.1.clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn ignore_timelock(&self) -> Vec<O> {
|
|
||||||
self.1.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Scanner {
|
|
||||||
/// Scan a transaction to discover the received outputs.
|
|
||||||
pub fn scan_transaction(&mut self, tx: &Transaction) -> Timelocked<ReceivedOutput> {
|
|
||||||
// Only scan RCT TXs since we can only spend RCT outputs
|
|
||||||
if tx.prefix.version != 2 {
|
|
||||||
return Timelocked(tx.prefix.timelock, vec![]);
|
|
||||||
}
|
|
||||||
|
|
||||||
let Ok(extra) = Extra::read::<&[u8]>(&mut tx.prefix.extra.as_ref()) else {
|
|
||||||
return Timelocked(tx.prefix.timelock, vec![]);
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some((tx_key, additional)) = extra.keys() else {
|
|
||||||
return Timelocked(tx.prefix.timelock, vec![]);
|
|
||||||
};
|
|
||||||
|
|
||||||
let payment_id = extra.payment_id();
|
|
||||||
|
|
||||||
let mut res = vec![];
|
|
||||||
for (o, output) in tx.prefix.outputs.iter().enumerate() {
|
|
||||||
// https://github.com/serai-dex/serai/issues/106
|
|
||||||
if let Some(burning_bug) = self.burning_bug.as_ref() {
|
|
||||||
if burning_bug.contains(&output.key) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let output_key = output.key.decompress();
|
|
||||||
if output_key.is_none() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let output_key = output_key.unwrap();
|
|
||||||
|
|
||||||
for key in [Some(Some(&tx_key)), additional.as_ref().map(|additional| additional.get(o))] {
|
|
||||||
let key = match key {
|
|
||||||
Some(Some(key)) => key,
|
|
||||||
Some(None) => {
|
|
||||||
// This is non-standard. There were additional keys, yet not one for this output
|
|
||||||
// https://github.com/monero-project/monero/
|
|
||||||
// blob/04a1e2875d6e35e27bb21497988a6c822d319c28/
|
|
||||||
// src/cryptonote_basic/cryptonote_format_utils.cpp#L1062
|
|
||||||
// TODO: Should this return? Where does Monero set the trap handler for this exception?
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let (view_tag, shared_key, payment_id_xor) = shared_key(
|
|
||||||
if self.burning_bug.is_none() { Some(uniqueness(&tx.prefix.inputs)) } else { None },
|
|
||||||
self.pair.view.deref() * key,
|
|
||||||
o,
|
|
||||||
);
|
|
||||||
|
|
||||||
let payment_id =
|
|
||||||
if let Some(PaymentId::Encrypted(id)) = payment_id.map(|id| id ^ payment_id_xor) {
|
|
||||||
id
|
|
||||||
} else {
|
|
||||||
payment_id_xor
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(actual_view_tag) = output.view_tag {
|
|
||||||
if actual_view_tag != view_tag {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// P - shared == spend
|
|
||||||
let subaddress =
|
|
||||||
self.subaddresses.get(&(output_key - (&shared_key * ED25519_BASEPOINT_TABLE)).compress());
|
|
||||||
if subaddress.is_none() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let subaddress = *subaddress.unwrap();
|
|
||||||
|
|
||||||
// If it has torsion, it'll subtract the non-torsioned shared key to a torsioned key
|
|
||||||
// We will not have a torsioned key in our HashMap of keys, so we wouldn't identify it as
|
|
||||||
// ours
|
|
||||||
// If we did though, it'd enable bypassing the included burning bug protection
|
|
||||||
assert!(output_key.is_torsion_free());
|
|
||||||
|
|
||||||
let mut key_offset = shared_key;
|
|
||||||
if let Some(subaddress) = subaddress {
|
|
||||||
key_offset += self.pair.subaddress_derivation(subaddress);
|
|
||||||
}
|
|
||||||
// Since we've found an output to us, get its amount
|
|
||||||
let mut commitment = Commitment::zero();
|
|
||||||
|
|
||||||
// Miner transaction
|
|
||||||
if let Some(amount) = output.amount {
|
|
||||||
commitment.amount = amount;
|
|
||||||
// Regular transaction
|
|
||||||
} else {
|
|
||||||
let (mask, amount) = match tx.rct_signatures.base.encrypted_amounts.get(o) {
|
|
||||||
Some(amount) => amount_decryption(amount, shared_key),
|
|
||||||
// This should never happen, yet it may be possible with miner transactions?
|
|
||||||
// Using get just decreases the possibility of a panic and lets us move on in that case
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Rebuild the commitment to verify it
|
|
||||||
commitment = Commitment::new(mask, amount);
|
|
||||||
// If this is a malicious commitment, move to the next output
|
|
||||||
// Any other R value will calculate to a different spend key and are therefore ignorable
|
|
||||||
if Some(&commitment.calculate()) != tx.rct_signatures.base.commitments.get(o) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if commitment.amount != 0 {
|
|
||||||
res.push(ReceivedOutput {
|
|
||||||
absolute: AbsoluteId { tx: tx.hash(), o: o.try_into().unwrap() },
|
|
||||||
|
|
||||||
data: OutputData { key: output_key, key_offset, commitment },
|
|
||||||
|
|
||||||
metadata: Metadata { subaddress, payment_id, arbitrary_data: extra.data() },
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(burning_bug) = self.burning_bug.as_mut() {
|
|
||||||
burning_bug.insert(output.key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Break to prevent public keys from being included multiple times, triggering multiple
|
|
||||||
// inclusions of the same output
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Timelocked(tx.prefix.timelock, res)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Scan a block to obtain its spendable outputs. Its the presence in a block giving these
|
|
||||||
/// transactions their global index, and this must be batched as asking for the index of specific
|
|
||||||
/// transactions is a dead giveaway for which transactions you successfully scanned. This
|
|
||||||
/// function obtains the output indexes for the miner transaction, incrementing from there
|
|
||||||
/// instead.
|
|
||||||
pub async fn scan<RPC: RpcConnection>(
|
|
||||||
&mut self,
|
|
||||||
rpc: &Rpc<RPC>,
|
|
||||||
block: &Block,
|
|
||||||
) -> Result<Vec<Timelocked<SpendableOutput>>, RpcError> {
|
|
||||||
let mut index = rpc.get_o_indexes(block.miner_tx.hash()).await?[0];
|
|
||||||
let mut txs = vec![block.miner_tx.clone()];
|
|
||||||
txs.extend(rpc.get_transactions(&block.txs).await?);
|
|
||||||
|
|
||||||
let map = |mut timelock: Timelocked<ReceivedOutput>, index| {
|
|
||||||
if timelock.1.is_empty() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(Timelocked(
|
|
||||||
timelock.0,
|
|
||||||
timelock
|
|
||||||
.1
|
|
||||||
.drain(..)
|
|
||||||
.map(|output| SpendableOutput {
|
|
||||||
global_index: index + u64::from(output.absolute.o),
|
|
||||||
output,
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut res = vec![];
|
|
||||||
for tx in txs {
|
|
||||||
if let Some(timelock) = map(self.scan_transaction(&tx), index) {
|
|
||||||
res.push(timelock);
|
|
||||||
}
|
|
||||||
index += u64::try_from(
|
|
||||||
tx.prefix
|
|
||||||
.outputs
|
|
||||||
.iter()
|
|
||||||
// Filter to v2 miner TX outputs/RCT outputs since we're tracking the RCT output index
|
|
||||||
.filter(|output| {
|
|
||||||
let is_v2_miner_tx =
|
|
||||||
(tx.prefix.version == 2) && matches!(tx.prefix.inputs.first(), Some(Input::Gen(..)));
|
|
||||||
is_v2_miner_tx || output.amount.is_none()
|
|
||||||
})
|
|
||||||
.count(),
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,315 +0,0 @@
|
|||||||
use core::ops::Deref;
|
|
||||||
use std_shims::{
|
|
||||||
sync::OnceLock,
|
|
||||||
vec::Vec,
|
|
||||||
string::{String, ToString},
|
|
||||||
collections::HashMap,
|
|
||||||
};
|
|
||||||
|
|
||||||
use zeroize::{Zeroize, Zeroizing};
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
|
||||||
|
|
||||||
use curve25519_dalek::scalar::Scalar;
|
|
||||||
|
|
||||||
use crate::{random_scalar, wallet::seed::SeedError};
|
|
||||||
|
|
||||||
pub(crate) const CLASSIC_SEED_LENGTH: usize = 24;
|
|
||||||
pub(crate) const CLASSIC_SEED_LENGTH_WITH_CHECKSUM: usize = 25;
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
|
||||||
pub enum Language {
|
|
||||||
Chinese,
|
|
||||||
English,
|
|
||||||
Dutch,
|
|
||||||
French,
|
|
||||||
Spanish,
|
|
||||||
German,
|
|
||||||
Italian,
|
|
||||||
Portuguese,
|
|
||||||
Japanese,
|
|
||||||
Russian,
|
|
||||||
Esperanto,
|
|
||||||
Lojban,
|
|
||||||
EnglishOld,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn trim(word: &str, len: usize) -> Zeroizing<String> {
|
|
||||||
Zeroizing::new(word.chars().take(len).collect())
|
|
||||||
}
|
|
||||||
|
|
||||||
struct WordList {
|
|
||||||
word_list: Vec<&'static str>,
|
|
||||||
word_map: HashMap<&'static str, usize>,
|
|
||||||
trimmed_word_map: HashMap<String, usize>,
|
|
||||||
unique_prefix_length: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WordList {
|
|
||||||
fn new(word_list: Vec<&'static str>, prefix_length: usize) -> WordList {
|
|
||||||
let mut lang = WordList {
|
|
||||||
word_list,
|
|
||||||
word_map: HashMap::new(),
|
|
||||||
trimmed_word_map: HashMap::new(),
|
|
||||||
unique_prefix_length: prefix_length,
|
|
||||||
};
|
|
||||||
|
|
||||||
for (i, word) in lang.word_list.iter().enumerate() {
|
|
||||||
lang.word_map.insert(word, i);
|
|
||||||
lang.trimmed_word_map.insert(trim(word, lang.unique_prefix_length).deref().clone(), i);
|
|
||||||
}
|
|
||||||
|
|
||||||
lang
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static LANGUAGES_CELL: OnceLock<HashMap<Language, WordList>> = OnceLock::new();
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
fn LANGUAGES() -> &'static HashMap<Language, WordList> {
|
|
||||||
LANGUAGES_CELL.get_or_init(|| {
|
|
||||||
HashMap::from([
|
|
||||||
(Language::Chinese, WordList::new(include!("./classic/zh.rs"), 1)),
|
|
||||||
(Language::English, WordList::new(include!("./classic/en.rs"), 3)),
|
|
||||||
(Language::Dutch, WordList::new(include!("./classic/nl.rs"), 4)),
|
|
||||||
(Language::French, WordList::new(include!("./classic/fr.rs"), 4)),
|
|
||||||
(Language::Spanish, WordList::new(include!("./classic/es.rs"), 4)),
|
|
||||||
(Language::German, WordList::new(include!("./classic/de.rs"), 4)),
|
|
||||||
(Language::Italian, WordList::new(include!("./classic/it.rs"), 4)),
|
|
||||||
(Language::Portuguese, WordList::new(include!("./classic/pt.rs"), 4)),
|
|
||||||
(Language::Japanese, WordList::new(include!("./classic/ja.rs"), 3)),
|
|
||||||
(Language::Russian, WordList::new(include!("./classic/ru.rs"), 4)),
|
|
||||||
(Language::Esperanto, WordList::new(include!("./classic/eo.rs"), 4)),
|
|
||||||
(Language::Lojban, WordList::new(include!("./classic/jbo.rs"), 4)),
|
|
||||||
(Language::EnglishOld, WordList::new(include!("./classic/ang.rs"), 4)),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub(crate) fn trim_by_lang(word: &str, lang: Language) -> String {
|
|
||||||
if lang != Language::EnglishOld {
|
|
||||||
word.chars().take(LANGUAGES()[&lang].unique_prefix_length).collect()
|
|
||||||
} else {
|
|
||||||
word.to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn checksum_index(words: &[Zeroizing<String>], lang: &WordList) -> usize {
|
|
||||||
let mut trimmed_words = Zeroizing::new(String::new());
|
|
||||||
for w in words {
|
|
||||||
*trimmed_words += &trim(w, lang.unique_prefix_length);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fn crc32_table() -> [u32; 256] {
|
|
||||||
let poly = 0xedb88320u32;
|
|
||||||
|
|
||||||
let mut res = [0; 256];
|
|
||||||
let mut i = 0;
|
|
||||||
while i < 256 {
|
|
||||||
let mut entry = i;
|
|
||||||
let mut b = 0;
|
|
||||||
while b < 8 {
|
|
||||||
let trigger = entry & 1;
|
|
||||||
entry >>= 1;
|
|
||||||
if trigger == 1 {
|
|
||||||
entry ^= poly;
|
|
||||||
}
|
|
||||||
b += 1;
|
|
||||||
}
|
|
||||||
res[i as usize] = entry;
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
res
|
|
||||||
}
|
|
||||||
const CRC32_TABLE: [u32; 256] = crc32_table();
|
|
||||||
|
|
||||||
let trimmed_words = trimmed_words.as_bytes();
|
|
||||||
let mut checksum = u32::MAX;
|
|
||||||
for i in 0 .. trimmed_words.len() {
|
|
||||||
checksum = CRC32_TABLE[usize::from(u8::try_from(checksum % 256).unwrap() ^ trimmed_words[i])] ^
|
|
||||||
(checksum >> 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
usize::try_from(!checksum).unwrap() % words.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert a private key to a seed
|
|
||||||
#[allow(clippy::needless_pass_by_value)]
|
|
||||||
fn key_to_seed(lang: Language, key: Zeroizing<Scalar>) -> ClassicSeed {
|
|
||||||
let bytes = Zeroizing::new(key.to_bytes());
|
|
||||||
|
|
||||||
// get the language words
|
|
||||||
let words = &LANGUAGES()[&lang].word_list;
|
|
||||||
let list_len = u64::try_from(words.len()).unwrap();
|
|
||||||
|
|
||||||
// To store the found words & add the checksum word later.
|
|
||||||
let mut seed = Vec::with_capacity(25);
|
|
||||||
|
|
||||||
// convert to words
|
|
||||||
// 4 bytes -> 3 words. 8 digits base 16 -> 3 digits base 1626
|
|
||||||
let mut segment = [0; 4];
|
|
||||||
let mut indices = [0; 4];
|
|
||||||
for i in 0 .. 8 {
|
|
||||||
// convert first 4 byte to u32 & get the word indices
|
|
||||||
let start = i * 4;
|
|
||||||
// convert 4 byte to u32
|
|
||||||
segment.copy_from_slice(&bytes[start .. (start + 4)]);
|
|
||||||
// Actually convert to a u64 so we can add without overflowing
|
|
||||||
indices[0] = u64::from(u32::from_le_bytes(segment));
|
|
||||||
indices[1] = indices[0];
|
|
||||||
indices[0] /= list_len;
|
|
||||||
indices[2] = indices[0] + indices[1];
|
|
||||||
indices[0] /= list_len;
|
|
||||||
indices[3] = indices[0] + indices[2];
|
|
||||||
|
|
||||||
// append words to seed
|
|
||||||
for i in indices.iter().skip(1) {
|
|
||||||
let word = usize::try_from(i % list_len).unwrap();
|
|
||||||
seed.push(Zeroizing::new(words[word].to_string()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
segment.zeroize();
|
|
||||||
indices.zeroize();
|
|
||||||
|
|
||||||
// create a checksum word for all languages except old english
|
|
||||||
if lang != Language::EnglishOld {
|
|
||||||
let checksum = seed[checksum_index(&seed, &LANGUAGES()[&lang])].clone();
|
|
||||||
seed.push(checksum);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut res = Zeroizing::new(String::new());
|
|
||||||
for (i, word) in seed.iter().enumerate() {
|
|
||||||
if i != 0 {
|
|
||||||
*res += " ";
|
|
||||||
}
|
|
||||||
*res += word;
|
|
||||||
}
|
|
||||||
ClassicSeed(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert a seed to bytes
|
|
||||||
pub(crate) fn seed_to_bytes(words: &str) -> Result<(Language, Zeroizing<[u8; 32]>), SeedError> {
|
|
||||||
// get seed words
|
|
||||||
let words = words.split_whitespace().map(|w| Zeroizing::new(w.to_string())).collect::<Vec<_>>();
|
|
||||||
if (words.len() != CLASSIC_SEED_LENGTH) && (words.len() != CLASSIC_SEED_LENGTH_WITH_CHECKSUM) {
|
|
||||||
panic!("invalid seed passed to seed_to_bytes");
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the language
|
|
||||||
let (matched_indices, lang_name, lang) = (|| {
|
|
||||||
let has_checksum = words.len() == CLASSIC_SEED_LENGTH_WITH_CHECKSUM;
|
|
||||||
let mut matched_indices = Zeroizing::new(vec![]);
|
|
||||||
|
|
||||||
// Iterate through all the languages
|
|
||||||
'language: for (lang_name, lang) in LANGUAGES() {
|
|
||||||
matched_indices.zeroize();
|
|
||||||
matched_indices.clear();
|
|
||||||
|
|
||||||
// Iterate through all the words and see if they're all present
|
|
||||||
for word in &words {
|
|
||||||
let trimmed = trim(word, lang.unique_prefix_length);
|
|
||||||
let word = if has_checksum { &trimmed } else { word };
|
|
||||||
|
|
||||||
if let Some(index) = if has_checksum {
|
|
||||||
lang.trimmed_word_map.get(word.deref())
|
|
||||||
} else {
|
|
||||||
lang.word_map.get(&word.as_str())
|
|
||||||
} {
|
|
||||||
matched_indices.push(*index);
|
|
||||||
} else {
|
|
||||||
continue 'language;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if has_checksum {
|
|
||||||
if lang_name == &Language::EnglishOld {
|
|
||||||
Err(SeedError::EnglishOldWithChecksum)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// exclude the last word when calculating a checksum.
|
|
||||||
let last_word = words.last().unwrap().clone();
|
|
||||||
let checksum = words[checksum_index(&words[.. words.len() - 1], lang)].clone();
|
|
||||||
|
|
||||||
// check the trimmed checksum and trimmed last word line up
|
|
||||||
if trim(&checksum, lang.unique_prefix_length) != trim(&last_word, lang.unique_prefix_length)
|
|
||||||
{
|
|
||||||
Err(SeedError::InvalidChecksum)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok((matched_indices, lang_name, lang));
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(SeedError::UnknownLanguage)?
|
|
||||||
})()?;
|
|
||||||
|
|
||||||
// convert to bytes
|
|
||||||
let mut res = Zeroizing::new([0; 32]);
|
|
||||||
let mut indices = Zeroizing::new([0; 4]);
|
|
||||||
for i in 0 .. 8 {
|
|
||||||
// read 3 indices at a time
|
|
||||||
let i3 = i * 3;
|
|
||||||
indices[1] = matched_indices[i3];
|
|
||||||
indices[2] = matched_indices[i3 + 1];
|
|
||||||
indices[3] = matched_indices[i3 + 2];
|
|
||||||
|
|
||||||
let inner = |i| {
|
|
||||||
let mut base = (lang.word_list.len() - indices[i] + indices[i + 1]) % lang.word_list.len();
|
|
||||||
// Shift the index over
|
|
||||||
for _ in 0 .. i {
|
|
||||||
base *= lang.word_list.len();
|
|
||||||
}
|
|
||||||
base
|
|
||||||
};
|
|
||||||
// set the last index
|
|
||||||
indices[0] = indices[1] + inner(1) + inner(2);
|
|
||||||
if (indices[0] % lang.word_list.len()) != indices[1] {
|
|
||||||
Err(SeedError::InvalidSeed)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let pos = i * 4;
|
|
||||||
let mut bytes = u32::try_from(indices[0]).unwrap().to_le_bytes();
|
|
||||||
res[pos .. (pos + 4)].copy_from_slice(&bytes);
|
|
||||||
bytes.zeroize();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((*lang_name, res))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Zeroize)]
|
|
||||||
pub struct ClassicSeed(Zeroizing<String>);
|
|
||||||
impl ClassicSeed {
|
|
||||||
pub(crate) fn new<R: RngCore + CryptoRng>(rng: &mut R, lang: Language) -> ClassicSeed {
|
|
||||||
key_to_seed(lang, Zeroizing::new(random_scalar(rng)))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::needless_pass_by_value)]
|
|
||||||
pub fn from_string(words: Zeroizing<String>) -> Result<ClassicSeed, SeedError> {
|
|
||||||
let (lang, entropy) = seed_to_bytes(&words)?;
|
|
||||||
|
|
||||||
// Make sure this is a valid scalar
|
|
||||||
let scalar = Scalar::from_canonical_bytes(*entropy);
|
|
||||||
if scalar.is_none().into() {
|
|
||||||
Err(SeedError::InvalidSeed)?;
|
|
||||||
}
|
|
||||||
let mut scalar = scalar.unwrap();
|
|
||||||
scalar.zeroize();
|
|
||||||
|
|
||||||
// Call from_entropy so a trimmed seed becomes a full seed
|
|
||||||
Ok(Self::from_entropy(lang, entropy).unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::needless_pass_by_value)]
|
|
||||||
pub fn from_entropy(lang: Language, entropy: Zeroizing<[u8; 32]>) -> Option<ClassicSeed> {
|
|
||||||
Option::from(Scalar::from_canonical_bytes(*entropy))
|
|
||||||
.map(|scalar| key_to_seed(lang, Zeroizing::new(scalar)))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn to_string(&self) -> Zeroizing<String> {
|
|
||||||
self.0.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn entropy(&self) -> Zeroizing<[u8; 32]> {
|
|
||||||
seed_to_bytes(&self.0).unwrap().1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user