Skip to content

Commit

Permalink
Integration tests (#332)
Browse files Browse the repository at this point in the history
* LinkedMultiQuery small fixes

* Add more linked multi query tests

* Improve Linked Multi Query tests

* Scaffold verifier integration-test

* WIP

* Fix getValidatorVerification helpers method

* Fix lmk10 groth16 wrapper in constants

* Remove console from Verifier.sol
  • Loading branch information
AndriianChestnykh authored Jan 21, 2025
1 parent d952287 commit 43e6e8f
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Copyright 2017 Christian Reitwiessner
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Check failure on line 3 in contracts/lib/groth16-verifiers/Groth16VerifierLinkedMultiQuery10Wrapper.sol

View workflow job for this annotation

GitHub Actions / solhint

Line length must be no more than 120 but current length is 435
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

Check failure on line 4 in contracts/lib/groth16-verifiers/Groth16VerifierLinkedMultiQuery10Wrapper.sol

View workflow job for this annotation

GitHub Actions / solhint

Line length must be no more than 120 but current length is 129
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Check failure on line 5 in contracts/lib/groth16-verifiers/Groth16VerifierLinkedMultiQuery10Wrapper.sol

View workflow job for this annotation

GitHub Actions / solhint

Line length must be no more than 120 but current length is 463
//
// 2019 OKIMS
// ported to solidity 0.6
// fixed linter warnings
// added requiere error messages
//
//
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.10;

import {IGroth16Verifier} from "../../interfaces/IGroth16Verifier.sol";
import {Groth16VerifierLinkedMultiQuery10} from "./Groth16VerifierLinkedMultiQuery10.sol";

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

/**
* @dev Verify the circuit with the groth16 proof π=([πa]1,[πb]2,[πc]1).
* @param a πa element of the groth16 proof.
* @param b πb element of the groth16 proof.
* @param c πc element of the groth16 proof.
* @param input Public inputs of the circuit.
* @return r true if the proof is valid.
*/
function verify(
uint256[2] calldata a,
uint256[2][2] calldata b,
uint256[2] calldata c,
uint256[] calldata input
) public view returns (bool r) {
uint[PUBSIGNALS_LENGTH] memory pubSignals;

require(input.length == PUBSIGNALS_LENGTH, "expected array length is 22");

for (uint256 i = 0; i < PUBSIGNALS_LENGTH; i++) {
pubSignals[i] = input[i];
}

return this.verifyProof(a, b, c, pubSignals);
}
}
14 changes: 12 additions & 2 deletions contracts/test-helpers/Groth16VerifierValidatorStub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ pragma solidity 0.8.27;
import {IGroth16Verifier} from "../interfaces/IGroth16Verifier.sol";

contract Groth16VerifierValidatorStub is IGroth16Verifier {
bool private verifyResult;

constructor() {
verifyResult = true;
}

function verify(
uint256[2] calldata,
uint256[2][2] calldata,
uint256[2] calldata,
uint256[] calldata
) external pure returns (bool r) {
return true;
) external view returns (bool r) {
return verifyResult;
}

function stub_setVerifyResult(bool result) external {
verifyResult = result;
}
}
145 changes: 94 additions & 51 deletions contracts/validators/LinkedMultiQueryValidator.sol
Original file line number Diff line number Diff line change
@@ -1,110 +1,151 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {ICircuitValidator} from "../interfaces/ICircuitValidator.sol";
import {IGroth16Verifier} from "../interfaces/IGroth16Verifier.sol";
import {IRequestValidator} from "../interfaces/IRequestValidator.sol";
import {IState} from "../interfaces/IState.sol";
import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

contract LinkedMultiQueryValidator is IRequestValidator, Ownable2StepUpgradeable {
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 {
// This should be limited to the real number of queries in which operator != 0
struct Query {
uint256[] claimPathKey;
uint256[] operator; // when checking SD take operator from here
uint256[] slotIndex;
uint256[][] value;
uint256[] queryHash;
string[] circuitIds; // TODO should it be here that way?
string[] circuitIds;
uint256 groupID;
uint256 verifierID;
}

/// @dev Main storage structure for the contract
/// @custom:storage-location iden3.storage.LinkedMultiQueryValidatorBaseStorage
struct LinkedMultiQueryValidatorBaseStorage {
//TODO do we need this mapping?
/// @custom:storage-location iden3.storage.LinkedMultiQueryValidatorStorage
struct LinkedMultiQueryValidatorStorage {
mapping(string circuitName => IGroth16Verifier) _supportedCircuits;
string[] _supportedCircuitIds;
}

// keccak256(abi.encode(uint256(keccak256("iden3.storage.LinkedMultiQueryValidatorBaseStorage")) - 1))
// keccak256(abi.encode(uint256(keccak256("iden3.storage.LinkedMultiQueryValidator")) - 1))
// & ~bytes32(uint256(0xff));
bytes32 private constant LinkedMultiQueryValidatorBaseStorageLocation =
0x2a12018e5edfc1fb8de8bb271d40c512afd1e683a34fc602c9c8e5cfd4529700;
bytes32 private constant LinkedMultiQueryValidatorStorageLocation =
0x85875fc21d0742149175681df1689e48bce1484a73b475e15e5042650a2d7800;

/// @dev Get the main storage using assembly to ensure specific storage location
function _getLinkedMultiQueryValidatorBaseStorage()
function _getLinkedMultiQueryValidatorStorage()
private
pure
returns (LinkedMultiQueryValidatorBaseStorage storage $)
returns (LinkedMultiQueryValidatorStorage storage $)
{
assembly {
$.slot := LinkedMultiQueryValidatorBaseStorageLocation
$.slot := LinkedMultiQueryValidatorStorageLocation
}
}

function getRequestParams(
bytes calldata params
) external view override returns (IRequestValidator.RequestParams memory) {
Query memory query = abi.decode(params, (Query));
return IRequestValidator.RequestParams(query.groupID, query.verifierID, 0);
}

struct PubSignals {
uint256 linkID;
uint256 merklized;
uint256[] operatorOutput;
uint256[] circuitQueryHash;
uint256[10] operatorOutput;
uint256[10] circuitQueryHash;
}

string public constant VERSION = "1.0.0-beta";
string internal constant CIRCUIT_ID = "linkedMultiQuery10";
uint256 internal constant QUERIES_NUMBER = 10;
uint256 internal constant QUERIES_COUNT = 10;

/**
* @dev Returns the version of the contract
* @return The version of the contract
*/
function version() external view override returns (string memory) {
return VERSION;
}

function initialize(address _groth16VerifierContractAddr, address owner) public initializer {
LinkedMultiQueryValidatorBaseStorage storage $ = _getLinkedMultiQueryValidatorBaseStorage();
/**
* @dev Initialize the contract
* @param _groth16VerifierContractAddr Address of the verifier contract
* @param _stateContractAddr Address of the state contract
* @param owner Owner of the contract
*/
function initialize(
address _groth16VerifierContractAddr,
address _stateContractAddr,
address owner
) public initializer {
LinkedMultiQueryValidatorStorage storage $ = _getLinkedMultiQueryValidatorStorage();
$._supportedCircuits[CIRCUIT_ID] = IGroth16Verifier(_groth16VerifierContractAddr);
$._supportedCircuitIds.push(CIRCUIT_ID);
}

/**
* @dev Verify the proof with the supported method informed in the request query data
* packed as bytes and that the proof was generated by the sender.
* @param proof Proof packed as bytes to verify.
* @param data Request query data of the credential to verify.
* @param sender Sender of the proof.
* @param state State contract to get identities and gist states to check.
* @return Array of response fields as result.
*/
function verify(
bytes calldata proof,
bytes calldata data,
address sender,
IState state
) external returns (IRequestValidator.ResponseField[] memory) {
LinkedMultiQueryValidatorBaseStorage storage $ = _getLinkedMultiQueryValidatorBaseStorage();
) external view returns (IRequestValidator.ResponseField[] memory) {
LinkedMultiQueryValidatorStorage storage $ = _getLinkedMultiQueryValidatorStorage();

// 0. Parse query
Query memory query = abi.decode(data, (Query));

// 1. Parse public signals
(
uint256[] memory inputs,
uint256[2] memory a,
uint256[2][2] memory b,
uint256[2] memory c
) = abi.decode(proof, (uint256[], uint256[2], uint256[2][2], uint256[2]));
PubSignals memory pubSignals = _parsePubSignals(inputs);

PubSignals memory pubSignals = _parsePubSignals(inputs, QUERIES_NUMBER);

// 1. Verify circuit query hash for 10
// TODO check
$._supportedCircuits[CIRCUIT_ID].verify(a, b, c, inputs);
_checkQueryHash(query, pubSignals);
_checkGroupId(query.groupID);

return _getSpecialSignals(pubSignals, query);
if (keccak256(bytes(query.circuitIds[0])) != keccak256(bytes(CIRCUIT_ID))) {
revert WrongCircuitID(query.circuitIds[0]);
}
if (!$._supportedCircuits[CIRCUIT_ID].verify(a, b, c, inputs)) {
revert InvalidGroth16Proof();
}

return _getResponseFields(pubSignals, query);
}

error InvalidQueryHash(uint256 expectedQueryHash, uint256 actualQueryHash);
error InvalidGroupID(uint256 groupID);
/**
* @dev Decodes special request parameters from the request params
* do be used by upper level clients of this contract.
* @param params Request parameters packed as bytes.
* @return Special request parameters extracted from the request data.
*/
function getRequestParams(
bytes calldata params
) external view override returns (IRequestValidator.RequestParams memory) {
Query memory query = abi.decode(params, (Query));
return IRequestValidator.RequestParams(query.groupID, query.verifierID, 0);
}

/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return
interfaceId == type(IRequestValidator).interfaceId ||
super.supportsInterface(interfaceId);
}

function _checkGroupId(uint256 groupID) internal pure {
if (groupID == 0) {
Expand All @@ -113,34 +154,36 @@ contract LinkedMultiQueryValidator is IRequestValidator, Ownable2StepUpgradeable
}

function _checkQueryHash(Query memory query, PubSignals memory pubSignals) internal pure {
for (uint256 i = 0; i < query.operator.length; i++) {
if (query.queryHash.length > QUERIES_COUNT) {
revert TooManyQueries(query.queryHash.length);
}
for (uint256 i = 0; i < query.queryHash.length; i++) {
if (query.queryHash[i] != pubSignals.circuitQueryHash[i]) {
revert InvalidQueryHash(query.queryHash[i], pubSignals.circuitQueryHash[i]);
}
}
}

function _parsePubSignals(
uint256[] memory inputs,
uint256 queriesNumber
) internal pure returns (PubSignals memory) {
PubSignals memory pubSignals = PubSignals(
0,
0,
new uint256[](queriesNumber),
new uint256[](queriesNumber)
);
function _parsePubSignals(uint256[] memory inputs) internal pure returns (PubSignals memory) {
uint256[QUERIES_COUNT] memory opsOutput;
uint256[QUERIES_COUNT] memory queryHashes;
PubSignals memory pubSignals = PubSignals({
linkID: 0,
merklized: 0,
operatorOutput: opsOutput,
circuitQueryHash: queryHashes
});

pubSignals.linkID = inputs[0];
pubSignals.merklized = inputs[1];
for (uint256 i = 0; i < queriesNumber; i++) {
for (uint256 i = 0; i < QUERIES_COUNT; i++) {
pubSignals.operatorOutput[i] = inputs[2 + i];
pubSignals.circuitQueryHash[i] = inputs[2 + queriesNumber + i];
pubSignals.circuitQueryHash[i] = inputs[2 + QUERIES_COUNT + i];
}
return pubSignals;
}

function _getSpecialSignals(
function _getResponseFields(
PubSignals memory pubSignals,
Query memory query
) internal pure returns (ResponseField[] memory) {
Expand All @@ -157,7 +200,7 @@ contract LinkedMultiQueryValidator is IRequestValidator, Ownable2StepUpgradeable

uint256 m = 1;
for (uint256 i = 0; i < query.operator.length; i++) {
// TODO consider if can be more gas efficient
// TODO consider if can be more gas efficient. Check via gasleft() first
if (query.operator[i] == 16) {
rfs[m++] = ResponseField(
string(abi.encodePacked("operatorOutput", Strings.toString(i))),
Expand Down
Loading

0 comments on commit 43e6e8f

Please sign in to comment.