-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.sol
65 lines (56 loc) · 2.83 KB
/
x.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
contract Hasher {
uint256 p = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256[20] c = [
0,
25823191961023811529686723375255045606187170120624741056268890390838310270028,
71153255768872006974285801937521995907343848376936063113800887806988124358800,
51253176922899201987938365653129780755804051536550826601168630951148399005246,
66651710483985382365580181188706173532487386392003341306307921015066514594406,
45887003413921204775397977044284378920236104620216194900669591190628189327887,
14399999722617037892747232478295923748665564430258345135947757381904956977453,
29376176727758177809204424209125257629638239807319618360680345079470240949145,
13768859312518298840937540532277016512087005174650120937309279832230513110846,
54749662990362840569021981534456448557155682756506853240029023635346061661615,
25161436470718351277017231215227846535148280460947816286575563945185127975034,
90370030464179443930112165274275271350651484239155016554738639197417116558730,
92014788260850167582827910417652439562305280453223492851660096740204889381255,
40376490640073034398204558905403523738912091909516510156577526370637723469243,
903792244391531377123276432892896247924738784402045372115602887103675299839,
112203415202699791888928570309186854585561656615192232544262649073999791317171,
114801681136748880679062548782792743842998635558909635247841799223004802934045,
111440818948676816539978930514468038603327388809824089593328295503672011604028,
64965960071752809090438003157362764845283225351402746675238539375404528707397,
98428510787134995495896453413714864789970336245473413374424598985988309743097
];
function MiMC5Feistel(uint256 _iL, uint256 _iR, uint256 _k) internal view returns(uint256 oL, uint256 oR) {
uint8 nRounds = 20;
uint256 lastL = _iL;
uint256 lastR = _iR;
uint256 mask;
uint256 mask2;
uint256 mask4;
uint256 temp;
for(uint8 i = 0; i < nRounds; i++){
mask = addmod(lastR, _k, p);
mask = addmod(mask, c[i], p);
mask2 = mulmod(mask, mask, p);
mask4 = mulmod(mask2, mask2, p);
mask = mulmod(mask4, mask, p);
temp = lastR;
lastR = addmod(lastL, mask, p);
lastL = temp;
}
return (lastL, lastR);
}
function MiMC5Sponge(uint256[2] memory _ins, uint256 _k) external view returns(uint256 h) {
uint256 lastR = 0;
uint256 lastC = 0;
for(uint8 i = 0; i < _ins.length; i++){
lastR = addmod(lastR, _ins[i], p);
(lastR, lastC) = MiMC5Feistel(lastR, lastC, _k);
}
h = lastR;
}
}