Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Improvements

- [\#302](https://github.com/arkworks-rs/algebra/pull/302) (ark-ff) Add the relaxed NAF computation.

### Bug fixes

## v0.3.0
Expand Down
61 changes: 60 additions & 1 deletion ff/src/biginteger/arithmetic.rs
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};

/// Calculate a + b + carry, returning the sum and modifying the
/// carry value.
Expand Down Expand Up @@ -104,3 +104,62 @@ pub fn find_wnaf(num: &[u64]) -> Vec<i64> {

res
}

pub fn find_relaxed_naf(num: &[u64]) -> Vec<i64> {
let mut res = find_wnaf(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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add here find_naf(&12u64]) which has a shorter length?

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
}
}