// 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.27; import { IOpenVmReceiptVerifier } from "@interfold/contracts/contracts/interfaces/IOpenVmReceiptVerifier.sol"; import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; import { IE3Program } from "@interfold/contracts/contracts/interfaces/IE3Program.sol"; import { IInterfold } from "@interfold/contracts/contracts/interfaces/IInterfold.sol"; import { ICiphernodeRegistry } from "@interfold/contracts/contracts/interfaces/ICiphernodeRegistry.sol"; import { E3 } from "@interfold/contracts/contracts/interfaces/IE3.sol"; import { OpenVmComputeProof } from "@interfold/contracts/contracts/lib/OpenVmComputeProof.sol"; import { LazyIMTData, InternalLazyIMT } from "@zk-kit/lazy-imt.sol/InternalLazyIMT.sol"; import { SNARK_SCALAR_FIELD } from "@zk-kit/lazy-imt.sol/Constants.sol"; import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/EIP712.sol"; import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; import { IHonkVerifier } from "./interfaces/IHonkVerifier.sol"; import { IVotesToken } from "./interfaces/IVotesToken.sol"; import { IERC6372Clock } from "./interfaces/IERC6372Clock.sol"; import { IDataAvailabilityVerifier, IE3ProgramDataAvailability } from "@interfold/contracts/contracts/interfaces/IDataAvailabilityVerifier.sol"; import { IERC165 } from "@openzeppelin/contracts/utils/introspection/IERC165.sol"; interface IInterfoldProgramRegistry { function e3Programs(IE3Program e3Program) external view returns (bool); } interface IInterfoldRegistryView { function ciphernodeRegistry() external view returns (ICiphernodeRegistry); } /// @notice The BFV parameter blob Interfold passes to `validate` as `e3ProgramParams`. /// @dev Field order must match `encode_bfv_params` (`crates/fhe-params/src/encoding.rs`) and `encodeBfvParams` /// in `packages/interfold-contracts/scripts/protocol/values.ts` and `packages/interfold-sdk/src/utils.ts`. struct BfvParameters { uint256 degree; uint256 plaintextModulus; uint256[] moduli; string error1Variance; } contract CRISPProgram is IE3Program, IE3ProgramDataAvailability, IERC165, Ownable, EIP712 { using InternalLazyIMT for LazyIMTData; /// @notice Enum to represent credit modes enum CreditMode { /// @notice Everyone has constant credits CONSTANT, /// @notice Credits are custom (can be based on token balance, etc) CUSTOM } /// @notice Where the eligible voter set for a round comes from. /// @dev Two sources with opposite economics. TOKEN derives the electorate from balances at a /// snapshot: the coordinator enumerates holders, which is expensive and needs an indexer, but it /// is the only way to answer "everyone holding this token". BY_REQUESTER asks the requesting /// contract, which already knows its own membership — a game roster, an allowlisted cohort, a /// committee — so nothing is enumerated and no indexer is involved. /// /// Declared explicitly rather than inferred. A coordinator that probed every requester and /// silently fell back on failure would turn a broken census provider into a token vote with the /// wrong electorate, and nothing would error. /// /// Required, not optional: params must carry it. Making it defaultable would mean a caller that /// forgot it silently got token discovery, which is the same silent-wrong-electorate failure one /// level up. enum CensusMode { /// @notice Derived from token balances by the coordinator. The default. TOKEN, /// @notice Supplied by the requester via `getCensus(uint256 e3Id) returns (address[])`. BY_REQUESTER, /// @notice Read from the token by this contract, one input at a time. No list is enumerated /// and no root is posted, so there is no census producer to trust. `publishInput` calls /// `getPastVotes` for the slot and passes the result to the circuit as a public input, which /// is why this mode uses the `crisp_onchain` verifier rather than the `crisp` one. ONCHAIN } /// @notice State of one proof-bound input while its ciphertext becomes available. struct InputRecord { /// @notice Reserved tree index plus one. Zero means no proof was accepted for this input. uint40 indexPlusOne; /// @notice Whether the ciphertext has a verified data-availability receipt. bool published; } /// @notice Struct to store all data related to a voting round struct RoundData { uint256 merkleRoot; bytes32 paramsHash; /// @notice The proven ciphertext commitment of every input, keyed by slot and tree index. /// @dev Keyed by both so a parent lookup for the wrong slot returns zero and is refused: it /// costs no more storage than a single-key map and removes a separate same-slot check. /// /// A history rather than one commitment per slot. An input names the entry it extends, and /// this contract cannot tell whether the bytes published with an entry deserialize to the /// ciphertext its commitment describes — only the Secure Process can, once the input window /// closes. Keeping only the newest commitment would therefore let anyone leave a slot whose /// head nobody but they can open, and a slot that cannot be masked is a slot whose every later /// input is provably its owner voting again. With the history, an entry like that is simply /// never extended: the next input names the same parent, and masking continues. mapping(address slot => mapping(uint40 index => bytes32 commitment)) inputCommitment; /// @notice Every accepted input proof, keyed by {inputId}. /// @dev The key binds every value needed to reproduce the input leaf, so a receipt can publish /// only the exact tuple whose Noir proof was accepted in the first transaction. /// /// Also the replay guard. The proof constrains the commitment, not who submits it, so anyone /// who observes a committed input can resubmit the identical calldata and the proof still /// verifies. The tally would not change — the replay names the same parent as the original, /// which is no longer the head, so the Secure Process drops it — but every append counts toward /// `inputLimit`, so enough replays would reach the limit and deny the round. In one round the /// identifier and the leaf derive from the same tuple, so refusing a known identifier refuses /// exactly the byte-identical resubmissions: two genuinely distinct inputs differ in bytes, /// commitment, slot or parent. mapping(bytes32 inputId => InputRecord) inputs; /// @notice Inputs whose proof is accepted but whose Avail receipt is not yet verified. uint40 pendingInputCount; LazyIMTData votes; uint256 numOptions; CreditMode creditMode; CensusMode censusMode; /// @notice The token that voting power is read from. Used by `CensusMode.ONCHAIN` and by every /// `CreditMode.CUSTOM` round. address token; /// @notice The smallest voting power that may cast an input. Only used by /// `CensusMode.ONCHAIN`, where eligibility is checked per input instead of by a census. uint256 minVotingPower; /// @notice Credits given to each eligible voter under `CreditMode.CONSTANT`. uint256 credits; /// @notice The timepoint that voting power is read at, in the ERC-6372 clock units of the /// token. Recorded when the round is requested, so it is the same for every input. Set for /// `CensusMode.ONCHAIN` rounds and for `CreditMode.CUSTOM` rounds, where the divisor is sized /// against the total supply at this timepoint. Zero otherwise. uint48 snapshot; /// @notice Divides raw token voting power into the units the ballot is encoded in. Never /// zero for a `CreditMode.CUSTOM` round, and zero for a `CreditMode.CONSTANT` round, where no /// scaling happens. uint256 votingPowerDivisor; /// @notice The most inputs the round accepts. Every input (vote, update, mask) counts. /// @dev Set by {_initCredits}. A mask needs no signature, so any account can use up the limit. /// The contract cannot tell a mask from a vote, so it cannot count votes alone. uint256 inputLimit; } // Constants /// @notice Encryption scheme ID used for the CRISP program. bytes32 public constant ENCRYPTION_SCHEME_ID = keccak256("fhe.rs:BFV"); /// @notice The depth of the input Merkle tree. uint8 public constant TREE_DEPTH = 20; /// @notice Minimum time available to create new input commitments after a worst-case key setup. uint256 public constant MIN_VOTING_DURATION = 1 hours; /// @notice Number of leading plaintext coefficients that carry the tally. /// @dev Must stay aligned with `@crisp-e3/sdk` and `crisp_utils` (`MAX_MSG_NON_ZERO_COEFFS`). /// Coefficient `o` for `o < numOptions` is the total on option `o`; the rest of these /// coefficients, and the coefficients up to the BFV degree, are zero padding. uint256 constant MAX_MSG_NON_ZERO_COEFFS = 50; /// @notice Maximum number of vote options a round may configure. /// @dev Bounded by the Noir circuit, which asserts `num_options <= MAX_OPTIONS` /// (`circuits/lib/src/constants.nr`). A round above this accepts no ballot, because every /// vote proof fails. Must stay aligned with the SDK constant of the same name. uint256 constant MAX_VOTE_OPTIONS = 10; /// @notice The most inputs any round accepts. /// @dev Every input (vote, update, mask) adds at most one fresh ciphertext to the tally sum. The /// BFV parameters stay correct for a fixed number of additions (`SEARCH_Z` in /// `crates/fhe-params/src/constants.rs`); every secure parameter set CRISP runs on must allow at /// least this many. The insecure-64 test preset allows the same number. uint256 public constant MAX_INPUTS_PER_ROUND = 100_000; // State variables IInterfold public interfold; IOpenVmReceiptVerifier public openVmVerifier; bytes32 public imageId; /// @notice Verifies ballots for the census modes that prove membership of a Merkle tree. IHonkVerifier private immutable honkVerifier; /// @notice Verifies ballots for `CensusMode.ONCHAIN`, whose circuit has no Merkle inputs and /// takes voting power as a public input instead. IHonkVerifier private immutable onchainHonkVerifier; /// @notice Frozen receipt verifier for every large object accepted by this program. IDataAvailabilityVerifier public immutable dataAvailabilityVerifier; /// @notice Tail of the Interfold input window reserved for DA finalization. /// @dev New proofs close this many seconds before the protocol input deadline. Existing /// commitments can still receive a VectorX proof during the reserved tail. uint256 public immutable availabilityFinalizationWindow; /// @notice Service that confirms it durably received a ciphertext before its proof is accepted. /// @dev The later VectorX receipt remains the authority for data availability. This signature /// only prevents a caller from committing a hash while withholding the bytes from the service. address public immutable inputAvailabilitySigner; /// @notice Maximum lifetime of an input availability promise. /// @dev The service starts this period after it validates and stores the complete ciphertext. uint64 public constant INPUT_AVAILABILITY_ATTESTATION_TTL = 10 minutes; bytes32 public constant INPUT_AVAILABILITY_TYPEHASH = keccak256("InputAvailability(uint256 e3Id,bytes32 inputId,uint64 expiresAt)"); /// @notice The EIP-712 type of the message a voter signs to authorise one ballot. /// @dev The digest binds the signature to the round, the slot, and this exact ciphertext. The /// chain id and this contract address come from the EIP-712 domain, so a signature cannot be /// replayed onto another round, another slot, another ballot, or another deployment. bytes32 private constant BALLOT_TYPEHASH = keccak256("Ballot(uint256 e3Id,address slot,bytes32 ciphertextCommitment)"); // Mappings mapping(uint256 e3Id => RoundData) e3Data; // Errors error CallerNotAuthorized(); error E3AlreadyInitialized(); error InterfoldAddressZero(); error InterfoldAlreadyBound(); error InterfoldNotContract(); error ProgramNotRegistered(); error OpenVmVerifierAddressZero(); /// @notice The receipt identity differs from the one the OpenVM receipt verifier accepts. error InvalidImageId(); error InvalidHonkVerifier(); error EmptyInputData(); error InvalidNoirProof(); error InvalidMerkleRoot(); error MerkleRootAlreadySet(); error InvalidTallyLength(); /// @notice A requester-supplied census names who may vote, not how much each vote weighs, so it /// only has meaning when every voter carries the same credits. error CensusModeRequiresConstantCredits(); /// @notice `CensusMode.ONCHAIN` reads voting power from a token, so it cannot run without one /// that answers `getPastVotes`. error CensusModeRequiresToken(); /// @notice An `ONCHAIN` round with constant credits must grant a non-zero allowance, or it /// bounds every ballot to zero and can only accept masks. error InvalidCredits(); /// @notice The slot holds less voting power than the round requires, so it cannot be written to. /// @dev Raised for mask inputs as well as real votes. Under `CensusMode.ONCHAIN` this check /// replaces the Merkle membership proof that gates both branches in the other modes. error SlotNotEligible(); error InvalidCensusMode(); /// @notice A `CreditMode.CONSTANT` round grants credits that do not fit in the BFV plaintext /// modulus. error CreditsExceedPlaintextModulus(uint256 credits, uint256 plaintextModulus); /// @notice The requested voting-power divisor would let the scaled voting power sum to the /// plaintext modulus or more. error VotingPowerDivisorBelowMinimum(uint256 divisor, uint256 minimum); /// @notice A `CreditMode.CUSTOM` round needs a votes token that answers `getPastTotalSupply`. error CustomCreditsRequireVotesToken(); /// @notice The round already holds as many inputs as it accepts. error InputLimitReached(uint256 e3Id, uint256 limit); /// @notice An ONCHAIN round's floor is below one ballot unit. /// @dev `minVotingPower` must be at least the divisor, so every slot that clears the floor /// carries at least one unit of weight. Below that, a slot could be eligible to write yet scale /// to zero — able to publish, but unable to carry any weight, which is disenfranchisement that /// nothing on-chain would report. error MinVotingPowerBelowScale(); /// @notice An input names a parent this slot never wrote to. /// @dev Includes an index belonging to another slot, which the per-slot commitment map reads as /// absent. Name no parent instead when there is nothing to extend. error UnknownParentInput(uint40 parentIndex); error InputAlreadyCommitted(bytes32 inputId); error InputNotCommitted(bytes32 inputId); error InvalidInputAvailabilityAttestation(); error InputAvailabilityAttestationExpired(uint64 expiresAt); error InputAvailabilitySignerAddressZero(); error InputAvailabilityPending(uint40 count); error SlotIsEmpty(); error MerkleRootNotSet(); error InvalidNumOptions(); error InputDeadlinePassed(uint256 e3Id, uint256 deadline); error InputCommitmentDeadlinePassed(uint256 e3Id, uint256 deadline); error InputWindowTooShort(uint256 e3Id, uint256 duration, uint256 required); error VotingWindowTooShort(uint256 e3Id, uint256 votingStartsAt, uint256 commitmentDeadline, uint256 required); error VotingStartsBeforeKeyDeadline(uint256 e3Id, uint256 earliestStart, uint256 actualStart); /// @notice The compute window leaves no budget for a late availability receipt. /// @dev ZEN2-02: a receipt that arrives after the compute deadline can never finalize, and the /// round cannot conclude while its input stays pending. error ComputeWindowTooShort(uint256 e3Id, uint256 computeWindow, uint256 required); error KeyNotPublished(uint256 e3Id); error E3NotAssignedToProgram(uint256 e3Id); error E3NotAcceptingInputs(uint256 e3Id); error InvalidComputeContext(); error InvalidDataAvailabilityVerifier(); error DataAvailabilityHashMismatch(bytes32 expected, bytes32 actual); error ZeroEncryptedVoteHash(); // Events event InterfoldBound(address indexed interfold); /// @notice A valid ballot proof was accepted before its ciphertext DA receipt was ready. event InputCommitted( uint256 indexed e3Id, bytes32 indexed inputId, address indexed slotAddress, bytes32 encryptedVoteCommitment, bytes32 encryptedVoteHash, uint40 parentIndexPlusOne, uint40 index ); /// @notice A committed ciphertext received a verified data-availability receipt. /// @dev The event carries the values needed to retrieve and validate the exact bytes. The slot, /// commitment, hash, parent, and reserved index were already exposed by {InputCommitted}. event InputPublished( uint256 indexed e3Id, address indexed slotAddress, bytes32 encryptedVoteCommitment, bytes32 encryptedVoteHash, uint32 availabilityBlock, uint128 availabilityLeafIndex, uint256 index, uint40 parentIndexPlusOne ); /// @notice Initialize the contract without an Interfold controller. /// @dev The owner binds the controller after Interfold registers this program. /// @param _initialOwner The account that can configure and bind this program. /// @param _openVmVerifier The OpenVM receipt verifier address /// @param _honkVerifier The honk verifier address /// @param _imageId The receipt identity: `imageId()` of the OpenVM receipt verifier, which derives it /// from the Halo2 verifier and the guest's executable and VM commitments. The constructor refuses /// any other value, because `verify` would then reject every proof. constructor( address _initialOwner, IOpenVmReceiptVerifier _openVmVerifier, IHonkVerifier _honkVerifier, IHonkVerifier _onchainHonkVerifier, IDataAvailabilityVerifier _dataAvailabilityVerifier, uint256 _availabilityFinalizationWindow, address _inputAvailabilitySigner, bytes32 _imageId ) Ownable(_initialOwner) EIP712("CRISP", "1") { if (address(_openVmVerifier) == address(0)) revert OpenVmVerifierAddressZero(); if (_imageId != _openVmVerifier.imageId()) revert InvalidImageId(); if (address(_honkVerifier) == address(0)) revert InvalidHonkVerifier(); if (address(_onchainHonkVerifier) == address(0)) revert InvalidHonkVerifier(); if (address(_dataAvailabilityVerifier).code.length == 0) revert InvalidDataAvailabilityVerifier(); openVmVerifier = _openVmVerifier; honkVerifier = _honkVerifier; onchainHonkVerifier = _onchainHonkVerifier; dataAvailabilityVerifier = _dataAvailabilityVerifier; availabilityFinalizationWindow = _availabilityFinalizationWindow; if (_inputAvailabilitySigner == address(0)) revert InputAvailabilitySignerAddressZero(); inputAvailabilitySigner = _inputAvailabilitySigner; imageId = _imageId; } /// @notice Bind this program to its permanent Interfold controller. /// @dev Interfold must register this program before the owner calls this function. /// @param _interfold The Interfold controller that registered this program. function bindInterfold(IInterfold _interfold) external onlyOwner { if (address(interfold) != address(0)) revert InterfoldAlreadyBound(); if (address(_interfold) == address(0)) revert InterfoldAddressZero(); if (address(_interfold).code.length == 0) revert InterfoldNotContract(); if (!IInterfoldProgramRegistry(address(_interfold)).e3Programs(IE3Program(address(this)))) { revert ProgramNotRegistered(); } interfold = _interfold; emit InterfoldBound(address(_interfold)); } /// @notice The digest a voter signs to authorise one ballot. /// @dev Computed for every input, and for mask inputs as well as real votes. The circuit ignores /// it on the mask branch, but the contract must not skip it: a digest that were computed only /// for real votes would let an observer tell the two apart on-chain, which is exactly what mask /// inputs exist to prevent. /// @param e3Id The E3 the ballot belongs to. /// @param slot The slot address the ballot is written to. /// @param ciphertextCommitment The commitment to the ballot ciphertext. /// @return The EIP-712 digest. function ballotDigest(uint256 e3Id, address slot, bytes32 ciphertextCommitment) public view returns (bytes32) { return _hashTypedDataV4(keccak256(abi.encode(BALLOT_TYPEHASH, e3Id, slot, ciphertextCommitment))); } /// @notice Sets the Merkle root for an E3 program. Can only be set once. /// @param _e3Id The E3 program ID /// @param _root The Merkle root to set. function setMerkleRoot(uint256 _e3Id, uint256 _root) external onlyOwner { if (_root == 0) revert InvalidMerkleRoot(); if (e3Data[_e3Id].merkleRoot != 0) revert MerkleRootAlreadySet(); e3Data[_e3Id].merkleRoot = _root; } /// @notice Set the OpenVM receipt verifier and take the receipt identity it accepts. /// @dev Both values are application state, not protocol state. Interfold snapshots the protocol /// ciphertext verifier for each E3 at request time, and that verifier's own `imageId` is /// immutable, so this change cannot replace a computation the protocol already accepted. It can /// still break an E3 that is in flight: `verify` would then require an identity that the receipt /// does not have, the round would fail as a compute timeout, and `FailurePayerLib` bills that to /// the requester. Change it only between rounds. /// @param _openVmVerifier The new OpenVM receipt verifier address function setOpenVmVerifier(IOpenVmReceiptVerifier _openVmVerifier) external onlyOwner { if (address(_openVmVerifier) == address(0)) revert OpenVmVerifierAddressZero(); openVmVerifier = _openVmVerifier; imageId = _openVmVerifier.imageId(); } /// @notice Get the params hash for an E3 program /// @param e3Id The E3 program ID /// @return The params hash function getParamsHash(uint256 e3Id) public view returns (bytes32) { return e3Data[e3Id].paramsHash; } /// @notice Get the details about an E3 such as the merkle root of the census /// @dev RoundData cannot be returned directly as it contains nested mappings /// @param e3Id The E3 program ID /// @return merkleRoot The census merkle root /// @return paramsHash The hash of the E3 program params /// @return numOptions The number of vote options /// @return creditMode The credit mode for the round /// @return inputRoot The current root of the input (votes) merkle tree /// @return numberOfVotes The number of leaves in the input merkle tree function getRoundData( uint256 e3Id ) public view returns (uint256 merkleRoot, bytes32 paramsHash, uint256 numOptions, CreditMode creditMode, uint256 inputRoot, uint40 numberOfVotes) { RoundData storage round = e3Data[e3Id]; merkleRoot = round.merkleRoot; paramsHash = round.paramsHash; numOptions = round.numOptions; creditMode = round.creditMode; inputRoot = round.votes._root(); numberOfVotes = round.votes.numberOfLeaves; } /// @notice The divisor applied to raw token voting power in a `CreditMode.CUSTOM` round. /// @dev A client must divide by exactly this before proving. For an ONCHAIN round the contract /// passes the scaled value to the circuit as public input 4 and the proof is checked against /// it; for a TOKEN round the coordinator scales every census leaf by it. Zero for a /// `CreditMode.CONSTANT` round, where no scaling happens. /// @param e3Id The E3 to look up. /// @return The divisor recorded at validation. function votingPowerDivisorOf(uint256 e3Id) external view returns (uint256) { return e3Data[e3Id].votingPowerDivisor; } /// @notice The timepoint at which a round reads voting power, in the ERC-6372 clock units of its token. /// @dev A coordinator must read a CUSTOM-credit census at exactly this timepoint: the divisor is /// sized against the total supply here, so balances read at another timepoint can sum past it. /// Zero for a round that reads no voting power. /// @param e3Id The E3 to look up. /// @return The snapshot recorded at validation. function snapshotOf(uint256 e3Id) external view returns (uint48) { return e3Data[e3Id].snapshot; } /// @notice The voting power a slot may spend in an ONCHAIN round, in ballot units. /// @dev The value `publishInput` will hand the circuit as public input 4, computed by the same /// contract that will check the proof. A client must prove against exactly this: recomputing it /// off-chain means re-deriving the snapshot, the divisor and the rounding, and any drift only /// shows up as an opaque verifier failure. Returns 0 for a round that is not ONCHAIN, where the /// bound comes from the census leaf instead. /// /// Does not apply the eligibility floor — it answers "how much weight", not "may this slot /// write". `publishInput` still enforces the floor. /// @param e3Id The round. /// @param slot The slot address. /// @return The spendable voting power, in ballot units. function votingPowerOf(uint256 e3Id, address slot) external view returns (uint256) { RoundData storage round = e3Data[e3Id]; if (round.censusMode != CensusMode.ONCHAIN) return 0; return _ballotPower(round, IVotesToken(round.token).getPastVotes(slot, round.snapshot)); } /// @notice The census source a round was requested with. /// @dev A separate getter rather than a sixth return value on `getRoundData`, whose tuple is /// already consumed by the server and the SDK — widening it would break them for a field most /// callers do not want. /// @param e3Id The E3 to look up. /// @return The census mode recorded at validation. function censusModeOf(uint256 e3Id) external view returns (CensusMode) { return e3Data[e3Id].censusMode; } /// @inheritdoc IE3Program function validate( uint256 e3Id, uint256, bytes calldata e3ProgramParams, bytes calldata, bytes calldata customParams ) external returns (bytes32) { if (msg.sender != address(interfold) && msg.sender != owner()) revert CallerNotAuthorized(); if (e3Data[e3Id].paramsHash != bytes32(0)) revert E3AlreadyInitialized(); // Interfold stores the provisional E3 and its selected program before it calls `validate`. // Read that record and refuse an E3 that Interfold assigned to a different program. Without // this check the owner can create parallel CRISP round state for another program's E3. E3 memory e3 = interfold.getE3(e3Id); if (address(e3.e3Program) != address(this)) revert E3NotAssignedToProgram(e3Id); // Delegated to its own frame: `validate` is close to the stack limit. _initRound(e3Id, customParams, abi.decode(e3ProgramParams, (BfvParameters)).plaintextModulus); _validateInputTiming(e3Id, e3); e3Data[e3Id].paramsHash = keccak256(e3ProgramParams); // Initialize the votes Merkle tree for this E3 ID. e3Data[e3Id].votes._init(TREE_DEPTH); return ENCRYPTION_SCHEME_ID; } /// @inheritdoc IERC165 /// @dev Interfold probes these interfaces before it registers a program. function supportsInterface(bytes4 interfaceId) external pure returns (bool) { return interfaceId == type(IE3Program).interfaceId || interfaceId == type(IE3ProgramDataAvailability).interfaceId || interfaceId == type(IERC165).interfaceId; } /// @notice Return the earliest voting start under the current committee timeouts. /// @dev The E3 request snapshots these settings. Callers should add time for their transaction /// to be mined when they choose a fixed start date. function earliestVotingStart() external view returns (uint256) { return block.timestamp + _committeeReservation(interfold.getTimeoutConfig()); } /// @notice The worst-case time from an E3 request to its published committee key. /// @param timeouts The timeout configuration to apply. function _committeeReservation(IInterfold.E3TimeoutConfig memory timeouts) internal view returns (uint256) { ICiphernodeRegistry registry = IInterfoldRegistryView(address(interfold)).ciphernodeRegistry(); return registry.randomnessRequestTimeout() + registry.sortitionSubmissionWindow() + timeouts.dkgWindow; } /// @notice Refuse a round that starts before the worst-case key deadline, ends before one hour /// of voting, or leaves no budget for a late availability receipt. /// @dev Interfold stores the E3 and its timeout snapshot before calling {validate}. Read those /// exact values instead of duplicating deployment-time settings. A zero finalization window is /// the synchronous local mock and keeps its short test rounds. function _validateInputTiming(uint256 e3Id, E3 memory e3) internal view { if (availabilityFinalizationWindow == 0) return; IInterfold.E3TimeoutConfig memory timeouts = interfold.getE3TimeoutConfig(e3Id); uint256 latestKeyAt = e3.requestBlock + _committeeReservation(timeouts); uint256 votingStartsAt = e3.inputWindow[0]; if (votingStartsAt < latestKeyAt) revert VotingStartsBeforeKeyDeadline(e3Id, latestKeyAt, votingStartsAt); uint256 commitmentDeadline = _commitmentDeadline(e3Id, e3); if (commitmentDeadline < votingStartsAt + MIN_VOTING_DURATION) { revert VotingWindowTooShort(e3Id, votingStartsAt, commitmentDeadline, MIN_VOTING_DURATION); } // ZEN2-02: `finalizeInput` and `verify` share one cutoff. `_finalizationE3` refuses a receipt // after the compute deadline, and `verify` refuses output while an input stays pending, so an // input whose availability receipt arrives late leaves the round unable to finalize it and // unable to conclude without it. The round then fails as a requester-paid `ComputeTimeout`. // The compute window is the whole recovery time after the inputs close, because Interfold // derives `computeDeadline` from `inputWindow[1]`. Require at least one more finalization // window there, so a receipt that misses the commitment deadline still has the same budget it // gets inside the input window. This rejects the round at request time, before a fee is paid. if (timeouts.computeWindow < availabilityFinalizationWindow) { revert ComputeWindowTooShort(e3Id, timeouts.computeWindow, availabilityFinalizationWindow); } } /// @notice Decode the round configuration and record it. /// @dev One decode, every field required. `censusMode` is read as a uint and range-checked /// rather than decoded straight into the enum, so an unrecognised value gives a named error /// instead of a bare panic. /// @param e3Id The E3 being configured. /// @param customParams The ABI-encoded round configuration. /// @param plaintextModulus The BFV plaintext modulus `t` of the round. function _initRound(uint256 e3Id, bytes calldata customParams, uint256 plaintextModulus) internal { ( address token, uint256 minVotingPower, uint256 numOptions, CreditMode creditMode, uint256 credits, uint256 rawCensusMode, uint256 requestedDivisor ) = abi.decode(customParams, (address, uint256, uint256, CreditMode, uint256, uint256, uint256)); // The circuit asserts `num_options <= MAX_OPTIONS`, so a round configured above it accepts no // ballot at all. Reject at request time rather than stranding a round nobody can vote in. if (numOptions < 2 || numOptions > MAX_VOTE_OPTIONS) revert InvalidNumOptions(); if (rawCensusMode > uint256(type(CensusMode).max)) revert InvalidCensusMode(); CensusMode censusMode = CensusMode(rawCensusMode); // Rejected here rather than by the coordinator, so a combination that can never work costs // nothing: this reverts in the same transaction that requests the E3, before any fee is paid. // For the same reason {_tokenSnapshot} refuses an ONCHAIN round without a votes token. if (censusMode == CensusMode.BY_REQUESTER && creditMode != CreditMode.CONSTANT) { revert CensusModeRequiresConstantCredits(); } // An ONCHAIN round hands `credits` to the circuit as the voting-power bound, so zero credits // bound every ballot to zero: only a mask would be accepted, and the round would tally // nothing. Checked for ONCHAIN only — the Merkle modes take the bound from the census leaf, // where `credits` never reaches the circuit and the contract has nothing to check. if (censusMode == CensusMode.ONCHAIN && creditMode == CreditMode.CONSTANT && credits == 0) { revert InvalidCredits(); } RoundData storage round = e3Data[e3Id]; // we need to know the number of options for decoding the tally round.numOptions = numOptions; // we want to save the credit mode so it can be verified on chain by everyone round.creditMode = creditMode; // recorded so anyone can verify which electorate the round was requested against round.censusMode = censusMode; round.token = token; round.minVotingPower = minVotingPower; round.credits = credits; _initCredits(round, requestedDivisor, plaintextModulus); } /// @notice Record how the round weighs a ballot and how many inputs it accepts, so every /// option total stays below the plaintext modulus. /// @dev A decrypted coefficient is exact only below `t`, the plaintext modulus. The tally holds /// one total per option, so the bound is on the sum of all weights in the round: /// /// - CONSTANT credits: each input carries at most `credits` weight, so `(t - 1) / credits` /// inputs keep the sum below `t`. No divisor is recorded. /// - CUSTOM credits: the scaled voting power of every holder sums to at most /// `totalSupply / divisor`, so a divisor of at least `totalSupply / t + 1` keeps the sum below /// `t`. The supply is read at the snapshot, the timepoint every voting-power read uses. This /// relies on the token: its past votes over all accounts must not sum above its past supply, /// which ERC20Votes guarantees. /// /// The snapshot is taken here rather than supplied by the requester. This function runs in the /// transaction that requests the E3, so `clock() - 1` is the last finalized timepoint of the /// round, and a requester cannot name a timepoint that suits it. Recording it once also makes /// every input of the round read the same electorate. /// @param round The round being configured. Its census mode, credit mode, token, credits and /// floor are already recorded. /// @param requestedDivisor The divisor the requester asked for, or zero for the minimum. Ignored /// by a CONSTANT round. /// @param plaintextModulus The BFV plaintext modulus `t`. function _initCredits(RoundData storage round, uint256 requestedDivisor, uint256 plaintextModulus) internal { bool onchain = round.censusMode == CensusMode.ONCHAIN; if (round.creditMode == CreditMode.CONSTANT) { uint256 credits = round.credits; // One ballot of `t` credits or more wraps an option total on its own; zero credits carry no weight. if (credits != 0 && credits >= plaintextModulus) revert CreditsExceedPlaintextModulus(credits, plaintextModulus); uint256 limit = credits == 0 ? MAX_INPUTS_PER_ROUND : (plaintextModulus - 1) / credits; round.inputLimit = limit < MAX_INPUTS_PER_ROUND ? limit : MAX_INPUTS_PER_ROUND; // A CONSTANT round hands the circuit `credits`, but ONCHAIN still reads eligibility from // the token at the snapshot. if (onchain) round.snapshot = _tokenSnapshot(round.token, true); return; } uint48 snapshot = _tokenSnapshot(round.token, onchain); uint256 divisor; // Relies on the code check in {_tokenSnapshot}. try IVotesToken(round.token).getPastTotalSupply(snapshot) returns (uint256 supply) { uint256 minimum = supply / plaintextModulus + 1; divisor = requestedDivisor == 0 ? minimum : requestedDivisor; if (divisor < minimum) revert VotingPowerDivisorBelowMinimum(divisor, minimum); } catch { revert CustomCreditsRequireVotesToken(); } // Where the floor and the divisor meet, the floor is raw and the bound is scaled, so they only // agree when the floor is worth at least one ballot unit. Requiring it here means every slot // that passes `_eligibility` carries weight, and it costs nothing: this reverts in the // transaction that requests the E3, not per input after the fee is paid. It also keeps masks // working — they run the same eligibility check as real votes, so a slot that scaled to zero // could not be masked without revealing which inputs were masks. A Merkle round takes its // floor from the census, so the check applies to ONCHAIN only. if (onchain && round.minVotingPower < divisor) revert MinVotingPowerBelowScale(); round.snapshot = snapshot; round.votingPowerDivisor = divisor; round.inputLimit = MAX_INPUTS_PER_ROUND; } /// @notice Resolve the snapshot of a round that reads voting power from a token. /// @dev The code check comes first: a call to a codeless address succeeds with empty return data, /// and the resulting decode failure escapes `try`, so an EOA would give a bare panic instead of a /// named error. /// /// An ONCHAIN round also probes the call every input makes. `_previousTimepoint` falls back to /// block numbers when `clock()` is missing, so an address that is not a votes token would pass /// validation and every `publishInput` would revert after the fee is paid. /// @param token The token voting power is read from. /// @param onchain Whether the round is `CensusMode.ONCHAIN`, which names its own errors. /// @return The last finalized timepoint of the token. function _tokenSnapshot(address token, bool onchain) internal view returns (uint48) { if (token.code.length == 0) { if (onchain) revert CensusModeRequiresToken(); revert CustomCreditsRequireVotesToken(); } uint48 snapshot = _previousTimepoint(token); if (onchain) { try IVotesToken(token).getPastVotes(address(0), snapshot) returns (uint256) {} catch { revert CensusModeRequiresToken(); } } return snapshot; } /// @inheritdoc IE3Program /// @dev This is the first transaction of the two-step input flow. It verifies the ballot but /// reserves its tree leaf and index immediately. The ciphertext is published separately and any /// account calls {finalizeInput} after a valid data-availability receipt exists. function publishInput(uint256 e3Id, bytes memory data) external { E3 memory e3 = _commitmentE3(e3Id); if (data.length == 0) revert EmptyInputData(); ( bytes memory noirProof, address slotAddress, bytes32 encryptedVoteCommitment, bytes32 encryptedVoteHash, uint40 parentIndexPlusOne, uint64 availabilityAttestationExpiresAt, bytes memory availabilityAttestation ) = abi.decode(data, (bytes, address, bytes32, bytes32, uint40, uint64, bytes)); if (block.timestamp >= availabilityAttestationExpiresAt) { revert InputAvailabilityAttestationExpired(availabilityAttestationExpiresAt); } bytes32 id = _verifyInputProof(e3Id, e3, noirProof, slotAddress, encryptedVoteCommitment, encryptedVoteHash, parentIndexPlusOne); if ( ECDSA.recover(inputAvailabilityDigest(e3Id, id, availabilityAttestationExpiresAt), availabilityAttestation) != inputAvailabilitySigner ) { revert InvalidInputAvailabilityAttestation(); } // Reserve the leaf and index now, not after VectorX finalizes. A later vote or mask can then // name this input as its parent instead of every pending input competing as a first write. uint40 voteIndex = _processVote(e3Id, id, slotAddress, encryptedVoteCommitment, encryptedVoteHash, parentIndexPlusOne); emit InputCommitted(e3Id, id, slotAddress, encryptedVoteCommitment, encryptedVoteHash, parentIndexPlusOne, voteIndex); } /// @notice Publish the DA receipt for a previously proven and reserved input. /// @dev Permissionless so the voter does not need to remain online while VectorX finalizes the /// Avail publication. The input identifier binds the receipt to the exact proof accepted by /// {publishInput}; changing the slot, commitment, content hash, or parent selects no commitment. function finalizeInput( uint256 e3Id, address slotAddress, bytes32 encryptedVoteCommitment, bytes32 encryptedVoteHash, uint40 parentIndexPlusOne, bytes calldata availabilityProof ) external { _finalizationE3(e3Id); bytes32 id = inputId(e3Id, encryptedVoteHash, encryptedVoteCommitment, slotAddress, parentIndexPlusOne); InputRecord storage input = e3Data[e3Id].inputs[id]; if (input.indexPlusOne == 0 || input.published) revert InputNotCommitted(id); IDataAvailabilityVerifier.DataReference memory availabilityReceipt = _verifyAvailability(encryptedVoteHash, availabilityProof); input.published = true; e3Data[e3Id].pendingInputCount--; uint40 voteIndex = input.indexPlusOne - 1; emit InputPublished( e3Id, slotAddress, encryptedVoteCommitment, encryptedVoteHash, availabilityReceipt.blockNumber, availabilityReceipt.leafIndex, voteIndex, parentIndexPlusOne ); } /// @notice Checks a ballot before its proof commitment is sent on chain. /// @dev This checks the same proof and timing conditions as {publishInput} without modifying /// state. The service does not pay an Avail fee until the commitment transaction is confirmed. function validateInputProof( uint256 e3Id, bytes calldata noirProof, address slotAddress, bytes32 encryptedVoteCommitment, bytes32 encryptedVoteHash, uint40 parentIndexPlusOne ) external view returns (bool) { E3 memory e3 = _commitmentE3(e3Id); _verifyInputProof(e3Id, e3, noirProof, slotAddress, encryptedVoteCommitment, encryptedVoteHash, parentIndexPlusOne); return true; } /// @notice Last timestamp before which a new input proof can be committed. /// @dev The deadline is exclusive. At the exact timestamp, the reserved finalization tail has /// started and no new proof is accepted. function inputCommitmentDeadline(uint256 e3Id) external view returns (uint256) { return _commitmentDeadline(e3Id, interfold.getE3(e3Id)); } /// @notice The commitment deadline of an E3 whose record is already loaded. /// @dev Refuses an input window no longer than the finalization tail, which leaves no time to /// commit an input. function _commitmentDeadline(uint256 e3Id, E3 memory e3) internal view returns (uint256) { uint256 duration = e3.inputWindow[1] - e3.inputWindow[0]; if (duration <= availabilityFinalizationWindow) { revert InputWindowTooShort(e3Id, duration, availabilityFinalizationWindow + 1); } return e3.inputWindow[1] - availabilityFinalizationWindow; } function _keyPublishedE3(uint256 e3Id) internal view returns (E3 memory e3) { e3 = interfold.getE3(e3Id); // Defense in depth. `validate` already refuses an E3 that belongs to a different program. if (address(e3.e3Program) != address(this)) revert E3NotAssignedToProgram(e3Id); if (interfold.getE3Stage(e3Id) != IInterfold.E3Stage.KeyPublished) { revert KeyNotPublished(e3Id); } if (block.timestamp < e3.inputWindow[0]) { revert E3NotAcceptingInputs(e3Id); } } function _commitmentE3(uint256 e3Id) internal view returns (E3 memory e3) { e3 = _keyPublishedE3(e3Id); uint256 deadline = _commitmentDeadline(e3Id, e3); if (block.timestamp >= deadline) { revert InputCommitmentDeadlinePassed(e3Id, deadline); } } function _finalizationE3(uint256 e3Id) internal view returns (E3 memory e3) { e3 = _keyPublishedE3(e3Id); // The configured finalization tail is a normal target, not a destructive cutoff. No new proof // can enter after the commitment deadline, and `verify` blocks computation while a receipt is // pending. A delayed VectorX proof can therefore recover until the compute deadline. uint256 deadline = interfold.getDeadlines(e3Id).computeDeadline; if (block.timestamp > deadline) { revert InputDeadlinePassed(e3Id, deadline); } } function _verifyInputProof( uint256 e3Id, E3 memory e3, bytes memory noirProof, address slotAddress, bytes32 encryptedVoteCommitment, bytes32 encryptedVoteHash, uint40 parentIndexPlusOne ) internal view returns (bytes32 id) { RoundData storage round = e3Data[e3Id]; // Every reserved leaf, pending or finalized, counts toward the limit. if (round.votes.numberOfLeaves >= round.inputLimit) revert InputLimitReached(e3Id, round.inputLimit); // A zero content hash matches an Avail padding leaf. Refuse it on every proof path so that // no committed input can later finalize against data that no party published, and so that // `validateInputProof` cannot accept a statement that `publishInput` rejects. if (encryptedVoteHash == bytes32(0)) revert ZeroEncryptedVoteHash(); id = inputId(e3Id, encryptedVoteHash, encryptedVoteCommitment, slotAddress, parentIndexPlusOne); if (round.inputs[id].indexPlusOne != 0) revert InputAlreadyCommitted(id); (bytes32 eligibility, IHonkVerifier verifier) = _eligibility(e3Id, slotAddress); bytes32 parentCommitment = _parentCommitment(e3Id, slotAddress, parentIndexPlusOne); bytes32[] memory noirPublicInputs = new bytes32[](9); noirPublicInputs[0] = parentCommitment; // A Keccak digest does not fit in one field element, so it enters the circuit as its two // 16-byte halves. The circuit rebuilds the 32 bytes with `digest_from_halves`. { uint256 digest = uint256(ballotDigest(e3Id, slotAddress, encryptedVoteCommitment)); noirPublicInputs[1] = bytes32(digest >> 128); noirPublicInputs[2] = bytes32(digest & type(uint128).max); } noirPublicInputs[3] = bytes32(uint256(uint160(slotAddress))); noirPublicInputs[4] = eligibility; noirPublicInputs[5] = bytes32(uint256(parentIndexPlusOne == 0 ? 1 : 0)); noirPublicInputs[6] = bytes32(round.numOptions); noirPublicInputs[7] = encryptedVoteCommitment; noirPublicInputs[8] = e3.committeePublicKey; // Check if the ciphertext was encrypted correctly if (!verifier.verify(noirProof, noirPublicInputs)) { revert InvalidNoirProof(); } } /// @notice The commitment of the entry an input names as its parent. /// @dev Zero when the input names none, which is what the circuit reads as `is_first_vote`. /// /// Naming no parent is allowed even when the slot already holds entries, and it has to be: a /// slot whose every entry is unusable has nothing to extend, and refusing that here would leave /// it permanently unwritable. Nothing is gained by refusing it either — the Secure Process /// accepts an entry only when its parent is the one currently selected for the slot, so an input /// that skips a usable parent is dropped from the tally wherever this contract lets it through. /// @param e3Id The round. /// @param slotAddress The slot the input is written to. /// @param parentIndexPlusOne The tree index of the parent entry plus one, or zero for none. /// @return The parent's commitment, or zero. function _parentCommitment(uint256 e3Id, address slotAddress, uint40 parentIndexPlusOne) internal view returns (bytes32) { if (parentIndexPlusOne == 0) return bytes32(0); bytes32 commitment = e3Data[e3Id].inputCommitment[slotAddress][parentIndexPlusOne - 1]; // Zero for an index this slot never wrote to, including one belonging to another slot. The // circuit would read it as `is_first_vote` while this contract reads it as an update, so the // two would disagree about the same input. if (commitment == bytes32(0)) revert UnknownParentInput(parentIndexPlusOne - 1); return commitment; } /// @notice Resolve the eligibility public input and the verifier for a round. /// @dev Returns the value that occupies index 4 of the circuit public inputs. The `crisp` and /// `crisp_onchain` circuits agree on every other position, so this one value and the verifier /// address are the whole difference between the two paths. /// @param e3Id The E3 the input belongs to. /// @param slotAddress The slot the input is written to. /// @return eligibility The Merkle root of the census, or the voting power of the slot in the /// units the circuit bounds the ballot by. /// @return verifier The verifier that matches the circuit of this round. function _eligibility(uint256 e3Id, address slotAddress) internal view returns (bytes32 eligibility, IHonkVerifier verifier) { RoundData storage round = e3Data[e3Id]; if (round.censusMode != CensusMode.ONCHAIN) { // We need to ensure that the CRISP admin set the merkle root of the census. if (round.merkleRoot == 0) revert MerkleRootNotSet(); return (bytes32(round.merkleRoot), honkVerifier); } uint256 rawPower = IVotesToken(round.token).getPastVotes(slotAddress, round.snapshot); // The floor is compared against RAW power, in the token's own units. `minVotingPower` is a // governance setting ("you need N tokens to vote") written the way every other token plugin // writes it, so scaling it here would reinterpret a configured value by the divisor. uint256 threshold = round.minVotingPower == 0 ? 1 : round.minVotingPower; if (rawPower < threshold) revert SlotNotEligible(); return (bytes32(_ballotPower(round, rawPower)), onchainHonkVerifier); } /// @notice Convert raw token voting power to the weight a ballot may carry in an ONCHAIN round. /// @dev CONSTANT gives every eligible slot the same credits; CUSTOM divides raw power by the /// divisor from {_initCredits}. /// @param round The round. /// @param rawPower The slot's voting power at the round snapshot, in the token's own units. /// @return The voting power in ballot units. function _ballotPower(RoundData storage round, uint256 rawPower) internal view returns (uint256) { return round.creditMode == CreditMode.CONSTANT ? round.credits : rawPower / round.votingPowerDivisor; } /// @notice The last finalized timepoint of a token, in its ERC-6372 clock units. /// @dev Falls back to block numbers when the token has no `clock()`, which matches the default /// ERC20Votes clock. /// @param token The token to read the clock of. /// @return The timepoint before the current one. function _previousTimepoint(address token) internal view returns (uint48) { try IERC6372Clock(token).clock() returns (uint48 current) { return current == 0 ? 0 : current - 1; } catch { return uint48(block.number - 1); } } /// @notice Decode the tally from the plaintext output /// @dev Coefficient `o` is the total weight on option `o`; {_initCredits} keeps it below the plaintext modulus. /// @param e3Id The E3 program ID /// @return votes - an array with the total weight of each option function decodeTally(uint256 e3Id) public view returns (uint256[] memory votes) { bytes memory output = interfold.getE3(e3Id).plaintextOutput; uint256 numOptions = e3Data[e3Id].numOptions; // A round that was never configured has nothing to decode. if (numOptions == 0) return new uint256[](0); // One little-endian uint64 per coefficient; the tally is in the first MAX_MSG_NON_ZERO_COEFFS. if (output.length % 8 != 0 || output.length < MAX_MSG_NON_ZERO_COEFFS * 8) revert InvalidTallyLength(); votes = new uint256[](numOptions); for (uint256 o = 0; o < numOptions; o++) { for (uint256 j = 0; j < 8; j++) votes[o] |= uint256(uint8(output[o * 8 + j])) << (j * 8); } } /// @notice The commitment this contract recorded for one entry of a slot. /// @dev Zero for an index this slot never wrote to. A client names a parent by index and must /// prove against exactly the commitment stored for it, so this is how it checks what that will /// be before proving. /// @param e3Id The round. /// @param slotAddress The slot address. /// @param index The tree index of the entry. /// @return The stored commitment, or zero when there is no such entry for this slot. function inputCommitmentOf(uint256 e3Id, address slotAddress, uint40 index) external view returns (bytes32) { return e3Data[e3Id].inputCommitment[slotAddress][index]; } /// @inheritdoc IE3Program function verify( uint256 e3Id, bytes32 ciphertextOutputHash, bytes32 ciphertextCommitment, bytes memory proof ) external view override returns (bool) { uint40 pending = e3Data[e3Id].pendingInputCount; if (pending != 0) revert InputAvailabilityPending(pending); E3 memory e3 = interfold.getE3(e3Id); bytes32 paramsHash = getParamsHash(e3Id); bytes32 inputRoot = bytes32(e3Data[e3Id].votes._root()); OpenVmComputeProof.Proof memory computeProof = OpenVmComputeProof.decode(proof); if (computeProof.paramsHash != paramsHash || computeProof.inputRoot != inputRoot) revert InvalidComputeContext(); bytes memory journal = OpenVmComputeProof.journal( bytes32(block.chainid), bytes32(uint256(uint160(address(interfold)))), bytes32(e3Id), e3.encryptionSchemeId, e3.committeePublicKey, ciphertextOutputHash, ciphertextCommitment, paramsHash, inputRoot ); openVmVerifier.verify(computeProof.seal, imageId, sha256(journal)); return true; } /// @notice Verify availability of an aggregate ciphertext for Interfold. /// @dev The same immutable verifier is used for inputs and outputs so one E3 cannot mix trust /// roots. Interfold independently checks the returned content hash before recording it. function verifyDataAvailability( bytes32 expectedContentHash, bytes calldata proof ) external view returns (IDataAvailabilityVerifier.DataReference memory receipt) { receipt = _verifyAvailability(expectedContentHash, proof); } /// @notice Verify a data-availability receipt and require it to name the expected content. function _verifyAvailability( bytes32 expectedContentHash, bytes calldata proof ) internal view returns (IDataAvailabilityVerifier.DataReference memory receipt) { receipt = dataAvailabilityVerifier.verifyDataAvailability(expectedContentHash, proof); if (receipt.contentHash != expectedContentHash) { revert DataAvailabilityHashMismatch(expectedContentHash, receipt.contentHash); } } /// @notice Record one accepted input: append its leaf, remember its commitment for later /// parents, and mark it pending until its availability receipt arrives. function _processVote( uint256 e3Id, bytes32 id, address slotAddress, bytes32 encryptedVoteCommitment, bytes32 encryptedVoteHash, uint40 parentIndexPlusOne ) internal returns (uint40 voteIndex) { RoundData storage round = e3Data[e3Id]; // Append-only. Updating a slot's leaf in place would let anyone who can write to a slot — and // the mask path needs no signature — replace the bytes of a vote that was already counted, // erasing it. Appending leaves the earlier entry in the tree, so the Secure Process can fall // back to it when a later entry is unusable, and nothing is lost. voteIndex = round.votes.numberOfLeaves; round.votes._insert(inputLeaf(encryptedVoteHash, encryptedVoteCommitment, slotAddress, parentIndexPlusOne)); round.inputCommitment[slotAddress][voteIndex] = encryptedVoteCommitment; round.inputs[id].indexPlusOne = voteIndex + 1; round.pendingInputCount++; } /// @notice Builds the input tree leaf for one committed input. /// @dev Binds four things the Secure Process must be able to trust: /// /// - the **bytes**, because the Noir proof constrains the commitment and never sees the /// serialized ciphertext, so the two can disagree and only the guest can tell; /// - the **commitment**, so a submitter cannot pair any commitment with any ciphertext; /// - the **slot**, because the tree is append-only and the guest tallies one entry per slot. /// Without the slot in the leaf a prover could re-group entries and change which one wins; /// - the **parent**, because that is what the guest walks the slot's chain by. An unbound parent /// would let a prover re-point entries and select a different one. /// /// Keccak hashes the serialized ciphertext into the same content digest that an external data /// availability receipt can expose. SHA-256 remains the outer hash because the zkVM accelerates /// it inline. function inputLeaf( bytes32 encryptedVoteHash, bytes32 commitment, address slotAddress, uint40 parentIndexPlusOne ) public pure returns (uint256) { return uint256(sha256(abi.encodePacked(encryptedVoteHash, commitment, slotAddress, parentIndexPlusOne))) % SNARK_SCALAR_FIELD; } /// @notice Identifier shared by the proof-commitment and DA-finalization transactions. /// @dev Domain-separated by this contract and the E3, so an accepted commitment cannot be /// replayed into another CRISP deployment or round. function inputId( uint256 e3Id, bytes32 encryptedVoteHash, bytes32 commitment, address slotAddress, uint40 parentIndexPlusOne ) public view returns (bytes32) { return keccak256(abi.encode("CRISP_INPUT_V1", address(this), e3Id, encryptedVoteHash, commitment, slotAddress, parentIndexPlusOne)); } /// @notice EIP-712 digest signed after the availability service stores the ciphertext. /// @dev Exposed so relays can ask this deployment for the exact chain-bound digest instead of /// reproducing its domain separator off chain. function inputAvailabilityDigest(uint256 e3Id, bytes32 id, uint64 expiresAt) public view returns (bytes32) { return _hashTypedDataV4(keccak256(abi.encode(INPUT_AVAILABILITY_TYPEHASH, e3Id, id, expiresAt))); } /// @notice Number of committed inputs still waiting for a verified DA receipt. function pendingInputCount(uint256 e3Id) external view returns (uint40) { return e3Data[e3Id].pendingInputCount; } /// @notice Whether the Noir proof for this exact input has been accepted. /// @dev Remains true after finalization so relays recover idempotently after a restart. function isInputCommitted( uint256 e3Id, bytes32 encryptedVoteHash, bytes32 commitment, address slotAddress, uint40 parentIndexPlusOne ) external view returns (bool) { bytes32 id = inputId(e3Id, encryptedVoteHash, commitment, slotAddress, parentIndexPlusOne); return e3Data[e3Id].inputs[id].indexPlusOne != 0; } /// @notice Whether this exact input has a verified data-availability receipt. /// @dev Availability relays use this after a restart to distinguish a transaction that landed /// from one that still needs submission. It exposes only the same public fact as InputPublished. function isInputPublished( uint256 e3Id, bytes32 encryptedVoteHash, bytes32 commitment, address slotAddress, uint40 parentIndexPlusOne ) external view returns (bool) { bytes32 id = inputId(e3Id, encryptedVoteHash, commitment, slotAddress, parentIndexPlusOne); return e3Data[e3Id].inputs[id].published; } }