1cbd624adSopenharmony_cimod stackvec;
2cbd624adSopenharmony_ci
3cbd624adSopenharmony_ciuse core::cmp;
4cbd624adSopenharmony_ciuse minimal_lexical::bigint;
5cbd624adSopenharmony_ciuse stackvec::{vec_from_u32, VecType};
6cbd624adSopenharmony_ci
7cbd624adSopenharmony_ci// u64::MAX and Limb::MAX for older Rustc versions.
8cbd624adSopenharmony_ciconst U64_MAX: u64 = 0xffff_ffff_ffff_ffff;
9cbd624adSopenharmony_ci// LIMB_MAX
10cbd624adSopenharmony_ci#[cfg(all(target_pointer_width = "64", not(target_arch = "sparc")))]
11cbd624adSopenharmony_ciconst LIMB_MAX: u64 = U64_MAX;
12cbd624adSopenharmony_ci#[cfg(not(all(target_pointer_width = "64", not(target_arch = "sparc"))))]
13cbd624adSopenharmony_ciconst LIMB_MAX: u32 = 0xffff_ffff;
14cbd624adSopenharmony_ci
15cbd624adSopenharmony_ci#[test]
16cbd624adSopenharmony_cifn simple_test() {
17cbd624adSopenharmony_ci    // Test the simple properties of the stack vector.
18cbd624adSopenharmony_ci    let mut x = VecType::from_u64(1);
19cbd624adSopenharmony_ci    assert_eq!(x.len(), 1);
20cbd624adSopenharmony_ci    assert_eq!(x.is_empty(), false);
21cbd624adSopenharmony_ci    assert_eq!(x.capacity(), bigint::BIGINT_LIMBS);
22cbd624adSopenharmony_ci    x.try_push(5).unwrap();
23cbd624adSopenharmony_ci    assert_eq!(x.len(), 2);
24cbd624adSopenharmony_ci    assert_eq!(x.pop(), Some(5));
25cbd624adSopenharmony_ci    assert_eq!(x.len(), 1);
26cbd624adSopenharmony_ci    assert_eq!(&*x, &[1]);
27cbd624adSopenharmony_ci    x.try_extend(&[2, 3, 4]).unwrap();
28cbd624adSopenharmony_ci    assert_eq!(x.len(), 4);
29cbd624adSopenharmony_ci    assert_eq!(&*x, &[1, 2, 3, 4]);
30cbd624adSopenharmony_ci    x.try_resize(6, 0).unwrap();
31cbd624adSopenharmony_ci    assert_eq!(x.len(), 6);
32cbd624adSopenharmony_ci    assert_eq!(&*x, &[1, 2, 3, 4, 0, 0]);
33cbd624adSopenharmony_ci    x.try_resize(0, 0).unwrap();
34cbd624adSopenharmony_ci    assert_eq!(x.len(), 0);
35cbd624adSopenharmony_ci    assert_eq!(x.is_empty(), true);
36cbd624adSopenharmony_ci
37cbd624adSopenharmony_ci    let x = VecType::try_from(&[5, 1]).unwrap();
38cbd624adSopenharmony_ci    assert_eq!(x.len(), 2);
39cbd624adSopenharmony_ci    assert_eq!(x.is_empty(), false);
40cbd624adSopenharmony_ci    if bigint::LIMB_BITS == 32 {
41cbd624adSopenharmony_ci        assert_eq!(x.hi64(), (0x8000000280000000, false));
42cbd624adSopenharmony_ci    } else {
43cbd624adSopenharmony_ci        assert_eq!(x.hi64(), (0x8000000000000002, true));
44cbd624adSopenharmony_ci    }
45cbd624adSopenharmony_ci    let rview = bigint::rview(&x);
46cbd624adSopenharmony_ci    assert_eq!(x[0], 5);
47cbd624adSopenharmony_ci    assert_eq!(x[1], 1);
48cbd624adSopenharmony_ci    assert_eq!(rview[0], 1);
49cbd624adSopenharmony_ci    assert_eq!(rview[1], 5);
50cbd624adSopenharmony_ci    assert_eq!(x.len(), 2);
51cbd624adSopenharmony_ci
52cbd624adSopenharmony_ci    assert_eq!(VecType::from_u64(U64_MAX).hi64(), (U64_MAX, false));
53cbd624adSopenharmony_ci}
54cbd624adSopenharmony_ci
55cbd624adSopenharmony_ci#[test]
56cbd624adSopenharmony_cifn hi64_test() {
57cbd624adSopenharmony_ci    assert_eq!(VecType::from_u64(0xA).hi64(), (0xA000000000000000, false));
58cbd624adSopenharmony_ci    assert_eq!(VecType::from_u64(0xAB).hi64(), (0xAB00000000000000, false));
59cbd624adSopenharmony_ci    assert_eq!(VecType::from_u64(0xAB00000000).hi64(), (0xAB00000000000000, false));
60cbd624adSopenharmony_ci    assert_eq!(VecType::from_u64(0xA23456789A).hi64(), (0xA23456789A000000, false));
61cbd624adSopenharmony_ci}
62cbd624adSopenharmony_ci
63cbd624adSopenharmony_ci#[test]
64cbd624adSopenharmony_cifn cmp_test() {
65cbd624adSopenharmony_ci    // Simple
66cbd624adSopenharmony_ci    let x = VecType::from_u64(1);
67cbd624adSopenharmony_ci    let y = VecType::from_u64(2);
68cbd624adSopenharmony_ci    assert_eq!(x.partial_cmp(&x), Some(cmp::Ordering::Equal));
69cbd624adSopenharmony_ci    assert_eq!(x.cmp(&x), cmp::Ordering::Equal);
70cbd624adSopenharmony_ci    assert_eq!(x.cmp(&y), cmp::Ordering::Less);
71cbd624adSopenharmony_ci
72cbd624adSopenharmony_ci    // Check asymmetric
73cbd624adSopenharmony_ci    let x = VecType::try_from(&[5, 1]).unwrap();
74cbd624adSopenharmony_ci    let y = VecType::from_u64(2);
75cbd624adSopenharmony_ci    assert_eq!(x.cmp(&x), cmp::Ordering::Equal);
76cbd624adSopenharmony_ci    assert_eq!(x.cmp(&y), cmp::Ordering::Greater);
77cbd624adSopenharmony_ci
78cbd624adSopenharmony_ci    // Check when we use reverse ordering properly.
79cbd624adSopenharmony_ci    let x = VecType::try_from(&[5, 1, 9]).unwrap();
80cbd624adSopenharmony_ci    let y = VecType::try_from(&[6, 2, 8]).unwrap();
81cbd624adSopenharmony_ci    assert_eq!(x.cmp(&x), cmp::Ordering::Equal);
82cbd624adSopenharmony_ci    assert_eq!(x.cmp(&y), cmp::Ordering::Greater);
83cbd624adSopenharmony_ci
84cbd624adSopenharmony_ci    // Complex scenario, check it properly uses reverse ordering.
85cbd624adSopenharmony_ci    let x = VecType::try_from(&[0, 1, 9]).unwrap();
86cbd624adSopenharmony_ci    let y = VecType::try_from(&[4294967295, 0, 9]).unwrap();
87cbd624adSopenharmony_ci    assert_eq!(x.cmp(&x), cmp::Ordering::Equal);
88cbd624adSopenharmony_ci    assert_eq!(x.cmp(&y), cmp::Ordering::Greater);
89cbd624adSopenharmony_ci}
90cbd624adSopenharmony_ci
91cbd624adSopenharmony_ci#[test]
92cbd624adSopenharmony_cifn math_test() {
93cbd624adSopenharmony_ci    let mut x = VecType::try_from(&[0, 1, 9]).unwrap();
94cbd624adSopenharmony_ci    assert_eq!(x.is_normalized(), true);
95cbd624adSopenharmony_ci    x.try_push(0).unwrap();
96cbd624adSopenharmony_ci    assert_eq!(&*x, &[0, 1, 9, 0]);
97cbd624adSopenharmony_ci    assert_eq!(x.is_normalized(), false);
98cbd624adSopenharmony_ci    x.normalize();
99cbd624adSopenharmony_ci    assert_eq!(&*x, &[0, 1, 9]);
100cbd624adSopenharmony_ci    assert_eq!(x.is_normalized(), true);
101cbd624adSopenharmony_ci
102cbd624adSopenharmony_ci    x.add_small(1);
103cbd624adSopenharmony_ci    assert_eq!(&*x, &[1, 1, 9]);
104cbd624adSopenharmony_ci    x.add_small(LIMB_MAX);
105cbd624adSopenharmony_ci    assert_eq!(&*x, &[0, 2, 9]);
106cbd624adSopenharmony_ci
107cbd624adSopenharmony_ci    x.mul_small(3);
108cbd624adSopenharmony_ci    assert_eq!(&*x, &[0, 6, 27]);
109cbd624adSopenharmony_ci    x.mul_small(LIMB_MAX);
110cbd624adSopenharmony_ci    let expected: VecType = if bigint::LIMB_BITS == 32 {
111cbd624adSopenharmony_ci        vec_from_u32(&[0, 4294967290, 4294967274, 26])
112cbd624adSopenharmony_ci    } else {
113cbd624adSopenharmony_ci        vec_from_u32(&[0, 0, 4294967290, 4294967295, 4294967274, 4294967295, 26])
114cbd624adSopenharmony_ci    };
115cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
116cbd624adSopenharmony_ci
117cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0xFFFFFFFF);
118cbd624adSopenharmony_ci    let y = VecType::from_u64(5);
119cbd624adSopenharmony_ci    x *= &y;
120cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0xFFFFFFFB, 0x4]);
121cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
122cbd624adSopenharmony_ci
123cbd624adSopenharmony_ci    // Test with carry
124cbd624adSopenharmony_ci    let mut x = VecType::from_u64(1);
125cbd624adSopenharmony_ci    assert_eq!(&*x, &[1]);
126cbd624adSopenharmony_ci    x.add_small(LIMB_MAX);
127cbd624adSopenharmony_ci    assert_eq!(&*x, &[0, 1]);
128cbd624adSopenharmony_ci}
129cbd624adSopenharmony_ci
130cbd624adSopenharmony_ci#[test]
131cbd624adSopenharmony_cifn scalar_add_test() {
132cbd624adSopenharmony_ci    assert_eq!(bigint::scalar_add(5, 5), (10, false));
133cbd624adSopenharmony_ci    assert_eq!(bigint::scalar_add(LIMB_MAX, 1), (0, true));
134cbd624adSopenharmony_ci}
135cbd624adSopenharmony_ci
136cbd624adSopenharmony_ci#[test]
137cbd624adSopenharmony_cifn scalar_mul_test() {
138cbd624adSopenharmony_ci    assert_eq!(bigint::scalar_mul(5, 5, 0), (25, 0));
139cbd624adSopenharmony_ci    assert_eq!(bigint::scalar_mul(5, 5, 1), (26, 0));
140cbd624adSopenharmony_ci    assert_eq!(bigint::scalar_mul(LIMB_MAX, 2, 0), (LIMB_MAX - 1, 1));
141cbd624adSopenharmony_ci}
142cbd624adSopenharmony_ci
143cbd624adSopenharmony_ci#[test]
144cbd624adSopenharmony_cifn small_add_test() {
145cbd624adSopenharmony_ci    let mut x = VecType::from_u64(4294967295);
146cbd624adSopenharmony_ci    bigint::small_add(&mut x, 5);
147cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[4, 1]);
148cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
149cbd624adSopenharmony_ci
150cbd624adSopenharmony_ci    let mut x = VecType::from_u64(5);
151cbd624adSopenharmony_ci    bigint::small_add(&mut x, 7);
152cbd624adSopenharmony_ci    let expected = VecType::from_u64(12);
153cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
154cbd624adSopenharmony_ci
155cbd624adSopenharmony_ci    // Single carry, internal overflow
156cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0x80000000FFFFFFFF);
157cbd624adSopenharmony_ci    bigint::small_add(&mut x, 7);
158cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[6, 0x80000001]);
159cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
160cbd624adSopenharmony_ci
161cbd624adSopenharmony_ci    // Double carry, overflow
162cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0xFFFFFFFFFFFFFFFF);
163cbd624adSopenharmony_ci    bigint::small_add(&mut x, 7);
164cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[6, 0, 1]);
165cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
166cbd624adSopenharmony_ci}
167cbd624adSopenharmony_ci
168cbd624adSopenharmony_ci#[test]
169cbd624adSopenharmony_cifn small_mul_test() {
170cbd624adSopenharmony_ci    // No overflow check, 1-int.
171cbd624adSopenharmony_ci    let mut x = VecType::from_u64(5);
172cbd624adSopenharmony_ci    bigint::small_mul(&mut x, 7);
173cbd624adSopenharmony_ci    let expected = VecType::from_u64(35);
174cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
175cbd624adSopenharmony_ci
176cbd624adSopenharmony_ci    // No overflow check, 2-ints.
177cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0x4000000040000);
178cbd624adSopenharmony_ci    bigint::small_mul(&mut x, 5);
179cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0x00140000, 0x140000]);
180cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
181cbd624adSopenharmony_ci
182cbd624adSopenharmony_ci    // Overflow, 1 carry.
183cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0x33333334);
184cbd624adSopenharmony_ci    bigint::small_mul(&mut x, 5);
185cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[4, 1]);
186cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
187cbd624adSopenharmony_ci
188cbd624adSopenharmony_ci    // Overflow, 1 carry, internal.
189cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0x133333334);
190cbd624adSopenharmony_ci    bigint::small_mul(&mut x, 5);
191cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[4, 6]);
192cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
193cbd624adSopenharmony_ci
194cbd624adSopenharmony_ci    // Overflow, 2 carries.
195cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0x3333333333333334);
196cbd624adSopenharmony_ci    bigint::small_mul(&mut x, 5);
197cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[4, 0, 1]);
198cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
199cbd624adSopenharmony_ci}
200cbd624adSopenharmony_ci
201cbd624adSopenharmony_ci#[test]
202cbd624adSopenharmony_cifn pow_test() {
203cbd624adSopenharmony_ci    let mut x = VecType::from_u64(1);
204cbd624adSopenharmony_ci    bigint::pow(&mut x, 2);
205cbd624adSopenharmony_ci    let expected = VecType::from_u64(25);
206cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
207cbd624adSopenharmony_ci
208cbd624adSopenharmony_ci    let mut x = VecType::from_u64(1);
209cbd624adSopenharmony_ci    bigint::pow(&mut x, 15);
210cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[452807053, 7]);
211cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
212cbd624adSopenharmony_ci
213cbd624adSopenharmony_ci    let mut x = VecType::from_u64(1);
214cbd624adSopenharmony_ci    bigint::pow(&mut x, 16);
215cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[2264035265, 35]);
216cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
217cbd624adSopenharmony_ci
218cbd624adSopenharmony_ci    let mut x = VecType::from_u64(1);
219cbd624adSopenharmony_ci    bigint::pow(&mut x, 17);
220cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[2730241733, 177]);
221cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
222cbd624adSopenharmony_ci
223cbd624adSopenharmony_ci    let mut x = VecType::from_u64(1);
224cbd624adSopenharmony_ci    bigint::pow(&mut x, 302);
225cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[
226cbd624adSopenharmony_ci        2443090281, 2149694430, 2297493928, 1584384001, 1279504719, 1930002239, 3312868939,
227cbd624adSopenharmony_ci        3735173465, 3523274756, 2025818732, 1641675015, 2431239749, 4292780461, 3719612855,
228cbd624adSopenharmony_ci        4174476133, 3296847770, 2677357556, 638848153, 2198928114, 3285049351, 2159526706,
229cbd624adSopenharmony_ci        626302612,
230cbd624adSopenharmony_ci    ]);
231cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
232cbd624adSopenharmony_ci}
233cbd624adSopenharmony_ci
234cbd624adSopenharmony_ci#[test]
235cbd624adSopenharmony_cifn large_add_test() {
236cbd624adSopenharmony_ci    // Overflow, both single values
237cbd624adSopenharmony_ci    let mut x = VecType::from_u64(4294967295);
238cbd624adSopenharmony_ci    let y = VecType::from_u64(5);
239cbd624adSopenharmony_ci    bigint::large_add(&mut x, &y);
240cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[4, 1]);
241cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
242cbd624adSopenharmony_ci
243cbd624adSopenharmony_ci    // No overflow, single value
244cbd624adSopenharmony_ci    let mut x = VecType::from_u64(5);
245cbd624adSopenharmony_ci    let y = VecType::from_u64(7);
246cbd624adSopenharmony_ci    bigint::large_add(&mut x, &y);
247cbd624adSopenharmony_ci    let expected = VecType::from_u64(12);
248cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
249cbd624adSopenharmony_ci
250cbd624adSopenharmony_ci    // Single carry, internal overflow
251cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0x80000000FFFFFFFF);
252cbd624adSopenharmony_ci    let y = VecType::from_u64(7);
253cbd624adSopenharmony_ci    bigint::large_add(&mut x, &y);
254cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[6, 0x80000001]);
255cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
256cbd624adSopenharmony_ci
257cbd624adSopenharmony_ci    // 1st overflows, 2nd doesn't.
258cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0x7FFFFFFFFFFFFFFF);
259cbd624adSopenharmony_ci    let y = VecType::from_u64(0x7FFFFFFFFFFFFFFF);
260cbd624adSopenharmony_ci    bigint::large_add(&mut x, &y);
261cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0xFFFFFFFE, 0xFFFFFFFF]);
262cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
263cbd624adSopenharmony_ci
264cbd624adSopenharmony_ci    // Both overflow.
265cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0x8FFFFFFFFFFFFFFF);
266cbd624adSopenharmony_ci    let y = VecType::from_u64(0x7FFFFFFFFFFFFFFF);
267cbd624adSopenharmony_ci    bigint::large_add(&mut x, &y);
268cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0xFFFFFFFE, 0x0FFFFFFF, 1]);
269cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
270cbd624adSopenharmony_ci}
271cbd624adSopenharmony_ci
272cbd624adSopenharmony_ci#[test]
273cbd624adSopenharmony_cifn large_mul_test() {
274cbd624adSopenharmony_ci    // Test by empty
275cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0xFFFFFFFF);
276cbd624adSopenharmony_ci    let y = VecType::new();
277cbd624adSopenharmony_ci    bigint::large_mul(&mut x, &y);
278cbd624adSopenharmony_ci    let expected = VecType::new();
279cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
280cbd624adSopenharmony_ci
281cbd624adSopenharmony_ci    // Simple case
282cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0xFFFFFFFF);
283cbd624adSopenharmony_ci    let y = VecType::from_u64(5);
284cbd624adSopenharmony_ci    bigint::large_mul(&mut x, &y);
285cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0xFFFFFFFB, 0x4]);
286cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
287cbd624adSopenharmony_ci
288cbd624adSopenharmony_ci    // Large u32, but still just as easy.
289cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0xFFFFFFFF);
290cbd624adSopenharmony_ci    let y = VecType::from_u64(0xFFFFFFFE);
291cbd624adSopenharmony_ci    bigint::large_mul(&mut x, &y);
292cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0x2, 0xFFFFFFFD]);
293cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
294cbd624adSopenharmony_ci
295cbd624adSopenharmony_ci    // Let's multiply two large values together.
296cbd624adSopenharmony_ci    let mut x: VecType = vec_from_u32(&[0xFFFFFFFE, 0x0FFFFFFF, 1]);
297cbd624adSopenharmony_ci    let y: VecType = vec_from_u32(&[0x99999999, 0x99999999, 0xCCCD9999, 0xCCCC]);
298cbd624adSopenharmony_ci    bigint::large_mul(&mut x, &y);
299cbd624adSopenharmony_ci    let expected: VecType =
300cbd624adSopenharmony_ci        vec_from_u32(&[0xCCCCCCCE, 0x5CCCCCCC, 0x9997FFFF, 0x33319999, 0x999A7333, 0xD999]);
301cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
302cbd624adSopenharmony_ci}
303cbd624adSopenharmony_ci
304cbd624adSopenharmony_ci#[test]
305cbd624adSopenharmony_cifn very_large_mul_test() {
306cbd624adSopenharmony_ci    // Test cases triggered to that would normally use `karatsuba_mul`.
307cbd624adSopenharmony_ci    // Karatsuba multiplication was ripped out, however, these are useful
308cbd624adSopenharmony_ci    // test cases.
309cbd624adSopenharmony_ci    let mut x: VecType = vec_from_u32(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
310cbd624adSopenharmony_ci    let y: VecType = vec_from_u32(&[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
311cbd624adSopenharmony_ci    bigint::large_mul(&mut x, &y);
312cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[
313cbd624adSopenharmony_ci        4, 13, 28, 50, 80, 119, 168, 228, 300, 385, 484, 598, 728, 875, 1040, 1224, 1340, 1435,
314cbd624adSopenharmony_ci        1508, 1558, 1584, 1585, 1560, 1508, 1428, 1319, 1180, 1010, 808, 573, 304,
315cbd624adSopenharmony_ci    ]);
316cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
317cbd624adSopenharmony_ci
318cbd624adSopenharmony_ci    // Test cases triggered to that would normally use `karatsuba_uneven_mul`.
319cbd624adSopenharmony_ci    let mut x: VecType = vec_from_u32(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
320cbd624adSopenharmony_ci    let y: VecType = vec_from_u32(&[
321cbd624adSopenharmony_ci        4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
322cbd624adSopenharmony_ci        28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
323cbd624adSopenharmony_ci    ]);
324cbd624adSopenharmony_ci    bigint::large_mul(&mut x, &y);
325cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[
326cbd624adSopenharmony_ci        4, 13, 28, 50, 80, 119, 168, 228, 300, 385, 484, 598, 728, 875, 1040, 1224, 1360, 1496,
327cbd624adSopenharmony_ci        1632, 1768, 1904, 2040, 2176, 2312, 2448, 2584, 2720, 2856, 2992, 3128, 3264, 3400, 3536,
328cbd624adSopenharmony_ci        3672, 3770, 3829, 3848, 3826, 3762, 3655, 3504, 3308, 3066, 2777, 2440, 2054, 1618, 1131,
329cbd624adSopenharmony_ci        592,
330cbd624adSopenharmony_ci    ]);
331cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
332cbd624adSopenharmony_ci}
333cbd624adSopenharmony_ci
334cbd624adSopenharmony_ci#[test]
335cbd624adSopenharmony_cifn bit_length_test() {
336cbd624adSopenharmony_ci    let x: VecType = vec_from_u32(&[0, 0, 0, 1]);
337cbd624adSopenharmony_ci    assert_eq!(bigint::bit_length(&x), 97);
338cbd624adSopenharmony_ci
339cbd624adSopenharmony_ci    let x: VecType = vec_from_u32(&[0, 0, 0, 3]);
340cbd624adSopenharmony_ci    assert_eq!(bigint::bit_length(&x), 98);
341cbd624adSopenharmony_ci
342cbd624adSopenharmony_ci    let x = VecType::from_u64(1 << 31);
343cbd624adSopenharmony_ci    assert_eq!(bigint::bit_length(&x), 32);
344cbd624adSopenharmony_ci}
345cbd624adSopenharmony_ci
346cbd624adSopenharmony_ci#[test]
347cbd624adSopenharmony_cifn shl_bits_test() {
348cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0xD2210408);
349cbd624adSopenharmony_ci    bigint::shl_bits(&mut x, 5);
350cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0x44208100, 0x1A]);
351cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
352cbd624adSopenharmony_ci}
353cbd624adSopenharmony_ci
354cbd624adSopenharmony_ci#[test]
355cbd624adSopenharmony_cifn shl_limbs_test() {
356cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0xD2210408);
357cbd624adSopenharmony_ci    bigint::shl_limbs(&mut x, 2);
358cbd624adSopenharmony_ci    let expected: VecType = if bigint::LIMB_BITS == 32 {
359cbd624adSopenharmony_ci        vec_from_u32(&[0, 0, 0xD2210408])
360cbd624adSopenharmony_ci    } else {
361cbd624adSopenharmony_ci        vec_from_u32(&[0, 0, 0, 0, 0xD2210408])
362cbd624adSopenharmony_ci    };
363cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
364cbd624adSopenharmony_ci}
365cbd624adSopenharmony_ci
366cbd624adSopenharmony_ci#[test]
367cbd624adSopenharmony_cifn shl_test() {
368cbd624adSopenharmony_ci    // Pattern generated via `''.join(["1" +"0"*i for i in range(20)])`
369cbd624adSopenharmony_ci    let mut x = VecType::from_u64(0xD2210408);
370cbd624adSopenharmony_ci    bigint::shl(&mut x, 5);
371cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0x44208100, 0x1A]);
372cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
373cbd624adSopenharmony_ci
374cbd624adSopenharmony_ci    bigint::shl(&mut x, 32);
375cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0, 0x44208100, 0x1A]);
376cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
377cbd624adSopenharmony_ci
378cbd624adSopenharmony_ci    bigint::shl(&mut x, 27);
379cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0, 0, 0xD2210408]);
380cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
381cbd624adSopenharmony_ci
382cbd624adSopenharmony_ci    // 96-bits of previous pattern
383cbd624adSopenharmony_ci    let mut x: VecType = vec_from_u32(&[0x20020010, 0x8040100, 0xD2210408]);
384cbd624adSopenharmony_ci    bigint::shl(&mut x, 5);
385cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0x400200, 0x802004, 0x44208101, 0x1A]);
386cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
387cbd624adSopenharmony_ci
388cbd624adSopenharmony_ci    bigint::shl(&mut x, 32);
389cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0, 0x400200, 0x802004, 0x44208101, 0x1A]);
390cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
391cbd624adSopenharmony_ci
392cbd624adSopenharmony_ci    bigint::shl(&mut x, 27);
393cbd624adSopenharmony_ci    let expected: VecType = vec_from_u32(&[0, 0, 0x20020010, 0x8040100, 0xD2210408]);
394cbd624adSopenharmony_ci    assert_eq!(&*x, &*expected);
395cbd624adSopenharmony_ci}
396