Skip to content

Commit 76b64cd

Browse files
committed
Fix linter errors.
1 parent 128f6cf commit 76b64cd

File tree

2 files changed

+65
-65
lines changed

2 files changed

+65
-65
lines changed

contracts/src/proving/Delta.sol

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ library Delta {
1212
/// @notice Thrown if the recovered delta public key doesn't match the delta instance.
1313
error DeltaMismatch(address expected, address actual);
1414

15+
/// @notice Verifies a delta proof.
16+
/// @param proof The delta proof.
17+
/// @param instance The transaction delta.
18+
/// @param verifyingKey The Keccak-256 hash of all nullifiers and commitments as ordered in the compliance units.
19+
function verify(bytes memory proof, uint256[2] memory instance, bytes32 verifyingKey) public pure {
20+
// Verify the delta proof using the ECDSA.recover API to obtain the address
21+
address recovered = ECDSA.recover({hash: verifyingKey, signature: proof});
22+
23+
// Convert the public key to an address
24+
address expected = toAccount(instance);
25+
26+
// Compare it with the recovered address
27+
if (recovered != expected) {
28+
revert DeltaMismatch({expected: expected, actual: recovered});
29+
}
30+
}
31+
1532
/// @notice Adds to ellipitic curve points and returns the resulting value.
1633
/// @param p1 The first curve point.
1734
/// @param p2 The second curve point.
@@ -40,21 +57,4 @@ library Delta {
4057
function computeVerifyingKey(bytes32[] memory tags) internal pure returns (bytes32 verifyingKey) {
4158
verifyingKey = keccak256(abi.encodePacked(tags));
4259
}
43-
44-
/// @notice Verifies a delta proof.
45-
/// @param proof The delta proof.
46-
/// @param instance The transaction delta.
47-
/// @param verifyingKey The Keccak-256 hash of all nullifiers and commitments as ordered in the compliance units.
48-
function verify(bytes memory proof, uint256[2] memory instance, bytes32 verifyingKey) public pure {
49-
// Verify the delta proof using the ECDSA.recover API to obtain the address
50-
address recovered = ECDSA.recover({hash: verifyingKey, signature: proof});
51-
52-
// Convert the public key to an address
53-
address expected = toAccount(instance);
54-
55-
// Compare it with the recovered address
56-
if (recovered != expected) {
57-
revert DeltaMismatch({expected: expected, actual: recovered});
58-
}
59-
}
6060
}

contracts/test/proofs/DeltaProof.t.sol

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ contract DeltaProofTest is Test {
2727
deltaInputs.rcv = deltaInputs.rcv % SECP256K1_ORDER;
2828
vm.assume(deltaInputs.rcv != 0);
2929
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);
3232
uint256 preDelta = addmod(prod, deltaInputs.rcv, SECP256K1_ORDER);
3333
vm.assume(preDelta != 0);
3434
// Derive address and public key from transaction delta
@@ -124,59 +124,19 @@ contract DeltaProofTest is Test {
124124
Delta.verify({proof: proof1, instance: instance2, verifyingKey: verifyingKey});
125125
}
126126

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-
167127
/// @notice Check that a balanced transaction does pass verification
168128
function test_verify_balanced_delta_succeeds(DeltaInstanceInputs[] memory deltaInputs, bytes32 verifyingKey) public {
169129
uint256[2] memory deltaAcc = [uint256(0), uint256(0)];
170130
// 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);
173133
// Make sure that the delta quantities balance out
174134
(DeltaInstanceInputs[] memory wrappedDeltaInputs, int128 quantity, uint256 rcv) = wrapDeltaInputs(deltaInputs);
175135
// Adjust the last delta so that the full sum is zero
176136
if(quantity != 0) {
177137
wrappedDeltaInputs[wrappedDeltaInputs.length - 1].quantity -= quantity;
178138
}
179-
for(uint i = 0; i < wrappedDeltaInputs.length; i++) {
139+
for(uint256 i = 0; i < wrappedDeltaInputs.length; i++) {
180140
// Compute the delta instance and accumulate it
181141
uint256[2] memory instance = generateDeltaInstance(wrappedDeltaInputs[i]);
182142
deltaAcc = Delta.add(deltaAcc, instance);
@@ -195,13 +155,13 @@ contract DeltaProofTest is Test {
195155
function test_verify_imbalanced_delta_fails(DeltaInstanceInputs[] memory deltaInputs, bytes32 verifyingKey) public {
196156
uint256[2] memory deltaAcc = [uint256(0), uint256(0)];
197157
// 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);
200160
// Accumulate the total quantity and randomness commitment
201161
(DeltaInstanceInputs[] memory wrappedDeltaInputs, int128 quantity, uint256 rcv) = wrapDeltaInputs(deltaInputs);
202162
// Assume that the deltas are imbalanced
203163
vm.assume(quantity != 0);
204-
for(uint i = 0; i < wrappedDeltaInputs.length; i++) {
164+
for(uint256 i = 0; i < wrappedDeltaInputs.length; i++) {
205165
// Compute the delta instance and accumulate it
206166
uint256[2] memory instance = generateDeltaInstance(wrappedDeltaInputs[i]);
207167
deltaAcc = Delta.add(deltaAcc, instance);
@@ -217,6 +177,46 @@ contract DeltaProofTest is Test {
217177
Delta.verify({proof: proof, instance: deltaAcc, verifyingKey: verifyingKey});
218178
}
219179

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+
220220
function test_signatureIntegrity() public pure {
221221
(uint8 v, bytes32 r, bytes32 s) = vm.sign(MockDelta.SIGNER_PRIVATE_KEY, MockDelta.MESSAGE_HASH);
222222
assertEq(r, MockDelta.R);

0 commit comments

Comments
 (0)