all files / contracts/verifierHelpers/ BaseVerifierHelper.sol

100% Statements 8/8
100% Branches 0/0
100% Functions 3/3
100% Lines 21/21
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                                                                                  35× 35× 35× 35× 35× 35×   35× 35×   35× 35×   35× 35×   35×     35×   35×                         331×       37× 37× 37× 37×     37×      
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
import {Unirep} from '../Unirep.sol';
import {IVerifier} from '../interfaces/IVerifier.sol';
 
/// @title BaseVerifierHelper
/// @dev https://developer.unirep.io/docs/contracts-api/verifiers/base-verifier-helper
contract BaseVerifierHelper {
    Unirep unirep;
    IVerifier verifier;
 
    uint256 public constant SNARK_SCALAR_FIELD =
        21888242871839275222246405745257275088548364400416034343698204186575808495617;
    uint256 public immutable chainid;
 
    struct EpochKeySignals {
        uint256 epochKey;
        uint256 stateTreeRoot;
        uint256 data;
        uint160 attesterId;
        uint48 epoch;
        uint48 chainId;
        uint8 nonce;
        bool revealNonce;
    }
 
    struct ReputationSignals {
        uint256 epochKey;
        uint256 stateTreeRoot;
        uint256 minRep;
        uint256 maxRep;
        uint256 graffiti;
        uint256 data;
        uint160 attesterId;
        uint48 epoch;
        uint48 chainId;
        uint8 nonce;
        bool revealNonce;
        bool proveMinRep;
        bool proveMaxRep;
        bool proveZeroRep;
        bool proveGraffiti;
    }
 
    error AttesterInvalid();
    error InvalidEpochKey();
    error InvalidProof();
    error InvalidEpoch();
    error InvalidStateTreeRoot(uint256 stateTreeRoot);
    error CallerInvalid();
    error ChainIdNotMatch(uint48 chainId);
 
    /// @dev https://developer.unirep.io/docs/contracts-api/verifiers/base-verifier-helper#decodeepochkeycontrol
    /// @param control The encoded control field
    /// @return nonce The epoch key nonce information in the control
    /// @return epoch The epoch information in the control
    /// @return attesterId The attester address in the control
    /// @return revealNonce Indicate if the user wants to reveal epoch key nonce in the control
    /// @return chainId The chain ID information in the control
    function decodeEpochKeyControl(
        uint256 control
    )
        public
        pure
        returns (
            uint8 nonce,
            uint48 epoch,
            uint160 attesterId,
            bool revealNonce,
            uint48 chainId
        )
    {
        uint8 nonceBits = 8;
        uint8 epochBits = 48;
        uint8 attesterIdBits = 160;
        uint8 revealNonceBit = 1;
        uint8 chainIdBits = 36;
        uint8 accBits = 0;
 
        nonce = uint8(shiftAndParse(control, accBits, nonceBits));
        accBits += nonceBits;
 
        epoch = uint48(shiftAndParse(control, accBits, epochBits));
        accBits += epochBits;
 
        attesterId = uint160(shiftAndParse(control, accBits, attesterIdBits));
        accBits += attesterIdBits;
 
        revealNonce = bool(
            shiftAndParse(control, accBits, revealNonceBit) != 0
        );
        accBits += revealNonceBit;
 
        chainId = uint48(shiftAndParse(control, accBits, chainIdBits));
    }
 
    /// @dev https://developer.unirep.io/docs/contracts-api/verifiers/base-verifier-helper#shiftAndParse
    /// @param data The raw data before parsed
    /// @param shiftBits The number of bits to be shifted
    /// @param variableBits The maximum number of bits of the output data
    /// @return parsedData The output data being shifted and parsed
    function shiftAndParse(
        uint256 data,
        uint8 shiftBits,
        uint8 variableBits
    ) public pure returns (uint256) {
        return (data >> shiftBits) & ((1 << variableBits) - 1);
    }
 
    constructor(Unirep _unirep, IVerifier _verifier) {
        unirep = _unirep;
        verifier = _verifier;
        uint256 id;
        assembly {
            id := chainid()
        }
        chainid = uint48(id);
    }
}