Skip to content

Commit fd2df01

Browse files
committed
Refactor bit iteration infrastructure:
* `to_bits` -> `to_bits_le` * `BitIterator` -> `BitIteratorLE` + `BitIteratorBE` * `found_one`/`seen_one` -> `BitIteratorBE::without_leading_zeros`
1 parent 131eff2 commit fd2df01

File tree

64 files changed

+601
-679
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+601
-679
lines changed

algebra-core/src/biginteger/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ macro_rules! bigint_impl {
169169
#[inline]
170170
fn to_bits(&self) -> Vec<bool> {
171171
let mut res = Vec::with_capacity(256);
172-
for b in BitIterator::new(self.0) {
172+
for b in BitIteratorBE::new(self.0) {
173173
res.push(b);
174174
}
175175
res

algebra-core/src/biginteger/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
bytes::{FromBytes, ToBytes},
3-
fields::BitIterator,
3+
fields::BitIteratorBE,
44
io::{Read, Result as IoResult, Write},
55
CanonicalDeserialize, CanonicalSerialize, ConstantSerializedSize, SerializationError,
66
UniformRand, Vec,

algebra-core/src/bytes.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ macro_rules! to_bytes {
152152
macro_rules! push_to_vec {
153153
($buf:expr, $y:expr, $($x:expr),*) => ({
154154
{
155-
ToBytes::write(&$y, &mut $buf)
155+
$crate::ToBytes::write(&$y, &mut $buf)
156156
}.and({$crate::push_to_vec!($buf, $($x),*)})
157157
});
158158

159159
($buf:expr, $x:expr) => ({
160-
ToBytes::write(&$x, &mut $buf)
160+
$crate::ToBytes::write(&$x, &mut $buf)
161161
})
162162
}
163163

@@ -311,7 +311,6 @@ impl<T: FromBytes> FromBytes for Option<T> {
311311

312312
#[cfg(test)]
313313
mod test {
314-
use super::ToBytes;
315314
use crate::Vec;
316315
#[test]
317316
fn test_macro_empty() {

algebra-core/src/curves/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,9 @@ pub trait ProjectiveCurve:
180180
/// Performs scalar multiplication of this element.
181181
fn mul<S: Into<<Self::ScalarField as PrimeField>::BigInt>>(mut self, other: S) -> Self {
182182
let mut res = Self::zero();
183-
184-
let mut found_one = false;
185-
186-
for i in crate::fields::BitIterator::new(other.into()) {
187-
if found_one {
188-
res.double_in_place();
189-
} else {
190-
found_one |= i;
191-
}
192-
193-
if i {
183+
for b in crate::fields::BitIteratorBE::without_leading_zeros(other.into()) {
184+
res.double_in_place();
185+
if b {
194186
res += self;
195187
}
196188
}

algebra-core/src/curves/models/bls12/g2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
short_weierstrass_jacobian::{GroupAffine, GroupProjective},
77
AffineCurve,
88
},
9-
fields::{BitIterator, Field, Fp2},
9+
fields::{BitIteratorBE, Field, Fp2},
1010
io::{Result as IoResult, Write},
1111
Vec,
1212
};
@@ -75,7 +75,7 @@ impl<P: Bls12Parameters> From<G2Affine<P>> for G2Prepared<P> {
7575
z: Fp2::one(),
7676
};
7777

78-
for i in BitIterator::new(P::X).skip(1) {
78+
for i in BitIteratorBE::new(P::X).skip(1) {
7979
ell_coeffs.push(doubling_step::<P>(&mut r, &two_inv));
8080

8181
if i {

algebra-core/src/curves/models/bls12/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
fp12_2over3over2::{Fp12, Fp12Parameters},
88
fp2::Fp2Parameters,
99
fp6_3over2::Fp6Parameters,
10-
BitIterator, Field, Fp2, PrimeField, SquareRootField,
10+
BitIteratorBE, Field, Fp2, PrimeField, SquareRootField,
1111
},
1212
};
1313
use num_traits::One;
@@ -105,7 +105,7 @@ impl<P: Bls12Parameters> PairingEngine for Bls12<P> {
105105

106106
let mut f = Self::Fqk::one();
107107

108-
for i in BitIterator::new(P::X).skip(1) {
108+
for i in BitIteratorBE::new(P::X).skip(1) {
109109
f.square_in_place();
110110

111111
for (p, ref mut coeffs) in &mut pairs {

algebra-core/src/curves/models/bw6/g2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
short_weierstrass_jacobian::{GroupAffine, GroupProjective},
77
AffineCurve,
88
},
9-
fields::{BitIterator, Field},
9+
fields::{BitIteratorBE, Field},
1010
io::{Result as IoResult, Write},
1111
Vec,
1212
};
@@ -83,7 +83,7 @@ impl<P: BW6Parameters> From<G2Affine<P>> for G2Prepared<P> {
8383
z: P::Fp::one(),
8484
};
8585

86-
for i in BitIterator::new(P::ATE_LOOP_COUNT_1).skip(1) {
86+
for i in BitIteratorBE::new(P::ATE_LOOP_COUNT_1).skip(1) {
8787
ell_coeffs_1.push(doubling_step::<P>(&mut r));
8888

8989
if i {

algebra-core/src/curves/models/bw6/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
fields::{
77
fp3::Fp3Parameters,
88
fp6_2over3::{Fp6, Fp6Parameters},
9-
BitIterator, Field, PrimeField, SquareRootField,
9+
BitIteratorBE, Field, PrimeField, SquareRootField,
1010
},
1111
};
1212
use num_traits::One;
@@ -238,7 +238,7 @@ impl<P: BW6Parameters> PairingEngine for BW6<P> {
238238
// f_{u+1,Q}(P)
239239
let mut f_1 = Self::Fqk::one();
240240

241-
for i in BitIterator::new(P::ATE_LOOP_COUNT_1).skip(1) {
241+
for i in BitIteratorBE::new(P::ATE_LOOP_COUNT_1).skip(1) {
242242
f_1.square_in_place();
243243

244244
for (p, ref mut coeffs) in &mut pairs_1 {

algebra-core/src/curves/models/mnt4/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
fields::{
77
fp2::{Fp2, Fp2Parameters},
88
fp4::{Fp4, Fp4Parameters},
9-
BitIterator, Field, PrimeField, SquareRootField,
9+
BitIteratorBE, Field, PrimeField, SquareRootField,
1010
},
1111
One, Zero,
1212
};
@@ -111,18 +111,9 @@ impl<P: MNT4Parameters> MNT4<P> {
111111
let mut dbl_idx: usize = 0;
112112
let mut add_idx: usize = 0;
113113

114-
let mut found_one = false;
115-
116-
for bit in BitIterator::new(P::ATE_LOOP_COUNT) {
117-
// code below gets executed for all bits (EXCEPT the MSB itself) of
118-
// mnt6_param_p (skipping leading zeros) in MSB to LSB order
119-
if !found_one && bit {
120-
found_one = true;
121-
continue;
122-
} else if !found_one {
123-
continue;
124-
}
125-
114+
// code below gets executed for all bits (EXCEPT the MSB itself) of
115+
// mnt6_param_p (skipping leading zeros) in MSB to LSB order
116+
for bit in BitIteratorBE::without_leading_zeros(P::ATE_LOOP_COUNT).skip(1) {
126117
let dc = &q.double_coefficients[dbl_idx];
127118
dbl_idx += 1;
128119

algebra-core/src/curves/models/mnt6/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
fields::{
77
fp3::{Fp3, Fp3Parameters},
88
fp6_2over3::{Fp6, Fp6Parameters},
9-
BitIterator, Field, PrimeField, SquareRootField,
9+
BitIteratorBE, Field, PrimeField, SquareRootField,
1010
},
1111
One, Zero,
1212
};
@@ -113,18 +113,9 @@ impl<P: MNT6Parameters> MNT6<P> {
113113
let mut dbl_idx: usize = 0;
114114
let mut add_idx: usize = 0;
115115

116-
let mut found_one = false;
117-
118-
for bit in BitIterator::new(P::ATE_LOOP_COUNT) {
119-
// code below gets executed for all bits (EXCEPT the MSB itself) of
120-
// mnt6_param_p (skipping leading zeros) in MSB to LSB order
121-
if !found_one && bit {
122-
found_one = true;
123-
continue;
124-
} else if !found_one {
125-
continue;
126-
}
127-
116+
// code below gets executed for all bits (EXCEPT the MSB itself) of
117+
// mnt6_param_p (skipping leading zeros) in MSB to LSB order
118+
for bit in BitIteratorBE::without_leading_zeros(P::ATE_LOOP_COUNT).skip(1) {
128119
let dc = &q.double_coefficients[dbl_idx];
129120
dbl_idx += 1;
130121

0 commit comments

Comments
 (0)