17
17
pragma solidity 0.5.7 ;
18
18
19
19
import "../../lib/MathUint.sol " ;
20
-
21
- import "../../thirdparty/MiMC.sol " ;
20
+ import "../../lib/Poseidon.sol " ;
22
21
23
22
24
23
/// @title ExchangeBalances.
@@ -37,8 +36,8 @@ library ExchangeBalances
37
36
uint32 nonce ,
38
37
uint96 balance ,
39
38
uint256 tradeHistoryRoot ,
40
- uint256 [20 ] memory accountMerkleProof ,
41
- uint256 [8 ] memory balanceMerkleProof
39
+ uint256 [30 ] memory accountMerkleProof ,
40
+ uint256 [12 ] memory balanceMerkleProof
42
41
)
43
42
public
44
43
pure
@@ -67,8 +66,8 @@ library ExchangeBalances
67
66
uint32 nonce ,
68
67
uint96 balance ,
69
68
uint256 tradeHistoryRoot ,
70
- uint256 [20 ] memory accountMerkleProof ,
71
- uint256 [8 ] memory balanceMerkleProof
69
+ uint256 [30 ] memory accountMerkleProof ,
70
+ uint256 [12 ] memory balanceMerkleProof
72
71
)
73
72
public
74
73
pure
@@ -96,29 +95,45 @@ library ExchangeBalances
96
95
uint16 tokenID ,
97
96
uint balance ,
98
97
uint tradeHistoryRoot ,
99
- uint256 [8 ] memory balanceMerkleProof
98
+ uint256 [12 ] memory balanceMerkleProof
100
99
)
101
100
internal
102
101
pure
103
102
returns (uint256 )
104
103
{
105
- uint256 [29 ] memory salts;
106
- getMerkleTreeHashingSalts (salts);
107
-
108
- uint256 [] memory balanceLeafElements = new uint256 [](2 );
109
- balanceLeafElements[0 ] = balance;
110
- balanceLeafElements[1 ] = tradeHistoryRoot;
111
- uint256 balanceItem = MiMC.Hash (balanceLeafElements, 1 );
112
-
113
- // Calculate merkle root of balances tree
104
+ uint256 balanceItem = hashImpl (balance, tradeHistoryRoot, 0 , 0 );
114
105
uint _id = tokenID;
115
- for (uint depth = 0 ; depth < 8 ; depth++ ) {
116
- if (_id & 1 == 1 ) {
117
- balanceItem = hashImpl (balanceMerkleProof[depth], balanceItem, salts[depth]);
118
- } else {
119
- balanceItem = hashImpl (balanceItem, balanceMerkleProof[depth], salts[depth]);
106
+ for (uint depth = 0 ; depth < 4 ; depth++ ) {
107
+ if (_id & 3 == 0 ) {
108
+ balanceItem = hashImpl (
109
+ balanceItem,
110
+ balanceMerkleProof[depth * 3 ],
111
+ balanceMerkleProof[depth * 3 + 1 ],
112
+ balanceMerkleProof[depth * 3 + 2 ]
113
+ );
114
+ } else if (_id & 3 == 1 ) {
115
+ balanceItem = hashImpl (
116
+ balanceMerkleProof[depth * 3 ],
117
+ balanceItem,
118
+ balanceMerkleProof[depth * 3 + 1 ],
119
+ balanceMerkleProof[depth * 3 + 2 ]
120
+ );
121
+ } else if (_id & 3 == 2 ) {
122
+ balanceItem = hashImpl (
123
+ balanceMerkleProof[depth * 3 ],
124
+ balanceMerkleProof[depth * 3 + 1 ],
125
+ balanceItem,
126
+ balanceMerkleProof[depth * 3 + 2 ]
127
+ );
128
+ } else if (_id & 3 == 3 ) {
129
+ balanceItem = hashImpl (
130
+ balanceMerkleProof[depth * 3 ],
131
+ balanceMerkleProof[depth * 3 + 1 ],
132
+ balanceMerkleProof[depth * 3 + 2 ],
133
+ balanceItem
134
+ );
120
135
}
121
- _id = _id / 2 ;
136
+ _id = _id >> 2 ;
122
137
}
123
138
return balanceItem;
124
139
}
@@ -129,89 +144,59 @@ library ExchangeBalances
129
144
uint256 pubKeyY ,
130
145
uint256 nonce ,
131
146
uint256 balancesRoot ,
132
- uint256 [20 ] memory accountMerkleProof
147
+ uint256 [30 ] memory accountMerkleProof
133
148
)
134
149
internal
135
150
pure
136
151
returns (uint256 )
137
152
{
138
- uint256 [29 ] memory salts;
139
- getMerkleTreeHashingSalts (salts);
140
-
141
- uint256 [] memory accountLeafElements = new uint256 [](4 );
142
- accountLeafElements[0 ] = pubKeyX;
143
- accountLeafElements[1 ] = pubKeyY;
144
- accountLeafElements[2 ] = nonce;
145
- accountLeafElements[3 ] = balancesRoot;
146
- uint256 accountItem = MiMC.Hash (accountLeafElements, 1 );
147
-
153
+ uint256 accountItem = hashImpl (pubKeyX, pubKeyY, nonce, balancesRoot);
148
154
uint _id = accountID;
149
- for (uint depth = 0 ; depth < 20 ; depth++ ) {
150
- if (_id & 1 == 1 ) {
151
- accountItem = hashImpl (accountMerkleProof[depth], accountItem, salts[depth]);
152
- } else {
153
- accountItem = hashImpl (accountItem, accountMerkleProof[depth], salts[depth]);
155
+ for (uint depth = 0 ; depth < 10 ; depth++ ) {
156
+ if (_id & 3 == 0 ) {
157
+ accountItem = hashImpl (
158
+ accountItem,
159
+ accountMerkleProof[depth * 3 ],
160
+ accountMerkleProof[depth * 3 + 1 ],
161
+ accountMerkleProof[depth * 3 + 2 ]
162
+ );
163
+ } else if (_id & 3 == 1 ) {
164
+ accountItem = hashImpl (
165
+ accountMerkleProof[depth * 3 ],
166
+ accountItem,
167
+ accountMerkleProof[depth * 3 + 1 ],
168
+ accountMerkleProof[depth * 3 + 2 ]
169
+ );
170
+ } else if (_id & 3 == 2 ) {
171
+ accountItem = hashImpl (
172
+ accountMerkleProof[depth * 3 ],
173
+ accountMerkleProof[depth * 3 + 1 ],
174
+ accountItem,
175
+ accountMerkleProof[depth * 3 + 2 ]
176
+ );
177
+ } else if (_id & 3 == 3 ) {
178
+ accountItem = hashImpl (
179
+ accountMerkleProof[depth * 3 ],
180
+ accountMerkleProof[depth * 3 + 1 ],
181
+ accountMerkleProof[depth * 3 + 2 ],
182
+ accountItem
183
+ );
154
184
}
155
- _id = _id / 2 ;
185
+ _id = _id >> 2 ;
156
186
}
157
187
return accountItem;
158
188
}
159
189
160
- function hashImpl (
161
- uint256 left ,
162
- uint256 right ,
163
- uint256 IV
190
+ function hashImpl (
191
+ uint256 t0 ,
192
+ uint256 t1 ,
193
+ uint256 t2 ,
194
+ uint256 t3
164
195
)
165
196
internal
166
197
pure
167
198
returns (uint256 )
168
199
{
169
- uint256 [] memory x = new uint256 [](2 );
170
- x[0 ] = left;
171
- x[1 ] = right;
172
-
173
- return MiMC.Hash (x, IV);
174
- }
175
-
176
- function getMerkleTreeHashingSalts (
177
- uint256 [29 ] memory salts
178
- )
179
- public
180
- pure
181
- {
182
- // Actually only up to 20 items in the list will be used in 3.0. We keep more in case
183
- // we will change the depth of the Merkle tree in the future.
184
-
185
- // When we calculate the tree hashes, the leaf nodes will use salts[0] as the hashing
186
- // salt, the parent nodes of the leafs will use salts[1], ...
187
- salts[0 ] = 149674538925118052205057075966660054952481571156186698930522557832224430770 ;
188
- salts[1 ] = 9670701465464311903249220692483401938888498641874948577387207195814981706974 ;
189
- salts[2 ] = 18318710344500308168304415114839554107298291987930233567781901093928276468271 ;
190
- salts[3 ] = 6597209388525824933845812104623007130464197923269180086306970975123437805179 ;
191
- salts[4 ] = 21720956803147356712695575768577036859892220417043839172295094119877855004262 ;
192
- salts[5 ] = 10330261616520855230513677034606076056972336573153777401182178891807369896722 ;
193
- salts[6 ] = 17466547730316258748333298168566143799241073466140136663575045164199607937939 ;
194
- salts[7 ] = 18881017304615283094648494495339883533502299318365959655029893746755475886610 ;
195
- salts[8 ] = 21580915712563378725413940003372103925756594604076607277692074507345076595494 ;
196
- salts[9 ] = 12316305934357579015754723412431647910012873427291630993042374701002287130550 ;
197
- salts[10 ] = 18905410889238873726515380969411495891004493295170115920825550288019118582494 ;
198
- salts[11 ] = 12819107342879320352602391015489840916114959026915005817918724958237245903353 ;
199
- salts[12 ] = 8245796392944118634696709403074300923517437202166861682117022548371601758802 ;
200
- salts[13 ] = 16953062784314687781686527153155644849196472783922227794465158787843281909585 ;
201
- salts[14 ] = 19346880451250915556764413197424554385509847473349107460608536657852472800734 ;
202
- salts[15 ] = 14486794857958402714787584825989957493343996287314210390323617462452254101347 ;
203
- salts[16 ] = 11127491343750635061768291849689189917973916562037173191089384809465548650641 ;
204
- salts[17 ] = 12217916643258751952878742936579902345100885664187835381214622522318889050675 ;
205
- salts[18 ] = 722025110834410790007814375535296040832778338853544117497481480537806506496 ;
206
- salts[19 ] = 15115624438829798766134408951193645901537753720219896384705782209102859383951 ;
207
- salts[20 ] = 11495230981884427516908372448237146604382590904456048258839160861769955046544 ;
208
- salts[21 ] = 16867999085723044773810250829569850875786210932876177117428755424200948460050 ;
209
- salts[22 ] = 1884116508014449609846749684134533293456072152192763829918284704109129550542 ;
210
- salts[23 ] = 14643335163846663204197941112945447472862168442334003800621296569318670799451 ;
211
- salts[24 ] = 1933387276732345916104540506251808516402995586485132246682941535467305930334 ;
212
- salts[25 ] = 7286414555941977227951257572976885370489143210539802284740420664558593616067 ;
213
- salts[26 ] = 16932161189449419608528042274282099409408565503929504242784173714823499212410 ;
214
- salts[27 ] = 16562533130736679030886586765487416082772837813468081467237161865787494093536 ;
215
- salts[28 ] = 6037428193077828806710267464232314380014232668931818917272972397574634037180 ;
200
+ return Poseidon.hash_t5f6p52 (t0, t1, t2, t3, 0 );
216
201
}
217
- }
202
+ }
0 commit comments