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

Add to_bytes to bigint #166

Merged
merged 11 commits into from
Jan 7, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The main features of this release are:
- #153 (ark-serialize) Add an impl of `CanonicalSerialize/Deserialize` for `Rc<T>`.
- #157 (ark-ec) Speed up `variable_base_msm` by not relying on unnecessary normalization.
- #158 (ark-serialize) Add an impl of `CanonicalSerialize/Deserialize` for `()`.
- #166 (ark-ff) Add a `to_bytes()` method to `BigInt`.

### Bug fixes
- #36 (ark-ec) In Short-Weierstrass curves, include an infinity bit in `ToConstraintField`.
Expand Down
19 changes: 17 additions & 2 deletions ff/src/biginteger/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,24 @@ macro_rules! bigint_impl {

#[inline]
fn to_bits(&self) -> Vec<bool> {
let mut res = Vec::with_capacity(256);
BigIteratorBE::new(self.0).collect::<Vec<_>>()
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
}

#[inline]
fn to_bytes(&self) -> Vec<u8> {
let mut res = Vec::with_capacity($num_limbs * 8);
let mut cur_byte = 0u8;
let mut bit_num_mod_8 = 0;
for b in BitIteratorBE::new(self.0) {
res.push(b);
cur_byte = (cur_byte * 2) + (b as u8);
bit_num_mod_8 = (bit_num_mod_8 + 1) % 8;
if bit_num_mod_8 == 0 {
res.push(cur_byte);
cur_byte = 0u8;
}
}
if bit_num_mod_8 != 0 {
res.push(cur_byte);
}
res
}
Expand Down
8 changes: 6 additions & 2 deletions ff/src/biginteger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ pub trait BigInteger:
/// array.
fn from_bits(bits: &[bool]) -> Self;

/// Returns the bit representation in a big endian boolean array, without
/// leading zeros.
/// Returns the bit representation in a big endian boolean array,
/// with leading zeroes.
fn to_bits(&self) -> Vec<bool>;

/// Returns the byte representation in a big endian byte array,
/// with leading zeros.
fn to_bytes(&self) -> Vec<u8>;

/// Returns a vector for wnaf.
fn find_wnaf(&self) -> Vec<i64>;

Expand Down