Security: swap math missing edge-reserve and u128 boundary tests [SEC-C01] #401
Labels
No labels
agent:fix_bugfix
agent:fix_conflicts
agent:fix_security
agent:gap_analysis
agent:implement
agent:implement
agent:implement
agent:open_issues
agent:ready
agent:research
agent:security_audit
agent:verify
architecture
backend
blocker:hybrid
blocker:launch
blocker:limit-orders
blocker:v2
block:log_only
block:security
bug
ci
contracts
correctness
deploy
dev
devops
docs
documentation
duplicate
e2e
enhancement
epic
feature
frontend
functional-completion
gas
good first issue
governance
help wanted
high-risk
hooks
hybrid
indexer
infra
infrastructure
integrators
invalid
launch-blocker
limit-orders
localnet
localterra
low priority
missing-implementation
needs-design
ops
performance
priority
high
priority
medium
product
qa
QA
question
ready
ready
research
scripts
security
security-hardening
smartcontracts
tech-debt
testing
ux
UX
v2
verification
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
code/cl8y-dex-terraclassic#401
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Checklist Item
SEC-C01: Verify constant-product swap accounting cannot return more value than reserves permit after fee/spread. Verify: unit/property tests for swap math, including edge reserves and tiny amounts.
Summary
Tiny-amount and large-amount swap tests exist and pass. However, no test exercises the constant-product formula with reserves near the u128 maximum or with extreme reserve imbalance ratios. An overflow or rounding failure at boundary values could cause the pool to return more than reserves permit.
What Was Checked
smartcontracts/tests/src/lib.rsline 12177:test_swap_one_microunit-- tiny amount coveredsmartcontracts/tests/src/lib.rsline 3934:test_swap_large_amount-- large amount coveredsmartcontracts/tests/src/lib.rsline 12338:test_no_zero_output_from_rounding-- rounding guard presentExpected (per checklist)
Tests must cover edge reserves including: reserves at or near u128 maximum, extreme reserve imbalance (e.g. 1:1_000_000), and tiny amounts. All cases must confirm that output does not exceed what reserves permit after fee and spread.
Actual
Tiny-amount and sequential-swap tests exist. No test constructs reserves near u128 limits or exercises extremely unbalanced reserve states that stress the constant-product arithmetic boundary.
Evidence
smartcontracts/tests/src/lib.rslines 3934, 12177, 12338: existing coverage for large, tiny, and rounding casessmartcontracts/tests/src/Suggested Fix
Add property tests or explicit unit tests in
lib.rsor a dedicated math test module covering:Verification Checklist
Labels
security,pre-launchCc: @PlasticDigits
mentioned in issue #381
mentioned in commit
72aedf3024mentioned in merge request !936
mentioned in commit
7227ecd0e5Verification — #401 / SEC-C01 (PASS)
Verified on
main@7227ecd0(merge ofissue-401-sec-c01-swap-boundary-tests, commit72aedf30).Acceptance checklist
sec_c01_swap_boundary_tests::test_swap_reserves_near_u128_max_no_overflowexercisesu128::MAX/2andu128::MAX-1reserves with 1-unit input; asserts gross output ≤ ask reserve, exact fee split, and on-chain balance conservation.test_swap_extreme_imbalance_one_to_one_million_offer_scarce_side(1M:1T, swap scarce A) andtest_swap_extreme_imbalance_one_to_one_million_offer_abundant_side(swap abundant B) both assertassert_pool_swap_accountingandactual_return ≤ output_reserve.cargo test sec_c01_swap_boundary_tests→ 3/3 ok;make test-contracts→ full suite green.gross ≤ output_reserveand zero-gross implies zero return+fee; existingdust_amount_tests::test_no_zero_output_from_roundingandtest_swap_one_microunitstill pass.Additional regression checks (issue body references)
test_swap_one_microunittest_swap_large_amounttest_no_zero_output_from_roundingImplementation location
smartcontracts/tests/src/lib.rs— modulesec_c01_swap_boundary_tests(lines ~12676–12948), helperassert_pool_swap_accountingenforces gross ≤ reserve and floor fee split.No repo changes required during verification; fix already merged.