fermat-core 0.1.0

128-bit fixed-point arithmetic for Solana sBPF — no_std, zero dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Arithmetic operations: add, sub, mul, div, mul_div, neg, abs.
//!
//! # Scale Alignment
//!
//! Before addition/subtraction, both operands are scaled to the same number of
//! decimal places (`max(a.scale, b.scale)`) via `align_scales`. This can itself
//! overflow if the mantissa is near `i128::MAX`; that is reported as
//! `ArithmeticError::Overflow`.
//!
//! # mul_div Safety
//!
//! `checked_mul_div` uses a 256-bit intermediate (`U256`) for `(a × b) / c` so
//! that the product does not silently wrap. See [`U256`] for the implementation.

use crate::decimal::{Decimal, MAX_SCALE};
use crate::error::ArithmeticError;

// ─── Powers of 10 table ──────────────────────────────────────────────────────

/// Pre-computed powers of 10 from `10^0` to `10^28`.
///
/// Using a lookup instead of `checked_pow` eliminates runtime loops and
/// guarantees O(1) access — important for CU budget on sBPF.
pub(crate) const POW10: [i128; 29] = [
    1,                                      // 10^0
    10,                                     // 10^1
    100,                                    // 10^2
    1_000,                                  // 10^3
    10_000,                                 // 10^4
    100_000,                                // 10^5
    1_000_000,                              // 10^6
    10_000_000,                             // 10^7
    100_000_000,                            // 10^8
    1_000_000_000,                          // 10^9
    10_000_000_000,                         // 10^10
    100_000_000_000,                        // 10^11
    1_000_000_000_000,                      // 10^12
    10_000_000_000_000,                     // 10^13
    100_000_000_000_000,                    // 10^14
    1_000_000_000_000_000,                  // 10^15
    10_000_000_000_000_000,                 // 10^16
    100_000_000_000_000_000,                // 10^17
    1_000_000_000_000_000_000,              // 10^18
    10_000_000_000_000_000_000,             // 10^19
    100_000_000_000_000_000_000,            // 10^20
    1_000_000_000_000_000_000_000,          // 10^21
    10_000_000_000_000_000_000_000,         // 10^22
    100_000_000_000_000_000_000_000,        // 10^23
    1_000_000_000_000_000_000_000_000,      // 10^24
    10_000_000_000_000_000_000_000_000,     // 10^25
    100_000_000_000_000_000_000_000_000,    // 10^26
    1_000_000_000_000_000_000_000_000_000,  // 10^27
    10_000_000_000_000_000_000_000_000_000, // 10^28
];

/// Return `10^exp` as `i128` or `Err(ScaleExceeded)` if `exp > MAX_SCALE`.
#[inline]
pub(crate) fn pow10(exp: u8) -> Result<i128, ArithmeticError> {
    POW10.get(exp as usize).copied().ok_or(ArithmeticError::ScaleExceeded)
}

// ─── Scale alignment ─────────────────────────────────────────────────────────

/// Align two operands to a common scale (`max(a.scale, b.scale)`).
///
/// Returns `(a_mantissa, b_mantissa, common_scale)`.
/// Fails with `Overflow` if multiplying to scale up overflows `i128`.
#[inline]
pub(crate) fn align_scales(
    a: Decimal,
    b: Decimal,
) -> Result<(i128, i128, u8), ArithmeticError> {
    use core::cmp::Ordering;
    match a.scale.cmp(&b.scale) {
        Ordering::Equal => Ok((a.mantissa, b.mantissa, a.scale)),
        Ordering::Less => {
            let diff = b.scale - a.scale;
            let factor = pow10(diff)?;
            let scaled = a
                .mantissa
                .checked_mul(factor)
                .ok_or(ArithmeticError::Overflow)?;
            Ok((scaled, b.mantissa, b.scale))
        }
        Ordering::Greater => {
            let diff = a.scale - b.scale;
            let factor = pow10(diff)?;
            let scaled = b
                .mantissa
                .checked_mul(factor)
                .ok_or(ArithmeticError::Overflow)?;
            Ok((a.mantissa, scaled, a.scale))
        }
    }
}

// ─── Sign helper for mul_div ──────────────────────────────────────────────────

#[derive(Clone, Copy)]
pub(crate) enum Sign {
    Positive,
    Negative,
    Zero,
}

/// Compute the sign of `a * b / c` given their `i128` values.
#[inline]
pub(crate) fn sign3(a: i128, b: i128, c: i128) -> Sign {
    if a == 0 || b == 0 {
        return Sign::Zero;
    }
    let neg_a = a < 0;
    let neg_b = b < 0;
    let neg_c = c < 0;
    let negative = (neg_a ^ neg_b) ^ neg_c;
    if negative { Sign::Negative } else { Sign::Positive }
}

// ─── 256-bit unsigned integer ─────────────────────────────────────────────────

/// 256-bit unsigned integer represented as two 128-bit limbs.
///
/// Used exclusively as an intermediate type in [`Decimal::checked_mul_div`] to
/// prevent the product `a × b` from silently overflowing `i128` / `u128`.
///
/// Layout: `value = hi * 2^128 + lo`
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct U256 {
    /// Least-significant 128 bits.
    pub lo: u128,
    /// Most-significant 128 bits.
    pub hi: u128,
}

impl U256 {
    pub const ZERO: Self = Self { lo: 0, hi: 0 };

    /// Exact 128 × 128 → 256-bit multiplication using four 64-bit limbs.
    ///
    /// ```text
    /// a = a_hi * 2^64 + a_lo
    /// b = b_hi * 2^64 + b_lo
    /// a*b = hh * 2^128 + mid * 2^64 + ll
    ///     where mid = a_hi*b_lo + a_lo*b_hi
    /// ```
    pub fn mul(a: u128, b: u128) -> Self {
        const MASK64: u128 = u64::MAX as u128;
        let a_lo = a & MASK64;
        let a_hi = a >> 64;
        let b_lo = b & MASK64;
        let b_hi = b >> 64;

        let ll = a_lo * b_lo;
        let lh = a_lo * b_hi;
        let hl = a_hi * b_lo;
        let hh = a_hi * b_hi;

        let (mid, mid_carry) = lh.overflowing_add(hl);
        let (lo, lo_carry) = ll.overflowing_add(mid << 64);
        let hi = hh
            .wrapping_add(mid >> 64)
            .wrapping_add(if mid_carry { 1u128 << 64 } else { 0 })
            .wrapping_add(lo_carry as u128);

        U256 { lo, hi }
    }

    /// 256-bit / 128-bit → `(quotient: u128, remainder: u128)`.
    ///
    /// Returns `None` if `d == 0` or `self.hi >= d` (quotient exceeds `u128`).
    ///
    /// ## Algorithm selection
    ///
    /// - **hi == 0**: simple 128-bit division (O(1)).
    /// - **d ≤ u64::MAX**: four-phase 64-bit long-division (O(1), fast path for
    ///   all realistic financial values where `d` is a token amount or scale factor).
    /// - **d > u64::MAX**: binary long-division over 256 bits (O(256), always
    ///   correct; rarely reached in DeFi contexts).
    pub fn checked_div(self, d: u128) -> Option<(u128, u128)> {
        if d == 0 {
            return None;
        }
        // Fast path: numerator fits in 128 bits
        if self.hi == 0 {
            return Some((self.lo / d, self.lo % d));
        }
        // Quotient overflow guard
        if self.hi >= d {
            return None;
        }

        // ── Fast path: d fits in 64 bits ─────────────────────────────────────
        // The four-phase algorithm computes (r * 2^64 + digit) / d in each phase.
        // This is safe iff (r * 2^64) doesn't overflow u128, i.e., r < 2^64.
        // Since r < d and d ≤ 2^64, r < 2^64. ✓
        if d <= u64::MAX as u128 {
            const HALF: u128 = 1u128 << 64;
            const MASK: u128 = HALF - 1;

            let hi_hi = self.hi >> 64;
            let hi_lo = self.hi & MASK;
            let lo_hi = self.lo >> 64;
            let lo_lo = self.lo & MASK;

            let r_a = hi_hi % d;
            let q_a = hi_hi / d;

            let n_b = r_a * HALF + hi_lo;
            let q_b = n_b / d;
            let r_b = n_b % d;

            let n_c = r_b * HALF + lo_hi;
            let q_c = n_c / d;
            let r_c = n_c % d;

            let n_d = r_c * HALF + lo_lo;
            let q_d = n_d / d;
            let r_d = n_d % d;

            if q_a != 0 || q_b != 0 {
                return None; // quotient > u128::MAX
            }

            return Some((q_c * HALF + q_d, r_d));
        }

        // ── General case: d > 2^64, binary long-division ─────────────────────
        //
        // Processes all 256 bits of the numerator from MSB to LSB.
        // Maintains invariant: r < d at the end of every iteration.
        //
        // When `r_hi` (the overflow bit from `r << 1`) is set, the actual
        // remainder is `2^128 + r_new`, which is guaranteed to be ≥ d and
        // < 2d (proved from r < d before shift). The wrapping subtraction
        // `r_new.wrapping_sub(d)` correctly computes `2^128 + r_new - d`.
        let mut q: u128 = 0;
        let mut r: u128 = 0;

        for i in (0..256_u32).rev() {
            let bit: u128 = if i >= 128 {
                (self.hi >> (i - 128)) & 1
            } else {
                (self.lo >> i) & 1
            };

            let r_hi = r >> 127; // top bit of r (will overflow into bit 128 after shift)
            let r_new = (r << 1) | bit;

            if r_hi == 1 {
                // Actual value is 2^128 + r_new; it must be ≥ d (and < 2d).
                // wrapping_sub gives (2^128 + r_new - d) mod 2^128 = correct result.
                r = r_new.wrapping_sub(d);
                if i < 128 {
                    q |= 1u128 << i;
                }
            } else if r_new >= d {
                r = r_new - d;
                if i < 128 {
                    q |= 1u128 << i;
                }
            } else {
                r = r_new;
            }
        }

        Some((q, r))
    }
}

// ─── Decimal: mul, div, neg, abs, mul_div ────────────────────────────────────

impl Decimal {
    /// Checked addition. Aligns scales then adds mantissas.
    pub fn checked_add(self, rhs: Decimal) -> Result<Decimal, ArithmeticError> {
        let (a, b, scale) = align_scales(self, rhs)?;
        let mantissa = a.checked_add(b).ok_or(ArithmeticError::Overflow)?;
        Decimal::new(mantissa, scale)
    }

    /// Checked subtraction. Aligns scales then subtracts mantissas.
    pub fn checked_sub(self, rhs: Decimal) -> Result<Decimal, ArithmeticError> {
        let (a, b, scale) = align_scales(self, rhs)?;
        let mantissa = a.checked_sub(b).ok_or(ArithmeticError::Overflow)?;
        Decimal::new(mantissa, scale)
    }

    /// Checked multiplication: `self * rhs`.
    ///
    /// Result scale = `self.scale + rhs.scale`.
    /// Returns `Err(ScaleExceeded)` if that exceeds `MAX_SCALE`.
    pub fn checked_mul(self, rhs: Decimal) -> Result<Decimal, ArithmeticError> {
        let new_scale = self
            .scale
            .checked_add(rhs.scale)
            .filter(|&s| s <= MAX_SCALE)
            .ok_or(ArithmeticError::ScaleExceeded)?;
        let mantissa = self
            .mantissa
            .checked_mul(rhs.mantissa)
            .ok_or(ArithmeticError::Overflow)?;
        Decimal::new(mantissa, new_scale)
    }

    /// Checked division: `self / rhs`.
    ///
    /// Scales the numerator up by `MAX_SCALE - self.scale` places before
    /// dividing to retain maximum precision.
    pub fn checked_div(self, rhs: Decimal) -> Result<Decimal, ArithmeticError> {
        if rhs.mantissa == 0 {
            return Err(ArithmeticError::DivisionByZero);
        }
        let extra = MAX_SCALE.saturating_sub(self.scale);
        let factor = pow10(extra)?;
        let scaled_num = self
            .mantissa
            .checked_mul(factor)
            .ok_or(ArithmeticError::Overflow)?;
        let mantissa = scaled_num
            .checked_div(rhs.mantissa)
            .ok_or(ArithmeticError::Overflow)?;
        let raw_scale = (self.scale as i32) + (extra as i32) - (rhs.scale as i32);
        if raw_scale < 0 {
            return Err(ArithmeticError::Underflow);
        }
        Decimal::new(mantissa, (raw_scale as u8).min(MAX_SCALE))
    }

    /// Negation: returns `-self`.
    ///
    /// Fails with `Err(Overflow)` for `Decimal::MIN` (two's-complement has no
    /// positive counterpart for `i128::MIN`).
    pub fn checked_neg(self) -> Result<Decimal, ArithmeticError> {
        let mantissa = self.mantissa.checked_neg().ok_or(ArithmeticError::Overflow)?;
        Decimal::new(mantissa, self.scale)
    }

    /// Absolute value: returns `|self|`.
    ///
    /// Fails with `Err(Overflow)` for `Decimal::MIN`.
    pub fn checked_abs(self) -> Result<Decimal, ArithmeticError> {
        if self.mantissa >= 0 {
            return Ok(self);
        }
        self.checked_neg()
    }

    /// Compound `(self × numerator) / denominator` with 256-bit intermediate.
    ///
    /// Prevents silent overflow that occurs when `self × numerator` exceeds
    /// `i128::MAX` in a naive two-step `mul` then `div`.
    pub fn checked_mul_div(
        self,
        numerator: Decimal,
        denominator: Decimal,
    ) -> Result<Decimal, ArithmeticError> {
        if denominator.mantissa == 0 {
            return Err(ArithmeticError::DivisionByZero);
        }

        let sign = sign3(self.mantissa, numerator.mantissa, denominator.mantissa);

        let a = self.mantissa.unsigned_abs();
        let b = numerator.mantissa.unsigned_abs();
        let c = denominator.mantissa.unsigned_abs();

        let product = U256::mul(a, b);
        let (quotient_u128, _rem) =
            product.checked_div(c).ok_or(ArithmeticError::Overflow)?;

        let mantissa_abs =
            i128::try_from(quotient_u128).map_err(|_| ArithmeticError::Overflow)?;

        let signed_mantissa = match sign {
            Sign::Zero => 0i128,
            Sign::Positive => mantissa_abs,
            Sign::Negative => {
                mantissa_abs.checked_neg().ok_or(ArithmeticError::Overflow)?
            }
        };

        let num_scale = self.scale as i32 + numerator.scale as i32;
        let den_scale = denominator.scale as i32;
        let result_scale = num_scale - den_scale;
        if result_scale < 0 || result_scale > MAX_SCALE as i32 {
            return Err(ArithmeticError::ScaleExceeded);
        }

        Decimal::new(signed_mantissa, result_scale as u8)
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pow10_table_spot_checks() {
        assert_eq!(pow10(0).unwrap(), 1);
        assert_eq!(pow10(6).unwrap(), 1_000_000);
        assert_eq!(pow10(18).unwrap(), 1_000_000_000_000_000_000);
        assert_eq!(
            pow10(28).unwrap(),
            10_000_000_000_000_000_000_000_000_000i128
        );
        assert!(pow10(29).is_err());
    }

    #[test]
    fn u256_mul_small() {
        assert_eq!(U256::mul(3, 7), U256 { lo: 21, hi: 0 });
    }

    #[test]
    fn u256_mul_max_times_max() {
        let r = U256::mul(u128::MAX, u128::MAX);
        assert_eq!(r.lo, 1);
        assert_eq!(r.hi, u128::MAX - 1);
    }

    #[test]
    fn u256_div_basic() {
        assert_eq!(U256 { lo: 21, hi: 0 }.checked_div(7), Some((3, 0)));
    }

    #[test]
    fn u256_div_by_zero() {
        assert_eq!(U256::ZERO.checked_div(0), None);
    }

    #[test]
    fn u256_div_overflow_check() {
        assert_eq!(U256 { lo: 0, hi: 100 }.checked_div(50), None);
    }
}