Skip to content
Verified contractPartialLGPL-3.0-only

BfvPkVerifier

Address0xfe6a2fc1d2fc3ed210889c58726db77440abebc6
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.28

 

Same code

3 more contracts

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
Yes, 1 runs
EVM version
paris
License
LGPL-3.0-only
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,507,856
Creation transaction
0xb8049e2707…22426fbc13
Main copy (most transactions)
0xba1854fd…7a15bdff

project/contracts/verifiers/bfv/BfvPkVerifier.sol

// SPDX-License-Identifier: LGPL-3.0-only
//
// This file is provided WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
pragma solidity 0.8.28;

import { IPkVerifier } from "../../interfaces/IPkVerifier.sol";
import { ICircuitVerifier } from "../../interfaces/ICircuitVerifier.sol";
import { CommitteeHashLib } from "../../lib/CommitteeHashLib.sol";

/**
 * @title BfvPkVerifier
 * @notice Verifies the DkgAggregator (EVM) proof produced by the recursive
 *         aggregation pipeline (node folds + C5/pk_aggregation verified
 *         internally) and binds it to the full on-chain call context.
 * @dev Used when the Interfold is configured with encryptionSchemeId
 *      keccak256("fhe.rs:BFV"). Constructor `h` must match the compiled
 *      DkgAggregator honest-set size (`lib::configs::default::H`).
 *
 *      Expected `publicInputs` layout for DkgAggregator EVM outputs:
 *        [0]                = expectedNodesFoldKeyHash  (VK anchor)
 *        [1]                = expectedC5KeyHash         (VK anchor)
 *        [2 .. 2+H)         = party_ids                 (H slots)
 *        [2+H]              = committee_hash_hi
 *        [3+H]              = committee_hash_lo
 *        [4+H .. 4+3H)      = expected_pk               (2*H slots)
 *        [4+3H]             = pk_commitment
 *        Total: expectedPublicInputsLen = 3*H + 6.
 *
 *      The two VK-hash slots are checked against contract immutables set at
 *      construction; this anchors the recursive aggregation trust and
 *      prevents a malicious aggregator from substituting a forged sub-VK.
 *
 *      NOTE — domain binding relaxation: wrapper-level chainId/deployment/e3Id
 *      binding requires a dedicated circuit public input. The current circuits
 *      do not expose such a slot. Full cryptographic enforcement tracked as
 *      future work. The caller-supplied `e3Id`, `committeeRoot`, and
 *      `sortedNodes` are preserved in the interface for forward compatibility.
 */
contract BfvPkVerifier is IPkVerifier {
    error InvalidCircuitVerifier(address verifier);
    error InvalidVerificationKeyHash();

    /// @dev `dkg_aggregator` return field count.
    uint256 internal constant DKG_RETURN_TAIL_LEN = 2;

    /// @notice Honest-set size `H` (`party_ids` length); must match the compiled DkgAggregator circuit.
    uint256 public immutable override h;

    /// @dev `publicInputs` index for `committee_hash_hi` (after VK anchors and `party_ids`).
    uint256 internal immutable committeeHashHiIdx;

    /// @dev `publicInputs` index for `committee_hash_lo`.
    uint256 internal immutable committeeHashLoIdx;

    /// @dev Total expected length of EVM public inputs for `dkg_aggregator`.
    uint256 internal immutable expectedPublicInputsLen;

    /// @dev Index of `pkCommitment` (last return field).
    uint256 internal immutable pkCommitmentIdx;

    /// @notice Underlying Honk verifier for the DkgAggregator circuit.
    ICircuitVerifier public immutable circuitVerifier;

    /// @notice keccak256 commitment to the node-fold recursive VK; expected at
    ///         `publicInputs[0]`. Provenance: `bb verify_key -b
    ///         circuits/bin/recursive_aggregation/node_fold/target/...` --
    ///         pinned at deployment time, must match the circuit version the
    ///         aggregator was built against.
    bytes32 public immutable expectedNodesFoldKeyHash;

    /// @notice keccak256 commitment to the C5 (pk_aggregation) recursive VK;
    ///         expected at `publicInputs[1]`. Same provenance as above.
    bytes32 public immutable expectedC5KeyHash;

    constructor(
        address _circuitVerifier,
        bytes32 _expectedNodesFoldKeyHash,
        bytes32 _expectedC5KeyHash,
        uint256 _h
    ) {
        require(_h > 0, "BfvPkVerifier: h=0");
        if (_circuitVerifier.code.length == 0) {
            revert InvalidCircuitVerifier(_circuitVerifier);
        }
        if (
            _expectedNodesFoldKeyHash == bytes32(0) ||
            _expectedC5KeyHash == bytes32(0)
        ) revert InvalidVerificationKeyHash();
        h = _h;
        committeeHashHiIdx = 2 + _h;
        committeeHashLoIdx = 3 + _h;
        expectedPublicInputsLen = (3 * _h) + 6;
        pkCommitmentIdx = expectedPublicInputsLen - 1;

        circuitVerifier = ICircuitVerifier(_circuitVerifier);
        expectedNodesFoldKeyHash = _expectedNodesFoldKeyHash;
        expectedC5KeyHash = _expectedC5KeyHash;
    }

    /// @inheritdoc IPkVerifier
    function verify(
        uint256 e3Id,
        uint256 committeeRoot,
        address[] calldata sortedNodes,
        bytes32 pkCommitment,
        bytes32 committeeHash,
        bytes calldata proof
    ) external view override returns (bool) {
        (bytes memory rawProof, bytes32[] memory publicInputs) = abi.decode(
            proof,
            (bytes, bytes32[])
        );

        if (publicInputs.length != expectedPublicInputsLen) {
            revert InvalidPublicInputsLength();
        }

        // Anchor recursive-aggregation trust to immutable VK hashes.
        if (publicInputs[0] != expectedNodesFoldKeyHash) {
            revert VkHashMismatch();
        }
        if (publicInputs[1] != expectedC5KeyHash) {
            revert VkHashMismatch();
        }

        // Bind to the on-chain committee hash (hi/lo split per Noir field convention).
        if (
            publicInputs[committeeHashHiIdx] !=
            CommitteeHashLib.hi(committeeHash)
        ) {
            revert DomainBindingMismatch();
        }
        if (
            publicInputs[committeeHashLoIdx] !=
            CommitteeHashLib.lo(committeeHash)
        ) {
            revert DomainBindingMismatch();
        }

        // Aggregated PK commitment is the last slot.
        if (publicInputs[pkCommitmentIdx] != pkCommitment) {
            revert PkCommitmentMismatch();
        }

        // Suppress unused-variable warnings for forward-compatibility params.
        // These will be used for circuit-level domain binding in a future circuit update.
        e3Id;
        committeeRoot;
        sortedNodes;

        // Bubble up as a revert instead of a silent `false`.
        if (!circuitVerifier.verify(rawProof, publicInputs)) {
            revert InvalidProof();
        }
        return true;
    }
}

project/contracts/interfaces/ICircuitVerifier.sol

// SPDX-License-Identifier: LGPL-3.0-only
//
// This file is provided WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
pragma solidity 0.8.28;

/**
 * @title ICircuitVerifier
 * @notice Interface for on-chain ZK circuit verifiers (e.g., DkgAggregatorVerifier, Honk verifiers)
 * @dev Standard interface matching the verification pattern used by Honk-generated verifiers.
 *      Set the circuit verifier address directly as the proofVerifier in a SlashPolicy.
 */
interface ICircuitVerifier {
    /// @notice Verify a ZK proof against public inputs
    /// @param _proof The raw proof bytes
    /// @param _publicInputs The public inputs to verify against
    /// @return True if the proof is valid
    function verify(
        bytes calldata _proof,
        bytes32[] calldata _publicInputs
    ) external view returns (bool);
}

project/contracts/interfaces/IPkVerifier.sol

// SPDX-License-Identifier: LGPL-3.0-only
//
// This file is provided WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
pragma solidity 0.8.28;

/**
 * @title IPkVerifier
 * @notice Interface for the DkgAggregator (EVM) proof verifier.
 * @dev The DkgAggregator circuit internally verifies the node-fold and C5
 *      (pk_aggregation) sub-proofs; this on-chain wrapper verifies the final
 *      EVM proof and enforces:
 *        - the immutable recursive sub-circuit VK hashes
 *        - the aggregated public-key commitment slot
 *        - a domain-binding slot binding the proof to
 *          (chainId, this, e3Id, committeeRoot, sortedNodes, pkCommitment)
 *      and reverts on any mismatch.
 */
interface IPkVerifier {
    /// @notice Required honest-party count compiled into the verifier.
    function h() external view returns (uint256);

    /// @notice Proof was structurally well-formed but the underlying honk
    ///         verifier rejected it. Used in place of a `bool false` return.
    error InvalidProof();
    /// @notice `publicInputs` is shorter than the layout the wrapper expects
    ///         (must hold at least the two VK-hash slots, the domain-binding slot
    ///         and the pk-commitment slot).
    error InvalidPublicInputsLength();
    /// @notice One of the recursive-aggregation sub-circuit VK hashes embedded
    ///         in the proof does not match the immutable value committed at
    ///         construction time.
    error VkHashMismatch();
    /// @notice The last public input does not equal the caller-supplied
    ///         `pkCommitment`.
    error PkCommitmentMismatch();
    /// @notice The domain-binding public-input slot does not equal the value
    ///         recomputed on-chain from the call context.
    error DomainBindingMismatch();

    /// @notice Verify a DkgAggregator EVM proof and bind it to the full
    ///         on-chain call context.
    /// @param e3Id Identifier of the E3 the committee was selected for.
    /// @param committeeRoot Ciphernode IMT root snapshotted at committee request time
    ///        (`CiphernodeRegistry.rootAt(e3Id)`).
    /// @param sortedNodes The on-chain-selected committee (`c.topNodes`), bound into
    ///        the domain-binding hash so a proof for one committee cannot be replayed
    ///        for another.
    /// @param pkCommitment Hash-based aggregated PK commitment the proof must attest to
    ///        (equals `publicInputs[publicInputs.length - 1]`).
    /// @param committeeHash `keccak256(abi.encodePacked(topNodes))` for the on-chain committee.
    /// @param proof ABI-encoded `(bytes rawProof, bytes32[] publicInputs)`.
    /// @return success Always `true` on success; the wrapper reverts on any failure.
    function verify(
        uint256 e3Id,
        uint256 committeeRoot,
        address[] calldata sortedNodes,
        bytes32 pkCommitment,
        bytes32 committeeHash,
        bytes calldata proof
    ) external view returns (bool success);
}

project/contracts/lib/CommitteeHashLib.sol

// SPDX-License-Identifier: LGPL-3.0-only
//
// This file is provided WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.

pragma solidity 0.8.28;

/**
 * @title CommitteeHashLib
 * @notice Canonical hash of ordered raw 20-byte committee addresses.
 * @dev Must match `e3_utils::committee_hash` (hi/lo split into two 128-bit limbs).
 */
library CommitteeHashLib {
    uint256 private constant _LO_MASK = (uint256(1) << 128) - 1;

    /// @notice `keccak256(concat(20-byte addresses))` for the ordered on-chain committee.
    /// @dev Must match `e3_utils::committee_hash::hash_committee_addresses`, which packs
    ///      each address as raw 20 bytes with no padding. NOTE: `abi.encodePacked(address[])`
    ///      pads each element to 32 bytes (left-padded), which does NOT match the off-chain
    ///      canonical encoding — so we build the 20*N byte buffer manually.
    function hash(address[] memory nodes) internal pure returns (bytes32) {
        uint256 n = nodes.length;
        bytes memory packed = new bytes(n * 20);
        for (uint256 i = 0; i < n; ++i) {
            bytes20 node = bytes20(nodes[i]);
            uint256 offset = i * 20;
            for (uint256 j = 0; j < 20; ++j) {
                packed[offset + j] = node[j];
            }
        }
        return keccak256(packed);
    }

    /// @notice High 128 bits of a committee hash (Noir public input `committee_hash_hi`).
    function hi(bytes32 committeeHash) internal pure returns (bytes32) {
        return bytes32(uint256(committeeHash) >> 128);
    }

    /// @notice Low 128 bits of a committee hash (Noir public input `committee_hash_lo`).
    function lo(bytes32 committeeHash) internal pure returns (bytes32) {
        return bytes32(uint256(committeeHash) & _LO_MASK);
    }
}
Compiler settingsShow
{
  "debug": {
    "revertStrings": "strip"
  },
  "metadata": {
    "bytecodeHash": "none"
  },
  "optimizer": {
    "runs": 1,
    "enabled": true
  },
  "evmVersion": "paris",
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.