all files / contracts/verifierHelpers/ EpochKeyLiteVerifierHelper.sol

100% Statements 13/13
91.67% Branches 11/12
100% Functions 4/4
100% Lines 17/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                          10× 10× 10×   10×               10×                       10×                                            
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
import {Unirep} from '../Unirep.sol';
import {IVerifier} from '../interfaces/IVerifier.sol';
import {BaseVerifierHelper} from './BaseVerifierHelper.sol';
 
/// @title EpochKeyLiteVerifierHelper
/// @dev https://developer.unirep.io/docs/contracts-api/verifiers/epoch-key-lite-verifier-helper
contract EpochKeyLiteVerifierHelper is BaseVerifierHelper {
    constructor(
        Unirep _unirep,
        IVerifier _verifier
    ) BaseVerifierHelper(_unirep, _verifier) {}
 
    /// @dev https://developer.unirep.io/docs/contracts-api/verifiers/epoch-key-lite-verifier-helper#decodeepochkeylitesignals
    /// @param publicSignals The public signals of the snark proof
    /// @return signals The EpochKeySignals
    function decodeEpochKeyLiteSignals(
        uint256[] calldata publicSignals
    ) public pure returns (EpochKeySignals memory) {
        EpochKeySignals memory signals;
        signals.epochKey = publicSignals[1];
        signals.data = publicSignals[2];
        // now decode the control values
        (
            signals.nonce,
            signals.epoch,
            signals.attesterId,
            signals.revealNonce,
            signals.chainId
        ) = super.decodeEpochKeyControl(publicSignals[0]);
 
        if (signals.epochKey >= SNARK_SCALAR_FIELD) revert InvalidEpochKey();
        Iif (signals.attesterId >= type(uint160).max) revert AttesterInvalid();
 
        return signals;
    }
 
    /// @dev https://developer.unirep.io/docs/contracts-api/verifiers/epoch-key-lite-verifier-helper#verifyandcheck
    /// @param publicSignals The public signals of the snark proof
    /// @param proof The proof data of the snark proof
    /// @return signals The EpochKeySignals
    function verifyAndCheck(
        uint256[] calldata publicSignals,
        uint256[8] calldata proof
    ) public view returns (EpochKeySignals memory) {
        EpochKeySignals memory signals = decodeEpochKeyLiteSignals(
            publicSignals
        );
 
        if (!verifier.verifyProof(publicSignals, proof)) revert InvalidProof();
 
        uint48 epoch = unirep.attesterCurrentEpoch(signals.attesterId);
        if (signals.epoch > epoch) revert InvalidEpoch();
 
        if (signals.chainId != chainid) revert ChainIdNotMatch(signals.chainId);
 
        return signals;
    }
 
    /// @dev https://developer.unirep.io/docs/contracts-api/verifiers/epoch-key-lite-verifier-helper#verifyandcheckcaller
    /// @param publicSignals The public signals of the snark proof
    /// @param proof The proof data of the snark proof
    /// @return signals The EpochKeySignals
    function verifyAndCheckCaller(
        uint256[] calldata publicSignals,
        uint256[8] calldata proof
    ) public view returns (EpochKeySignals memory) {
        EpochKeySignals memory signals = verifyAndCheck(publicSignals, proof);
 
        if (signals.attesterId != uint160(msg.sender)) {
            revert CallerInvalid();
        }
 
        return signals;
    }
}