/rust/registry/src/index.crates.io-6f17d22bba15001f/ring-0.17.14/src/arithmetic/constant.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::limb::LeakyLimb; |
2 | | use core::mem::size_of; |
3 | | |
4 | 0 | const fn parse_digit(d: u8) -> u8 { |
5 | 0 | match d.to_ascii_lowercase() { |
6 | 0 | b'0'..=b'9' => d - b'0', |
7 | 0 | b'a'..=b'f' => d - b'a' + 10, |
8 | 0 | _ => panic!(), |
9 | | } |
10 | 0 | } |
11 | | |
12 | | // TODO: this would be nicer as a trait, but currently traits don't support const functions |
13 | 0 | pub const fn limbs_from_hex<const LIMBS: usize>(hex: &str) -> [LeakyLimb; LIMBS] { |
14 | 0 | let hex = hex.as_bytes(); |
15 | 0 | let mut limbs = [0; LIMBS]; |
16 | 0 | let limb_nibbles = size_of::<LeakyLimb>() * 2; |
17 | 0 | let mut i = 0; |
18 | | |
19 | 0 | while i < hex.len() { |
20 | 0 | let char = hex[hex.len() - 1 - i]; |
21 | 0 | let val = parse_digit(char); |
22 | 0 | limbs[i / limb_nibbles] |= (val as LeakyLimb) << ((i % limb_nibbles) * 4); |
23 | 0 | i += 1; |
24 | 0 | } |
25 | | |
26 | 0 | limbs |
27 | 0 | } |