Skip to content

Conversation

@jinoosss
Copy link
Member

Descriptions

Add comprehensive fuzz tests for uint256 operations to verify correctness through randomized property-based testing.

Changes

New Test Files

Added 6 new test files under contract/r/gnoswap/test/fuzz/:

  • uint256_arithmetic_fuzz_params_test.gno - Parameter definitions for arithmetic law tests
  • uint256_arithmetic_fuzz_test.gno - Arithmetic property tests (associativity, commutativity, distributivity, identity)
  • uint256_fuzz_params_test.gno - Parameter definitions for basic operations
  • uint256_fuzz_test.gno - Basic operation tests (Mul, Div, Mod, Add, Sub, overflow checks)
  • uint256_math_fuzz_params_test.gno - Parameter definitions for math functions
  • uint256_math_fuzz_test.gno - Math function tests (MulDiv, MulDivRoundingUp, DivRoundingUp)

Test Categories

Arithmetic Laws

  • Addition/Multiplication associativity and commutativity
  • Distributive law: a * (b + c) = a * b + a * c
  • Identity laws: a + 0 = a, a * 1 = a, a * 0 = 0
  • Subtraction inverse: a - a = 0
  • DivMod relation: (a / b) * b + (a % b) = a

Basic Operations

  • Mul, MulOverflow
  • Div, Mod, DivMod
  • Add, AddOverflow
  • Sub, SubOverflow

Math Functions

  • MulDiv with identity and zero numerator tests
  • MulDivRoundingUp with ceiling comparison tests
  • DivRoundingUp with exact division and remainder tests

Sparse Multiplication (umul)

  • Edge cases: max values, asymmetric operands, zero values, alternating bits
  • Commutativity and identity verification

Validation Logic

Each test includes IsValid() predicate functions that accurately predict operation outcomes:

  • mulHi() function computes upper 256 bits of 512-bit multiplication for overflow detection
  • Overflow/underflow conditions are validated against actual operation results
  • Tests panic on invalid states, allowing the fuzz framework to verify prediction accuracy

@sonarqubecloud
Copy link

@jinoosss jinoosss merged commit 26433f9 into main Nov 27, 2025
87 checks passed
@jinoosss jinoosss deleted the test-uint256-add-randomize-tests branch November 27, 2025 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants