Skip to content
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

p521: Use unsaturated limbs #945

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions p521/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,54 @@ impl FieldElement {
Self::from_uint_unchecked(U576::from_u64(w))
}

/// Converts the words into a byte array to be consumed by [`fiat_p521_from_bytes`]
const fn words_to_le_bytes_unchecked(words: [u64; 9]) -> [u8; 66] {
MasterAwesome marked this conversation as resolved.
Show resolved Hide resolved
let mut result: [u8; 66] = [0u8; 66];
let mut i = 0;
while i < words.len() - 1 {
let word = words[i].to_le_bytes();
let start = i * 8;
result[start] = word[0];
result[start + 1] = word[1];
result[start + 2] = word[2];
result[start + 3] = word[3];
result[start + 4] = word[4];
result[start + 5] = word[5];
result[start + 6] = word[6];
result[start + 7] = word[7];
i += 1;
}
let last_word = words[8].to_le_bytes();
debug_assert!(
last_word[1] <= 0x1,
"Input bound for the result[65] is [0x0 ~> 0x1]"
);
debug_assert!(
last_word[2] == 0
&& last_word[3] == 0
&& last_word[4] == 0
&& last_word[5] == 0
&& last_word[6] == 0
&& last_word[7] == 0,
"Expected last word to have leading zeroes"
);

result[i * 8] = last_word[0];
result[(i * 8) + 1] = last_word[1];

result
}

/// Decode [`FieldElement`] from [`U576`].
///
/// Does *not* perform a check that the field element does not overflow the order.
///
/// Used incorrectly this can lead to invalid results!
#[cfg(target_pointer_width = "32")]
pub(crate) const fn from_uint_unchecked(w: U576) -> Self {
Self(u32x18_to_u64x9(w.as_words()))
Self(fiat_p521_from_bytes(&Self::words_to_le_bytes_unchecked(
u32x18_to_u64x9(w.as_words()),
)))
}

/// Decode [`FieldElement`] from [`U576`].
Expand All @@ -118,7 +158,9 @@ impl FieldElement {
/// Used incorrectly this can lead to invalid results!
#[cfg(target_pointer_width = "64")]
pub(crate) const fn from_uint_unchecked(w: U576) -> Self {
Self(w.to_words())
Self(fiat_p521_from_bytes(&Self::words_to_le_bytes_unchecked(
w.to_words(),
)))
}

/// Returns the big-endian encoding of this [`FieldElement`].
Expand Down