Skip to content

Commit fff1892

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

File tree

2 files changed

+72
-65
lines changed

2 files changed

+72
-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: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ import { Example } from "./../mocks/Example.sol";
1010
import { MockDelta } from "./../mocks/MockDelta.sol";
1111

1212
contract DeltaProofTest is Test {
13+
// The parameters required to generate a delta instance
1314
struct DeltaInstanceInputs {
15+
// The identifier of the asset
1416
uint128 kind;
17+
// The quantity of the asset
1518
int128 quantity;
19+
// Value commitment randomness
1620
uint256 rcv;
1721
}
1822

23+
// The parameters required to generate a delta proof
1924
struct DeltaProofInputs {
25+
// Value commitment randomness
2026
uint256 rcv;
27+
// The hash being signed over
2128
bytes32 verifyingKey;
2229
}
2330

@@ -27,8 +34,8 @@ contract DeltaProofTest is Test {
2734
deltaInputs.rcv = deltaInputs.rcv % SECP256K1_ORDER;
2835
vm.assume(deltaInputs.rcv != 0);
2936
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);
37+
int256 prodAux = (int256(uint256(deltaInputs.kind)) * int256(deltaInputs.quantity));
38+
uint256 prod = prodAux >= 0 ? uint256(prodAux) : SECP256K1_ORDER - (uint256(-prodAux) % SECP256K1_ORDER);
3239
uint256 preDelta = addmod(prod, deltaInputs.rcv, SECP256K1_ORDER);
3340
vm.assume(preDelta != 0);
3441
// Derive address and public key from transaction delta
@@ -124,59 +131,19 @@ contract DeltaProofTest is Test {
124131
Delta.verify({proof: proof1, instance: instance2, verifyingKey: verifyingKey});
125132
}
126133

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-
167134
/// @notice Check that a balanced transaction does pass verification
168135
function test_verify_balanced_delta_succeeds(DeltaInstanceInputs[] memory deltaInputs, bytes32 verifyingKey) public {
169136
uint256[2] memory deltaAcc = [uint256(0), uint256(0)];
170137
// Truncate the delta inputs to improve test performance
171-
uint MAX_DELTA_LEN = 10;
172-
deltaInputs = truncateDeltaInputs(deltaInputs, deltaInputs.length % MAX_DELTA_LEN);
138+
uint256 maxDeltaLen = 10;
139+
deltaInputs = truncateDeltaInputs(deltaInputs, deltaInputs.length % maxDeltaLen);
173140
// Make sure that the delta quantities balance out
174141
(DeltaInstanceInputs[] memory wrappedDeltaInputs, int128 quantity, uint256 rcv) = wrapDeltaInputs(deltaInputs);
175142
// Adjust the last delta so that the full sum is zero
176143
if(quantity != 0) {
177144
wrappedDeltaInputs[wrappedDeltaInputs.length - 1].quantity -= quantity;
178145
}
179-
for(uint i = 0; i < wrappedDeltaInputs.length; i++) {
146+
for(uint256 i = 0; i < wrappedDeltaInputs.length; i++) {
180147
// Compute the delta instance and accumulate it
181148
uint256[2] memory instance = generateDeltaInstance(wrappedDeltaInputs[i]);
182149
deltaAcc = Delta.add(deltaAcc, instance);
@@ -195,13 +162,13 @@ contract DeltaProofTest is Test {
195162
function test_verify_imbalanced_delta_fails(DeltaInstanceInputs[] memory deltaInputs, bytes32 verifyingKey) public {
196163
uint256[2] memory deltaAcc = [uint256(0), uint256(0)];
197164
// Truncate the delta inputs to improve test performance
198-
uint MAX_DELTA_LEN = 10;
199-
deltaInputs = truncateDeltaInputs(deltaInputs, deltaInputs.length % MAX_DELTA_LEN);
165+
uint256 maxDeltaLen= 10;
166+
deltaInputs = truncateDeltaInputs(deltaInputs, deltaInputs.length % maxDeltaLen);
200167
// Accumulate the total quantity and randomness commitment
201168
(DeltaInstanceInputs[] memory wrappedDeltaInputs, int128 quantity, uint256 rcv) = wrapDeltaInputs(deltaInputs);
202169
// Assume that the deltas are imbalanced
203170
vm.assume(quantity != 0);
204-
for(uint i = 0; i < wrappedDeltaInputs.length; i++) {
171+
for(uint256 i = 0; i < wrappedDeltaInputs.length; i++) {
205172
// Compute the delta instance and accumulate it
206173
uint256[2] memory instance = generateDeltaInstance(wrappedDeltaInputs[i]);
207174
deltaAcc = Delta.add(deltaAcc, instance);
@@ -217,6 +184,46 @@ contract DeltaProofTest is Test {
217184
Delta.verify({proof: proof, instance: deltaAcc, verifyingKey: verifyingKey});
218185
}
219186

187+
/// @notice Wrap the delta inputs in such a way that they can be balanced
188+
/// and also return the total quantity and value commitment randomness
189+
function wrapDeltaInputs(DeltaInstanceInputs[] memory deltaInputs) public pure returns (DeltaInstanceInputs[] memory wrappedDeltaInputs, int128 quantityAcc, uint256 rcvAcc) {
190+
// Grab the kind to use for all deltas
191+
uint128 kind = deltaInputs.length > 0 ? deltaInputs[0].kind : 0;
192+
// Compute the window into which the deltas should sum
193+
int128 halfMax = type(int128).max >> 1;
194+
int128 halfMin = type(int128).min >> 1;
195+
// Track the current quantity and value commitment randomness
196+
quantityAcc = 0;
197+
rcvAcc = 0;
198+
// Wrap the deltas
199+
for(uint256 i = 0; i < deltaInputs.length; i++) {
200+
// Ensure that all the deltas have the same kind
201+
deltaInputs[i].kind = kind;
202+
// Accumulate the randomness commitments modulo SECP256K1_ORDER
203+
rcvAcc = addmod(rcvAcc, deltaInputs[i].rcv, SECP256K1_ORDER);
204+
// Adjust the delta inputs so that the sum remains in a specific range
205+
if(deltaInputs[i].quantity >= 0 && quantityAcc > halfMax - deltaInputs[i].quantity) {
206+
int128 overflow = quantityAcc - (halfMax - deltaInputs[i].quantity);
207+
deltaInputs[i].quantity = halfMin + overflow - 1 - quantityAcc;
208+
} else if(deltaInputs[i].quantity < 0 && quantityAcc < halfMin - deltaInputs[i].quantity) {
209+
int128 underflow = (halfMin - deltaInputs[i].quantity) - quantityAcc;
210+
deltaInputs[i].quantity = halfMax + 1 - underflow - quantityAcc;
211+
}
212+
// Finally, accumulate the adjusted quantity
213+
quantityAcc += deltaInputs[i].quantity;
214+
}
215+
// Finally, return tbe wrapped deltas
216+
wrappedDeltaInputs = deltaInputs;
217+
}
218+
219+
/// @notice Grab the first length elements from deltaInputs
220+
function truncateDeltaInputs(DeltaInstanceInputs[] memory deltaInputs, uint256 length) public pure returns (DeltaInstanceInputs[] memory truncatedDeltaInputs) {
221+
truncatedDeltaInputs = new DeltaInstanceInputs[](length);
222+
for(uint256 i = 0; i < length; i++) {
223+
truncatedDeltaInputs[i] = deltaInputs[i];
224+
}
225+
}
226+
220227
function test_signatureIntegrity() public pure {
221228
(uint8 v, bytes32 r, bytes32 s) = vm.sign(MockDelta.SIGNER_PRIVATE_KEY, MockDelta.MESSAGE_HASH);
222229
assertEq(r, MockDelta.R);

0 commit comments

Comments
 (0)