@@ -27,8 +27,8 @@ contract DeltaProofTest is Test {
27
27
deltaInputs.rcv = deltaInputs.rcv % SECP256K1_ORDER;
28
28
vm.assume (deltaInputs.rcv != 0 );
29
29
vm.assume (deltaInputs.kind != 0 );
30
- int256 prod_aux = (int256 (uint256 (deltaInputs.kind)) * int256 (deltaInputs.quantity));
31
- uint256 prod = prod_aux >= 0 ? uint256 (prod_aux ) : SECP256K1_ORDER - (uint256 (- prod_aux ) % SECP256K1_ORDER);
30
+ int256 prodAux = (int256 (uint256 (deltaInputs.kind)) * int256 (deltaInputs.quantity));
31
+ uint256 prod = prodAux >= 0 ? uint256 (prodAux ) : SECP256K1_ORDER - (uint256 (- prodAux ) % SECP256K1_ORDER);
32
32
uint256 preDelta = addmod (prod, deltaInputs.rcv, SECP256K1_ORDER);
33
33
vm.assume (preDelta != 0 );
34
34
// Derive address and public key from transaction delta
@@ -124,59 +124,19 @@ contract DeltaProofTest is Test {
124
124
Delta.verify ({proof: proof1, instance: instance2, verifyingKey: verifyingKey});
125
125
}
126
126
127
- /// @notice Wrap the delta inputs in such a way that they can be balanced
128
- /// and also return the total quantity and value commitment randomness
129
- function wrapDeltaInputs (DeltaInstanceInputs[] memory deltaInputs ) public pure returns (DeltaInstanceInputs[] memory wrappedDeltaInputs , int128 quantity_acc , uint256 rcv_acc ) {
130
- // Grab the kind to use for all deltas
131
- uint128 kind = deltaInputs.length > 0 ? deltaInputs[0 ].kind : 0 ;
132
- // Compute the window into which the deltas should sum
133
- int128 half_max = type (int128 ).max >> 1 ;
134
- int128 half_min = type (int128 ).min >> 1 ;
135
- // Track the current quantity and value commitment randomness
136
- quantity_acc = 0 ;
137
- rcv_acc = 0 ;
138
- // Wrap the deltas
139
- for (uint i = 0 ; i < deltaInputs.length ; i++ ) {
140
- // Ensure that all the deltas have the same kind
141
- deltaInputs[i].kind = kind;
142
- // Accumulate the randomness commitments modulo SECP256K1_ORDER
143
- rcv_acc = addmod (rcv_acc, deltaInputs[i].rcv, SECP256K1_ORDER);
144
- // Adjust the delta inputs so that the sum remains in a specific range
145
- if (deltaInputs[i].quantity >= 0 && quantity_acc > half_max - deltaInputs[i].quantity) {
146
- int128 overflow = quantity_acc - (half_max - deltaInputs[i].quantity);
147
- deltaInputs[i].quantity = half_min + overflow - 1 - quantity_acc;
148
- } else if (deltaInputs[i].quantity < 0 && quantity_acc < half_min - deltaInputs[i].quantity) {
149
- int128 underflow = (half_min - deltaInputs[i].quantity) - quantity_acc;
150
- deltaInputs[i].quantity = half_max + 1 - underflow - quantity_acc;
151
- }
152
- // Finally, accumulate the adjusted quantity
153
- quantity_acc += deltaInputs[i].quantity;
154
- }
155
- // Finally, return tbe wrapped deltas
156
- wrappedDeltaInputs = deltaInputs;
157
- }
158
-
159
- /// @notice Grab the first length elements from deltaInputs
160
- function truncateDeltaInputs (DeltaInstanceInputs[] memory deltaInputs , uint length ) public pure returns (DeltaInstanceInputs[] memory truncatedDeltaInputs ) {
161
- truncatedDeltaInputs = new DeltaInstanceInputs [](length);
162
- for (uint i = 0 ; i < length; i++ ) {
163
- truncatedDeltaInputs[i] = deltaInputs[i];
164
- }
165
- }
166
-
167
127
/// @notice Check that a balanced transaction does pass verification
168
128
function test_verify_balanced_delta_succeeds (DeltaInstanceInputs[] memory deltaInputs , bytes32 verifyingKey ) public {
169
129
uint256 [2 ] memory deltaAcc = [uint256 (0 ), uint256 (0 )];
170
130
// Truncate the delta inputs to improve test performance
171
- uint MAX_DELTA_LEN = 10 ;
172
- deltaInputs = truncateDeltaInputs (deltaInputs, deltaInputs.length % MAX_DELTA_LEN );
131
+ uint256 maxDeltaLen = 10 ;
132
+ deltaInputs = truncateDeltaInputs (deltaInputs, deltaInputs.length % maxDeltaLen );
173
133
// Make sure that the delta quantities balance out
174
134
(DeltaInstanceInputs[] memory wrappedDeltaInputs , int128 quantity , uint256 rcv ) = wrapDeltaInputs (deltaInputs);
175
135
// Adjust the last delta so that the full sum is zero
176
136
if (quantity != 0 ) {
177
137
wrappedDeltaInputs[wrappedDeltaInputs.length - 1 ].quantity -= quantity;
178
138
}
179
- for (uint i = 0 ; i < wrappedDeltaInputs.length ; i++ ) {
139
+ for (uint256 i = 0 ; i < wrappedDeltaInputs.length ; i++ ) {
180
140
// Compute the delta instance and accumulate it
181
141
uint256 [2 ] memory instance = generateDeltaInstance (wrappedDeltaInputs[i]);
182
142
deltaAcc = Delta.add (deltaAcc, instance);
@@ -195,13 +155,13 @@ contract DeltaProofTest is Test {
195
155
function test_verify_imbalanced_delta_fails (DeltaInstanceInputs[] memory deltaInputs , bytes32 verifyingKey ) public {
196
156
uint256 [2 ] memory deltaAcc = [uint256 (0 ), uint256 (0 )];
197
157
// Truncate the delta inputs to improve test performance
198
- uint MAX_DELTA_LEN = 10 ;
199
- deltaInputs = truncateDeltaInputs (deltaInputs, deltaInputs.length % MAX_DELTA_LEN );
158
+ uint256 maxDeltaLen = 10 ;
159
+ deltaInputs = truncateDeltaInputs (deltaInputs, deltaInputs.length % maxDeltaLen );
200
160
// Accumulate the total quantity and randomness commitment
201
161
(DeltaInstanceInputs[] memory wrappedDeltaInputs , int128 quantity , uint256 rcv ) = wrapDeltaInputs (deltaInputs);
202
162
// Assume that the deltas are imbalanced
203
163
vm.assume (quantity != 0 );
204
- for (uint i = 0 ; i < wrappedDeltaInputs.length ; i++ ) {
164
+ for (uint256 i = 0 ; i < wrappedDeltaInputs.length ; i++ ) {
205
165
// Compute the delta instance and accumulate it
206
166
uint256 [2 ] memory instance = generateDeltaInstance (wrappedDeltaInputs[i]);
207
167
deltaAcc = Delta.add (deltaAcc, instance);
@@ -217,6 +177,46 @@ contract DeltaProofTest is Test {
217
177
Delta.verify ({proof: proof, instance: deltaAcc, verifyingKey: verifyingKey});
218
178
}
219
179
180
+ /// @notice Wrap the delta inputs in such a way that they can be balanced
181
+ /// and also return the total quantity and value commitment randomness
182
+ function wrapDeltaInputs (DeltaInstanceInputs[] memory deltaInputs ) public pure returns (DeltaInstanceInputs[] memory wrappedDeltaInputs , int128 quantityAcc , uint256 rcvAcc ) {
183
+ // Grab the kind to use for all deltas
184
+ uint128 kind = deltaInputs.length > 0 ? deltaInputs[0 ].kind : 0 ;
185
+ // Compute the window into which the deltas should sum
186
+ int128 halfMax = type (int128 ).max >> 1 ;
187
+ int128 halfMin = type (int128 ).min >> 1 ;
188
+ // Track the current quantity and value commitment randomness
189
+ quantityAcc = 0 ;
190
+ rcvAcc = 0 ;
191
+ // Wrap the deltas
192
+ for (uint256 i = 0 ; i < deltaInputs.length ; i++ ) {
193
+ // Ensure that all the deltas have the same kind
194
+ deltaInputs[i].kind = kind;
195
+ // Accumulate the randomness commitments modulo SECP256K1_ORDER
196
+ rcvAcc = addmod (rcvAcc, deltaInputs[i].rcv, SECP256K1_ORDER);
197
+ // Adjust the delta inputs so that the sum remains in a specific range
198
+ if (deltaInputs[i].quantity >= 0 && quantityAcc > halfMax - deltaInputs[i].quantity) {
199
+ int128 overflow = quantityAcc - (halfMax - deltaInputs[i].quantity);
200
+ deltaInputs[i].quantity = halfMin + overflow - 1 - quantityAcc;
201
+ } else if (deltaInputs[i].quantity < 0 && quantityAcc < halfMin - deltaInputs[i].quantity) {
202
+ int128 underflow = (halfMin - deltaInputs[i].quantity) - quantityAcc;
203
+ deltaInputs[i].quantity = halfMax + 1 - underflow - quantityAcc;
204
+ }
205
+ // Finally, accumulate the adjusted quantity
206
+ quantityAcc += deltaInputs[i].quantity;
207
+ }
208
+ // Finally, return tbe wrapped deltas
209
+ wrappedDeltaInputs = deltaInputs;
210
+ }
211
+
212
+ /// @notice Grab the first length elements from deltaInputs
213
+ function truncateDeltaInputs (DeltaInstanceInputs[] memory deltaInputs , uint256 length ) public pure returns (DeltaInstanceInputs[] memory truncatedDeltaInputs ) {
214
+ truncatedDeltaInputs = new DeltaInstanceInputs [](length);
215
+ for (uint256 i = 0 ; i < length; i++ ) {
216
+ truncatedDeltaInputs[i] = deltaInputs[i];
217
+ }
218
+ }
219
+
220
220
function test_signatureIntegrity () public pure {
221
221
(uint8 v , bytes32 r , bytes32 s ) = vm.sign (MockDelta.SIGNER_PRIVATE_KEY, MockDelta.MESSAGE_HASH);
222
222
assertEq (r, MockDelta.R);
0 commit comments