Skip to content

Commit

Permalink
Merge pull request #1884 from ljedrz/post_wetb
Browse files Browse the repository at this point in the history
Further ToBits/ToBytes-related improvements
  • Loading branch information
howardwu authored Aug 15, 2023
2 parents 5aef7a3 + b4f9aa2 commit 8b65a77
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 108 deletions.
4 changes: 2 additions & 2 deletions algorithms/src/polycommit/kzg10/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ impl<E: PairingEngine> ToBytes for KZGCommitment<E> {
}

impl<E: PairingEngine> ToMinimalBits for KZGCommitment<E> {
fn to_minimal_bits(&self) -> Vec<bool> {
self.0.to_minimal_bits()
fn write_minimal_bits(&self, vec: &mut Vec<bool>) {
self.0.write_minimal_bits(vec);
}
}

Expand Down
43 changes: 14 additions & 29 deletions algorithms/src/polycommit/sonic_pc/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,19 @@ impl<E: PairingEngine> FromBytes for CommitterKey<E> {

// Construct the hash of the group elements.
let mut hash_input = powers_of_beta_g.to_bytes_le().map_err(|_| error("Could not serialize powers"))?;

hash_input.extend_from_slice(
&powers_of_beta_times_gamma_g
.to_bytes_le()
.map_err(|_| error("Could not serialize powers_of_beta_times_gamma_g"))?,
);
powers_of_beta_times_gamma_g
.write_le(&mut hash_input)
.map_err(|_| error("Could not serialize powers_of_beta_times_gamma_g"))?;

if let Some(shifted_powers_of_beta_g) = &shifted_powers_of_beta_g {
hash_input.extend_from_slice(
&shifted_powers_of_beta_g
.to_bytes_le()
.map_err(|_| error("Could not serialize shifted_powers_of_beta_g"))?,
);
shifted_powers_of_beta_g
.write_le(&mut hash_input)
.map_err(|_| error("Could not serialize shifted_powers_of_beta_g"))?;
}

if let Some(shifted_powers_of_beta_times_gamma_g) = &shifted_powers_of_beta_times_gamma_g {
for value in shifted_powers_of_beta_times_gamma_g.values() {
hash_input.extend_from_slice(
&value.to_bytes_le().map_err(|_| error("Could not serialize shifted_power_of_gamma_g"))?,
);
value.write_le(&mut hash_input).map_err(|_| error("Could not serialize shifted_power_of_gamma_g"))?;
}
}

Expand Down Expand Up @@ -249,27 +242,19 @@ impl<E: PairingEngine> ToBytes for CommitterKey<E> {

// Construct the hash of the group elements.
let mut hash_input = self.powers_of_beta_g.to_bytes_le().map_err(|_| error("Could not serialize powers"))?;

hash_input.extend_from_slice(
&self
.powers_of_beta_times_gamma_g
.to_bytes_le()
.map_err(|_| error("Could not serialize powers_of_beta_times_gamma_g"))?,
);
self.powers_of_beta_times_gamma_g
.write_le(&mut hash_input)
.map_err(|_| error("Could not serialize powers_of_beta_times_gamma_g"))?;

if let Some(shifted_powers_of_beta_g) = &self.shifted_powers_of_beta_g {
hash_input.extend_from_slice(
&shifted_powers_of_beta_g
.to_bytes_le()
.map_err(|_| error("Could not serialize shifted_powers_of_beta_g"))?,
);
shifted_powers_of_beta_g
.write_le(&mut hash_input)
.map_err(|_| error("Could not serialize shifted_powers_of_beta_g"))?;
}

if let Some(shifted_powers_of_beta_times_gamma_g) = &self.shifted_powers_of_beta_times_gamma_g {
for value in shifted_powers_of_beta_times_gamma_g.values() {
hash_input.extend_from_slice(
&value.to_bytes_le().map_err(|_| error("Could not serialize shifted_power_of_gamma_g"))?,
);
value.write_le(&mut hash_input).map_err(|_| error("Could not serialize shifted_power_of_gamma_g"))?;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use snarkvm_utilities::{
string::String,
FromBytes,
FromBytesDeserializer,
ToBits,
ToBytes,
ToBytesSerializer,
ToMinimalBits,
Expand All @@ -43,7 +44,7 @@ pub struct CircuitVerifyingKey<E: PairingEngine> {
}

impl<E: PairingEngine> ToMinimalBits for CircuitVerifyingKey<E> {
fn to_minimal_bits(&self) -> Vec<bool> {
fn write_minimal_bits(&self, vec: &mut Vec<bool>) {
let constraint_domain = EvaluationDomain::<E::Fr>::new(self.circuit_info.num_constraints)
.ok_or(SynthesisError::PolynomialDegreeTooLarge)
.unwrap();
Expand All @@ -67,37 +68,12 @@ impl<E: PairingEngine> ToMinimalBits for CircuitVerifyingKey<E> {
let non_zero_domain_b_size = non_zero_domain_b.size() as u64;
let non_zero_domain_c_size = non_zero_domain_c.size() as u64;

let constraint_domain_size_bits = constraint_domain_size
.to_le_bytes()
.iter()
.flat_map(|&byte| (0..8).map(move |i| (byte >> i) & 1u8 == 1u8))
.collect::<Vec<bool>>();
let non_zero_domain_size_a_bits = non_zero_domain_a_size
.to_le_bytes()
.iter()
.flat_map(|&byte| (0..8).map(move |i| (byte >> i) & 1u8 == 1u8))
.collect::<Vec<bool>>();
let non_zero_domain_size_b_bits = non_zero_domain_b_size
.to_le_bytes()
.iter()
.flat_map(|&byte| (0..8).map(move |i| (byte >> i) & 1u8 == 1u8))
.collect::<Vec<bool>>();
let non_zero_domain_size_c_bits = non_zero_domain_c_size
.to_le_bytes()
.iter()
.flat_map(|&byte| (0..8).map(move |i| (byte >> i) & 1u8 == 1u8))
.collect::<Vec<bool>>();

let circuit_commitments_bits = self.circuit_commitments.to_minimal_bits();

[
constraint_domain_size_bits,
non_zero_domain_size_a_bits,
non_zero_domain_size_b_bits,
non_zero_domain_size_c_bits,
circuit_commitments_bits,
]
.concat()
constraint_domain_size.write_bits_le(vec);
non_zero_domain_a_size.write_bits_le(vec);
non_zero_domain_b_size.write_bits_le(vec);
non_zero_domain_c_size.write_bits_le(vec);

self.circuit_commitments.write_minimal_bits(vec);
}
}

Expand Down
29 changes: 20 additions & 9 deletions console/program/src/data/identifier/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,8 @@ impl<N: Network> Debug for Identifier<N> {
impl<N: Network> Display for Identifier<N> {
/// Prints the identifier as a string.
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
// Convert the identifier to bits.
let bits_le = self.0.to_bits_le();

// Convert the bits to bytes.
let bytes = bits_le
.chunks(8)
.map(|byte| u8::from_bits_le(byte).map_err(|_| fmt::Error))
.collect::<Result<Vec<u8>, _>>()?;
// Convert the identifier to bytes.
let bytes = self.0.to_bytes_le().map_err(|_| fmt::Error)?;

// Parse the bytes as a UTF-8 string.
let string = String::from_utf8(bytes).map_err(|_| fmt::Error)?;
Expand All @@ -98,7 +92,7 @@ impl<N: Network> Display for Identifier<N> {
#[cfg(test)]
mod tests {
use super::*;
use crate::data::identifier::tests::sample_identifier_as_string;
use crate::data::identifier::tests::{sample_identifier, sample_identifier_as_string};
use snarkvm_console_network::Testnet3;

type CurrentNetwork = Testnet3;
Expand Down Expand Up @@ -226,4 +220,21 @@ mod tests {
assert_eq!("foo_bar", format!("{identifier}"));
Ok(())
}

#[test]
fn test_proxy_bits_equivalence() -> Result<()> {
let mut rng = TestRng::default();
let identifier: Identifier<CurrentNetwork> = sample_identifier(&mut rng)?;

// Direct conversion to bytes.
let bytes1 = identifier.0.to_bytes_le()?;

// Combined conversion via bits.
let bits_le = identifier.0.to_bits_le();
let bytes2 = bits_le.chunks(8).map(u8::from_bits_le).collect::<Result<Vec<u8>, _>>()?;

assert_eq!(bytes1, bytes2);

Ok(())
}
}
9 changes: 4 additions & 5 deletions curves/src/templates/short_weierstrass_jacobian/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,10 @@ impl<P: Parameters> AffineCurve for Affine<P> {
}

impl<P: Parameters> ToMinimalBits for Affine<P> {
fn to_minimal_bits(&self) -> Vec<bool> {
let mut res_bits = self.x.to_bits_le();
res_bits.push(*self.y.to_bits_le().first().unwrap());
res_bits.push(self.infinity);
res_bits
fn write_minimal_bits(&self, vec: &mut Vec<bool>) {
self.x.write_bits_le(vec);
vec.push(*self.y.to_bits_le().first().unwrap());
vec.push(self.infinity);
}
}

Expand Down
4 changes: 2 additions & 2 deletions curves/src/templates/twisted_edwards_extended/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ impl<P: Parameters> AffineCurve for Affine<P> {
}

impl<P: Parameters> ToMinimalBits for Affine<P> {
fn to_minimal_bits(&self) -> Vec<bool> {
self.x.to_bits_le()
fn write_minimal_bits(&self, vec: &mut Vec<bool>) {
self.x.write_bits_le(vec);
}
}

Expand Down
16 changes: 9 additions & 7 deletions fields/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,28 @@ macro_rules! sqrt_impl {
l_s.iter().take(j).sum::<u64>() + 1 + l_s.iter().skip(i + 1).sum::<u64>()
};

let calc_gamma = |i: usize, q_s: &[Vec<bool>], last: bool| -> $Self {
let calc_gamma = |i: usize, q_s: &[u64], last: bool| -> $Self {
let mut gamma = $Self::one();
if i != 0 {
q_s.iter().zip(l_s.iter()).enumerate().for_each(|(j, (q_bits, l))| {
q_s.iter().zip(l_s.iter()).enumerate().for_each(|(j, (q, l))| {
let mut kappa = calc_kappa(i, j, &l_s);
if last {
kappa -= 1;
}
q_bits.iter().enumerate().take(*l as usize).for_each(|(k, bit)| {
if *bit {
let mut value = *q;
(0..*l as usize).for_each(|k| {
let bit = value & 1 == 1;
if bit {
gamma *= $Self($P::POWERS_OF_ROOTS_OF_UNITY[(kappa as usize) + k], PhantomData);
}
value = value.wrapping_shr(1u32);
});
});
}
gamma
};

let mut q_s = Vec::<Vec<bool>>::with_capacity(k as usize);
let mut q_s = Vec::<u64>::with_capacity(k as usize);
let two_to_n_minus_l = 2u64.pow((n - l) as u32);
let two_to_n_minus_l_minus_one = 2u64.pow((n - l_minus_one) as u32);
x_s.enumerate().for_each(|(i, x)| {
Expand All @@ -172,8 +175,7 @@ macro_rules! sqrt_impl {
let gamma = calc_gamma(i, &q_s, false);
let alpha = x * gamma;
q_s.push(
(eval(alpha) / if i < k_1 as usize { two_to_n_minus_l_minus_one } else { two_to_n_minus_l })
.to_bits_le(),
eval(alpha) / if i < k_1 as usize { two_to_n_minus_l_minus_one } else { two_to_n_minus_l },
);
});

Expand Down
4 changes: 3 additions & 1 deletion ledger/coinbase/src/helpers/epoch_challenge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ impl<N: Network> EpochChallenge<N> {
/// Initializes a new epoch challenge.
pub fn new(epoch_number: u32, epoch_block_hash: N::BlockHash, degree: u32) -> Result<Self> {
// Construct the 'input' as '( epoch_number || epoch_block_hash )'
let input: Vec<u8> = epoch_number.to_le_bytes().into_iter().chain(epoch_block_hash.to_bytes_le()?).collect();
let mut input = vec![];
epoch_number.write_le(&mut input)?;
epoch_block_hash.write_le(&mut input)?;

let product_domain = CoinbasePuzzle::<N>::product_domain(degree)?;

Expand Down
9 changes: 5 additions & 4 deletions ledger/coinbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,11 @@ impl<N: Network> CoinbasePuzzle<N> {
) -> Result<DensePolynomial<<N::PairingCurve as PairingEngine>::Fr>> {
let input = {
let mut bytes = [0u8; 76];
bytes[..4].copy_from_slice(&epoch_challenge.epoch_number().to_bytes_le()?);
bytes[4..36].copy_from_slice(&epoch_challenge.epoch_block_hash().to_bytes_le()?);
bytes[36..68].copy_from_slice(&address.to_bytes_le()?);
bytes[68..].copy_from_slice(&nonce.to_le_bytes());
epoch_challenge.epoch_number().write_le(&mut bytes[..4])?;
epoch_challenge.epoch_block_hash().write_le(&mut bytes[4..36])?;
address.write_le(&mut bytes[36..68])?;
nonce.write_le(&mut bytes[68..])?;

bytes
};
Ok(hash_to_polynomial::<<N::PairingCurve as PairingEngine>::Fr>(&input, epoch_challenge.degree()))
Expand Down
6 changes: 3 additions & 3 deletions ledger/narwhal/batch-certificate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ impl<N: Network> BatchCertificate<N> {
pub fn compute_certificate_id(batch_id: Field<N>, signatures: &IndexMap<Signature<N>, i64>) -> Result<Field<N>> {
let mut preimage = Vec::new();
// Insert the batch ID.
preimage.extend_from_slice(&batch_id.to_bytes_le()?);
batch_id.write_le(&mut preimage)?;
// Insert the signatures.
for (signature, timestamp) in signatures {
// Insert the signature.
preimage.extend_from_slice(&signature.to_bytes_le()?);
signature.write_le(&mut preimage)?;
// Insert the timestamp.
preimage.extend_from_slice(&timestamp.to_bytes_le()?);
timestamp.write_le(&mut preimage)?;
}
// Hash the preimage.
N::hash_bhp1024(&preimage.to_bits_le())
Expand Down
2 changes: 1 addition & 1 deletion ledger/narwhal/transmission/src/helpers/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<T: FromBytes + ToBytes + Send + 'static> Data<T> {

pub fn serialize_blocking_into<W: Write>(&self, writer: &mut W) -> Result<()> {
match self {
Self::Object(x) => Ok(writer.write_all(&x.to_bytes_le()?)?),
Self::Object(x) => Ok(x.write_le(writer)?),
Self::Buffer(bytes) => Ok(writer.write_all(bytes)?),
}
}
Expand Down
6 changes: 5 additions & 1 deletion utilities/src/biginteger/bigint_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ impl FromBits for BigInteger256 {
impl ToBytes for BigInteger256 {
#[inline]
fn write_le<W: Write>(&self, writer: W) -> IoResult<()> {
self.0.write_le(writer)
let mut arr = [0u8; 8 * 4];
for (i, num) in self.0.iter().enumerate() {
arr[i * 8..(i + 1) * 8].copy_from_slice(&num.to_le_bytes());
}
arr.write_le(writer)
}
}

Expand Down
6 changes: 5 additions & 1 deletion utilities/src/biginteger/bigint_384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ impl FromBits for BigInteger384 {
impl ToBytes for BigInteger384 {
#[inline]
fn write_le<W: Write>(&self, writer: W) -> IoResult<()> {
self.0.write_le(writer)
let mut arr = [0u8; 8 * 6];
for (i, num) in self.0.iter().enumerate() {
arr[i * 8..(i + 1) * 8].copy_from_slice(&num.to_le_bytes());
}
arr.write_le(writer)
}
}
impl FromBytes for BigInteger384 {
Expand Down
18 changes: 14 additions & 4 deletions utilities/src/bititerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::iter::ExactSizeIterator;

/// Iterates over a slice of `u64` in *big-endian* order.
#[derive(Debug)]
pub struct BitIteratorBE<Slice> {
Expand All @@ -31,10 +33,6 @@ impl<Slice: AsRef<[u64]>> BitIteratorBE<Slice> {
pub fn new_without_leading_zeros(s: Slice) -> impl Iterator<Item = bool> {
Self::new(s).skip_while(|b| !b)
}

pub fn len(&self) -> usize {
self.n
}
}

impl<Slice: AsRef<[u64]>> Iterator for BitIteratorBE<Slice> {
Expand All @@ -53,6 +51,12 @@ impl<Slice: AsRef<[u64]>> Iterator for BitIteratorBE<Slice> {
}
}

impl<Slice: AsRef<[u64]>> ExactSizeIterator for BitIteratorBE<Slice> {
fn len(&self) -> usize {
self.n
}
}

/// Iterates over a slice of `u64` in *little-endian* order.
#[derive(Debug)]
pub struct BitIteratorLE<Slice: AsRef<[u64]>> {
Expand Down Expand Up @@ -85,6 +89,12 @@ impl<Slice: AsRef<[u64]>> Iterator for BitIteratorLE<Slice> {
}
}

impl<Slice: AsRef<[u64]>> ExactSizeIterator for BitIteratorLE<Slice> {
fn len(&self) -> usize {
self.max_len
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 8b65a77

Please sign in to comment.