-
Notifications
You must be signed in to change notification settings - Fork 366
Add the relaxed NAF #302
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
Merged
Merged
Add the relaxed NAF #302
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5f16b39
add the relaxed NAF
weikengchen 34fca8e
fmt; rename
weikengchen 170c8ec
simplify the algorithm
weikengchen 8743ff8
CHANGELOG
weikengchen f15c376
make find_relaxed_naf public
weikengchen 875d4b1
Merge branch 'master' into add-relaxed-naf
weikengchen bf13bc0
Update CHANGELOG.md
weikengchen 3414022
cleaned up
weikengchen 1f897aa
show the usefulness
weikengchen ff93bcc
Merge branch 'master' into add-relaxed-naf
weikengchen 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 hidden or 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 hidden or 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,4 +1,4 @@ | ||
| use ark_std::vec::Vec; | ||
| use ark_std::{vec, vec::Vec}; | ||
|
|
||
| macro_rules! adc { | ||
| ($a:expr, $b:expr, &mut $carry:expr$(,)?) => {{ | ||
|
|
@@ -66,8 +66,8 @@ pub(crate) fn mac_discard(a: u64, b: u64, c: u64, carry: &mut u64) { | |
| *carry = (tmp >> 64) as u64; | ||
| } | ||
|
|
||
| /// Compute the Window NAF (non-adjacent form) of num | ||
| pub fn find_wnaf(num: &[u64]) -> Vec<i64> { | ||
| /// Compute the NAF (non-adjacent form) of num | ||
| pub fn find_naf(num: &[u64]) -> Vec<i64> { | ||
| let is_zero = |num: &[u64]| num.iter().all(|x| *x == 0u64); | ||
| let is_odd = |num: &[u64]| num[0] & 1 == 1; | ||
| let sub_noborrow = |num: &mut [u64], z: u64| { | ||
|
|
@@ -119,3 +119,75 @@ pub fn find_wnaf(num: &[u64]) -> Vec<i64> { | |
|
|
||
| res | ||
| } | ||
|
|
||
| // We define relaxed NAF as a variant of NAF with a very small tweak. | ||
| // | ||
| // Note that the cost of scalar multiplication grows with the length of the sequence (for doubling) | ||
| // plus the Hamming weight of the sequence (for addition, or subtraction). | ||
| // | ||
| // NAF is optimizing for the Hamming weight only and therefore can be suboptimal. | ||
| // For example, NAF may generate a sequence (in little-endian) of the form ...0 -1 0 1. | ||
| // | ||
| // This can be rewritten as ...0 1 1 to avoid one doubling, at the cost that we are making an | ||
| // exception of non-adjacence for the most significant bit. | ||
| // | ||
| // Since this representation is no longer a strict NAF, we call it ``relaxed NAF''. | ||
| // | ||
| pub fn find_relaxed_naf(num: &[u64]) -> Vec<i64> { | ||
| let mut res = find_naf(num); | ||
|
|
||
| let len = res.len(); | ||
| if res[len - 2] == 0 && res[len - 3] == -1 { | ||
| res[len - 3] = 1; | ||
| res[len - 2] = 1; | ||
| res.resize(len - 1, 0); | ||
| } | ||
|
|
||
| res | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_find_relaxed_naf_usefulness() { | ||
| let vec = find_relaxed_naf(&[12u64]); | ||
| assert_eq!(vec.len(), 4); | ||
|
Comment on lines
+154
to
+155
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. Maybe we can add here
Member
Author
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. Yes. Added. It has a slightly longer len---5. |
||
| } | ||
|
|
||
| #[test] | ||
| fn test_find_relaxed_naf_correctness() { | ||
| use ark_std::{One, UniformRand, Zero}; | ||
| use num_bigint::BigInt; | ||
|
|
||
| let mut rng = ark_std::test_rng(); | ||
|
|
||
| for _ in 0..10 { | ||
| let num = [ | ||
| u64::rand(&mut rng), | ||
| u64::rand(&mut rng), | ||
| u64::rand(&mut rng), | ||
| u64::rand(&mut rng), | ||
| ]; | ||
| let relaxed_naf = find_relaxed_naf(&num); | ||
|
|
||
| let test = { | ||
| let mut sum = BigInt::zero(); | ||
| let mut cur = BigInt::one(); | ||
| for v in relaxed_naf { | ||
| sum += cur.clone() * v; | ||
| cur = cur * 2; | ||
| } | ||
| sum | ||
| }; | ||
|
|
||
| let test_expected = { | ||
| let mut sum = BigInt::zero(); | ||
| let mut cur = BigInt::one(); | ||
| for v in num.iter() { | ||
| sum += cur.clone() * v; | ||
| cur = cur << 64; | ||
| } | ||
| sum | ||
| }; | ||
|
|
||
| assert_eq!(test, test_expected); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.