Three functions from Raydium CP-Swap, checked against a Lean spec

Independent work by Lean Directory. It was not requested or reviewed by Raydium, and it is not an audit. The deployed program was not checked against this commit.

What was proven

lean-solana prove translated three leaf functions from raydium-cp-swap at commit 3d69122 into Lean with Charon and Aeneas. The Lean kernel checked one theorem per function against AmmMath.ConstantProduct, with no sorry and only the axioms propext, Classical.choice and Quot.sound.

AmmMath.ConstantProduct has related results about these definitions, which need further caller assumptions and are not part of these three theorems.

Lean ProveLib/AmmConstantProduct.lean

∀ amount rate, trading_fee amount rate ⦃ o => match o with
  | some fee => amount.val * rate.val + FEE_DENOM ≤ U128.max ∧ fee.val = tradeFee amount.val rate.val
  | none => U128.max < amount.val * rate.val + FEE_DENOM ⦄

∀ input x y, input.val * y.val ≤ U128.max → x.val + input.val ≤ U128.max → 0 < x.val + input.val →
  swap_base_input_without_fees input x y ⦃ out => out.val = swapOut x.val y.val input.val 0 ⦄

∀ lp supply v0 v1 dir, lp_tokens_to_trading_tokens lp supply v0 v1 dir ⦃ o => match o with
  | some r => lp.val * v0.val ≤ U128.max ∧ lp.val * v1.val ≤ U128.max ∧ 0 < supply.val ∧
      (match dir with
        | .Floor => r.token_0_amount.val = lpToTokensFloor lp.val supply.val v0.val ∧
            r.token_1_amount.val = lpToTokensFloor lp.val supply.val v1.val
        | .Ceiling => r.token_0_amount.val = lpToTokensCeil lp.val supply.val v0.val ∧
            r.token_1_amount.val = lpToTokensCeil lp.val supply.val v1.val)
  | none => U128.max < lp.val * v0.val ∨ U128.max < lp.val * v1.val ∨ supply.val = 0 ⦄

f args ⦃ r => P r ⦄ means the translated function returns normally and its result satisfies P. U128.max is 2128 − 1 and FEE_DENOM is 1 000 000.

What is trusted

  • Charon's and Aeneas's translation of the Rust into Lean, Aeneas's models of u128 arithmetic and Option::unwrap, and the two models prove supplies for ? on Option.
  • The spec definitions tradeFee, swapOut, lpToTokensFloor and lpToTokensCeil in packages/amm-math/AmmMath/ConstantProduct.lean.
  • For swap_base_input_without_fees, that callers stay inside the stated domain. That is checked by reading and is not proven.
  • That the extracted crate holds the upstream bytes. Each file's upstream git blob id and kept line ranges are recorded, and a live test refetches the files and compares them.
  • The receipt published today comes from a WSL run without container isolation. A receipt from the Docker runner in CI replaces it.

Not covered

  • In the extracted crate: swap_base_output_without_fees, floor_div, protocol_fee, fund_fee, creator_fee, creator_fee_shared_amount, split_creator_fee, calculate_pre_fee_amount, CheckedCeilDiv::checked_ceil_div.
  • Everything else in raydium-cp-swap, including CurveCalculator::swap_base_input and the program's instructions.
  • The deployed program CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C. Lean Directory's own program check reports it as unverified with no attestation; nothing here links it to commit 3d69122.

How to reproduce

From a checkout of the Lean Directory repository, with the prove toolchain ready (lean-solana prove doctor):

node packages/prove/bin/prove.mjs docs/case-studies/raydium-cp-swap/crate --spec amm-cp --json
LEANSVM_PROVE_LIVE=1 node --test --test-concurrency=1 packages/prove/test/prove.test.mjs

The prove run prints a receipt with ok: true and three proven obligations. The live tests also check that ten deliberately broken versions of the three functions each fail, and refetch the upstream files to compare bytes. The extracted crate, its extraction record, the receipt and the program-check record are in docs/case-studies/raydium-cp-swap/.