det-prime 0.1.1

Deterministic, zero-entropy primality testing, prime hashing, and modular square roots backed by Dirichlet-GRH witness bounds.
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
use core::cmp::Ordering;
use core::fmt;

use crate::Error;

/// Stack-allocated unsigned integer with `LIMBS` 64-bit little-endian limbs.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Uint<const LIMBS: usize>(pub [u64; LIMBS]);

pub type U128 = Uint<2>;
pub type U256 = Uint<4>;
pub type U384 = Uint<6>;
pub type U512 = Uint<8>;
pub type U1024 = Uint<16>;
pub type U2048 = Uint<32>;
pub type U4096 = Uint<64>;

impl<const LIMBS: usize> Default for Uint<LIMBS> {
    fn default() -> Self {
        Self::ZERO
    }
}

impl<const LIMBS: usize> Ord for Uint<LIMBS> {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        let mut i = LIMBS;
        while i > 0 {
            i -= 1;
            match self.0[i].cmp(&other.0[i]) {
                Ordering::Equal => continue,
                ord => return ord,
            }
        }
        Ordering::Equal
    }
}

impl<const LIMBS: usize> PartialOrd for Uint<LIMBS> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<const LIMBS: usize> fmt::Debug for Uint<LIMBS> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x")?;
        if self.is_zero() {
            return write!(f, "0");
        }
        let mut started = false;
        let mut i = LIMBS;
        while i > 0 {
            i -= 1;
            let w = self.0[i];
            if !started {
                if w != 0 {
                    write!(f, "{w:x}")?;
                    started = true;
                }
            } else {
                write!(f, "{w:016x}")?;
            }
        }
        Ok(())
    }
}

impl<const LIMBS: usize> fmt::Display for Uint<LIMBS> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl<const LIMBS: usize> Uint<LIMBS> {
    pub const ZERO: Self = Self([0u64; LIMBS]);

    #[inline]
    pub const fn from_limbs(limbs: [u64; LIMBS]) -> Self {
        Self(limbs)
    }

    pub const ONE: Self = {
        let mut limbs = [0u64; LIMBS];
        if LIMBS > 0 {
            limbs[0] = 1;
        }
        Self(limbs)
    };

    pub const TWO: Self = {
        let mut limbs = [0u64; LIMBS];
        if LIMBS > 0 {
            limbs[0] = 2;
        }
        Self(limbs)
    };

    #[inline]
    pub const fn from_u64(v: u64) -> Self {
        let mut limbs = [0u64; LIMBS];
        if LIMBS > 0 {
            limbs[0] = v;
        }
        Self(limbs)
    }

    #[inline]
    pub const fn as_u64(&self) -> Option<u64> {
        if LIMBS == 0 {
            return Some(0);
        }
        let mut i = 1;
        while i < LIMBS {
            if self.0[i] != 0 {
                return None;
            }
            i += 1;
        }
        Some(self.0[0])
    }

    #[inline]
    pub const fn is_zero(&self) -> bool {
        let mut i = 0;
        while i < LIMBS {
            if self.0[i] != 0 {
                return false;
            }
            i += 1;
        }
        true
    }

    #[inline]
    pub const fn is_one(&self) -> bool {
        if LIMBS == 0 || self.0[0] != 1 {
            return false;
        }
        let mut i = 1;
        while i < LIMBS {
            if self.0[i] != 0 {
                return false;
            }
            i += 1;
        }
        true
    }

    #[inline]
    pub const fn is_even(&self) -> bool {
        if LIMBS == 0 {
            true
        } else {
            (self.0[0] & 1) == 0
        }
    }

    #[inline]
    pub const fn is_odd(&self) -> bool {
        !self.is_even()
    }

    /// Parses a big-endian byte slice into `Uint<LIMBS>`.
    pub fn from_be_bytes(bytes: &[u8]) -> Result<Self, Error> {
        let first_nz = bytes.iter().position(|&b| b != 0).unwrap_or(bytes.len());
        let sig = &bytes[first_nz..];
        if sig.len() > LIMBS * 8 {
            return Err(Error::Overflow);
        }
        let mut limbs = [0u64; LIMBS];
        for (idx, &b) in sig.iter().rev().enumerate() {
            let limb_idx = idx / 8;
            let shift = (idx % 8) * 8;
            limbs[limb_idx] |= (b as u64) << shift;
        }
        Ok(Self(limbs))
    }

    /// Writes `self` into `out` as big-endian bytes if `out` is large enough.
    pub fn write_be_bytes(&self, out: &mut [u8]) -> Result<(), Error> {
        let needed = (self.bits() as usize).div_ceil(8);
        if out.len() < needed {
            return Err(Error::Overflow);
        }
        out.fill(0);
        let len = out.len();
        for idx in 0..needed {
            let limb_idx = idx / 8;
            let shift = (idx % 8) * 8;
            out[len - 1 - idx] = ((self.0[limb_idx] >> shift) & 0xff) as u8;
        }
        Ok(())
    }

    /// Parses an ASCII hexadecimal string (with optional `0x` prefix and `_` separators).
    pub fn from_hex(s: &str) -> Result<Self, Error> {
        let raw = s
            .strip_prefix("0x")
            .or_else(|| s.strip_prefix("0X"))
            .unwrap_or(s);
        if raw.is_empty() {
            return Err(Error::InvalidInput);
        }
        let mut acc = Self::ZERO;
        let mut saw_digit = false;
        for b in raw.bytes() {
            if b == b'_' {
                continue;
            }
            let nibble = match b {
                b'0'..=b'9' => b - b'0',
                b'a'..=b'f' => b - b'a' + 10,
                b'A'..=b'F' => b - b'A' + 10,
                _ => return Err(Error::InvalidInput),
            };
            saw_digit = true;
            acc = acc
                .checked_mul_u64(16)
                .and_then(|v| v.checked_add_u64(nibble as u64))
                .ok_or(Error::Overflow)?;
        }
        if !saw_digit {
            return Err(Error::InvalidInput);
        }
        Ok(acc)
    }

    /// Parses an ASCII base-10 string (with optional `_` separators).
    pub fn from_decimal(s: &str) -> Result<Self, Error> {
        if s.is_empty() {
            return Err(Error::InvalidInput);
        }
        let mut acc = Self::ZERO;
        let mut saw_digit = false;
        for b in s.bytes() {
            if b == b'_' {
                continue;
            }
            if !b.is_ascii_digit() {
                return Err(Error::InvalidInput);
            }
            saw_digit = true;
            acc = acc
                .checked_mul_u64(10)
                .and_then(|v| v.checked_add_u64((b - b'0') as u64))
                .ok_or(Error::Overflow)?;
        }
        if !saw_digit {
            return Err(Error::InvalidInput);
        }
        Ok(acc)
    }

    /// Bit length of `self` (`0` for `ZERO`).
    #[inline]
    pub const fn bits(&self) -> u32 {
        let mut i = LIMBS;
        while i > 0 {
            i -= 1;
            let w = self.0[i];
            if w != 0 {
                return (i as u32) * 64 + (64 - w.leading_zeros());
            }
        }
        0
    }

    /// Returns bit `idx` (0-indexed from least significant bit).
    #[inline]
    pub const fn bit(&self, idx: u32) -> bool {
        let limb = (idx / 64) as usize;
        let offset = idx % 64;
        if limb >= LIMBS {
            false
        } else {
            ((self.0[limb] >> offset) & 1) != 0
        }
    }

    /// Number of trailing zero bits (`64 * LIMBS` if `self == ZERO`).
    #[inline]
    pub const fn trailing_zeros(&self) -> u32 {
        let mut i = 0;
        while i < LIMBS {
            if self.0[i] != 0 {
                return (i as u32) * 64 + self.0[i].trailing_zeros();
            }
            i += 1;
        }
        (LIMBS as u32) * 64
    }

    /// Extracts `(k, m)` for `self >= 2` such that `bits = k + 1` and `m` is the top
    /// 4 fractional bits after the leading 1 bit (`2^k <= self < 2^k * (17 + m) / 16`).
    #[inline]
    pub const fn log2_mantissa4(&self) -> (u32, u32) {
        let b = self.bits();
        if b < 2 {
            return (0, 0);
        }
        let k = b - 1;
        let mut m = 0u32;
        let mut step = 0u32;
        while step < 4 {
            m <<= 1;
            if k > step && self.bit(k - 1 - step) {
                m |= 1;
            }
            step += 1;
        }
        (k, m)
    }

    #[inline]
    pub fn carrying_add(&self, rhs: &Self) -> (Self, bool) {
        let mut out = [0u64; LIMBS];
        let mut carry = 0u128;
        for (i, dst) in out.iter_mut().enumerate() {
            let sum = (self.0[i] as u128) + (rhs.0[i] as u128) + carry;
            *dst = sum as u64;
            carry = sum >> 64;
        }
        (Self(out), carry != 0)
    }

    #[inline]
    pub fn checked_add(&self, rhs: &Self) -> Option<Self> {
        let (sum, carry) = self.carrying_add(rhs);
        if carry {
            None
        } else {
            Some(sum)
        }
    }

    #[inline]
    pub fn checked_add_u64(&self, rhs: u64) -> Option<Self> {
        if LIMBS == 0 {
            return if rhs == 0 { Some(Self::ZERO) } else { None };
        }
        let mut out = self.0;
        let mut carry = rhs as u128;
        for limb in &mut out {
            if carry == 0 {
                break;
            }
            let sum = (*limb as u128) + carry;
            *limb = sum as u64;
            carry = sum >> 64;
        }
        if carry != 0 {
            None
        } else {
            Some(Self(out))
        }
    }

    #[inline]
    pub fn borrowing_sub(&self, rhs: &Self) -> (Self, bool) {
        let mut out = [0u64; LIMBS];
        let mut borrow = 0u128;
        for (i, dst) in out.iter_mut().enumerate() {
            let a = self.0[i] as u128;
            let b = (rhs.0[i] as u128) + borrow;
            if a >= b {
                *dst = (a - b) as u64;
                borrow = 0;
            } else {
                *dst = ((1u128 << 64) + a - b) as u64;
                borrow = 1;
            }
        }
        (Self(out), borrow != 0)
    }

    #[inline]
    pub fn checked_sub(&self, rhs: &Self) -> Option<Self> {
        let (diff, borrow) = self.borrowing_sub(rhs);
        if borrow {
            None
        } else {
            Some(diff)
        }
    }

    #[inline]
    pub fn wrapping_sub(&self, rhs: &Self) -> Self {
        self.borrowing_sub(rhs).0
    }

    #[inline]
    pub fn checked_mul_u64(&self, rhs: u64) -> Option<Self> {
        let mut out = [0u64; LIMBS];
        let mut carry = 0u128;
        for (i, dst) in out.iter_mut().enumerate() {
            let prod = (self.0[i] as u128) * (rhs as u128) + carry;
            *dst = prod as u64;
            carry = prod >> 64;
        }
        if carry != 0 {
            None
        } else {
            Some(Self(out))
        }
    }

    /// Remainder `self % d` for `d > 0`.
    #[inline]
    pub fn rem_u64(&self, d: u64) -> u64 {
        debug_assert!(d != 0);
        let mut rem = 0u128;
        let d128 = d as u128;
        let mut i = LIMBS;
        while i > 0 {
            i -= 1;
            rem = ((rem << 64) | (self.0[i] as u128)) % d128;
        }
        rem as u64
    }

    /// Right-shifts `self` by `bits` positions.
    pub fn shr(&self, shift: u32) -> Self {
        let total_bits = (LIMBS as u32) * 64;
        if shift >= total_bits {
            return Self::ZERO;
        }
        let word_shift = (shift / 64) as usize;
        let bit_shift = shift % 64;
        let mut out = [0u64; LIMBS];
        for (i, dst) in out.iter_mut().enumerate().take(LIMBS - word_shift) {
            let mut v = self.0[i + word_shift] >> bit_shift;
            if bit_shift != 0 && i + word_shift + 1 < LIMBS {
                v |= self.0[i + word_shift + 1] << (64 - bit_shift);
            }
            *dst = v;
        }
        Self(out)
    }

    #[inline]
    pub fn shr1_assign(&mut self) {
        let mut carry = 0u64;
        let mut i = LIMBS;
        while i > 0 {
            i -= 1;
            let next_carry = self.0[i] << 63;
            self.0[i] = (self.0[i] >> 1) | carry;
            carry = next_carry;
        }
    }

    /// Modular addition `(a + b) mod m`, assuming `a < m` and `b < m`.
    #[inline]
    pub fn add_mod(a: &Self, b: &Self, m: &Self) -> Self {
        let (sum, carry) = a.carrying_add(b);
        if carry || &sum >= m {
            sum.wrapping_sub(m)
        } else {
            sum
        }
    }

    /// Modular subtraction `(a - b) mod m`, assuming `a < m` and `b < m`.
    #[inline]
    pub fn sub_mod(a: &Self, b: &Self, m: &Self) -> Self {
        if a >= b {
            a.wrapping_sub(b)
        } else {
            let diff = b.wrapping_sub(a);
            m.wrapping_sub(&diff)
        }
    }

    /// Binary GCD (Stein's algorithm).
    pub fn gcd(&self, other: &Self) -> Self {
        if self.is_zero() {
            return *other;
        }
        if other.is_zero() {
            return *self;
        }
        let shift = self.trailing_zeros().min(other.trailing_zeros());
        let mut u = self.shr(self.trailing_zeros());
        let mut v = other.shr(other.trailing_zeros());
        loop {
            if u > v {
                core::mem::swap(&mut u, &mut v);
            }
            v = v.wrapping_sub(&u);
            if v.is_zero() {
                // Shift `u` left by `shift`.
                return u.shl_exact(shift);
            }
            v = v.shr(v.trailing_zeros());
        }
    }

    fn shl_exact(&self, shift: u32) -> Self {
        if shift == 0 {
            return *self;
        }
        let word_shift = (shift / 64) as usize;
        let bit_shift = shift % 64;
        let mut out = [0u64; LIMBS];
        for (i, dst) in out.iter_mut().enumerate().skip(word_shift) {
            let src = i - word_shift;
            let mut v = self.0[src] << bit_shift;
            if bit_shift != 0 && src > 0 {
                v |= self.0[src - 1] >> (64 - bit_shift);
            }
            *dst = v;
        }
        Self(out)
    }
}

/// Montgomery arithmetic context for an odd modulus `m > 1`.
#[derive(Clone, Copy, Debug)]
pub struct MontCtx<const LIMBS: usize> {
    m: Uint<LIMBS>,
    mu: u64,
    r2: Uint<LIMBS>,
    one_mont: Uint<LIMBS>,
    neg_one_mont: Uint<LIMBS>,
}

impl<const LIMBS: usize> MontCtx<LIMBS> {
    /// Builds a Montgomery context for an odd modulus `m >= 3`.
    pub fn new(m: Uint<LIMBS>) -> Option<Self> {
        if LIMBS == 0 || m.is_even() || m <= Uint::ONE {
            return None;
        }
        let m0 = m.0[0];
        let mut inv = 1u64;
        for _ in 0..6 {
            inv = inv.wrapping_mul(2u64.wrapping_sub(m0.wrapping_mul(inv)));
        }
        let mu = inv.wrapping_neg();

        // Compute R mod m (`one_mont`) and R^2 mod m (`r2`) by repeated modular doubling.
        let mut r = Uint::ONE;
        for _ in 0..(64 * LIMBS) {
            r = Uint::add_mod(&r, &r, &m);
        }
        let one_mont = r;
        for _ in 0..(64 * LIMBS) {
            r = Uint::add_mod(&r, &r, &m);
        }
        let r2 = r;
        let neg_one_mont = m.wrapping_sub(&one_mont);

        Some(Self {
            m,
            mu,
            r2,
            one_mont,
            neg_one_mont,
        })
    }

    #[inline]
    pub const fn modulus(&self) -> &Uint<LIMBS> {
        &self.m
    }

    #[inline]
    pub const fn one(&self) -> Uint<LIMBS> {
        self.one_mont
    }

    #[inline]
    pub const fn neg_one(&self) -> Uint<LIMBS> {
        self.neg_one_mont
    }

    /// Converts a reduced residue `0 <= x < m` into Montgomery form `x * R mod m`.
    #[inline]
    pub fn to_mont(&self, x: &Uint<LIMBS>) -> Uint<LIMBS> {
        self.mul(x, &self.r2)
    }

    /// Converts `u64` directly into `(x mod m) * R mod m`.
    #[inline]
    pub fn u64_to_mont(&self, x: u64) -> Uint<LIMBS> {
        let mut v = Uint::<LIMBS>::from_u64(x);
        if v >= self.m {
            // Only possible when LIMBS == 1 or m fits in u64.
            if let Some(m64) = self.m.as_u64() {
                v = Uint::from_u64(x % m64);
            }
        }
        self.to_mont(&v)
    }

    /// Converts out of Montgomery form `x * R^-1 mod m`.
    #[inline]
    pub fn from_mont(&self, x: &Uint<LIMBS>) -> Uint<LIMBS> {
        self.mul(x, &Uint::ONE)
    }

    /// CIOS Montgomery multiplication `(a * b * R^-1) mod m` for `0 <= a, b < m`.
    /// Handles full-width odd moduli up to `2^(64 * LIMBS) - 1`.
    #[inline(always)]
    #[allow(clippy::needless_range_loop)]
    pub fn mul(&self, a: &Uint<LIMBS>, b: &Uint<LIMBS>) -> Uint<LIMBS> {
        let mut t = [0u64; LIMBS];
        let mut t_extra: u128 = 0;

        for i in 0..LIMBS {
            let mut carry: u128 = 0;
            let ai = a.0[i] as u128;
            for j in 0..LIMBS {
                let prod = (t[j] as u128) + ai * (b.0[j] as u128) + carry;
                t[j] = prod as u64;
                carry = prod >> 64;
            }
            let sum_hi = t_extra + carry;

            let q = t[0].wrapping_mul(self.mu) as u128;
            let prod0 = (t[0] as u128) + q * (self.m.0[0] as u128);
            let mut carry2: u128 = prod0 >> 64;
            for j in 1..LIMBS {
                let prod = (t[j] as u128) + q * (self.m.0[j] as u128) + carry2;
                t[j - 1] = prod as u64;
                carry2 = prod >> 64;
            }
            let top = sum_hi + carry2;
            t[LIMBS - 1] = top as u64;
            t_extra = top >> 64;
        }

        let res = Uint(t);
        if t_extra != 0 || res >= self.m {
            res.wrapping_sub(&self.m)
        } else {
            res
        }
    }

    #[inline(always)]
    pub fn sqr(&self, a: &Uint<LIMBS>) -> Uint<LIMBS> {
        self.mul(a, a)
    }

    /// Modular exponentiation `base^exp` in Montgomery domain using a 4-bit sliding odd window.
    pub fn pow(&self, base_mont: &Uint<LIMBS>, exp: &Uint<LIMBS>) -> Uint<LIMBS> {
        let top = exp.bits();
        if top == 0 {
            return self.one_mont;
        }
        if top < 16 {
            let mut result = *base_mont;
            let mut i = top - 1;
            while i > 0 {
                i -= 1;
                result = self.sqr(&result);
                if exp.bit(i) {
                    result = self.mul(&result, base_mont);
                }
            }
            return result;
        }

        let b2 = self.sqr(base_mont);
        let mut odd = [*base_mont; 8];
        let mut k = 1usize;
        while k < 8 {
            odd[k] = self.mul(&odd[k - 1], &b2);
            k += 1;
        }

        let mut result = self.one_mont;
        let mut started = false;
        let mut i = top;
        while i > 0 {
            if !exp.bit(i - 1) {
                result = self.sqr(&result);
                i -= 1;
            } else {
                let win_len = if i >= 4 { 4 } else { i };
                let mut w = 0u32;
                let mut bit_off = 0u32;
                while bit_off < win_len {
                    w = (w << 1) | (exp.bit(i - 1 - bit_off) as u32);
                    bit_off += 1;
                }
                let tz = w.trailing_zeros();
                let eff_len = win_len - tz;
                let odd_val = w >> tz;
                if !started {
                    result = odd[(odd_val >> 1) as usize];
                    started = true;
                } else {
                    for _ in 0..eff_len {
                        result = self.sqr(&result);
                    }
                    result = self.mul(&result, &odd[(odd_val >> 1) as usize]);
                }
                i -= eff_len;
            }
        }
        result
    }
}