-
Notifications
You must be signed in to change notification settings - Fork 708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace limbs initializer with const from_hex #1702
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,54 +12,24 @@ | |
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
use crate::arithmetic::limbs_from_hex; | ||
|
||
use super::{ | ||
elem::{binary_op, binary_op_assign}, | ||
elem_sqr_mul, elem_sqr_mul_acc, Modulus, *, | ||
}; | ||
use core::marker::PhantomData; | ||
|
||
macro_rules! p384_limbs { | ||
[$($limb:expr),+] => { | ||
limbs![$($limb),+] | ||
}; | ||
} | ||
|
||
pub static COMMON_OPS: CommonOps = CommonOps { | ||
num_limbs: 384 / LIMB_BITS, | ||
|
||
q: Modulus { | ||
p: p384_limbs![ | ||
0xffffffff, 0x00000000, 0x00000000, 0xffffffff, 0xfffffffe, 0xffffffff, 0xffffffff, | ||
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff | ||
], | ||
rr: p384_limbs![1, 0xfffffffe, 0, 2, 0, 0xfffffffe, 0, 2, 1, 0, 0, 0], | ||
}, | ||
n: Elem { | ||
limbs: p384_limbs![ | ||
0xccc52973, 0xecec196a, 0x48b0a77a, 0x581a0db2, 0xf4372ddf, 0xc7634d81, 0xffffffff, | ||
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff | ||
], | ||
m: PhantomData, | ||
encoding: PhantomData, // Unencoded | ||
}, | ||
|
||
a: Elem { | ||
limbs: p384_limbs![ | ||
0xfffffffc, 0x00000003, 0x00000000, 0xfffffffc, 0xfffffffb, 0xffffffff, 0xffffffff, | ||
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff | ||
], | ||
m: PhantomData, | ||
encoding: PhantomData, // Unreduced | ||
p: limbs_from_hex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff"), | ||
rr: limbs_from_hex("10000000200000000fffffffe000000000000000200000000fffffffe00000001"), | ||
}, | ||
b: Elem { | ||
limbs: p384_limbs![ | ||
0x9d412dcc, 0x08118871, 0x7a4c32ec, 0xf729add8, 0x1920022e, 0x77f2209b, 0x94938ae2, | ||
0xe3374bee, 0x1f022094, 0xb62b21f4, 0x604fbff9, 0xcd08114b | ||
], | ||
m: PhantomData, | ||
encoding: PhantomData, // Unreduced | ||
}, | ||
|
||
n: Elem::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973"), | ||
a: Elem::from_hex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffffffc0000000000000003fffffffc"), | ||
b: Elem::from_hex("cd08114b604fbff9b62b21f41f022094e3374bee94938ae277f2209b1920022ef729add87a4c32ec081188719d412dcc") | ||
, | ||
elem_mul_mont: p384_elem_mul_mont, | ||
elem_sqr_mont: p384_elem_sqr_mont, | ||
|
||
|
@@ -131,23 +101,9 @@ fn p384_elem_inv_squared(a: &Elem<R>) -> Elem<R> { | |
|
||
fn p384_point_mul_base_impl(a: &Scalar) -> Point { | ||
// XXX: Not efficient. TODO: Precompute multiples of the generator. | ||
static GENERATOR: (Elem<R>, Elem<R>) = ( | ||
Elem { | ||
limbs: p384_limbs![ | ||
0x49c0b528, 0x3dd07566, 0xa0d6ce38, 0x20e378e2, 0x541b4d6e, 0x879c3afc, 0x59a30eff, | ||
0x64548684, 0x614ede2b, 0x812ff723, 0x299e1513, 0x4d3aadc2 | ||
], | ||
m: PhantomData, | ||
encoding: PhantomData, | ||
}, | ||
Elem { | ||
limbs: p384_limbs![ | ||
0x4b03a4fe, 0x23043dad, 0x7bb4a9ac, 0xa1bfa8bf, 0x2e83b050, 0x8bade756, 0x68f4ffd9, | ||
0xc6c35219, 0x3969a840, 0xdd800226, 0x5a15c5e9, 0x2b78abc2 | ||
], | ||
m: PhantomData, | ||
encoding: PhantomData, | ||
}, | ||
const GENERATOR: (Elem<R>, Elem<R>) = ( | ||
Elem::from_hex("4d3aadc2299e1513812ff723614ede2b6454868459a30eff879c3afc541b4d6e20e378e2a0d6ce383dd0756649c0b528"), | ||
Elem::from_hex("2b78abc25a15c5e9dd8002263969a840c6c3521968f4ffd98bade7562e83b050a1bfa8bf7bb4a9ac23043dad4b03a4fe"), | ||
); | ||
|
||
PRIVATE_KEY_OPS.point_mul(a, &GENERATOR) | ||
|
@@ -168,25 +124,13 @@ pub static PUBLIC_SCALAR_OPS: PublicScalarOps = PublicScalarOps { | |
public_key_ops: &PUBLIC_KEY_OPS, | ||
private_key_ops: &PRIVATE_KEY_OPS, | ||
|
||
q_minus_n: Elem { | ||
limbs: p384_limbs![ | ||
0x333ad68c, 0x1313e696, 0xb74f5885, 0xa7e5f24c, 0x0bc8d21f, 0x389cb27e, 0, 0, 0, 0, 0, | ||
0 | ||
], | ||
|
||
m: PhantomData, | ||
encoding: PhantomData, // Unencoded | ||
}, | ||
q_minus_n: Elem::from_hex("389cb27e0bc8d21fa7e5f24cb74f58851313e696333ad68c"), | ||
}; | ||
|
||
pub static PRIVATE_SCALAR_OPS: PrivateScalarOps = PrivateScalarOps { | ||
scalar_ops: &SCALAR_OPS, | ||
|
||
oneRR_mod_n: Scalar { | ||
limbs: N_RR_LIMBS, | ||
m: PhantomData, | ||
encoding: PhantomData, // R | ||
}, | ||
oneRR_mod_n: Scalar::from_hex(N_RR_HEX), | ||
}; | ||
|
||
fn p384_scalar_inv_to_mont(a: &Scalar<Unencoded>) -> Scalar<R> { | ||
|
@@ -232,11 +176,7 @@ fn p384_scalar_inv_to_mont(a: &Scalar<Unencoded>) -> Scalar<R> { | |
} | ||
|
||
fn to_mont(a: &Scalar<Unencoded>) -> Scalar<R> { | ||
static N_RR: Scalar<Unencoded> = Scalar { | ||
limbs: N_RR_LIMBS, | ||
m: PhantomData, | ||
encoding: PhantomData, | ||
}; | ||
const N_RR: Scalar<Unencoded> = Scalar::from_hex(N_RR_HEX); | ||
binary_op(p384_scalar_mul_mont, a, &N_RR) | ||
} | ||
|
||
|
@@ -332,10 +272,7 @@ unsafe extern "C" fn p384_elem_sqr_mont( | |
p384_elem_mul_mont(r, a, a); | ||
} | ||
|
||
const N_RR_LIMBS: [Limb; MAX_LIMBS] = p384_limbs![ | ||
0x19b409a9, 0x2d319b24, 0xdf1aa419, 0xff3d81e5, 0xfcb82947, 0xbc3e483a, 0x4aab1cc5, 0xd40d4917, | ||
0x28266895, 0x3fb05b7a, 0x2b39bf21, 0x0c84ee01 | ||
]; | ||
const N_RR_HEX: &str = "0c84ee012b39bf213fb05b7a28266895d40d49174aab1cc5bc3e483afcb82947ff3d81e5df1aa4192d319b2419b409a9"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to have the leading zero, right? I will fix this after I merge it. |
||
|
||
prefixed_extern! { | ||
fn p384_elem_mul_mont( | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comma shouldn't be on its own line; it should be at the end of the line above. I will fix this after I merge this.