Skip to content
This repository has been archived by the owner on Aug 27, 2021. It is now read-only.

Add padding to allocated nonnative to_bytes #43

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 5 additions & 10 deletions src/allocated_nonnative_field_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,18 +577,13 @@ impl<TargetField: PrimeField, BaseField: PrimeField> ToBytesGadget<BaseField>
{
#[tracing::instrument(target = "r1cs")]
fn to_bytes(&self) -> R1CSResult<Vec<UInt8<BaseField>>> {
let bits = self.to_bits_le()?;

let mut bytes = Vec::<UInt8<BaseField>>::new();
bits.chunks(8).for_each(|bits_per_byte| {
let mut bits_per_byte: Vec<Boolean<BaseField>> = bits_per_byte.to_vec();
if bits_per_byte.len() < 8 {
bits_per_byte.resize_with(8, || Boolean::<BaseField>::constant(false));
}
let mut bits = self.to_bits_le()?;

bytes.push(UInt8::<BaseField>::from_bits_le(&bits_per_byte));
});
let num_bits = TargetField::BigInt::NUM_LIMBS * 64;
assert!(bits.len() <= num_bits);
bits.resize_with(num_bits, || Boolean::constant(false));

let bytes = bits.chunks(8).map(UInt8::from_bits_le).collect();
Ok(bytes)
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/to_bytes_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ark_ec::PairingEngine;
use ark_ff::Zero;
use ark_ff::{to_bytes, Zero};
use ark_mnt4_298::MNT4_298;
use ark_mnt6_298::MNT6_298;
use ark_nonnative_field::NonNativeFieldVar;
Expand Down Expand Up @@ -33,6 +33,8 @@ fn to_bytes_test() {
for byte in target_to_bytes.iter().skip(3) {
assert_eq!(*byte, 0);
}

assert_eq!(to_bytes!(target_test_elem).unwrap(), target_to_bytes);
}

#[test]
Expand Down