-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace limbs initializer with const from_hex
The current macro initializer is not uniform across architectures, and doesn't work when the number of 64bit limbs is not exactly half that of 32bit limbs, (i.e. 521 bit). This change replaces all limbs! initializers with a new const function with hex string initializers, which is more portable and more ergonomic.
- Loading branch information
Showing
7 changed files
with
68 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
#[cfg(target_pointer_width = "32")] | ||
macro_rules! limbs { | ||
( $($limb:expr),+ ) => { | ||
[ $($limb),+ ] | ||
}; | ||
use crate::limb::Limb; | ||
|
||
const fn parse_digit(d: u8) -> u8 { | ||
match d.to_ascii_lowercase() { | ||
b'0'..=b'9' => d - b'0', | ||
b'a'..=b'f' => d - b'a' + 10, | ||
_ => panic!(), | ||
} | ||
} | ||
|
||
#[cfg(target_pointer_width = "64")] | ||
macro_rules! limbs { | ||
( $($limb_lo:expr, $limb_hi:expr),+) => { | ||
[ $((($limb_hi | 0u64) << 32) | $limb_lo),+ ] | ||
}; | ||
// TODO: this would be nicer as a trait, but currently traits don't support const functions | ||
pub const fn limbs_from_hex<const LIMBS: usize>(hex: &str) -> [Limb; LIMBS] { | ||
let hex = hex.as_bytes(); | ||
let mut limbs = [0; LIMBS]; | ||
let limb_nibbles = core::mem::size_of::<Limb>() * 2; | ||
let mut i = 0; | ||
|
||
while i < hex.len() { | ||
let char = hex[hex.len() - 1 - i]; | ||
let val = parse_digit(char); | ||
limbs[i / limb_nibbles] |= (val as Limb) << ((i % limb_nibbles) * 4); | ||
i += 1; | ||
} | ||
|
||
limbs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters