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
use crate::{
arch::word::Word,
math,
memory::{self, MemoryAllocation},
modular::modulo::{Modulo, ModuloLarge, ModuloRepr, ModuloSmall},
primitive::{double_word, split_double_word, PrimitiveUnsigned, WORD_BITS, WORD_BITS_USIZE},
ubig::{Repr::*, UBig},
};
impl Modulo<'_> {
pub fn pow(&self, exp: &UBig) -> Modulo {
match self.repr() {
ModuloRepr::Small(self_small) => self_small.pow(exp).into(),
ModuloRepr::Large(self_large) => self_large.pow(exp).into(),
}
}
}
impl ModuloSmall<'_> {
fn pow(&self, exp: &UBig) -> ModuloSmall {
match exp.repr() {
Small(0) => ModuloSmall::from_ubig(&UBig::from_word(1), self.ring()),
Small(1) => self.clone(),
Small(2) => {
let mut a = self.clone();
a.mul_in_place(self);
a
}
_ => self.pow_nontrivial(exp),
}
}
fn pow_nontrivial(&self, exp: &UBig) -> ModuloSmall {
debug_assert!(*exp >= UBig::from_word(3));
let exp_words = exp.as_words();
let mut val = self.clone();
let mut bit = exp.bit_len() - 1;
while bit != 0 {
bit -= 1;
val.square_in_place();
if exp_words[bit / WORD_BITS_USIZE] & (1 << (bit % WORD_BITS_USIZE)) != 0 {
val.mul_in_place(self);
}
}
val
}
}
impl ModuloLarge<'_> {
fn pow(&self, exp: &UBig) -> ModuloLarge {
match exp.repr() {
Small(0) => ModuloLarge::from_ubig(UBig::from_word(1), self.ring()),
Small(1) => self.clone(),
_ => self.pow_nontrivial(exp),
}
}
fn pow_nontrivial(&self, exp: &UBig) -> ModuloLarge {
debug_assert!(*exp >= UBig::from_word(2));
let n = self.ring().normalized_modulus().len();
let window_len = ModuloLarge::choose_pow_window_len(exp.bit_len());
let table_words = ((1usize << (window_len - 1)) - 1)
.checked_mul(n)
.unwrap_or_else(|| memory::panic_out_of_memory());
let memory_requirement = memory::add_layout(
memory::array_layout::<Word>(table_words),
self.ring().mul_memory_requirement(),
);
let mut allocation = MemoryAllocation::new(memory_requirement);
let mut memory = allocation.memory();
let (table, mut memory) = memory.allocate_slice_fill::<Word>(table_words, 0);
let mut val = self.clone();
val.mul_in_place(self, &mut memory);
for i in 1..(1 << (window_len - 1)) {
let (prev, cur) = if i == 1 {
(self.normalized_value(), &mut table[0..n])
} else {
let (prev, cur) = (&mut table[(i - 2) * n..i * n]).split_at_mut(n);
(&*prev, cur)
};
cur.copy_from_slice(self.ring().mul_normalized_values(
prev,
val.normalized_value(),
&mut memory,
));
}
let exp_words = exp.as_words();
let mut bit = exp.bit_len() - 2;
loop {
let word_idx = bit / WORD_BITS_USIZE;
let bit_idx = (bit % WORD_BITS_USIZE) as u32;
let cur_word = exp_words[word_idx];
if cur_word & (1 << bit_idx) != 0 {
let next_word = if word_idx == 0 {
0
} else {
exp_words[word_idx - 1]
};
let (mut window, _) = split_double_word(
double_word(next_word, cur_word) >> (bit_idx + 1 + WORD_BITS - window_len),
);
window &= math::ones::<Word>(window_len);
let num_bits = window_len - window.trailing_zeros();
window >>= window_len - num_bits;
for _ in 0..num_bits - 1 {
val.square_in_place(&mut memory);
}
bit -= (num_bits as usize) - 1;
debug_assert!(window & 1 == 1);
let entry_idx = (window >> 1) as usize;
let entry = if entry_idx == 0 {
self.normalized_value()
} else {
&table[(entry_idx - 1) * n..entry_idx * n]
};
val.mul_normalized_value_in_place(entry, &mut memory);
}
if bit == 0 {
break;
}
bit -= 1;
val.square_in_place(&mut memory);
}
val
}
fn choose_pow_window_len(n: usize) -> u32 {
let cost = |window_size| (1usize << (window_size - 1)) - 1 + n / (window_size as usize + 1);
let mut window_size = 1;
let mut c = cost(window_size);
while window_size + 1 < WORD_BITS.min(usize::BIT_SIZE) {
let c2 = cost(window_size + 1);
if c <= c2 {
break;
}
window_size += 1;
c = c2;
}
window_size
}
}