8000 Refactor Validators by AndriianChestnykh · Pull Request #354 · iden3/contracts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Refactor Validators #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions contracts/interfaces/IRequestValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ interface IRequestValidator {
string memory paramName
) external view returns (RequestParam memory);

/**
* @dev Get the index of the request param by name
* @param name Name of the request param
* @return Index of the request param
*/
function requestParamIndexOf(string memory name) external view returns (uint256);

/**
* @dev Get the index of the public input of the circuit by name
* @param name Name of the public input
Expand Down
4 changes: 2 additions & 2 deletions contracts/test-helpers/RequestValidatorStub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ contract RequestValidatorStub is IRequestValidator, ERC165 {
return requestParams[keccak256(params)][requestParamIndexOf(paramName)];
}

function requestParamIndexOf(string memory name) public view override returns (uint256) {
function requestParamIndexOf(string memory name) public view returns (uint256) {
uint256 index = _requestParamNameToIndex[name];
if (index == 0) revert RequestParamNameNotFound();
return --index; // we save 1-based index, but return 0-based
}

function inputIndexOf(string memory name) public view virtual returns (uint256) {
function inputIndexOf(string memory name) public view returns (uint256) {
uint256 index = _inputNameToIndex[name];
if (index == 0) {
revert InputNameNotFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ contract CredentialAtomicQueryMTPV2Validator is CredentialAtomicQueryV2Validator
_setInputToIndex("issuerClaimNonRevState", 9);
_setInputToIndex("timestamp", 10);

_setRequestParamToIndex("groupID", 0);
_setRequestParamToIndex("verifierID", 1);
_setRequestParamToIndex("nullifierSessionID", 2);

_initDefaultStateVariables(_stateContractAddr, _verifierContractAddr, CIRCUIT_ID, owner);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ contract CredentialAtomicQuerySigV2Validator is CredentialAtomicQueryV2Validator
_setInputToIndex("issuerClaimNonRevState", 9);
_setInputToIndex("timestamp", 10);

_setRequestParamToIndex("groupID", 0);
_setRequestParamToIndex("verifierID", 1);
_setRequestParamToIndex("nullifierSessionID", 2);

_initDefaultStateVariables(_stateContractAddress, _verifierContractAddr, CIRCUIT_ID, owner);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ contract CredentialAtomicQueryV3Validator is CredentialAtomicQueryValidatorBase
_setInputToIndex("timestamp", 12);
_setInputToIndex("isBJJAuthEnabled", 13);

_setRequestParamToIndex("groupID", 0);
_setRequestParamToIndex("verifierID", 1);
_setRequestParamToIndex("nullifierSessionID", 2);

_initDefaultStateVariables(_stateContractAddr, _verifierContractAddr, CIRCUIT_ID, owner);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {IRequestValidator} from "../../interfaces/IRequestValidator.sol";
import {IGroth16Verifier} from "../../interfaces/IGroth16Verifier.sol";
import {IState} from "../../interfaces/IState.sol";
import {PrimitiveTypeUtils} from "../../lib/PrimitiveTypeUtils.sol";
import {RequestValidatorBase} from "./RequestValidatorBase.sol";

error ChallengeShouldMatchTheSender();
error GistRootIsExpired();
Expand All @@ -21,20 +22,16 @@ error IssuerIsNotOnTheAllowedIssuersList();
*/
abstract contract CredentialAtomicQueryValidatorBase is
Ownable2StepUpgradeable,
IRequestValidator,
RequestValidatorBase,
ERC165
{
/// @dev Main storage structure for the contract
/// @custom:storage-location iden3.storage.CredentialAtomicQueryValidator
struct CredentialAtomicQueryValidatorBaseStorage {
mapping(string => IGroth16Verifier) _circuitIdToVerifier;
string[] _supportedCircuitIds;
IState state;
uint256 revocationStateExpirationTimeout;
uint256 proofExpirationTimeout;
uint256 gistRootExpirationTimeout;
mapping(string => uint256) _inputNameToIndex;
mapping(string => uint256) _requestParamNameToIndex;
}

// keccak256(abi.encode(uint256(keccak256("iden3.storage.CredentialAtomicQueryValidator")) - 1))
Expand Down Expand Up @@ -145,51 +142,6 @@ abstract contract CredentialAtomicQueryValidatorBase is
bytes calldata responseMetadata
) external view virtual returns (ResponseField[] memory);

/**
* @dev Get supported circuit ids
* @return ids Array of circuit ids supported
*/
function getSupportedCircuitIds() external view virtual returns (string[] memory ids) {
return _getCredentialAtomicQueryValidatorBaseStorage()._supportedCircuitIds;
}

/**
* @dev Get the verifier by circuit id
* @param circuitId Circuit id
* @return The verifier
*/
function getVerifierByCircuitId(
string memory circuitId
) public view virtual returns (IGroth16Verifier) {
return _getCredentialAtomicQueryValidatorBaseStorage()._circuitIdToVerifier[circuitId];
}

/**
* @dev Get the index of the public input of the circuit by name
* @param name Name of the public input
* @return Index of the public input
*/
function inputIndexOf(string memory name) public view virtual returns (uint256) {
uint256 index = _getCredentialAtomicQueryValidatorBaseStorage()._inputNameToIndex[name];
if (index == 0) {
revert InputNameNotFound();
}
return --index; // we save 1-based index, but return 0-based
}

/**
* @dev Get the index of the request param by name
* @param name Name of the request param
* @return Index of the request param
*/
function requestParamIndexOf(string memory name) public view override returns (uint256) {
uint256 index = _getCredentialAtomicQueryValidatorBaseStorage()._requestParamNameToIndex[
name
];
if (index == 0) revert RequestParamNameNotFound();
return --index; // we save 1-based index, but return 0-based
}

/**
* @dev See {IERC165-supportsInterface}.
*/
Expand All @@ -211,9 +163,9 @@ abstract contract CredentialAtomicQueryValidatorBase is
s.revocationStateExpirationTimeout = 1 hours;
s.proofExpirationTimeout = 1 hours;
s.gistRootExpirationTimeout = 1 hours;
s._supportedCircuitIds = [circuitId];
s._circuitIdToVerifier[circuitId] = IGroth16Verifier(_verifierContractAddr);
s.state = IState(_stateContractAddr);
_setGroth16Verifier(circuitId, IGroth16Verifier(_verifierContractAddr));

__Ownable_init(owner);
}

Expand Down Expand Up @@ -284,16 +236,4 @@ abstract contract CredentialAtomicQueryValidatorBase is
revert ChallengeShouldMatchTheSender();
}
}

function _setInputToIndex(string memory inputName, uint256 index) internal {
// increment index to avoid 0
_getCredentialAtomicQueryValidatorBaseStorage()._inputNameToIndex[inputName] = ++index;
}

function _setRequestParamToIndex(string memory requestParamName, uint256 index) internal {
// increment index to avoid 0
_getCredentialAtomicQueryValidatorBaseStorage()._requestParamNameToIndex[
requestParamName
] = ++index;
}
}
78 changes: 4 additions & 74 deletions contracts/validators/request/LinkedMultiQueryValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {IGroth16Verifier} from "../../interfaces/IGroth16Verifier.sol";
import {IRequestValidator} from "../../interfaces/IRequestValidator.sol";
import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {RequestValidatorBase} from "./RequestValidatorBase.sol";

error WrongCircuitID(string circuitID);
error InvalidQueryHash(uint256 expectedQueryHash, uint256 actualQueryHash);
error InvalidGroupID(uint256 groupID);
error TooManyQueries(uint256 operatorCount);
error InvalidGroth16Proof();

contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator, ERC165 {
contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, RequestValidatorBase, ERC165 {
// This should be limited to the real number of queries in which operator != 0
struct Query {
uint256[] claimPathKey;
Expand All @@ -36,33 +37,6 @@ contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator
bytes32 private constant NULLIFIERSESSIONID_NAME =
0x24cea8e4716dcdf091e4abcbd3ea617d9a5dd308b90afb5da0d75e56b3c0bc95;

/// @dev Main storage structure for the contract
/// @custom:storage-location iden3.storage.LinkedMultiQueryValidatorStorage
struct LinkedMultiQueryValidatorStorage {
mapping(string circuitName => IGroth16Verifier) _supportedCircuits;
string[] _supportedCircuitIds;
mapping(string => uint256) _requestParamNameToIndex;
mapping(string => uint256) _inputNameToIndex;
}

// keccak256(abi.encode(uint256(keccak256("iden3.storage.LinkedMultiQueryValidator")) - 1))
// & ~bytes32(uint256(0xff));
// solhint-disable-next-line const-name-snakecase
bytes32 private constant LinkedMultiQuer F438 yValidatorStorageLocation =
0x85875fc21d0742149175681df1689e48bce1484a73b475e15e5042650a2d7800;

/// @dev Get the main storage using assembly to ensure specific storage location
function _getLinkedMultiQueryValidatorStorage()
private
pure
returns (LinkedMultiQueryValidatorStorage storage $)
{
// solhint-disable-next-line no-inline-assembly
assembly {
$.slot := LinkedMultiQueryValidatorStorageLocation
}
}

struct PubSignals {
uint256 linkID;
uint256 merklized;
Expand All @@ -88,9 +62,7 @@ contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator
* @param owner Owner of the contract
*/
function initialize(address _groth16VerifierContractAddr, address owner) public initializer {
LinkedMultiQueryValidatorStorage storage $ = _getLinkedMultiQueryValidatorStorage();
$._supportedCircuits[CIRCUIT_ID] = IGroth16Verifier(_groth16VerifierContractAddr);
$._supportedCircuitIds.push(CIRCUIT_ID);
_setGroth16Verifier(CIRCUIT_ID, IGroth16Verifier(_groth16VerifierContractAddr));

_setInputToIndex("linkID", 0);
_setInputToIndex("merklized", 1);
Expand All @@ -105,10 +77,6 @@ contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator
);
}

_setRequestParamToIndex("groupID", 0);
_setRequestParamToIndex("verifierID", 1);
_setRequestParamToIndex("nullifierSessionID", 2);

__Ownable_init(owner);
}

Expand All @@ -129,8 +97,6 @@ contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator
// solhint-disable-next-line no-unused-vars
bytes calldata responseMetadata
) external view returns (IRequestValidator.ResponseField[] memory) {
LinkedMultiQueryValidatorStorage storage $ = _getLinkedMultiQueryValidatorStorage();

Query memory query = abi.decode(requestParams, (Query));
(
uint256[] memory inputs,
Expand All @@ -146,7 +112,7 @@ contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator
if (keccak256(bytes(query.circuitIds[0])) != keccak256(bytes(CIRCUIT_ID))) {
revert WrongCircuitID(query.circuitIds[0]);
}
if (!$._supportedCircuits[CIRCUIT_ID].verify(a, b, c, inputs)) {
if (!getVerifierByCircuitId(CIRCUIT_ID).verify(a, b, c, inputs)) {
revert InvalidGroth16Proof();
}

Expand Down Expand Up @@ -175,32 +141,6 @@ contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator
revert RequestParamNameNotFound();
}

/**
* @dev Get the index of the request param by name
* @param name Name of the request param
* @return Index of the request param
*/
function requestParamIndexOf(
string memory name
) public view virtual override returns (uint256) {
uint256 index = _getLinkedMultiQueryValidatorStorage()._requestParamNameToIndex[name];
if (index == 0) revert RequestParamNameNotFound();
return --index; // we save 1-based index, but return 0-based
}

/**
* @dev Get the index of the public input of the circuit by name
* @param name Name of the public input
* @return Index of the public input
*/
function inputIndexOf(string memory name) public view virtual returns (uint256) {
uint256 index = _getLinkedMultiQueryValidatorStorage()._inputNameToIndex[name];
if (index == 0) {
revert InputNameNotFound();
}
return --index; // we save 1-based index, but return 0-based
}

8266 /**
* @dev See {IERC165-supportsInterface}.
*/
Expand Down Expand Up @@ -275,14 +215,4 @@ contract LinkedMultiQueryValidator is Ownable2StepUpgradeable, IRequestValidator

return rfs;
}

function _setRequestParamToIndex(string memory requestParamName, uint256 index) internal {
// increment index to avoid 0
_getLinkedMultiQueryValidatorStorage()._requestParamNameToIndex[requestParamName] = ++index;
}

function _setInputToIndex(string memory inputName, uint256 index) internal {
// increment index to avoid 0
_getLinkedMultiQueryValidatorStorage()._inputNameToIndex[inputName] = ++index;
}
}
Loading
Loading
0