For the complete documentation index, see llms.txt. This page is also available as Markdown.

Hinkal

Overview

Hinkal is the primary entrypoint to the Hinkal Protocol. It orchestrates:

  • zk-proof verification (Groth16 via VerifierFacade),

  • accounting invariants (slippage and balance conservation),

  • token/NFT movement (via Transferer),

  • UTXO and Merkle state updates (HinkalBase),

  • optional pre/post hook logic, and

  • external integrations (registered actions) — all within a single atomic transaction.

The contract itself is intentionally thin on business logic. Complex behaviors are delegated through delegatecall to hinkalInLogic.

Inheritance

  • IHinkal

  • VerifierFacade

  • HinkalBase

  • DeltaAmountCalculator

Constructor

constructor(
    IMerkle.MerkleConstructorArgs memory constructorArgs,
    address _hinkalHelper,
    address _hinkalInLogic,
    address _hinkalHelperManager
)

Forwards initialization to HinkalBase (Merkle tree, helper/logic addresses, roles).

Key Merkle params (via IMerkle.MerkleConstructorArgs):

  • levels: tree depth controlling capacity

  • poseidon4, poseidon5: Poseidon hash contracts for commitments

  • poseidon2: Poseidon for Merkle hashing


Public/External Functions

  1. registerExternalAction

  • Registers a DeFi/external integration address by ID.

  • Effects: externalActionMap[externalActionId] = externalActionAddress.

  • Emits: ExternalActionRegistered(externalActionAddress).

  • Access: DEFAULT_ADMIN_ROLE.

  1. transact

  • Purpose: Executes a full private transaction validated by a Groth16 proof and protocol invariants.

  • Execution lifecycle:

    1. Input shaping and checks

      • Helper constructs the exact public signals array expected by the circuit and validates metadata: token registry, relayer validity, calldata hash, access token membership, etc.

    2. Proof gate

      • verifierId = buildVerifierId(dimensions, externalActionId) binds the verifier to circuit shape and the chosen external integration. Prevents proof reuse across different op types or dimensions.

      • verifyProof enforces cryptographic validity over the supplied public signals.

    3. State root gate

      • rootHashExists(rootHashHinkal) checks the Merkle root is one of the recent known roots, enabling proofs against a sliding window of historical states.

    4. Pre-effects and hooks

      • performSideEffects can e.g. store per-tx metadata, validate fees, or record intent prior to balance movement.

      • Optional preHookContract runs custom logic (e.g., additional allowlists or rate limits).

    5. Balance snapshots and approvals

      • oldBalances are captured to compute deltas after execution.

      • If doPreTxApproval is true, _handleApprovalUtxos resolves approval deltas per token based on approval UTXOs consumed/created in the logic.

    6. Execution branch

      • Internal: _internalTransact handles pure deposits/withdrawals with fee accounting.

      • In-Hinkal: _inHinkalTransact delegates to logic for complex internal behaviors (netting, reorg of UTXOs, buffered approvals, etc.).

      • External: _internalRunExternalAction validates the action mapping and delegates to logic which performs external protocol calls atomically.

    7. Build on-chain commitments

      • Any returned UTXO[] from the logic path are converted to commitments and later appended to the Merkle tree.

    8. Accounting invariants (per token index i)

      • Slippage guard: minimum received amount enforced.

      • Conservation equation: ensures on-chain balance delta equals the sum of off-chain proof changes, on-chain UTXO creations, approval changes, and execute-time approval effects.

      • This double gates both economic correctness and circuit-contracted intents.

    9. Post hook and state writes

      • Optional afterTransact hook executes with full circomData.

      • insertNullifiers defends against replay by marking spent nullifiers.

      • insertCommitments appends commitments and emits events for indexers.

  • Reverts:

    • "Invalid Proof"

    • "Hinkal Root Hash is Incorrect"

    • "slippage param is violated"

    • "Balance Diff Should be equal to sum of onchain and offchain created commitments"

  1. prooflessDeposit

Purpose: Deposit funds and create UTXOs without a zk-proof for simpler onboarding.

Flow:

  1. Validation only (no proof): token registry, array shapes, and receiver structures.

  2. Delegate to logic for UTXO building and transfer handling (including ETH semantics via Transferer).

  3. Commitments created on-chain are appended to the Merkle tree and emitted.

Use when UX simplicity is preferred over full private spend logic (e.g., first-time deposits to a stealth address).


Security Considerations

  • nonReentrant guards on state-changing entrypoints.

  • Strong zk-proof verification with dynamic verifierId built from Dimensions and externalActionId.

  • Merkle root validation for state consistency.

  • Per-token slippage guard and conservation equation.

  • Replay protection via nullifiers.

  • Allowlist for external actions via externalActionMap.


Worked Scenarios

  • Private withdrawal with relay fee:

    • Circuit proves spend of UTXOs and outputs with amountChanges[i] < 0 for a token that equals the feeToken.

    • _internalTransact computes relayFee = helper.calculateRelayFee(...), enforces it does not exceed withdrawal, pays relay, and sends the remainder to externalAddress.

    • Balance equation validates that the on-chain delta equals proof off-chain change plus any on-chain UTXO creations and approval adjustments.

  • External AMM swap:

    • User submits proof binding externalActionId = id and slippage thresholds.

    • _internalRunExternalAction ensures externalAddress == ammAddress, delegates execution to logic (which performs the AMM call), returns new UTXOs, and then the Merkle tree is updated.

  • Helper (IHinkalHelper): checks, side-effects, fee computation.

  • Logic (IHinkalInLogic): complex flows, proofless deposits, approval handling, external actions.

  • Hooks: IPreTransactHook and ITransactHook.

  • Verifier management via VerifierFacade.


Common Revert Reasons

  • "Invalid Proof"

  • "Hinkal Root Hash is Incorrect"

  • "slippage param is violated"

  • "Balance Diff Should be equal to sum of onchain and offchain created commitments"

  • "Deposit should come from the sender"

  • "Relay Fee is over withdraw amount"

  • "relay not paid"

  • "Unknown externalAddress"

Last updated