Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbochok committed Oct 20, 2020
1 parent a489285 commit 12a9698
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 75 deletions.
4 changes: 2 additions & 2 deletions core/lib/circuit/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn sign_sha256<E>(
where
E: JubjubEngine,
{
let message_bytes = pack_bits_into_bytes(msg_data.to_vec());
let message_bytes = BitConvert::into_bytes(msg_data.to_vec());

let seed = Seed::deterministic_seed(&private_key, &message_bytes);
let signature = private_key.musig_sha256_sign(&message_bytes, &seed, p_g, params);
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn sign_rescue<E>(
where
E: RescueEngine + JubjubEngine,
{
let message_bytes = pack_bits_into_bytes(msg_data.to_vec());
let message_bytes = BitConvert::into_bytes(msg_data.to_vec());

let seed = Seed::deterministic_seed(&private_key, &message_bytes);
let signature =
Expand Down
2 changes: 1 addition & 1 deletion core/lib/circuit/src/witness/change_pubkey_offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl ChangePubkeyOffChainWitness<Bn256> {
let fee_token_fe = Fr::from_str(&change_pubkey_offcahin.fee_token.to_string()).unwrap();
let fee_as_field_element = Fr::from_str(&change_pubkey_offcahin.fee.to_string()).unwrap();

let fee_bits = FloatConversions::convert_to_float(
let fee_bits = FloatConversions::to_float(
change_pubkey_offcahin.fee,
FEE_EXPONENT_BIT_WIDTH,
FEE_MANTISSA_BIT_WIDTH,
Expand Down
4 changes: 2 additions & 2 deletions core/lib/circuit/src/witness/close_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl CloseAccountWitness<Bn256> {
// use super::*;
// use crate::witness::utils::public_data_commitment;
// use zksync_types::merkle_tree::PedersenHasher;
// use zksync_types::primitives::bytes_into_be_bits;
// use zksync_types::primitives::BitConvert::from_be_bytes;
//
// use crate::circuit::ZkSyncCircuit;
// use bellman::Circuit;
Expand Down Expand Up @@ -267,7 +267,7 @@ impl CloseAccountWitness<Bn256> {
// );
// let packed_public_key = PackedPublicKey(sender_pk);
// let packed_public_key_bytes = packed_public_key.serialize_packed().unwrap();
// let signer_packed_key_bits: Vec<_> = bytes_into_be_bits(&packed_public_key_bytes)
// let signer_packed_key_bits: Vec<_> = BitConvert::from_be_bytes(&packed_public_key_bytes)
// .iter()
// .map(|x| Some(input.x))
// .collect();
Expand Down
4 changes: 2 additions & 2 deletions core/lib/circuit/src/witness/forced_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl ForcedExitWitness<Bn256> {
let token_fe = Fr::from_str(&forced_exit.token.to_string()).unwrap();
let amount_as_field_element = Fr::from_str(&forced_exit.amount.to_string()).unwrap();

let amount_bits = FloatConversions::convert_to_float(
let amount_bits = FloatConversions::to_float(
forced_exit.amount,
AMOUNT_EXPONENT_BIT_WIDTH,
AMOUNT_MANTISSA_BIT_WIDTH,
Expand All @@ -265,7 +265,7 @@ impl ForcedExitWitness<Bn256> {

let fee_as_field_element = Fr::from_str(&forced_exit.fee.to_string()).unwrap();

let fee_bits = FloatConversions::convert_to_float(
let fee_bits = FloatConversions::to_float(
forced_exit.fee,
FEE_EXPONENT_BIT_WIDTH,
FEE_MANTISSA_BIT_WIDTH,
Expand Down
4 changes: 2 additions & 2 deletions core/lib/circuit/src/witness/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl TransferWitness<Bn256> {
let token_fe = Fr::from_str(&transfer.token.to_string()).unwrap();
let amount_as_field_element = Fr::from_str(&transfer.amount.to_string()).unwrap();

let amount_bits = FloatConversions::convert_to_float(
let amount_bits = FloatConversions::to_float(
transfer.amount,
AMOUNT_EXPONENT_BIT_WIDTH,
AMOUNT_MANTISSA_BIT_WIDTH,
Expand All @@ -228,7 +228,7 @@ impl TransferWitness<Bn256> {

let fee_as_field_element = Fr::from_str(&transfer.fee.to_string()).unwrap();

let fee_bits = FloatConversions::convert_to_float(
let fee_bits = FloatConversions::to_float(
transfer.fee,
FEE_EXPONENT_BIT_WIDTH,
FEE_MANTISSA_BIT_WIDTH,
Expand Down
4 changes: 2 additions & 2 deletions core/lib/circuit/src/witness/transfer_to_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl TransferToNewWitness<Bn256> {
let token_fe = Fr::from_str(&transfer_to_new.token.to_string()).unwrap();
let amount_as_field_element = Fr::from_str(&transfer_to_new.amount.to_string()).unwrap();

let amount_bits = FloatConversions::convert_to_float(
let amount_bits = FloatConversions::to_float(
transfer_to_new.amount,
AMOUNT_EXPONENT_BIT_WIDTH,
AMOUNT_MANTISSA_BIT_WIDTH,
Expand All @@ -264,7 +264,7 @@ impl TransferToNewWitness<Bn256> {
"test transfer_to_new fee_as_field_element = {}",
fee_as_field_element
);
let fee_bits = FloatConversions::convert_to_float(
let fee_bits = FloatConversions::to_float(
transfer_to_new.fee,
FEE_EXPONENT_BIT_WIDTH,
FEE_MANTISSA_BIT_WIDTH,
Expand Down
8 changes: 4 additions & 4 deletions core/lib/circuit/src/witness/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,25 @@ impl SigDataInput {
pub_key: &PackedPublicKey,
) -> Result<SigDataInput, anyhow::Error> {
let (r_bytes, s_bytes) = sig_bytes.split_at(32);
let r_bits: Vec<_> = zksync_crypto::primitives::bytes_into_be_bits(&r_bytes)
let r_bits: Vec<_> = zksync_crypto::primitives::BitConvert::from_be_bytes(&r_bytes)
.iter()
.map(|x| Some(*x))
.collect();
let s_bits: Vec<_> = zksync_crypto::primitives::bytes_into_be_bits(&s_bytes)
let s_bits: Vec<_> = zksync_crypto::primitives::BitConvert::from_be_bytes(&s_bytes)
.iter()
.map(|x| Some(*x))
.collect();
let signature = SignatureData {
r_packed: r_bits,
s: s_bits,
};
let sig_bits: Vec<bool> = zksync_crypto::primitives::bytes_into_be_bits(&tx_bytes);
let sig_bits: Vec<bool> = zksync_crypto::primitives::BitConvert::from_be_bytes(&tx_bytes);

let (first_sig_msg, second_sig_msg, third_sig_msg) = self::generate_sig_witness(&sig_bits);

let signer_packed_key_bytes = pub_key.serialize_packed()?;
let signer_pub_key_packed: Vec<_> =
zksync_crypto::primitives::bytes_into_be_bits(&signer_packed_key_bytes)
zksync_crypto::primitives::BitConvert::from_be_bytes(&signer_packed_key_bytes)
.iter()
.map(|x| Some(*x))
.collect();
Expand Down
4 changes: 2 additions & 2 deletions core/lib/circuit/src/witness/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl WithdrawWitness<Bn256> {
let token_fe = Fr::from_str(&withdraw.token.to_string()).unwrap();
let amount_as_field_element = Fr::from_str(&withdraw.amount.to_string()).unwrap();

let amount_bits = FloatConversions::convert_to_float(
let amount_bits = FloatConversions::to_float(
withdraw.amount,
AMOUNT_EXPONENT_BIT_WIDTH,
AMOUNT_MANTISSA_BIT_WIDTH,
Expand All @@ -210,7 +210,7 @@ impl WithdrawWitness<Bn256> {

let fee_as_field_element = Fr::from_str(&withdraw.fee.to_string()).unwrap();

let fee_bits = FloatConversions::convert_to_float(
let fee_bits = FloatConversions::to_float(
withdraw.fee,
FEE_EXPONENT_BIT_WIDTH,
FEE_MANTISSA_BIT_WIDTH,
Expand Down
121 changes: 69 additions & 52 deletions core/lib/crypto/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,60 +127,85 @@ impl<E: AsRef<[u64]>> Iterator for BitIteratorLe<E> {
}
}

pub fn pack_bits_into_bytes(bits: Vec<bool>) -> Vec<u8> {
assert_eq!(bits.len() % 8, 0);
let mut message_bytes: Vec<u8> = vec![];

let byte_chunks = bits.chunks(8);
for byte_chunk in byte_chunks {
let mut byte = 0u8;
for (i, bit) in byte_chunk.iter().enumerate() {
if *bit {
byte |= 1 << i;
pub struct BitConvert;

impl BitConvert {
/// Сonverts a set of bits to a set of bytes in direct order.
#[allow(clippy::wrong_self_convention)]
pub fn into_bytes(bits: Vec<bool>) -> Vec<u8> {
assert_eq!(bits.len() % 8, 0);
let mut message_bytes: Vec<u8> = vec![];

let byte_chunks = bits.chunks(8);
for byte_chunk in byte_chunks {
let mut byte = 0u8;
for (i, bit) in byte_chunk.iter().enumerate() {
if *bit {
byte |= 1 << i;
}
}
message_bytes.push(byte);
}
message_bytes.push(byte);

message_bytes
}

message_bytes
}
/// Сonverts a set of bits to a set of bytes in reverse order for each byte.
#[allow(clippy::wrong_self_convention)]
pub fn into_bytes_ordered(bits: Vec<bool>) -> Vec<u8> {
assert_eq!(bits.len() % 8, 0);
let mut message_bytes: Vec<u8> = vec![];

let byte_chunks = bits.chunks(8);
for byte_chunk in byte_chunks {
let mut byte = 0u8;
for (i, bit) in byte_chunk.iter().rev().enumerate() {
if *bit {
byte |= 1 << i;
}
}
message_bytes.push(byte);
}

pub fn pack_bits_into_bytes_in_order(bits: Vec<bool>) -> Vec<u8> {
assert_eq!(bits.len() % 8, 0);
let mut message_bytes: Vec<u8> = vec![];
message_bytes
}

let byte_chunks = bits.chunks(8);
for byte_chunk in byte_chunks {
let mut byte = 0u8;
for (i, bit) in byte_chunk.iter().rev().enumerate() {
if *bit {
byte |= 1 << i;
/// Сonverts a set of Big Endian bytes to a set of bits.
pub fn from_be_bytes(bytes: &[u8]) -> Vec<bool> {
let mut bits = vec![];
for byte in bytes {
let mut temp = *byte;
for _ in 0..8 {
bits.push(temp & 0x80 == 0x80);
temp <<= 1;
}
}
message_bytes.push(byte);
bits
}

message_bytes
}

/// Convert Uint to the floating-point and vice versa.
pub struct FloatConversions;

impl FloatConversions {
/// Packs a BigUint less than 2 ^ 128 to a floating-point number with an exponent base = 10.
/// Can lose accuracy with small parameters `exponent_len` and `mantissa_len`.
pub fn pack(number: &BigUint, exponent_len: usize, mantissa_len: usize) -> Vec<u8> {
let uint = number.to_u128().expect("Only u128 allowed");

let mut vec =
Self::convert_to_float(uint, exponent_len, mantissa_len, 10).expect("packing error");
let mut vec = Self::to_float(uint, exponent_len, mantissa_len, 10).expect("packing error");
vec.reverse();
pack_bits_into_bytes_in_order(vec)
BitConvert::into_bytes_ordered(vec)
}

/// Unpacks a floating point number with the given parameters.
/// Returns `None` for numbers greater than 2 ^ 128.
pub fn unpack(data: &[u8], exponent_len: usize, mantissa_len: usize) -> Option<u128> {
if exponent_len + mantissa_len != data.len() * 8 {
return None;
}

let bits = bytes_into_be_bits(data);
let bits = BitConvert::from_be_bytes(data);

let mut mantissa = 0u128;
for (i, bit) in bits[0..mantissa_len].iter().rev().enumerate() {
Expand All @@ -205,7 +230,10 @@ impl FloatConversions {
mantissa.checked_mul(exponent)
}

pub fn convert_to_float(
/// Packs a u128 to a floating-point number with the given parameters.
/// Can lose accuracy with small parameters `exponent_len` and `mantissa_len`.
#[allow(clippy::wrong_self_convention)]
pub fn to_float(
integer: u128,
exponent_length: usize,
mantissa_length: usize,
Expand Down Expand Up @@ -280,33 +308,22 @@ impl FloatConversions {
}
}

pub fn bytes_into_be_bits(bytes: &[u8]) -> Vec<bool> {
let mut bits = vec![];
for byte in bytes {
let mut temp = *byte;
for _ in 0..8 {
bits.push(temp & 0x80 == 0x80);
temp <<= 1;
}
}
bits
}

pub fn rescue_hash_tx_msg(msg: &[u8]) -> Vec<u8> {
let mut msg_bits = bytes_into_be_bits(msg);
let mut msg_bits = BitConvert::from_be_bytes(msg);
msg_bits.resize(params::PAD_MSG_BEFORE_HASH_BITS_LEN, false);
let hasher = &params::RESCUE_HASHER as &BabyRescueHasher;
let hash_fr = hasher.hash_bits(msg_bits.into_iter());
let mut hash_bits = Vec::new();
append_le_fixed_width(&mut hash_bits, &hash_fr, 256);
pack_bits_into_bytes(hash_bits)
BitConvert::into_bytes(hash_bits)
}

pub trait FromBytes: Sized {
/// Converts a sequence of bytes to a number.
fn from_bytes(bytes: &[u8]) -> Option<Self>;
}

macro_rules! impl_primitive {
macro_rules! impl_from_bytes_for_primitive {
($Type:ty) => {
impl FromBytes for $Type {
fn from_bytes(bytes: &[u8]) -> Option<Self> {
Expand All @@ -322,9 +339,9 @@ macro_rules! impl_primitive {
};
}

impl_primitive!(u16);
impl_primitive!(u32);
impl_primitive!(u128);
impl_from_bytes_for_primitive!(u16);
impl_from_bytes_for_primitive!(u32);
impl_from_bytes_for_primitive!(u128);

#[cfg(test)]
mod test {
Expand All @@ -351,13 +368,13 @@ mod test {
bits.extend(vec![false, false, true, true, false, true, true, false]);
bits.extend(vec![false, false, false, false, false, false, false, true]);

let bytes = pack_bits_into_bytes(bits.clone());
let bytes = BitConvert::into_bytes(bits.clone());
assert_eq!(bytes, vec![89, 108, 128]);

let bytes = pack_bits_into_bytes_in_order(bits.clone());
let bytes = BitConvert::into_bytes_ordered(bits.clone());
assert_eq!(bytes, vec![154, 54, 1]);

assert_eq!(bytes_into_be_bits(&[154, 54, 1]), bits);
assert_eq!(BitConvert::from_be_bytes(&[154, 54, 1]), bits);
}

#[test]
Expand All @@ -369,7 +386,7 @@ mod test {
FloatConversions::pack(&num::BigUint::from(number), exponent_len, mantissa_len);
let unpacked_number = FloatConversions::unpack(&packed_number, exponent_len, mantissa_len);
let convert_number =
FloatConversions::convert_to_float(number, exponent_len, mantissa_len, exponent_base);
FloatConversions::to_float(number, exponent_len, mantissa_len, exponent_base);

assert_eq!(unpacked_number, Some(number));
assert_eq!(packed_number, vec![27, 213, 183, 213, 224]);
Expand Down
10 changes: 4 additions & 6 deletions core/lib/types/benches/criterion/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
use criterion::{black_box, criterion_group, BatchSize, Bencher, Criterion, Throughput};
// Local uses
use zksync_crypto::circuit::account::CircuitAccount;
use zksync_crypto::primitives::{
bytes_into_be_bits, pack_bits_into_bytes, pack_bits_into_bytes_in_order, BitIteratorLe, GetBits,
};
use zksync_crypto::primitives::{BitConvert, BitIteratorLe, GetBits};
use zksync_types::{Account, Address, PubKeyHash};

/// Input size for byte slices (module-wide for calculating the throughput).
Expand All @@ -24,7 +22,7 @@ fn bench_bytes_into_be_bits(b: &mut Bencher<'_>) {
let value_ref: &[u8] = value.as_ref();

b.iter(|| {
let _ = bytes_into_be_bits(black_box(value_ref));
let _ = BitConvert::from_be_bytes(black_box(value_ref));
});
}

Expand All @@ -36,7 +34,7 @@ fn bench_pack_bits_into_bytes(b: &mut Bencher<'_>) {
b.iter_batched(
setup,
|value| {
let _ = pack_bits_into_bytes(black_box(value));
let _ = BitConvert::into_bytes(black_box(value));
},
BatchSize::SmallInput,
);
Expand All @@ -50,7 +48,7 @@ fn bench_pack_bits_into_bytes_in_order(b: &mut Bencher<'_>) {
b.iter_batched(
setup,
|value| {
let _ = pack_bits_into_bytes_in_order(black_box(value));
let _ = BitConvert::into_bytes_ordered(black_box(value));
},
BatchSize::SmallInput,
);
Expand Down

0 comments on commit 12a9698

Please sign in to comment.