ring/arithmetic/
constant.rs
1use crate::limb::LeakyLimb;
2use core::mem::size_of;
3
4const fn parse_digit(d: u8) -> u8 {
5 match d.to_ascii_lowercase() {
6 b'0'..=b'9' => d - b'0',
7 b'a'..=b'f' => d - b'a' + 10,
8 _ => panic!(),
9 }
10}
11
12pub const fn limbs_from_hex<const LIMBS: usize>(hex: &str) -> [LeakyLimb; LIMBS] {
14 let hex = hex.as_bytes();
15 let mut limbs = [0; LIMBS];
16 let limb_nibbles = size_of::<LeakyLimb>() * 2;
17 let mut i = 0;
18
19 while i < hex.len() {
20 let char = hex[hex.len() - 1 - i];
21 let val = parse_digit(char);
22 limbs[i / limb_nibbles] |= (val as LeakyLimb) << ((i % limb_nibbles) * 4);
23 i += 1;
24 }
25
26 limbs
27}