{"record":{"id":"2749810f7be05ec5","repo":"nautechsystems/nautilus_trader","slug":"result-would-overflow-256-bits","errorCode":null,"errorMessage":"Result would overflow 256 bits","messagePattern":"Result would overflow 256 bits","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/model/src/defi/tick_map/full_math.rs","lineNumber":52,"sourceCode":"    /// # Errors\n    ///\n    /// Returns error if `denominator` is zero or the result would overflow 256 bits.\n    pub fn mul_div(a: U256, b: U256, mut denominator: U256) -> anyhow::Result<U256> {\n        // 512-bit multiply [prod1 prod0] = a * b\n        // Compute the product mod 2**256 and mod 2**256 - 1\n        // then use the Chinese Remainder Theorem to reconstruct\n        // the 512 bit result. The result is stored in two 256\n        // variables such that product = prod1 * 2**256 + prod0\n        let mm = a.mul_mod(b, U256::MAX);\n\n        // Least significant 256 bits of the product\n        let mut prod_0 = a * b;\n        let mut prod_1 = mm - prod_0 - U256::from_limbs([u64::from(mm < prod_0), 0, 0, 0]);\n\n        // Make sure the result is less than 2**256.\n        // Also prevents denominator == 0\n        if denominator <= prod_1 {\n            anyhow::bail!(\"Result would overflow 256 bits\");\n        }\n\n        ///////////////////////////////////////////////\n        // 512 by 256 division.\n        ///////////////////////////////////////////////\n\n        // Make division exact by subtracting the remainder from [prod1 prod0]\n        // Compute remainder using mul_mod\n        let remainder = a.mul_mod(b, denominator);\n\n        // Subtract 256 bit number from 512 bit number\n        prod_1 -= U256::from_limbs([u64::from(remainder > prod_0), 0, 0, 0]);\n        prod_0 -= remainder;\n\n        // Factor powers of two out of denominator\n        // Compute largest power of two divisor of denominator.\n        // Always >= 1.\n        let mut twos = (-denominator) & denominator;","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/defi/tick_map/full_math.rs#L34-L70","documentation":"mul_div computes floor(a*b/denominator) with full 512-bit intermediate precision. If the 512-bit product's high word is >= the denominator, the quotient would exceed 256 bits, so the function bails instead of returning a wrapped result. This also implicitly rejects denominator == 0.","triggerScenarios":"Calling mul_div(a, b, denominator) where a*b/denominator >= 2**256 — very large a and b relative to denominator, or denominator zero (which makes prod_1 compare true).","commonSituations":"Tick/liquidity math on adversarial or corrupted inputs; ported Uniswap V3 FullMath behavior when scaling amounts with extreme ratios; forgetting to normalize amounts before multiplication.","solutions":["Reduce a or b before multiplying (factor out common terms with the denominator)","Validate inputs so a*b/denominator fits in U256 before calling","Treat denominator == 0 separately and reject it at the call site","Match Uniswap's solidity behavior: this error mirrors the 'mulDiv overflow' revert"],"exampleFix":"// before\nlet out = mul_div(a, b, denom)?;\n// after\nanyhow::ensure!(!denom.is_zero(), \"denominator must be non-zero\");\nlet out = mul_div(a, b, denom)?; // now only genuine overflow remains possible","handlingStrategy":"try-catch","validationCode":"fn mul_div_fits(a: U256, b: U256, denom: U256) -> bool {\n    !denom.is_zero() && a.checked_mul(b).map(|p| p / denom < U256::MAX).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"let out = mul_div(a, b, denom)\n    .map_err(|e| MyError::MathOverflow(e.to_string()))?;","preventionTips":["Reject zero denominators before calling","Normalize or reduce amounts before multiplying large values","Port tests from Uniswap FullMath edge cases"],"tags":["rust","math","u256","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}