{"record":{"id":"0c84e87444ac0386","repo":"nautechsystems/nautilus_trader","slug":"scaled-result-exceeds-256-bit-range","errorCode":null,"errorMessage":"Scaled result exceeds 256-bit range","messagePattern":"Scaled result exceeds 256-bit range","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/defi/tick_map/full_math.rs","lineNumber":130,"sourceCode":"        // is no longer required.\n        let result = prod_0 * inv;\n\n        Ok(result)\n    }\n\n    pub(crate) fn mul_div_scaled(\n        a: U256,\n        b: U256,\n        denominator: U256,\n        scales: &[U256],\n    ) -> anyhow::Result<U256> {\n        let mut quotient = Self::mul_div(a, b, denominator)?;\n        let mut remainder = a.mul_mod(b, denominator);\n\n        for &scale in scales {\n            let scaled_quotient = quotient\n                .checked_mul(scale)\n                .ok_or_else(|| anyhow::anyhow!(\"Scaled result exceeds 256-bit range\"))?;\n            let scaled_remainder = Self::mul_div(remainder, scale, denominator)?;\n            quotient = scaled_quotient\n                .checked_add(scaled_remainder)\n                .ok_or_else(|| anyhow::anyhow!(\"Scaled result exceeds 256-bit range\"))?;\n            remainder = remainder.mul_mod(scale, denominator);\n        }\n\n        Ok(quotient)\n    }\n\n    pub(crate) fn check_decimal_exponent(exponent: u8) -> anyhow::Result<()> {\n        anyhow::ensure!(\n            exponent <= DECIMAL_EXPONENT_MAX,\n            \"Decimal exponent {exponent} exceeds supported maximum {DECIMAL_EXPONENT_MAX}\"\n        );\n        Ok(())\n    }\n","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/defi/tick_map/full_math.rs#L112-L148","documentation":"`mul_div_scaled` computes a high-precision a*b/denominator with extra decimal-scale correction steps. During each scale step the intermediate `quotient * scale` must still fit in U256; the library throws this error when that checked multiplication overflows the 256-bit range.","triggerScenarios":"Inputs where the preliminary quotient from `mul_div` is so large that multiplying by the decimal scale factor (10^k) exceeds 2^256 — extremely large `a` or `b` combined with a small denominator and a large scale.","commonSituations":"Feeding raw on-chain uint256 amounts near the U256 maximum into price/liquidity math with high decimal-exponent scales (e.g. 18-decimal tokens) rather than normalized values.","solutions":["Normalize/scale inputs before calling (use smaller intermediate values or divide early).","Pre-check that quotient <= U256::MAX / scale before invoking.","Reduce the decimal exponent used for scaling if the math allows (recompute the correction differently).","Use U512 internally for the scaled step and convert back only if it fits."],"exampleFix":"// before\nlet q = mul_div_scaled(a, b, denominator, &[scale])?;\n// after\nlet prelim = FullMath::mul_div(a, b, denominator)?;\nensure!(prelim <= U256::MAX / scale, \"inputs too large for scaled mul_div\");\nlet q = mul_div_scaled(a, b, denominator, &[scale])?;","handlingStrategy":"validation","validationCode":"fn can_scale(quotient: U256, scale: U256) -> bool {\n    quotient.checked_mul(scale).is_some()\n}","typeGuard":null,"tryCatchPattern":"match mul_div_scaled(&a, &b, &denominator, scales) {\n    Ok(q) => q,\n    Err(e) if e.to_string().contains(\"exceeds 256-bit\") => normalize_inputs_and_retry(),\n    Err(e) => return Err(e),\n}","preventionTips":["Pre-normalize raw uint256 on-chain values before high-precision math","Estimate result magnitude from inputs and reject early with a domain-level error","Keep decimal scale exponents as small as the protocol math allows"],"tags":["math","u256-overflow","tick-map","arithmetic"],"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"}