Davide Crapis
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # ZK API Usage Credits: LLMs and Beyond *Davide Crapis and Vitalik Buterin* *This is v2 -- compared to [v1](https://ethresear.ch/t/zk-api-usage-credits-llms-and-beyond/24104), it replaces the client-side list of refund tickets with a server-signed homomorphic running total of refunds, so users no longer need to store (or prove over) an ever-growing refund history.* A core challenge in API metering is achieving **privacy**, **security**, and **efficiency** simultaneously. This is particularly critical for AI inference with LLMs, where users submit highly sensitive personal data, but applies generally to any high-frequency digital service. Currently, API providers are forced to choose between two suboptimal paths: 1. **Web2 Identity:** Require authentication (email/credit card), which links every request to a real-world identity, creating massive privacy leaks and profiling risks. 2. **On-Chain Payments:** Require a transaction per request, which is prohibitively slow, expensive, and makes it difficult to obfuscate the full user's transaction graph. We need a system where a user can **deposit funds once and make thousands of API calls anonymously, securely, and efficiently**. The provider must be guaranteed payment and protection against spam, while the user must be guaranteed that their requests cannot be linked to their identity or to each other. We focus on LLM inference as the motivating use case, but the approach is general and also applies to RPC calls or any other fixed-cost API, image generation, cloud computing services, VPNs, public data APIs, etc. **Examples:** 1. **LLM inference:** A user deposits 100 USDC into a smart contract and makes 500 queries to a hosted LLM. The provider receives 500 valid, paid requests but cannot link them to the same depositor (or to each other), while the user’s prompts remain unlinkable to the user identity. 2. **Ethereum RPC:** A user deposits 10 USDC and makes 10,000 requests to an Ethereum RPC node (e.g., `eth_call` / `eth_getLogs`) to power a wallet, indexer, or a bot. The RPC provider is protected against spam and guaranteed payment, but cannot correlate the requests into a persistent user profile. **Proposal Overview:** We leverage [Rate-Limit Nullifiers](https://rate-limiting-nullifier.github.io/rln-docs/rln.html) (RLN) to bind anonymity to a financial stake: honest users who stay within protocol limits remain *unlinkable*, while users who double-spend (or otherwise exceed their allowed capacity) cryptographically reveal their secret key, enabling slashing. We design the protocol to work when API usage incurs variable costs, but it also directly supports the simpler fixed-cost-per-call as a special case. We use a flexible accounting protocol in which each request sets a maximum cost per call up front and once the actual cost is determined at the end of the call the server issues a refund. The server updates (and signs) a homomorphically encrypted running total of refunds that the user can carry forward between requests. A Dual Staking mechanism lets the server enforce compliance policies while remaining publicly accountable. ## ZK API Usage Credit Protocol The protocol utilizes **server refunds** paired with a **server-signed homomorphic running total** of refunds that the user can carry forward privately between requests. The model enforces solvency by requiring the user to prove that their cumulative spending—represented by their current **ticket index**—remains strictly within the bounds of their initial deposit and their verified refund history. Anti-spam protection is enforced economically: a user's throughput is naturally capped by their available deposit buffer, while any attempt to reuse a specific ticket index (double-spending) is prevented by the Rate-Limit Nullifier. ### **Primitives** - $k$: User's Secret Key. - $D$: Initial Deposit. - **$C_{max}$**: The maximum cost per request (deducted upfront). - **$i$**: The Ticket Index (A strictly increasing counter: $0, 1, 2, \dots$). - $E(R)$: A homomorphic encryption (e.g., Pedersen commitment or lattice-based HE) of the user's total refunds received so far. - $\sigma_{srv}$: A server-issued signature over the current encrypted total $E(R)$. ### **Protocol Flow** **Registration** The user generates secret $k$, derives an identity commitment $ID = Hash(k)$, and deposits $D$ into the smart contract. The contract inserts $ID$ into the on-chain Merkle Tree. **Rerandomize State** The user picks a new random blinding factor $\eta'$ and derives a fresh, anonymous commitment: $E(R)_{anon} = E(R) \oplus E(0; \eta')$. **Request Generation** The user picks the next available Ticket Index $i$. The user generates a ZK-STARK $\pi_{req}$ proving: 1. **Membership:** $ID \in$ MerkleRoot. 2. **State Consistency:** The anonymous $E(R)_{anon}$ is a valid rerandomization of the commitment $E(R)$ previously signed by the server with $\sigma_{srv}$. 3. **Solvency (The Credit Check):** $$(i + 1) \cdot C_{max} \le D + R$$ (*The total potential spend at index $i$ is covered by the deposit plus the sum of all verified refunds.*) 4. **RLN Share & Nullifier:** - Slope: $a = Hash(k, i)$ - *Note: Keyed to the Index $i$ instead of a previous hash to allow for parallel generation.* - Signal: $y = k + a \cdot Hash(M)$ - Nullifier: $Nullifier = Hash(a)$ **Submission** User sends: Payload (M) + Nullifier + Signal (x, y) + Proof + current $E(R)_{anon}$. **Verification & Slashing** The Server checks the Nullifier in its "Spent Tickets" database: - **Fork/Double-Spend Check:** If the Nullifier exists with a different $x$ (Message), the user tried to spend the same ticket on two different requests. Solve for $k$ and SLASH. - **Solvency Check:** Verify $\pi_{req}$ to ensure the ticket index $i$ is authorized by the user's current funding level. **Settlement & Refund Update** - Server executes request and determines actual refund $r = (C_{max} - C_{actual})$. - **Homomorphic Update:** The server homomorphically adds $r$ to the current encryption: $E(R^{new}) = E(R)_{anon} \oplus E(r)$. - **Signature:** The server signs the new total $E(R^{new})$ and sends it (along with the new signature $\sigma^{new}$) back to the user. ## Server-Side Accountability (Dual Staking) To deter API abuse beyond simple rate-limiting (e.g., violating Terms of Service, generating illegal content, or jailbreaking attempts), we introduce a secondary staking layer. For example, a user might submit a prompt asking the model to generate instructions for building a weapon or to help them bypass security controls—requests that would violate many providers’ usage policies and that the provider may want to prevent. The user deposits a total sum $Total = D + S$. * **$D$ (RLN Stake):** Governed by the math of the protocol. Can be claimed by *anyone* (including the Server) who provides mathematical proof of double-signaling (revealed secret $k$). * **$S$ (Policy Stake):** Governed by Server Policy. Can be slashed (burned), but *not claimed*, by the Server if the user violates usage policies. The purpose of doing this, instead of simply setting $D$ higher, is to remove the server's incentive to fraudulently take away users' deposits, which could be high depending on how high the deposit is. ### Slashing Mechanism for S If a user submits a valid RLN request that violates policy (but does not trigger the mathematical double-spend trap): 1. **Violation:** Server detects policy violation in the request payload (e.g., prohibited content). 2. **Burn Transaction:** The Server calls a `slashPolicyStake()` function on the smart contract. * **Input:** The `Nullifier` of the offending request and the `ViolationEvidence` (optional hash/reason). * **Action:** The contract burns amount $S$ from the user's deposit. * **Constraint:** The Server *cannot* claim $S$ for itself, it is sent to a burn address. This prevents the server from being incentivized to falsely ban users for profit. 3. **Public Accountability:** The slashing event is recorded on-chain with the associated `Nullifier`. While the user's identity remains hidden, the community can audit the *rate* at which the Server burns stakes and the posted evidence for these burns.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully