Skip to content

Commit 18d853d

Browse files
authored
More testing (#16)
* Added more tests/comments * Added some extra tests with partial filling of orders
1 parent b38d64c commit 18d853d

16 files changed

+1143
-129
lines changed

Circuits/DepositCircuit.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class DepositGadget : public GadgetT
5757
publicKeyY(pb, 256, FMT(prefix, ".publicKeyY")),
5858

5959
// Calculate the new balance
60+
// We can't let the deposit fail (it's onchain so it needs to be included),
61+
// and we do want to cap the balance to NUM_BITS_AMOUNT bits max, so cap the balance even
62+
// if it means that the user loses some tokens (NUM_BITS_AMOUNT bits should be more than enough).
6063
uncappedBalanceAfter(pb, balanceBefore.balance, amount.packed, FMT(prefix, ".uncappedBalanceAfter")),
6164
balanceAfter(pb, uncappedBalanceAfter.result(), constants.maxAmount, NUM_BITS_AMOUNT + 1, FMT(prefix, ".balanceAfter")),
6265

@@ -121,7 +124,7 @@ class DepositGadget : public GadgetT
121124
amount.bits};
122125
}
123126

124-
const VariableT getNewAccountsRoot() const
127+
const VariableT& getNewAccountsRoot() const
125128
{
126129
return updateAccount.result();
127130
}
@@ -210,7 +213,7 @@ class DepositCircuit : public GadgetT
210213
publicData.generate_r1cs_constraints();
211214

212215
// Check the new merkle root
213-
forceEqual(pb, deposits.back().getNewAccountsRoot(), merkleRootAfter.packed, "newMerkleRoot");
216+
requireEqual(pb, deposits.back().getNewAccountsRoot(), merkleRootAfter.packed, "newMerkleRoot");
214217
}
215218

216219
bool generateWitness(const DepositBlock& block)

Circuits/OffchainWithdrawalCircuit.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class OffchainWithdrawalGadget : public GadgetT
8080
balanceBefore(pb, FMT(prefix, ".balanceBefore")),
8181
accountBefore(pb, FMT(prefix, ".accountBefore")),
8282
// Operator state
83-
balanceBefore_O(pb, FMT(prefix, ".accountBefore_O")),
83+
balanceBefore_O(pb, FMT(prefix, ".balanceBefore_O")),
8484

8585
// Inputs
8686
accountID(pb, NUM_BITS_ACCOUNT, FMT(prefix, ".accountID")),
@@ -246,12 +246,12 @@ class OffchainWithdrawalGadget : public GadgetT
246246
fFee.bits()};
247247
}
248248

249-
const VariableT getNewAccountsRoot() const
249+
const VariableT& getNewAccountsRoot() const
250250
{
251251
return updateAccount_A.result();
252252
}
253253

254-
const VariableT getNewOperatorBalancesRoot() const
254+
const VariableT& getNewOperatorBalancesRoot() const
255255
{
256256
return updateBalanceF_O.result();
257257
}
@@ -378,7 +378,7 @@ class OffchainWithdrawalCircuit : public GadgetT
378378
publicData.generate_r1cs_constraints();
379379

380380
// Check the new merkle root
381-
forceEqual(pb, updateAccount_O->result(), merkleRootAfter.packed, "newMerkleRoot");
381+
requireEqual(pb, updateAccount_O->result(), merkleRootAfter.packed, "newMerkleRoot");
382382
}
383383

384384
bool generateWitness(const OffchainWithdrawalBlock& block)

Circuits/OnchainWithdrawalCircuit.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class OnchainWithdrawalGadget : public GadgetT
3131
// Calculate how much can be withdrawn
3232
MinGadget amountToWithdrawMin;
3333
TernaryGadget amountToWithdraw;
34+
// Float
3435
FloatGadget amountWithdrawn;
3536
RequireAccuracyGadget requireAccuracyAmountWithdrawn;
3637

@@ -67,17 +68,19 @@ class OnchainWithdrawalGadget : public GadgetT
6768
amountRequested(pb, NUM_BITS_AMOUNT, FMT(prefix, ".amountRequested")),
6869

6970
// Calculate how much can be withdrawn
71+
// In shutdown mode always withdraw the complete balance
7072
amountToWithdrawMin(pb, amountRequested.packed, balanceBefore.balance, NUM_BITS_AMOUNT, FMT(prefix, ".min(amountRequested, balance)")),
7173
amountToWithdraw(pb, bShutdownMode, balanceBefore.balance, amountToWithdrawMin.result(), FMT(prefix, ".amountToWithdraw")),
74+
// Float
7275
amountWithdrawn(pb, constants, Float28Encoding, FMT(prefix, ".amountWithdrawn")),
7376
requireAccuracyAmountWithdrawn(pb, amountWithdrawn.value(), amountToWithdraw.result(), Float28Accuracy, NUM_BITS_AMOUNT, FMT(prefix, ".requireAccuracyAmountRequested")),
7477

75-
// Shutdown mode
78+
// Shutdown mode - Reset values to genesis state
7679
amountToSubtract(pb, bShutdownMode, amountToWithdraw.result(), amountWithdrawn.value(), FMT(prefix, ".amountToSubtract")),
7780
tradingHistoryAfter(pb, bShutdownMode, constants.emptyTradeHistory, balanceBefore.tradingHistory, FMT(prefix, ".tradingHistoryAfter")),
7881
publicKeyXAfter(pb, bShutdownMode, constants.zero, accountBefore.publicKey.x, FMT(prefix, ".publicKeyXAfter")),
7982
publicKeyYAfter(pb, bShutdownMode, constants.zero, accountBefore.publicKey.y, FMT(prefix, ".publicKeyYAfter")),
80-
nonceAfter(pb, bShutdownMode, constants.zero, accountBefore.nonce, FMT(prefix, ".tradingHistoryAfter")),
83+
nonceAfter(pb, bShutdownMode, constants.zero, accountBefore.nonce, FMT(prefix, ".nonceAfter")),
8184

8285
// Calculate the new balance
8386
balance_after(pb, balanceBefore.balance, amountToSubtract.result(), FMT(prefix, ".balance_after")),
@@ -109,6 +112,7 @@ class OnchainWithdrawalGadget : public GadgetT
109112
// Withdrawal calculations
110113
amountToWithdrawMin.generate_r1cs_witness();
111114
amountToWithdraw.generate_r1cs_witness();
115+
// Float
112116
amountWithdrawn.generate_r1cs_witness(toFloat(pb.val(amountToWithdraw.result()), Float28Encoding));
113117
requireAccuracyAmountWithdrawn.generate_r1cs_witness();
114118

@@ -137,6 +141,7 @@ class OnchainWithdrawalGadget : public GadgetT
137141
// Withdrawal calculations
138142
amountToWithdrawMin.generate_r1cs_constraints();
139143
amountToWithdraw.generate_r1cs_constraints();
144+
// Float
140145
amountWithdrawn.generate_r1cs_constraints();
141146
requireAccuracyAmountWithdrawn.generate_r1cs_constraints();
142147

@@ -194,7 +199,6 @@ class OnchainWithdrawalCircuit : public GadgetT
194199
EqualGadget bShutdownMode;
195200

196201
// Withdrawals
197-
bool onchainDataAvailability;
198202
unsigned int numWithdrawals;
199203
std::vector<OnchainWithdrawalGadget> withdrawals;
200204
std::vector<sha256_many> hashers;
@@ -219,9 +223,8 @@ class OnchainWithdrawalCircuit : public GadgetT
219223

220224
}
221225

222-
void generate_r1cs_constraints(bool onchainDataAvailability, int numWithdrawals)
226+
void generate_r1cs_constraints(int numWithdrawals)
223227
{
224-
this->onchainDataAvailability = onchainDataAvailability;
225228
this->numWithdrawals = numWithdrawals;
226229

227230
constants.generate_r1cs_constraints();
@@ -275,7 +278,7 @@ class OnchainWithdrawalCircuit : public GadgetT
275278
publicData.generate_r1cs_constraints();
276279

277280
// Check the new merkle root
278-
forceEqual(pb, withdrawals.back().getNewAccountsRoot(), merkleRootAfter.packed, "newMerkleRoot");
281+
requireEqual(pb, withdrawals.back().getNewAccountsRoot(), merkleRootAfter.packed, "newMerkleRoot");
279282
}
280283

281284
bool generateWitness(const OnchainWithdrawalBlock& block)

Circuits/OrderCancellationCircuit.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ class OrderCancellationGadget : public GadgetT
7979

8080
// User state
8181
tradeHistoryBefore(pb, FMT(prefix, ".tradeHistoryBefore")),
82-
balanceTBefore(pb, FMT(prefix, ".balanceFBefore")),
83-
balanceFBefore(pb, FMT(prefix, ".balanceBefore")),
82+
balanceTBefore(pb, FMT(prefix, ".balanceTBefore")),
83+
balanceFBefore(pb, FMT(prefix, ".balanceFBefore")),
8484
accountBefore(pb, FMT(prefix, ".accountBefore")),
8585
// Operator state
86-
balanceBefore_O(pb, FMT(prefix, ".accountBefore_O")),
86+
balanceBefore_O(pb, FMT(prefix, ".balanceBefore_O")),
8787

8888
// Inputs
8989
accountID(pb, NUM_BITS_ACCOUNT, FMT(prefix, ".accountID")),
@@ -249,12 +249,12 @@ class OrderCancellationGadget : public GadgetT
249249
fFee.bits()};
250250
}
251251

252-
const VariableT getNewAccountsRoot() const
252+
const VariableT& getNewAccountsRoot() const
253253
{
254254
return updateAccount_A.result();
255255
}
256256

257-
const VariableT getNewOperatorBalancesRoot() const
257+
const VariableT& getNewOperatorBalancesRoot() const
258258
{
259259
return updateBalanceF_O.result();
260260
}
@@ -375,7 +375,7 @@ class OrderCancellationCircuit : public GadgetT
375375
publicData.generate_r1cs_constraints();
376376

377377
// Check the new merkle root
378-
forceEqual(pb, updateAccount_O->result(), merkleRootAfter.packed, "newMerkleRoot");
378+
requireEqual(pb, updateAccount_O->result(), merkleRootAfter.packed, "newMerkleRoot");
379379
}
380380

381381
bool generateWitness(const Loopring::OrderCancellationBlock& block)

Circuits/RingSettlementCircuit.h

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,14 @@ class RingSettlementGadget : public GadgetT
159159
TransferGadget protocolFeeA_from_balanceAO_to_balanceAP;
160160
TransferGadget protocolFeeB_from_balanceBO_to_balanceBP;
161161

162-
// Update trading history
163-
UpdateTradeHistoryGadget updateTradeHistoryA;
164-
UpdateTradeHistoryGadget updateTradeHistoryB;
165-
166162
// Update UserA
163+
UpdateTradeHistoryGadget updateTradeHistory_A;
167164
UpdateBalanceGadget updateBalanceS_A;
168165
UpdateBalanceGadget updateBalanceB_A;
169166
UpdateAccountGadget updateAccount_A;
170167

171168
// Update UserB
169+
UpdateTradeHistoryGadget updateTradeHistory_B;
172170
UpdateBalanceGadget updateBalanceS_B;
173171
UpdateBalanceGadget updateBalanceB_B;
174172
UpdateAccountGadget updateAccount_B;
@@ -209,7 +207,7 @@ class RingSettlementGadget : public GadgetT
209207
balanceA_P(pb, FMT(prefix, ".balanceA_P")),
210208
balanceB_P(pb, FMT(prefix, ".balanceB_P")),
211209
balanceA_O(pb, FMT(prefix, ".balanceA_O")),
212-
balanceB_O(pb, FMT(prefix, ".balanceA_O")),
210+
balanceB_O(pb, FMT(prefix, ".balanceB_O")),
213211
// Initial trading history roots
214212
tradingHistoryRootA_O(make_variable(pb, FMT(prefix, ".tradingHistoryRootA_O"))),
215213
tradingHistoryRootB_O(make_variable(pb, FMT(prefix, ".tradingHistoryRootB_O"))),
@@ -249,20 +247,14 @@ class RingSettlementGadget : public GadgetT
249247
protocolFeeA_from_balanceAO_to_balanceAP(pb, balanceA_O, balanceA_P, feeCalculatorA.getProtocolFee(), FMT(prefix, ".protocolFeeA_from_balanceAO_to_balanceAP")),
250248
protocolFeeB_from_balanceBO_to_balanceBP(pb, balanceB_O, balanceB_P, feeCalculatorB.getProtocolFee(), FMT(prefix, ".protocolFeeB_from_balanceBO_to_balanceBP")),
251249

252-
// Update trading history
253-
updateTradeHistoryA(pb, orderA.balanceSBefore.tradingHistory, subArray(orderA.orderID.bits, 0, NUM_BITS_TRADING_HISTORY),
254-
{orderA.tradeHistoryBefore.filled, orderA.tradeHistoryBefore.cancelled, orderA.tradeHistoryBefore.orderID},
255-
{filledAfterA.result(), orderA.tradeHistory.getCancelledToStore(), orderA.tradeHistory.getOrderIDToStore()},
256-
FMT(prefix, ".updateTradeHistoryA")),
257-
updateTradeHistoryB(pb, orderB.balanceSBefore.tradingHistory, subArray(orderB.orderID.bits, 0, NUM_BITS_TRADING_HISTORY),
258-
{orderB.tradeHistoryBefore.filled, orderB.tradeHistoryBefore.cancelled, orderB.tradeHistoryBefore.orderID},
259-
{filledAfterB.result(), orderB.tradeHistory.getCancelledToStore(), orderB.tradeHistory.getOrderIDToStore()},
260-
FMT(prefix, ".updateTradeHistoryB")),
261-
262250
// Update UserA
251+
updateTradeHistory_A(pb, orderA.balanceSBefore.tradingHistory, subArray(orderA.orderID.bits, 0, NUM_BITS_TRADING_HISTORY),
252+
{orderA.tradeHistoryBefore.filled, orderA.tradeHistoryBefore.cancelled, orderA.tradeHistoryBefore.orderID},
253+
{filledAfterA.result(), orderA.tradeHistory.getCancelledToStore(), orderA.tradeHistory.getOrderIDToStore()},
254+
FMT(prefix, ".updateTradeHistory_A")),
263255
updateBalanceS_A(pb, orderA.accountBefore.balancesRoot, orderA.tokenS.bits,
264256
{balanceS_A.front(), orderA.balanceSBefore.tradingHistory},
265-
{balanceS_A.back(), updateTradeHistoryA.result()},
257+
{balanceS_A.back(), updateTradeHistory_A.result()},
266258
FMT(prefix, ".updateBalanceS_A")),
267259
updateBalanceB_A(pb, updateBalanceS_A.result(), orderA.tokenB.bits,
268260
{balanceB_A.front(), orderA.balanceBBefore.tradingHistory},
@@ -274,9 +266,13 @@ class RingSettlementGadget : public GadgetT
274266
FMT(prefix, ".updateAccount_A")),
275267

276268
// Update UserB
269+
updateTradeHistory_B(pb, orderB.balanceSBefore.tradingHistory, subArray(orderB.orderID.bits, 0, NUM_BITS_TRADING_HISTORY),
270+
{orderB.tradeHistoryBefore.filled, orderB.tradeHistoryBefore.cancelled, orderB.tradeHistoryBefore.orderID},
271+
{filledAfterB.result(), orderB.tradeHistory.getCancelledToStore(), orderB.tradeHistory.getOrderIDToStore()},
272+
FMT(prefix, ".updateTradeHistory_B")),
277273
updateBalanceS_B(pb, orderB.accountBefore.balancesRoot, orderB.tokenS.bits,
278274
{balanceS_B.front(), orderB.balanceSBefore.tradingHistory},
279-
{balanceS_B.back(), updateTradeHistoryB.result()},
275+
{balanceS_B.back(), updateTradeHistory_B.result()},
280276
FMT(prefix, ".updateBalanceS_B")),
281277
updateBalanceB_B(pb, updateBalanceS_B.result(), orderB.tokenB.bits,
282278
{balanceB_B.front(), orderB.balanceBBefore.tradingHistory},
@@ -368,16 +364,14 @@ class RingSettlementGadget : public GadgetT
368364
protocolFeeA_from_balanceAO_to_balanceAP.generate_r1cs_witness();
369365
protocolFeeB_from_balanceBO_to_balanceBP.generate_r1cs_witness();
370366

371-
// Update trading history
372-
updateTradeHistoryA.generate_r1cs_witness(ringSettlement.tradeHistoryUpdate_A.proof);
373-
updateTradeHistoryB.generate_r1cs_witness(ringSettlement.tradeHistoryUpdate_B.proof);
374-
375367
// Update UserA
368+
updateTradeHistory_A.generate_r1cs_witness(ringSettlement.tradeHistoryUpdate_A.proof);
376369
updateBalanceS_A.generate_r1cs_witness(ringSettlement.balanceUpdateS_A.proof);
377370
updateBalanceB_A.generate_r1cs_witness(ringSettlement.balanceUpdateB_A.proof);
378371
updateAccount_A.generate_r1cs_witness(ringSettlement.accountUpdate_A.proof);
379372

380373
// Update UserB
374+
updateTradeHistory_B.generate_r1cs_witness(ringSettlement.tradeHistoryUpdate_B.proof);
381375
updateBalanceS_B.generate_r1cs_witness(ringSettlement.balanceUpdateS_B.proof);
382376
updateBalanceB_B.generate_r1cs_witness(ringSettlement.balanceUpdateB_B.proof);
383377
updateAccount_B.generate_r1cs_witness(ringSettlement.accountUpdate_B.proof);
@@ -433,16 +427,14 @@ class RingSettlementGadget : public GadgetT
433427
protocolFeeA_from_balanceAO_to_balanceAP.generate_r1cs_constraints();
434428
protocolFeeB_from_balanceBO_to_balanceBP.generate_r1cs_constraints();
435429

436-
// Update trading history
437-
updateTradeHistoryA.generate_r1cs_constraints();
438-
updateTradeHistoryB.generate_r1cs_constraints();
439-
440430
// Update UserA
431+
updateTradeHistory_A.generate_r1cs_constraints();
441432
updateBalanceS_A.generate_r1cs_constraints();
442433
updateBalanceB_A.generate_r1cs_constraints();
443434
updateAccount_A.generate_r1cs_constraints();
444435

445436
// Update UserB
437+
updateTradeHistory_B.generate_r1cs_constraints();
446438
updateBalanceS_B.generate_r1cs_constraints();
447439
updateBalanceB_B.generate_r1cs_constraints();
448440
updateAccount_B.generate_r1cs_constraints();
@@ -473,17 +465,17 @@ class RingSettlementGadget : public GadgetT
473465
};
474466
}
475467

476-
const VariableT getNewAccountsRoot() const
468+
const VariableT& getNewAccountsRoot() const
477469
{
478470
return updateAccount_B.result();
479471
}
480472

481-
const VariableT getNewProtocolBalancesRoot() const
473+
const VariableT& getNewProtocolBalancesRoot() const
482474
{
483475
return updateBalanceB_P.result();
484476
}
485477

486-
const VariableT getNewOperatorBalancesRoot() const
478+
const VariableT& getNewOperatorBalancesRoot() const
487479
{
488480
return updateBalanceB_O.result();
489481
}
@@ -662,7 +654,7 @@ class RingSettlementCircuit : public GadgetT
662654
signatureVerifier.generate_r1cs_constraints();
663655

664656
// Check the new merkle root
665-
forceEqual(pb, updateAccount_O->result(), merkleRootAfter.packed, "newMerkleRoot");
657+
requireEqual(pb, updateAccount_O->result(), merkleRootAfter.packed, "newMerkleRoot");
666658
}
667659

668660
bool generateWitness(const RingSettlementBlock& block)

0 commit comments

Comments
 (0)