From 298c4e5a9ddf8e5b83fbc616bc1df3728d7ff912 Mon Sep 17 00:00:00 2001 From: Cryptskii <47649969+cryptskii@users.noreply.github.com> Date: Sat, 29 Aug 2026 20:05:09 -0400 Subject: [PATCH] =?UTF-8?q?test(dlv):=20pin=20the=20SoFi=20=C2=A75.1=20fus?= =?UTF-8?q?ed-rounding=20rule=20with=20a=20conformance=20vector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The spec fixes ONE floor division for the constant-product output and forbids rounding the fee-adjusted input first. Nothing pinned that: an implementation that floors `a·(D−f)/D` before the curve passes every existing test and silently collapses a sub-unit input to zero, taking the whole output with it. At `a=1, x=1, y=3, fee_bps=30` the two rules part company — fused yields 1, doubly-rounded yields 0 — and the vector asserts both sides so a future edit cannot satisfy it by agreeing with the wrong rule. It also records the boundaries where the rules AGREE (`f=0`, `f=9999`). The spec's own example claims the divergence holds for "any fee_bps in the legal range", which overstates it; writing the agreeing cases down means a later reader cannot "fix" the vector by widening it to the spec's overstatement. Test-only. No production behaviour changes. --- .../dsm/src/dlv/route_commit.rs | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/dsm_client/deterministic_state_machine/dsm/src/dlv/route_commit.rs b/dsm_client/deterministic_state_machine/dsm/src/dlv/route_commit.rs index 25897b9f..51d77d1c 100644 --- a/dsm_client/deterministic_state_machine/dsm/src/dlv/route_commit.rs +++ b/dsm_client/deterministic_state_machine/dsm/src/dlv/route_commit.rs @@ -196,3 +196,65 @@ pub fn verify_route_commit_hop( fee_bps: hop.fee_bps, }) } + +#[cfg(test)] +mod tests { + use super::*; + + /// THE FUSED-ROUNDING CONFORMANCE VECTOR (SoFi v2.0 §5.1). + /// + /// The spec fixes one floor division and forbids rounding the fee-adjusted + /// input first. At `a=1, x=1, y=3, fee_bps=30` the two rules genuinely + /// part company: the fused rule yields 1, while flooring `a·(D−f)/D` first + /// collapses a sub-unit input to zero and takes the whole output with it. + /// + /// Note the spec's own example says this holds for "any fee_bps in the + /// legal range", which overstates it: at `f=0` there is no fee to round + /// and at `f=9999` both rules yield 0. The divergence is real for ordinary + /// fees, which is where a low-liquidity vault actually operates, so the + /// vector pins a fee that exhibits it rather than quoting the claim. + #[test] + fn the_fee_adjusted_input_is_never_rounded_before_the_curve() { + const D: u128 = 10_000; + let doubly_rounded = |a: u128, x: u128, y: u128, f: u128| -> u128 { + let pre = (a * (D - f)) / D; // the forbidden first rounding + if pre == 0 { + return 0; + } + (y * pre * D) / (x * D + pre * D) + }; + + assert_eq!( + constant_product_output(1, 1, 3, 30), + Some(1), + "the fused rule keeps the sub-unit input alive" + ); + assert_eq!( + doubly_rounded(1, 1, 3, 30), + 0, + "…and the doubly-rounded variant loses the whole output" + ); + + // The same shape across a spread of ordinary fees, so the vector is + // not a single lucky point. + for f in [1u32, 5, 30, 100, 300] { + assert_eq!( + constant_product_output(1, 1, 3, f), + Some(1), + "fused rule at fee_bps={f}" + ); + assert_eq!( + doubly_rounded(1, 1, 3, f as u128), + 0, + "doubly-rounded variant at fee_bps={f}" + ); + } + + // Honest boundaries: with no fee there is nothing to round, and a + // near-total fee zeroes both. Recording them stops a later reader + // from "fixing" the vector by widening it to every legal fee. + assert_eq!(constant_product_output(1, 1, 3, 0), Some(1)); + assert_eq!(doubly_rounded(1, 1, 3, 0), 1, "no fee, no divergence"); + assert_eq!(constant_product_output(1, 1, 3, 9_999), None); + } +}