-
Notifications
You must be signed in to change notification settings - Fork 46
Allow wnaf to represent dense values #46
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -94,12 +94,12 @@ pub(crate) fn wnaf_form<S: AsRef<[u8]>>(wnaf: &mut Vec<i64>, c: S, window: usize | |||||||||||||||||||||||||||||||||||||||||||
| // Required by the NAF definition | ||||||||||||||||||||||||||||||||||||||||||||
| debug_assert!(window >= 2); | ||||||||||||||||||||||||||||||||||||||||||||
| // Required so that the NAF digits fit in i64 | ||||||||||||||||||||||||||||||||||||||||||||
| debug_assert!(window <= 64); | ||||||||||||||||||||||||||||||||||||||||||||
| debug_assert!(window < 64); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| let bit_len = c.as_ref().len() * 8; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| wnaf.truncate(0); | ||||||||||||||||||||||||||||||||||||||||||||
| wnaf.reserve(bit_len); | ||||||||||||||||||||||||||||||||||||||||||||
| wnaf.reserve(bit_len + 1); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Initialise the current and next limb buffers. | ||||||||||||||||||||||||||||||||||||||||||||
| let mut limbs = LimbBuffer::new(c.as_ref()); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -109,7 +109,7 @@ pub(crate) fn wnaf_form<S: AsRef<[u8]>>(wnaf: &mut Vec<i64>, c: S, window: usize | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| let mut pos = 0; | ||||||||||||||||||||||||||||||||||||||||||||
| let mut carry = 0; | ||||||||||||||||||||||||||||||||||||||||||||
| while pos < bit_len { | ||||||||||||||||||||||||||||||||||||||||||||
| while pos <= bit_len { | ||||||||||||||||||||||||||||||||||||||||||||
| // Construct a buffer of bits of the scalar, starting at bit `pos` | ||||||||||||||||||||||||||||||||||||||||||||
| let u64_idx = pos / 64; | ||||||||||||||||||||||||||||||||||||||||||||
| let bit_idx = pos % 64; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -144,6 +144,7 @@ pub(crate) fn wnaf_form<S: AsRef<[u8]>>(wnaf: &mut Vec<i64>, c: S, window: usize | |||||||||||||||||||||||||||||||||||||||||||
| pos += window; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| wnaf.truncate(wnaf.len().saturating_sub(window - 1)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Member
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. The purpose of this line is unclear, and it could accidentally lead to unintended truncation of meaningful limbs if a future refactor altered how the earlier logic works. Document this line. |
||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Performs w-NAF exponentiation with the provided window table and w-NAF form scalar. | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -504,3 +505,28 @@ impl<G: Group, const WINDOW_SIZE: usize> Mul<&WnafScalar<G::Scalar, WINDOW_SIZE> | |||||||||||||||||||||||||||||||||||||||||||
| wnaf_exp(&self.table, &rhs.wnaf) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn test_wnaf_form() { | ||||||||||||||||||||||||||||||||||||||||||||
| fn from_wnaf(wnaf: &Vec<i64>) -> u128 { | ||||||||||||||||||||||||||||||||||||||||||||
| wnaf.iter().rev().fold(0, |acc, next| { | ||||||||||||||||||||||||||||||||||||||||||||
| let mut acc = acc * 2; | ||||||||||||||||||||||||||||||||||||||||||||
| acc += *next as u128; | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+513
to
+514
Member
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. These lines panic in debug mode, which forbids integer overflow. I've replaced them with explicit wrapping operations, and added documentation to explain why it works.
Suggested change
Contributor
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. @str4d sidebar: you might consider adding the |
||||||||||||||||||||||||||||||||||||||||||||
| acc | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| for w in 2..64 { | ||||||||||||||||||||||||||||||||||||||||||||
| for e in 0..=u16::MAX { | ||||||||||||||||||||||||||||||||||||||||||||
| let mut wnaf = vec![]; | ||||||||||||||||||||||||||||||||||||||||||||
| wnaf_form(&mut wnaf, e.to_le_bytes(), w); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(e as u128, from_wnaf(&wnaf)); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| for w in 2..64 { | ||||||||||||||||||||||||||||||||||||||||||||
| for e in u128::MAX - 10000..=u128::MAX { | ||||||||||||||||||||||||||||||||||||||||||||
| let mut wnaf = vec![]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+517
to
+527
Member
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| wnaf_form(&mut wnaf, e.to_le_bytes(), w); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(e, from_wnaf(&wnaf)); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
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 seems like a separate issue, and fixing it in this PR is confusing. Rather than reducing the allowed values of
window, we should fix the bug by makingwidthau128, and adjusting the rest of the code accordingly.