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
use crate::Uint;
use thiserror::Error;
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)]
pub enum BaseConvertError {
#[error("The value is too large to fit the target type")]
Overflow,
#[error("The requested number base {0} is less than two")]
InvalidBase(u64),
#[error("digit {0} is out of range for base {1}")]
InvalidDigit(u64, u64),
}
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
pub fn to_base_le(&self, base: u64) -> impl Iterator<Item = u64> {
assert!(base > 1);
SpigotLittle {
base,
limbs: self.limbs,
}
}
pub fn to_base_be(&self, base: u64) -> impl Iterator<Item = u64> {
assert!(base > 1);
OwnedVecIterator {
vec: self.to_base_le(base).collect(),
}
}
pub fn from_base_le<I: IntoIterator<Item = u64>>(
base: u64,
digits: I,
) -> Result<Self, BaseConvertError> {
let digits: Vec<_> = digits.into_iter().collect();
Self::from_base_be(base, digits)
}
pub fn from_base_be<I: IntoIterator<Item = u64>>(
base: u64,
digits: I,
) -> Result<Self, BaseConvertError> {
if base < 2 {
return Err(BaseConvertError::InvalidBase(base));
}
let mut result = Self::ZERO;
for digit in digits {
if digit >= base {
return Err(BaseConvertError::InvalidDigit(digit, base));
}
let mut carry: u128 = u128::from(digit);
#[allow(clippy::cast_possible_truncation)]
for limb in result.limbs.iter_mut() {
carry += u128::from(*limb) * u128::from(base);
*limb = carry as u64;
carry >>= 64;
}
if carry > 0 {
return Err(BaseConvertError::Overflow);
}
}
Ok(result)
}
}
struct SpigotLittle<const LIMBS: usize> {
base: u64,
limbs: [u64; LIMBS],
}
impl<const LIMBS: usize> Iterator for SpigotLittle<LIMBS> {
type Item = u64;
#[allow(clippy::cast_possible_truncation)]
fn next(&mut self) -> Option<Self::Item> {
let mut zero: u64 = 0_u64;
let mut remainder = 0_u128;
for limb in self.limbs.iter_mut().rev() {
zero |= *limb;
remainder <<= 64;
remainder |= u128::from(*limb);
*limb = (remainder / u128::from(self.base)) as u64;
remainder %= u128::from(self.base);
}
if zero == 0 {
None
} else {
Some(remainder as u64)
}
}
}
struct OwnedVecIterator {
vec: Vec<u64>,
}
impl Iterator for OwnedVecIterator {
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
self.vec.pop()
}
}
#[cfg(test)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::zero_prefixed_literal)]
mod tests {
use super::*;
const N: Uint<256, 4> = Uint::from_limbs([
0xa8ec92344438aaf4_u64,
0x9819ebdbd1faaab1_u64,
0x573b1a7064c19c1a_u64,
0xc85ef7d79691fe79_u64,
]);
#[test]
fn test_base_le() {
assert_eq!(
Uint::<64, 1>::from(123456789)
.to_base_le(10)
.collect::<Vec<_>>(),
vec![9, 8, 7, 6, 5, 4, 3, 2, 1]
);
assert_eq!(
N.to_base_le(10000000000000000000_u64).collect::<Vec<_>>(),
vec![
2372330524102404852,
0534330209902435677,
7066324924582287843,
0630363884335538722,
9
]
);
}
#[test]
fn test_base_be() {
assert_eq!(
Uint::<64, 1>::from(123456789)
.to_base_be(10)
.collect::<Vec<_>>(),
vec![1, 2, 3, 4, 5, 6, 7, 8, 9]
);
assert_eq!(
N.to_base_be(10000000000000000000_u64).collect::<Vec<_>>(),
vec![
9,
0630363884335538722,
7066324924582287843,
0534330209902435677,
2372330524102404852
]
);
}
}