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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
//! A small pure-Rust bigint foundation for public-key primitives.
//!
//! The representation uses little-endian `u64` limbs because the surrounding
//! algorithms are naturally word-oriented. This is intentionally simple:
//! schoolbook multiplication and bitwise long division are easy to audit and
//! track the public-key formulas directly, while keeping the public-key layer
//! fully in Rust with no external arithmetic backend.
//!
//! Local references for planned multiplication-kernel upgrades:
//! - `pubs/comba-1990-exponentiation-cryptosystems-on-the-ibm-pc.pdf`
//! - `pubs/karatsuba-ofman-1963-multiplication-of-multidigit-numbers-on-automata.pdf`
use core::cmp::Ordering;
// Heuristic crossover where the recursive split starts beating schoolbook in
// this pure-Rust implementation on our benchmark hardware.
const KARATSUBA_THRESHOLD_LIMBS: usize = 32;
// Limit highly lopsided splits; beyond this ratio the extra recursion/temporary
// cost usually outweighs Karatsuba's multiplication count reduction.
const KARATSUBA_MAX_IMBALANCE: usize = 2;
/// Sign of a [`BigInt`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Sign {
/// Strictly positive value.
Positive,
/// Strictly negative value.
Negative,
/// Zero.
Zero,
}
/// Unsigned multiprecision integer stored as little-endian `u64` limbs.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BigUint {
limbs: Vec<u64>,
}
/// Signed multiprecision integer used by later public-key helpers.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BigInt {
sign: Sign,
magnitude: BigUint,
}
/// Montgomery arithmetic context for a fixed odd modulus.
///
/// Public-key schemes spend most of their time doing repeated modular
/// multiplication under one long-lived odd modulus. Precomputing the
/// Montgomery constants once avoids paying the setup cost on every multiply
/// while keeping the scheme code readable.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MontgomeryCtx {
modulus: BigUint,
// n0_inv = -n^{-1} mod 2^64 (Montgomery reduction coefficient).
n0_inv: u64,
// R^2 mod n with R = 2^(64 * limbs(n)): conversion factor into Montgomery form.
r2_mod: BigUint,
// 1 encoded in Montgomery form, i.e. R mod n.
one_mont: BigUint,
}
impl Ord for BigUint {
fn cmp(&self, other: &Self) -> Ordering {
// Ordering assumes normalized limb vectors (no most-significant zero
// limbs). All constructors/arithmetic paths call `normalize()`.
debug_assert!(
self.limbs.last().copied() != Some(0),
"BigUint invariant: no leading zero limbs",
);
debug_assert!(
other.limbs.last().copied() != Some(0),
"BigUint invariant: no leading zero limbs",
);
match self.limbs.len().cmp(&other.limbs.len()) {
Ordering::Equal => {}
ord => return ord,
}
for (&lhs, &rhs) in self.limbs.iter().rev().zip(other.limbs.iter().rev()) {
match lhs.cmp(&rhs) {
Ordering::Equal => {}
ord => return ord,
}
}
Ordering::Equal
}
}
impl PartialOrd for BigUint {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl BigUint {
/// Construct zero.
#[must_use]
pub fn zero() -> Self {
Self { limbs: Vec::new() }
}
/// Construct one.
#[must_use]
pub fn one() -> Self {
Self { limbs: vec![1] }
}
/// Construct from a machine word.
#[must_use]
pub fn from_u64(value: u64) -> Self {
if value == 0 {
Self::zero()
} else {
Self { limbs: vec![value] }
}
}
/// Construct from a `u128`.
///
/// # Panics
///
/// Panics only if the internal limb split invariants fail unexpectedly.
#[must_use]
pub fn from_u128(value: u128) -> Self {
if value == 0 {
return Self::zero();
}
let lo =
u64::try_from(value & u128::from(u64::MAX)).expect("low 64 bits always fit into u64");
let hi = u64::try_from(value >> 64).expect("high 64 bits always fit into u64");
if hi == 0 {
Self { limbs: vec![lo] }
} else {
Self {
limbs: vec![lo, hi],
}
}
}
/// Decode big-endian bytes.
///
/// Internally, limb 0 always stores the least-significant 64 bits.
#[must_use]
pub fn from_be_bytes(bytes: &[u8]) -> Self {
if bytes.is_empty() {
return Self::zero();
}
let mut limbs = Vec::with_capacity(bytes.len().div_ceil(8));
let mut acc = 0u64;
let mut shift = 0u32;
// Walk bytes from least-significant (last byte of the big-endian input)
// to most-significant, packing eight bytes at a time into a 64-bit limb.
// When `shift` reaches 64, the current limb is full — push it and start
// the next one. Any remaining bytes at the end form a partial limb.
for &byte in bytes.iter().rev() {
acc |= u64::from(byte) << shift;
shift += 8;
if shift == 64 {
limbs.push(acc);
acc = 0;
shift = 0;
}
}
if shift != 0 {
limbs.push(acc);
}
let mut out = Self { limbs };
out.normalize();
out
}
/// Encode as big-endian bytes without leading zero bytes.
///
/// Internally, limb 0 stores the least-significant 64 bits, so encoding
/// walks the limbs in reverse order and strips only the leading zero bytes
/// introduced by the fixed-width `u64` representation.
///
/// # Panics
///
/// Panics only if the internal representation is corrupt and a non-zero
/// value contains no non-zero bytes.
#[must_use]
pub fn to_be_bytes(&self) -> Vec<u8> {
if self.is_zero() {
return vec![0];
}
let mut out = Vec::with_capacity(self.limbs.len() * 8);
for &limb in self.limbs.iter().rev() {
out.extend_from_slice(&limb.to_be_bytes());
}
let first_nonzero = out
.iter()
.position(|&byte| byte != 0)
.expect("non-zero bigint must encode to at least one non-zero byte");
out.drain(0..first_nonzero);
out
}
/// Return whether the value is zero.
#[must_use]
pub fn is_zero(&self) -> bool {
self.limbs.is_empty()
}
/// Return whether the value is odd.
#[must_use]
pub fn is_odd(&self) -> bool {
!self.is_zero() && (self.limbs[0] & 1) == 1
}
/// Return whether the value is exactly one.
#[must_use]
pub fn is_one(&self) -> bool {
self.limbs.len() == 1 && self.limbs[0] == 1
}
/// Number of significant bits.
///
/// # Panics
///
/// Panics only if the internal representation is corrupt and a non-zero
/// value contains no limbs.
#[must_use]
pub fn bits(&self) -> usize {
if self.is_zero() {
return 0;
}
let top = *self
.limbs
.last()
.expect("non-zero bigint has at least one limb");
let top_bits = (u64::BITS - top.leading_zeros()) as usize;
(self.limbs.len() - 1) * 64 + top_bits
}
/// Integer square root: the largest `r` such that `r^2 <= self`.
#[must_use]
pub fn sqrt_floor(&self) -> Self {
if self.is_zero() {
return Self::zero();
}
if self.is_one() {
return Self::one();
}
let mut low = Self::one();
let mut high = Self::zero();
// Choose `high` so the search starts with `low^2 <= self < high^2`.
// Setting bit `ceil(bits(self) / 2)` makes
// `high = 2^ceil(bits(self)/2)`, so `high^2 >= 2^bits(self) > self`.
// That gives the binary search a proved upper bound from the start.
high.set_bit(self.bits().div_ceil(2));
while {
let next_low = low.add_ref(&Self::one());
next_low < high
} {
let mut middle = low.add_ref(&high);
middle.shr1();
let square = middle.square_ref();
if square <= *self {
low = middle;
} else {
high = middle;
}
}
low
}
/// Test bit `index`.
#[must_use]
pub fn bit(&self, index: usize) -> bool {
let limb = index / 64;
let shift = index % 64;
if limb >= self.limbs.len() {
false
} else {
((self.limbs[limb] >> shift) & 1) == 1
}
}
/// Set bit `index`.
pub fn set_bit(&mut self, index: usize) {
let limb = index / 64;
let shift = index % 64;
if self.limbs.len() <= limb {
self.limbs.resize(limb + 1, 0);
}
self.limbs[limb] |= 1u64 << shift;
}
/// Add another bigint in place.
///
/// # Panics
///
/// Panics only if the internal `u128` accumulator cannot be split back
/// into `u64` limbs, which would indicate a logic error.
pub fn add_assign_ref(&mut self, other: &Self) {
if other.is_zero() {
return;
}
if self.limbs.len() < other.limbs.len() {
self.limbs.resize(other.limbs.len(), 0);
}
let mut carry = 0u128;
for i in 0..other.limbs.len() {
let sum = u128::from(self.limbs[i]) + u128::from(other.limbs[i]) + carry;
self.limbs[i] = low_u64(sum);
carry = sum >> 64;
}
let mut i = other.limbs.len();
while carry != 0 && i < self.limbs.len() {
let sum = u128::from(self.limbs[i]) + carry;
self.limbs[i] = low_u64(sum);
carry = sum >> 64;
i += 1;
}
if carry != 0 {
self.limbs
.push(u64::try_from(carry).expect("final carry from u64 addition is at most 1"));
}
}
/// Return `self + other`.
#[must_use]
pub fn add_ref(&self, other: &Self) -> Self {
let mut out = self.clone();
out.add_assign_ref(other);
out
}
/// Subtract another bigint in place. Panics if `self < other`.
///
/// # Panics
///
/// Panics if `self < other`.
pub fn sub_assign_ref(&mut self, other: &Self) {
assert!((*self).cmp(other) != Ordering::Less, "BigUint underflow");
if other.is_zero() {
return;
}
let mut borrow = 0u128;
for i in 0..self.limbs.len() {
let lhs = u128::from(self.limbs[i]);
let rhs = if i < other.limbs.len() {
u128::from(other.limbs[i])
} else {
0
};
let subtrahend = rhs + borrow;
if lhs >= subtrahend {
self.limbs[i] = low_u64(lhs - subtrahend);
borrow = 0;
} else {
self.limbs[i] = low_u64((1u128 << 64) + lhs - subtrahend);
borrow = 1;
}
}
self.normalize();
}
/// Return `self - other`. Panics if `self < other`.
#[must_use]
pub fn sub_ref(&self, other: &Self) -> Self {
let mut out = self.clone();
out.sub_assign_ref(other);
out
}
/// Multiply two big integers.
///
/// # Panics
///
/// Panics only if the internal `u128` accumulators cannot be split back
/// into `u64` limbs, which would indicate a logic error.
#[must_use]
pub fn mul_ref(&self, other: &Self) -> Self {
if self.is_zero() || other.is_zero() {
return Self::zero();
}
if Self::should_use_karatsuba(self, other) {
return self.mul_karatsuba_ref(other);
}
Self::mul_schoolbook_ref(self, other)
}
/// Multiply a value by itself.
#[must_use]
pub fn square_ref(&self) -> Self {
self.mul_ref(self)
}
fn split_at_limb(&self, split: usize) -> (Self, Self) {
let low_end = split.min(self.limbs.len());
let mut low = Self {
limbs: self.limbs[..low_end].to_vec(),
};
low.normalize();
if split >= self.limbs.len() {
return (low, Self::zero());
}
let mut high = Self {
limbs: self.limbs[split..].to_vec(),
};
high.normalize();
(low, high)
}
fn should_use_karatsuba(lhs: &Self, rhs: &Self) -> bool {
let short = lhs.limbs.len().min(rhs.limbs.len());
let long = lhs.limbs.len().max(rhs.limbs.len());
short >= KARATSUBA_THRESHOLD_LIMBS && long <= short * KARATSUBA_MAX_IMBALANCE
}
fn mul_karatsuba_ref(&self, other: &Self) -> Self {
let split = self.limbs.len().max(other.limbs.len()) / 2;
if split == 0 {
return Self::mul_schoolbook_ref(self, other);
}
let (a0, a1) = self.split_at_limb(split);
let (b0, b1) = other.split_at_limb(split);
if a1.is_zero() || b1.is_zero() {
return Self::mul_schoolbook_ref(self, other);
}
let z0 = a0.mul_ref(&b0);
let z2 = a1.mul_ref(&b1);
let a_sum = a0.add_ref(&a1);
let b_sum = b0.add_ref(&b1);
let mut z1 = a_sum.mul_ref(&b_sum);
z1.sub_assign_ref(&z0);
z1.sub_assign_ref(&z2);
let mut out = z0;
z1.shl_bits(split * 64);
out.add_assign_ref(&z1);
let mut z2_shifted = z2;
z2_shifted.shl_bits(split * 128);
out.add_assign_ref(&z2_shifted);
out
}
fn mul_schoolbook_ref(lhs: &Self, rhs: &Self) -> Self {
let mut out = vec![0u64; lhs.limbs.len() + rhs.limbs.len()];
for (i, &lhs_limb) in lhs.limbs.iter().enumerate() {
let mut carry = 0u128;
for (j, &rhs_limb) in rhs.limbs.iter().enumerate() {
let idx = i + j;
let acc =
u128::from(out[idx]) + u128::from(lhs_limb) * u128::from(rhs_limb) + carry;
out[idx] = low_u64(acc);
carry = acc >> 64;
}
let mut idx = i + rhs.limbs.len();
while carry != 0 {
let acc = u128::from(out[idx]) + carry;
out[idx] = low_u64(acc);
carry = acc >> 64;
idx += 1;
}
}
let mut result = Self { limbs: out };
// A normalized non-zero multiplicand and multiplier cannot produce a
// spuriously zero high limb except through the carry chain itself, so
// one post-pass normalization is enough.
result.normalize();
result
}
/// Shift left by one bit.
pub fn shl1(&mut self) {
if self.is_zero() {
return;
}
let mut carry = 0u64;
for limb in &mut self.limbs {
let next = *limb >> 63;
*limb = (*limb << 1) | carry;
carry = next;
}
if carry != 0 {
self.limbs.push(carry);
}
// A left shift on an already-normalized value cannot introduce a
// leading zero limb, so no normalize() pass is required here.
}
/// Shift right by one bit.
pub fn shr1(&mut self) {
if self.is_zero() {
return;
}
let mut carry = 0u64;
for limb in self.limbs.iter_mut().rev() {
let next = (*limb & 1) << 63;
*limb = (*limb >> 1) | carry;
carry = next;
}
self.normalize();
}
/// XOR another bigint into `self` in place (GF(2^m) field addition).
///
/// Extends `self.limbs` with zeros if shorter than `other.limbs`, then
/// XORs each corresponding limb pair. The result is normalized to strip
/// any leading zero limbs produced by XOR cancellation.
pub fn bitxor_assign(&mut self, other: &BigUint) {
if self.limbs.len() < other.limbs.len() {
self.limbs.resize(other.limbs.len(), 0);
}
for (s, &o) in self.limbs.iter_mut().zip(other.limbs.iter()) {
*s ^= o;
}
self.normalize();
}
/// Left-shift by `n` bits.
///
/// Implemented as `n / 64` full-limb shifts (inserting zero limbs at the
/// low end) followed by up to 63 single-bit left shifts, which avoids
/// undefined behaviour from shifting a `u64` by 64 or more positions.
pub fn shl_bits(&mut self, n: usize) {
if self.is_zero() || n == 0 {
return;
}
let limb_shifts = n / 64;
let bit_shifts = n % 64;
// Full-limb shift: prepend zeros at the low (index 0) end.
if limb_shifts > 0 {
let mut new_limbs = vec![0u64; limb_shifts];
new_limbs.extend_from_slice(&self.limbs);
self.limbs = new_limbs;
}
// Remaining bit-level shift (0 < bit_shifts < 64, so 64 - bit_shifts is safe).
if bit_shifts > 0 {
let mut carry = 0u64;
for limb in &mut self.limbs {
let next_carry = *limb >> (64 - bit_shifts);
*limb = (*limb << bit_shifts) | carry;
carry = next_carry;
}
if carry != 0 {
self.limbs.push(carry);
}
}
// A left-shift on a normalized value cannot introduce a leading zero
// limb, so no normalize() pass is needed here.
}
/// Compute `self mod modulus`.
#[must_use]
pub fn modulo(&self, modulus: &Self) -> Self {
let (_, remainder) = self.div_rem(modulus);
remainder
}
/// Compute the remainder modulo a machine word.
///
/// # Panics
///
/// Panics if `modulus == 0`.
#[must_use]
pub fn rem_u64(&self, modulus: u64) -> u64 {
assert!(modulus != 0, "division by zero");
if self.is_zero() {
return 0;
}
let mut remainder = 0u128;
// Horner's method in base `2^64`: carry the remainder of the already
// processed high limbs, then append the next limb as the next base
// digit before reducing again.
for &limb in self.limbs.iter().rev() {
let acc = (remainder << 64) | u128::from(limb);
remainder = acc % u128::from(modulus);
}
u64::try_from(remainder).expect("remainder modulo u64 fits into u64")
}
/// Compute `(lhs * rhs) mod modulus`.
///
/// Odd moduli use a fresh Montgomery context so the common public-key path
/// avoids the division-heavy fallback. Even moduli keep the old
/// double-and-add reducer because Montgomery requires an odd modulus.
/// Rewriting one multiplicand as `y - 1` plus one extra add can change the
/// operand parity, but it does not change the modulus parity; the core
/// Montgomery requirement is `gcd(R, n) = 1`, so an even modulus still
/// needs a non-Montgomery path.
///
/// # Panics
///
/// Panics if `modulus == 0`.
#[must_use]
pub fn mod_mul(lhs: &Self, rhs: &Self, modulus: &Self) -> Self {
assert!(!modulus.is_zero(), "modulus must be non-zero");
if modulus == &Self::one() {
return Self::zero();
}
if let Some(ctx) = MontgomeryCtx::new(modulus) {
return ctx.mul(lhs, rhs);
}
Self::mod_mul_plain(lhs, rhs, modulus)
}
/// Compute `(lhs * rhs) mod modulus` using the simple double-and-add
/// fallback implementation.
///
/// The result is mathematically correct, but repeated division-based
/// reduction makes it much slower than Montgomery multiplication for the
/// odd moduli that dominate public-key code. The current scheme code only
/// reaches this path for even moduli, so it remains as the explicit
/// fallback and readable reference for non-Montgomery cases.
#[must_use]
pub(crate) fn mod_mul_plain(lhs: &Self, rhs: &Self, modulus: &Self) -> Self {
if lhs.is_zero() || rhs.is_zero() {
return Self::zero();
}
let mut a = lhs.modulo(modulus);
let mut b = rhs.clone();
let mut out = Self::zero();
while !b.is_zero() {
if b.is_odd() {
out = out.add_ref(&a).modulo(modulus);
}
a = a.add_ref(&a).modulo(modulus);
b.shr1();
}
out
}
/// Return `(quotient, remainder)` for Euclidean division. Panics on zero divisor.
///
/// # Panics
///
/// Panics if `divisor == 0`.
#[must_use]
pub fn div_rem(&self, divisor: &Self) -> (Self, Self) {
assert!(!divisor.is_zero(), "division by zero");
if self.cmp(divisor) == Ordering::Less {
return (Self::zero(), self.clone());
}
let mut quotient = Self::zero();
let mut remainder = Self::zero();
// Bit-by-bit long division. `remainder` holds the partially
// reconstructed dividend prefix; each step shifts it left, appends the
// next source bit, and subtracts the divisor if the prefix is already
// large enough.
for bit in (0..self.bits()).rev() {
remainder.shl1();
if self.bit(bit) {
if remainder.is_zero() {
remainder.limbs.push(1);
} else {
remainder.limbs[0] |= 1;
}
}
if remainder.cmp(divisor) != Ordering::Less {
remainder.sub_assign_ref(divisor);
quotient.set_bit(bit);
}
}
(quotient, remainder)
}
fn normalize(&mut self) {
// Canonical representation invariant:
// - zero has `limbs.is_empty()`
// - non-zero values have a non-zero top limb
while self.limbs.last().copied() == Some(0) {
self.limbs.pop();
}
}
fn limb_or_zero(&self, idx: usize) -> u64 {
self.limbs.get(idx).copied().unwrap_or(0)
}
fn montgomery_mul_odd_with_workspace(
lhs: &Self,
rhs: &Self,
modulus: &Self,
n0_inv: u64,
workspace: &mut Vec<u64>,
) -> Self {
debug_assert!(modulus.is_odd(), "Montgomery path requires an odd modulus");
let width = modulus.limbs.len();
// `2 * width` limbs hold the schoolbook product. The extra two limbs
// are carry headroom so neither pass can run off the end.
let needed = width * 2 + 2;
if workspace.len() != needed {
workspace.resize(needed, 0);
} else {
workspace.fill(0);
}
// First pass: accumulate the ordinary product `lhs * rhs`.
for i in 0..width {
let lhs_limb = lhs.limb_or_zero(i);
let mut carry = 0u128;
for j in 0..width {
let idx = i + j;
let acc = u128::from(workspace[idx])
+ u128::from(lhs_limb) * u128::from(rhs.limb_or_zero(j))
+ carry;
workspace[idx] = low_u64(acc);
carry = acc >> 64;
}
let mut idx = i + width;
while carry != 0 {
let acc = u128::from(workspace[idx]) + carry;
workspace[idx] = low_u64(acc);
carry = acc >> 64;
idx += 1;
}
}
// Second pass: Montgomery reduction. Choose `m` so the current low
// limb cancels modulo `2^64`, then add `m * modulus`. Each round
// zeros one more low limb; after `width` rounds the discarded low half
// accounts for the implicit division by `R = 2^(64w)`, so the high
// half is `lhs * rhs * R^-1 mod n`. That is why copying out
// `workspace[width..]` yields the Montgomery product.
for i in 0..width {
let m = workspace[i].wrapping_mul(n0_inv);
let mut carry = 0u128;
for j in 0..width {
let idx = i + j;
let acc = u128::from(workspace[idx])
+ u128::from(m) * u128::from(modulus.limb_or_zero(j))
+ carry;
workspace[idx] = low_u64(acc);
carry = acc >> 64;
}
let mut idx = i + width;
while carry != 0 {
let acc = u128::from(workspace[idx]) + carry;
workspace[idx] = low_u64(acc);
carry = acc >> 64;
idx += 1;
}
}
let mut out = Self {
limbs: workspace[width..=(width * 2)].to_vec(),
};
out.normalize();
// Montgomery reduction leaves a value in `[0, 2n)`, so at most one
// subtraction is needed to return to the canonical residue range.
if out >= *modulus {
out.sub_assign_ref(modulus);
}
out
}
}
impl MontgomeryCtx {
fn encode_with_workspace(&self, value: &BigUint, workspace: &mut Vec<u64>) -> BigUint {
if value.is_zero() {
return BigUint::zero();
}
BigUint::montgomery_mul_odd_with_workspace(
&value.modulo(&self.modulus),
&self.r2_mod,
&self.modulus,
self.n0_inv,
workspace,
)
}
fn decode_with_workspace(&self, value: &BigUint, workspace: &mut Vec<u64>) -> BigUint {
BigUint::montgomery_mul_odd_with_workspace(
value,
&BigUint::one(),
&self.modulus,
self.n0_inv,
workspace,
)
}
fn pow_encoded_with_workspace(
&self,
base_mont: &BigUint,
exponent: &BigUint,
workspace: &mut Vec<u64>,
) -> BigUint {
if self.modulus == BigUint::one() {
return BigUint::zero();
}
let mut result = self.one_mont.clone();
let mut power = base_mont.clone();
for bit in 0..exponent.bits() {
if exponent.bit(bit) {
result = BigUint::montgomery_mul_odd_with_workspace(
&result,
&power,
&self.modulus,
self.n0_inv,
workspace,
);
}
power = BigUint::montgomery_mul_odd_with_workspace(
&power,
&power,
&self.modulus,
self.n0_inv,
workspace,
);
}
self.decode_with_workspace(&result, workspace)
}
/// Build a Montgomery context for a non-zero odd modulus.
#[must_use]
pub fn new(modulus: &BigUint) -> Option<Self> {
if modulus.is_zero() || !modulus.is_odd() {
return None;
}
let n0_inv = montgomery_n0_inv(modulus.limbs[0]);
// With `w` limbs, Montgomery arithmetic uses `R = 2^(64w)`. `R^2 mod
// n` is the standard conversion factor for entering the Montgomery
// domain because `montgomery_mul(a, R^2) = a * R^2 * R^-1 = aR`, the
// Montgomery encoding of the ordinary residue `a`.
let mut r2 = BigUint::zero();
r2.set_bit(modulus.limbs.len() * 128);
let r2_mod = r2.modulo(modulus);
// `R mod n` is the Montgomery encoding of 1, stored so exponentiation
// can start its accumulator in the correct domain.
let mut r = BigUint::zero();
r.set_bit(modulus.limbs.len() * 64);
let one_mont = r.modulo(modulus);
Some(Self {
modulus: modulus.clone(),
n0_inv,
r2_mod,
one_mont,
})
}
/// Return the odd modulus this context was built for.
#[must_use]
pub fn modulus(&self) -> &BigUint {
&self.modulus
}
/// Convert an ordinary residue into Montgomery form.
#[must_use]
pub fn encode(&self, value: &BigUint) -> BigUint {
let mut workspace = Vec::new();
self.encode_with_workspace(value, &mut workspace)
}
/// Convert a Montgomery residue back to the ordinary representation.
#[must_use]
pub fn decode(&self, value: &BigUint) -> BigUint {
let mut workspace = Vec::new();
self.decode_with_workspace(value, &mut workspace)
}
/// Multiply two ordinary residues modulo the context modulus.
#[must_use]
pub fn mul(&self, lhs: &BigUint, rhs: &BigUint) -> BigUint {
let mut workspace = Vec::new();
let lhs_mont = self.encode_with_workspace(lhs, &mut workspace);
let rhs_mont = self.encode_with_workspace(rhs, &mut workspace);
let product_mont = BigUint::montgomery_mul_odd_with_workspace(
&lhs_mont,
&rhs_mont,
&self.modulus,
self.n0_inv,
&mut workspace,
);
self.decode_with_workspace(&product_mont, &mut workspace)
}
/// Square one ordinary residue modulo the context modulus.
#[must_use]
pub fn square(&self, value: &BigUint) -> BigUint {
let mut workspace = Vec::new();
let value_mont = self.encode_with_workspace(value, &mut workspace);
let square_mont = BigUint::montgomery_mul_odd_with_workspace(
&value_mont,
&value_mont,
&self.modulus,
self.n0_inv,
&mut workspace,
);
self.decode_with_workspace(&square_mont, &mut workspace)
}
/// Compute `base^exponent mod modulus` inside the context.
#[must_use]
pub fn pow(&self, base: &BigUint, exponent: &BigUint) -> BigUint {
let mut workspace = Vec::new();
let base_mont = self.encode_with_workspace(&base.modulo(&self.modulus), &mut workspace);
self.pow_encoded_with_workspace(&base_mont, exponent, &mut workspace)
}
/// Compute `base^exponent mod modulus` with `base` already in Montgomery form.
///
/// This is useful when callers reuse the same base and can cache the
/// encoded value once.
#[must_use]
pub fn pow_encoded(&self, base_mont: &BigUint, exponent: &BigUint) -> BigUint {
let mut workspace = Vec::new();
self.pow_encoded_with_workspace(base_mont, exponent, &mut workspace)
}
}
impl Drop for BigUint {
fn drop(&mut self) {
// BigUint backs private exponents, prime factors, and nonces in the
// public-key layer. Clear the limb buffer on drop so those values do
// not linger in freed heap memory.
crate::ct::zeroize_slice(self.limbs.as_mut_slice());
}
}
#[inline]
fn low_u64(value: u128) -> u64 {
u64::try_from(value & u128::from(u64::MAX)).expect("masked low 64 bits always fit into u64")
}
fn montgomery_n0_inv(n0: u64) -> u64 {
debug_assert!(n0 & 1 == 1, "Montgomery path requires an odd modulus");
// Newton iteration in Z_(2^64): each step doubles the number of correct
// low bits in the inverse of `n0`. Six iterations are enough to converge
// to the full 64-bit inverse, and Montgomery reduction wants `-n0^-1`.
let mut inv = 1u64;
for _ in 0..6 {
inv = inv.wrapping_mul(2u64.wrapping_sub(n0.wrapping_mul(inv)));
}
inv.wrapping_neg()
}
impl BigInt {
/// Construct zero.
#[must_use]
pub fn zero() -> Self {
Self {
sign: Sign::Zero,
magnitude: BigUint::zero(),
}
}
/// Construct from an explicit sign and magnitude.
#[must_use]
pub fn from_parts(sign: Sign, magnitude: BigUint) -> Self {
if magnitude.is_zero() {
return Self::zero();
}
let canonical_sign = match sign {
Sign::Zero => Sign::Positive,
other => other,
};
Self {
sign: canonical_sign,
magnitude,
}
}
/// Construct a non-negative signed integer from an unsigned value.
#[must_use]
pub fn from_biguint(magnitude: BigUint) -> Self {
Self::from_parts(Sign::Positive, magnitude)
}
/// Return the sign.
#[must_use]
pub fn sign(&self) -> Sign {
self.sign
}
/// Return the absolute value.
#[must_use]
pub fn magnitude(&self) -> &BigUint {
&self.magnitude
}
/// Negate the integer.
#[must_use]
pub fn negated(&self) -> Self {
let sign = match self.sign {
Sign::Positive => Sign::Negative,
Sign::Negative => Sign::Positive,
Sign::Zero => Sign::Zero,
};
Self {
sign,
magnitude: self.magnitude.clone(),
}
}
/// Return `self + other`.
#[must_use]
pub fn add_ref(&self, other: &Self) -> Self {
match (self.sign, other.sign) {
(Sign::Zero, _) => other.clone(),
(_, Sign::Zero) => self.clone(),
(Sign::Positive, Sign::Positive) => {
Self::from_parts(Sign::Positive, self.magnitude.add_ref(&other.magnitude))
}
(Sign::Negative, Sign::Negative) => {
Self::from_parts(Sign::Negative, self.magnitude.add_ref(&other.magnitude))
}
(Sign::Positive, Sign::Negative) => self.sub_ref(&other.negated()),
(Sign::Negative, Sign::Positive) => other.sub_ref(&self.negated()),
}
}
/// Return `self - other`.
#[must_use]
pub fn sub_ref(&self, other: &Self) -> Self {
match (self.sign, other.sign) {
(_, Sign::Zero) => self.clone(),
(Sign::Zero, _) => other.negated(),
(Sign::Positive, Sign::Negative) => {
Self::from_parts(Sign::Positive, self.magnitude.add_ref(&other.magnitude))
}
(Sign::Negative, Sign::Positive) => {
Self::from_parts(Sign::Negative, self.magnitude.add_ref(&other.magnitude))
}
(Sign::Positive, Sign::Positive) => match self.magnitude.cmp(&other.magnitude) {
Ordering::Greater => {
Self::from_parts(Sign::Positive, self.magnitude.sub_ref(&other.magnitude))
}
Ordering::Less => {
Self::from_parts(Sign::Negative, other.magnitude.sub_ref(&self.magnitude))
}
Ordering::Equal => Self::zero(),
},
(Sign::Negative, Sign::Negative) => match self.magnitude.cmp(&other.magnitude) {
Ordering::Greater => {
Self::from_parts(Sign::Negative, self.magnitude.sub_ref(&other.magnitude))
}
Ordering::Less => {
Self::from_parts(Sign::Positive, other.magnitude.sub_ref(&self.magnitude))
}
Ordering::Equal => Self::zero(),
},
}
}
/// Return `self * factor` for a non-negative factor.
#[must_use]
pub fn mul_biguint_ref(&self, factor: &BigUint) -> Self {
if factor.is_zero() || self.sign == Sign::Zero {
return Self::zero();
}
Self::from_parts(self.sign, self.magnitude.mul_ref(factor))
}
/// Reduce modulo a positive modulus and return the least non-negative residue.
///
/// # Panics
///
/// Panics if `modulus == 0`.
#[must_use]
pub fn modulo_positive(&self, modulus: &BigUint) -> BigUint {
assert!(!modulus.is_zero(), "modulus must be non-zero");
match self.sign {
Sign::Zero => BigUint::zero(),
Sign::Positive => self.magnitude.modulo(modulus),
Sign::Negative => {
let rem = self.magnitude.modulo(modulus);
if rem.is_zero() {
BigUint::zero()
} else {
modulus.sub_ref(&rem)
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::{BigInt, BigUint, MontgomeryCtx, Sign};
fn lcg_next(state: &mut u64) -> u64 {
*state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
*state
}
fn seeded_biguint(words: usize, state: &mut u64) -> BigUint {
let mut limbs = Vec::with_capacity(words);
for _ in 0..words {
limbs.push(lcg_next(state));
}
if words > 0 && limbs[words - 1] == 0 {
limbs[words - 1] = 1;
}
BigUint { limbs }
}
#[test]
fn bytes_roundtrip() {
let value =
BigUint::from_be_bytes(&[0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x11, 0x22]);
assert_eq!(
value.to_be_bytes(),
vec![0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x11, 0x22]
);
}
#[test]
fn add_sub_mul_small_values() {
let a = BigUint::from_u128(1_000_000_000_000);
let b = BigUint::from_u128(777_777_777_777);
assert_eq!(a.add_ref(&b), BigUint::from_u128(1_777_777_777_777));
assert_eq!(
a.sub_ref(&BigUint::from_u64(1)),
BigUint::from_u128(999_999_999_999)
);
assert_eq!(
a.mul_ref(&b),
BigUint::from_u128(777_777_777_777_000_000_000_000)
);
}
#[test]
fn square_ref_matches_mul_ref() {
let mut seed = 0x9e37_79b9_7f4a_7c15;
for words in [1usize, 2, 8, 32, 48] {
for _ in 0..8 {
let value = seeded_biguint(words, &mut seed);
assert_eq!(value.square_ref(), value.mul_ref(&value));
}
}
}
#[test]
fn karatsuba_dispatch_matches_schoolbook() {
let mut seed = 0x243f_6a88_85a3_08d3;
for words in [32usize, 40, 64] {
for _ in 0..6 {
let lhs = seeded_biguint(words, &mut seed);
let rhs = seeded_biguint(words, &mut seed);
let dispatched = lhs.mul_ref(&rhs);
let schoolbook = BigUint::mul_schoolbook_ref(&lhs, &rhs);
assert_eq!(dispatched, schoolbook);
}
}
}
#[test]
fn division_roundtrip() {
let dividend = BigUint::from_u128(1_234_567_890_123_456_789);
let divisor = BigUint::from_u64(37);
let (q, r) = dividend.div_rem(&divisor);
assert_eq!(q, BigUint::from_u128(33_366_699_733_066_399));
assert_eq!(r, BigUint::from_u64(26));
assert_eq!(q.mul_ref(&divisor).add_ref(&r), dividend);
}
#[test]
fn sqrt_floor_small_values() {
assert_eq!(BigUint::from_u64(0).sqrt_floor(), BigUint::from_u64(0));
assert_eq!(BigUint::from_u64(1).sqrt_floor(), BigUint::from_u64(1));
assert_eq!(BigUint::from_u64(2).sqrt_floor(), BigUint::from_u64(1));
assert_eq!(BigUint::from_u64(15).sqrt_floor(), BigUint::from_u64(3));
assert_eq!(BigUint::from_u64(16).sqrt_floor(), BigUint::from_u64(4));
assert_eq!(BigUint::from_u64(17).sqrt_floor(), BigUint::from_u64(4));
assert_eq!(
BigUint::from_u128(17_184_849_881).sqrt_floor(),
BigUint::from_u64(131_090)
);
}
#[test]
fn mod_mul_matches_small_arithmetic() {
let a = BigUint::from_u64(123_456_789);
let b = BigUint::from_u64(987_654_321);
let m = BigUint::from_u64(1_000_000_007);
assert_eq!(BigUint::mod_mul(&a, &b, &m), BigUint::from_u64(259_106_859));
}
#[test]
fn montgomery_mod_pow_matches_small_arithmetic() {
let ctx = MontgomeryCtx::new(&BigUint::from_u64(1_000_000_007))
.expect("odd modulus builds a context");
let base = BigUint::from_u64(123_456_789);
let exponent = BigUint::from_u64(65_537);
assert_eq!(ctx.pow(&base, &exponent), BigUint::from_u64(560_583_526));
}
#[test]
fn montgomery_ctx_mul_matches_small_arithmetic() {
let ctx = MontgomeryCtx::new(&BigUint::from_u64(1_000_000_007))
.expect("odd modulus builds a context");
let a = BigUint::from_u64(123_456_789);
let b = BigUint::from_u64(987_654_321);
assert_eq!(ctx.mul(&a, &b), BigUint::from_u64(259_106_859));
}
#[test]
fn mod_mul_even_modulus_uses_fallback_path() {
let a = BigUint::from_u64(37);
let b = BigUint::from_u64(19);
let modulus = BigUint::from_u64(100);
assert_eq!(BigUint::mod_mul(&a, &b, &modulus), BigUint::from_u64(3));
}
#[test]
fn bigint_sign_normalization() {
let zero = BigInt::from_parts(Sign::Negative, BigUint::zero());
assert_eq!(zero.sign(), Sign::Zero);
let value = BigInt::from_parts(Sign::Positive, BigUint::from_u64(7));
assert_eq!(value.negated().sign(), Sign::Negative);
assert_eq!(value.magnitude(), &BigUint::from_u64(7));
}
#[test]
fn bigint_add_sub_and_modulo() {
let a = BigInt::from_biguint(BigUint::from_u64(10));
let b = BigInt::from_parts(Sign::Negative, BigUint::from_u64(3));
assert_eq!(a.add_ref(&b), BigInt::from_biguint(BigUint::from_u64(7)));
assert_eq!(
b.sub_ref(&a),
BigInt::from_parts(Sign::Negative, BigUint::from_u64(13))
);
assert_eq!(
BigInt::from_parts(Sign::Negative, BigUint::from_u64(3))
.modulo_positive(&BigUint::from_u64(11)),
BigUint::from_u64(8)
);
}
}