Skip to content

Commit

Permalink
p521: simplify FieldElement::from_uint_unchecked (#1015)
Browse files Browse the repository at this point in the history
Uses the newly added `const fn`-friendly version of `Uint::to_le_bytes`
  • Loading branch information
tarcieri authored Jan 12, 2024
1 parent 4365b2b commit c0129b6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io.crypto-bigint]
git = "https://github.com/RustCrypto/crypto-bigint.git"
27 changes: 4 additions & 23 deletions p521/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ use elliptic_curve::{
Error, FieldBytesEncoding,
};

#[cfg(target_pointer_width = "32")]
use super::util;

/// Field modulus: p = 2^{521} − 1
pub(crate) const MODULUS: U576 = U576::from_be_hex("00000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");

Expand Down Expand Up @@ -107,34 +104,18 @@ impl FieldElement {
pub(crate) const fn from_uint_unchecked(w: U576) -> Self {
// Converts the saturated representation used by `U576` into a 66-byte array with a
// little-endian byte ordering.
//
// TODO(tarcieri): use `FieldBytesEncoding::encode_field_bytes` when `const impl` is stable
#[cfg(target_pointer_width = "32")]
let words = util::u32x18_to_u64x9(w.as_words());
#[cfg(target_pointer_width = "64")]
let words = w.as_words();
let le_bytes_wide = w.to_le_bytes();

let mut le_bytes = [0u8; 66];
let mut i = 0;

while i < words.len() - 1 {
let word = words[i].to_le_bytes();
let start = i * 8;
le_bytes[start] = word[0];
le_bytes[start + 1] = word[1];
le_bytes[start + 2] = word[2];
le_bytes[start + 3] = word[3];
le_bytes[start + 4] = word[4];
le_bytes[start + 5] = word[5];
le_bytes[start + 6] = word[6];
le_bytes[start + 7] = word[7];
// Extract the first 66-bytes of the 72-byte (576-bit) little endian serialized value
while i < le_bytes.len() {
le_bytes[i] = le_bytes_wide[i];
i += 1;
}

let last_word = words[8].to_le_bytes();
le_bytes[i * 8] = last_word[0];
le_bytes[(i * 8) + 1] = last_word[1];

// Decode the little endian serialization into the unsaturated big integer form used by
// the fiat-crypto synthesized code.
Self(fiat_p521_from_bytes(&le_bytes))
Expand Down

0 comments on commit c0129b6

Please sign in to comment.