Skip to content

Commit

Permalink
fix solhint warnings for custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
daveroga committed Feb 3, 2025
1 parent ccd69f9 commit 0473eca
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity 0.8.27;
import {Groth16VerifierAuthV2} from "./Groth16VerifierAuthV2.sol";
import {IGroth16Verifier} from "../../interfaces/IGroth16Verifier.sol";

error ExpectedArrayLenght(uint256 expected, uint256 actual);

contract Groth16VerifierAuthV2Wrapper is Groth16VerifierAuthV2, IGroth16Verifier {
/**
* @dev Number of public signals for atomic mtp circuit
Expand All @@ -26,7 +28,9 @@ contract Groth16VerifierAuthV2Wrapper is Groth16VerifierAuthV2, IGroth16Verifier
) public view returns (bool r) {
uint[PUBSIGNALS_LENGTH] memory pubSignals;

require(signals.length == PUBSIGNALS_LENGTH, "expected array length is 3");
if (signals.length != PUBSIGNALS_LENGTH) {
revert ExpectedArrayLenght(PUBSIGNALS_LENGTH, signals.length);
}

for (uint256 i = 0; i < PUBSIGNALS_LENGTH; i++) {
pubSignals[i] = signals[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ pragma solidity ^0.8.10;
import {IGroth16Verifier} from "../../interfaces/IGroth16Verifier.sol";
import {Groth16VerifierLinkedMultiQuery10} from "./Groth16VerifierLinkedMultiQuery10.sol";

error ExpectedArrayLenght(uint256 expected, uint256 actual);

contract Groth16VerifierLinkedMultiQuery10Wrapper is
Groth16VerifierLinkedMultiQuery10,
IGroth16Verifier
{
/**
* @dev Number of public signals for atomic mtp circuit
*/
uint constant PUBSIGNALS_LENGTH = 22;
uint256 constant PUBSIGNALS_LENGTH = 22;

/**
* @dev Verify the circuit with the groth16 proof π=([πa]1,[πb]2,[πc]1).
Expand All @@ -29,7 +31,9 @@ contract Groth16VerifierLinkedMultiQuery10Wrapper is
) public view returns (bool r) {
uint[PUBSIGNALS_LENGTH] memory pubSignals;

require(signals.length == PUBSIGNALS_LENGTH, "expected array length is 22");
if (signals.length != PUBSIGNALS_LENGTH) {
revert ExpectedArrayLenght(PUBSIGNALS_LENGTH, signals.length);
}

for (uint256 i = 0; i < PUBSIGNALS_LENGTH; i++) {
pubSignals[i] = signals[i];
Expand Down
8 changes: 6 additions & 2 deletions contracts/lib/groth16-verifiers/Groth16VerifierMTPWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ pragma solidity 0.8.27;
import "./Groth16VerifierMTP.sol";
import "../../interfaces/IGroth16Verifier.sol";

error ExpectedArrayLenght(uint256 expected, uint256 actual);

contract Groth16VerifierMTPWrapper is Groth16VerifierMTP, IGroth16Verifier {
/**
* @dev Number of public signals for atomic mtp circuit
*/
uint constant PUBSIGNALS_LENGTH = 11;
uint256 constant PUBSIGNALS_LENGTH = 11;

/**
* @dev Verify the circuit with the groth16 proof π=([πa]1,[πb]2,[πc]1).
Expand All @@ -38,7 +40,9 @@ contract Groth16VerifierMTPWrapper is Groth16VerifierMTP, IGroth16Verifier {
) public view returns (bool r) {
uint[PUBSIGNALS_LENGTH] memory pubSignals;

require(signals.length == PUBSIGNALS_LENGTH, "expected array length is 11");
if (signals.length != PUBSIGNALS_LENGTH) {
revert ExpectedArrayLenght(PUBSIGNALS_LENGTH, signals.length);
}

for (uint256 i = 0; i < PUBSIGNALS_LENGTH; i++) {
pubSignals[i] = signals[i];
Expand Down
8 changes: 6 additions & 2 deletions contracts/lib/groth16-verifiers/Groth16VerifierSigWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ pragma solidity 0.8.27;
import "./Groth16VerifierSig.sol";
import "../../interfaces/IGroth16Verifier.sol";

error ExpectedArrayLenght(uint256 expected, uint256 actual);

contract Groth16VerifierSigWrapper is Groth16VerifierSig, IGroth16Verifier {
/**
* @dev Number of public signals for atomic sig circuit
*/
uint constant PUBSIGNALS_LENGTH = 11;
uint256 constant PUBSIGNALS_LENGTH = 11;

/**
* @dev Verify the circuit with the groth16 proof π=([πa]1,[πb]2,[πc]1).
Expand All @@ -39,7 +41,9 @@ contract Groth16VerifierSigWrapper is Groth16VerifierSig, IGroth16Verifier {
// slither-disable-next-line uninitialized-local
uint[PUBSIGNALS_LENGTH] memory pubSignals;

require(signals.length == PUBSIGNALS_LENGTH, "expected array length is 11");
if (signals.length != PUBSIGNALS_LENGTH) {
revert ExpectedArrayLenght(PUBSIGNALS_LENGTH, signals.length);
}

for (uint256 i = 0; i < PUBSIGNALS_LENGTH; i++) {
pubSignals[i] = signals[i];
Expand Down
8 changes: 6 additions & 2 deletions contracts/lib/groth16-verifiers/Groth16VerifierV3Wrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ pragma solidity 0.8.27;
import "./Groth16VerifierV3.sol";
import "../../interfaces/IGroth16Verifier.sol";

error ExpectedArrayLenght(uint256 expected, uint256 actual);

contract Groth16VerifierV3Wrapper is Groth16VerifierV3, IGroth16Verifier {
/**
* @dev Number of public signals for atomic V3 circuit
*/
uint constant PUBSIGNALS_LENGTH = 14;
uint256 constant PUBSIGNALS_LENGTH = 14;

/**
* @dev Verify the circuit with the groth16 proof π=([πa]1,[πb]2,[πc]1).
Expand All @@ -39,7 +41,9 @@ contract Groth16VerifierV3Wrapper is Groth16VerifierV3, IGroth16Verifier {
// slither-disable-next-line uninitialized-local
uint[PUBSIGNALS_LENGTH] memory pubSignals;

require(signals.length == PUBSIGNALS_LENGTH, "expected array length is 14");
if (signals.length != PUBSIGNALS_LENGTH) {
revert ExpectedArrayLenght(PUBSIGNALS_LENGTH, signals.length);
}

for (uint256 i = 0; i < PUBSIGNALS_LENGTH; i++) {
pubSignals[i] = signals[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import {CredentialAtomicQueryValidatorBase} from "./CredentialAtomicQueryValidat
import {IGroth16Verifier} from "../../interfaces/IGroth16Verifier.sol";
import {IRequestValidator} from "../../interfaces/IRequestValidator.sol";

error CircuitsLengthShouldBeOne();
error VerifierAddressShouldNotBeZero();
error ProofIsNotValid();
error QueryHashDoesNotMatchTheRequestedOne(uint256 expected, uint256 actual);
error MerklizedValueIsNotCorrect();
error RevocationCheckShouldMatchTheQuery(uint256 expected, uint256 actual);

/**
* @dev Base contract for credential atomic query v2 validators circuits.
*/
Expand Down Expand Up @@ -107,22 +114,30 @@ abstract contract CredentialAtomicQueryV2ValidatorBase is CredentialAtomicQueryV

CredentialAtomicQuery memory credAtomicQuery = abi.decode(params, (CredentialAtomicQuery));

require(credAtomicQuery.circuitIds.length == 1, "circuitIds length is not equal to 1");
if (credAtomicQuery.circuitIds.length != 1) {
revert CircuitsLengthShouldBeOne();
}

IGroth16Verifier g16Verifier = getVerifierByCircuitId(credAtomicQuery.circuitIds[0]);

require(g16Verifier != IGroth16Verifier(address(0)), "Verifier address should not be zero");
if (g16Verifier == IGroth16Verifier(address(0))) {
revert VerifierAddressShouldNotBeZero();
}

// verify that zkp is valid
require(g16Verifier.verify(a, b, c, inputs), "Proof is not valid");
if (!g16Verifier.verify(a, b, c, inputs)) {
revert ProofIsNotValid();
}

PubSignals memory pubSignals = parsePubSignals(inputs);

// check circuitQueryHash
require(
pubSignals.circuitQueryHash == credAtomicQuery.queryHash,
"Query hash does not match the requested one"
);
if (pubSignals.circuitQueryHash != credAtomicQuery.queryHash) {
revert QueryHashDoesNotMatchTheRequestedOne(
credAtomicQuery.queryHash,
pubSignals.circuitQueryHash
);
}

// TODO: add support for query to specific userID and then verifying it

Expand All @@ -147,7 +162,9 @@ abstract contract CredentialAtomicQueryV2ValidatorBase is CredentialAtomicQueryV

function _checkMerklized(uint256 merklized, uint256 queryClaimPathKey) internal pure {
uint256 shouldBeMerklized = queryClaimPathKey != 0 ? 1 : 0;
require(merklized == shouldBeMerklized, "Merklized value is not correct");
if (merklized != shouldBeMerklized) {
revert MerklizedValueIsNotCorrect();
}
}

function _checkIsRevocationChecked(
Expand All @@ -158,10 +175,12 @@ abstract contract CredentialAtomicQueryV2ValidatorBase is CredentialAtomicQueryV
if (skipClaimRevocationCheck) {
expectedIsRevocationChecked = 0;
}
require(
isRevocationChecked == expectedIsRevocationChecked,
"Revocation check should match the query"
);
if (isRevocationChecked != expectedIsRevocationChecked) {
revert RevocationCheckShouldMatchTheQuery(
expectedIsRevocationChecked,
isRevocationChecked
);
}
}

function _getResponseFields(
Expand Down
68 changes: 42 additions & 26 deletions contracts/validators/request/CredentialAtomicQueryV3Validator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import {CredentialAtomicQueryValidatorBase} from "./CredentialAtomicQueryValidat
import {IGroth16Verifier} from "../../interfaces/IGroth16Verifier.sol";
import {GenesisUtils} from "../../lib/GenesisUtils.sol";
import {IRequestValidator} from "../../interfaces/IRequestValidator.sol";
import {IState} from "../../interfaces/IState.sol";

error VerifierIDNotSet();
error QueryHashDoesNotMatchTheRequestedOne(uint256 expected, uint256 actual);
error VerifierAddressShouldNotBeZero();
error CircuitsLengthShouldBeOne();
error ProofIsNotValid();
error InvalidLinkIDPubSignal();
error ProofTypeShouldMatchTheRequestedOneInQuery();
error InvalidNullifyPubSignal();
error UserIDDoesNotCorrespondToTheSender();

/**
* @dev CredentialAtomicQueryV3 validator
Expand Down Expand Up @@ -213,10 +220,12 @@ contract CredentialAtomicQueryV3Validator is CredentialAtomicQueryValidatorBase
_checkChallenge(pubSignals.challenge, sender);

// check circuitQueryHash
require(
pubSignals.circuitQueryHash == credAtomicQuery.queryHash,
"Query hash does not match the requested one"
);
if (pubSignals.circuitQueryHash != credAtomicQuery.queryHash) {
revert QueryHashDoesNotMatchTheRequestedOne(
credAtomicQuery.queryHash,
pubSignals.circuitQueryHash
);
}

// if operator == 16 then we have selective disclosure
return (pubSignals, credAtomicQuery.operator == 16);
Expand All @@ -229,42 +238,49 @@ contract CredentialAtomicQueryV3Validator is CredentialAtomicQueryValidatorBase
uint256[2] memory c,
CredentialAtomicQueryV3 memory credAtomicQuery
) internal view {
require(credAtomicQuery.circuitIds.length == 1, "circuitIds length is not equal to 1");
if (credAtomicQuery.circuitIds.length != 1) {
revert CircuitsLengthShouldBeOne();
}

IGroth16Verifier g16Verifier = getVerifierByCircuitId(credAtomicQuery.circuitIds[0]);
require(g16Verifier != IGroth16Verifier(address(0)), "Verifier address should not be zero");
if (g16Verifier == IGroth16Verifier(address(0))) {
revert VerifierAddressShouldNotBeZero();
}

// verify that zkp is valid
require(g16Verifier.verify(a, b, c, inputs), "Proof is not valid");
if (!g16Verifier.verify(a, b, c, inputs)) {
revert ProofIsNotValid();
}
}

function _checkLinkID(uint256 groupID, uint256 linkID) internal pure {
require(
(groupID == 0 && linkID == 0) || (groupID != 0 && linkID != 0),
"Invalid Link ID pub signal"
);
if (!((groupID == 0 && linkID == 0) || (groupID != 0 && linkID != 0))) {
revert InvalidLinkIDPubSignal();
}
}

function _checkProofType(uint256 queryProofType, uint256 pubSignalProofType) internal pure {
require(
queryProofType == 0 || queryProofType == pubSignalProofType,
"Proof type should match the requested one in query"
);
if (queryProofType != 0 && queryProofType != pubSignalProofType) {
revert ProofTypeShouldMatchTheRequestedOneInQuery();
}
}

function _checkNullify(uint256 nullifier, uint256 nullifierSessionID) internal pure {
require(nullifierSessionID == 0 || nullifier != 0, "Invalid nullify pub signal");
if (nullifierSessionID != 0 && nullifier == 0) {
revert InvalidNullifyPubSignal();
}
}

function _checkAuth(uint256 userID, address ethIdentityOwner) internal view {
require(
userID ==
GenesisUtils.calcIdFromEthAddress(
_getState().getIdTypeIfSupported(userID),
ethIdentityOwner
),
"UserID does not correspond to the sender"
);
if (
userID !=
GenesisUtils.calcIdFromEthAddress(
_getState().getIdTypeIfSupported(userID),
ethIdentityOwner
)
) {
revert UserIDDoesNotCorrespondToTheSender();
}
}

function _getResponseFields(
Expand All @@ -275,7 +291,7 @@ contract CredentialAtomicQueryV3Validator is CredentialAtomicQueryValidatorBase
IRequestValidator.ResponseField[]
memory responseFields = new IRequestValidator.ResponseField[](numSignals);

uint i = 0;
uint256 i = 0;
responseFields[i++] = IRequestValidator.ResponseField({
name: "userID",
value: pubSignals.userID
Expand Down
Loading

0 comments on commit 0473eca

Please sign in to comment.