snarkvm_utilities/bits.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
// Copyright 2024 Aleo Network Foundation
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::Vec;
use anyhow::{Result, ensure};
/// Takes as input a sequence of objects, and converts them to a series of little-endian bits.
/// All traits that implement `ToBits` can be automatically converted to bits in this manner.
#[macro_export]
macro_rules! to_bits_le {
($($x:expr),*) => ({
let mut buffer = $crate::vec![];
$($x.write_bits_le(&mut buffer);)*
buffer
});
($($x:expr),*; $size:expr) => ({
let mut buffer = $crate::Vec::with_capacity($size);
$($x.write_bits_le(&mut buffer);)*
buffer
});
}
pub trait ToBits: Sized {
/// Writes `self` into the given vector as a boolean array in little-endian order.
fn write_bits_le(&self, vec: &mut Vec<bool>);
/// Writes `self` into the given vector as a boolean array in big-endian order.
fn write_bits_be(&self, vec: &mut Vec<bool>);
/// Returns `self` as a boolean array in little-endian order.
fn to_bits_le(&self) -> Vec<bool> {
let mut bits = Vec::new();
self.write_bits_le(&mut bits);
bits
}
/// Returns `self` as a boolean array in big-endian order.
fn to_bits_be(&self) -> Vec<bool> {
let mut bits = Vec::new();
self.write_bits_be(&mut bits);
bits
}
/// An optional indication of how many bits an object can be represented with.
fn num_bits() -> Option<usize> {
None
}
}
pub trait FromBits: Sized {
/// Reads `Self` from a boolean array in little-endian order.
fn from_bits_le(bits: &[bool]) -> Result<Self>;
/// Reads `Self` from a boolean array in big-endian order.
fn from_bits_be(bits: &[bool]) -> Result<Self>;
}
/********************/
/****** Tuples ******/
/********************/
/// A helper macro to implement `ToBits` for a tuple of `ToBits` circuits.
macro_rules! to_bits_tuple {
(($t0:ident, $i0:tt), $(($ty:ident, $idx:tt)),+) => {
impl<$t0: ToBits, $($ty: ToBits),+> ToBits for ($t0, $($ty),+) {
/// A helper method to return a concatenated list of little-endian bits from the circuits.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
// The tuple is order-preserving, meaning the first circuit in is the first circuit bits out.
(&self).write_bits_le(vec);
}
/// A helper method to return a concatenated list of big-endian bits from the circuits.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
// The tuple is order-preserving, meaning the first circuit in is the first circuit bits out.
(&self).write_bits_be(vec);
}
}
impl<'a, $t0: ToBits, $($ty: ToBits),+> ToBits for &'a ($t0, $($ty),+) {
/// A helper method to return a concatenated list of little-endian bits from the circuits.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
// The tuple is order-preserving, meaning the first circuit in is the first circuit bits out.
self.$i0.write_bits_le(vec);
$(self.$idx.write_bits_le(vec);)+
}
/// A helper method to return a concatenated list of big-endian bits from the circuits.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
// The tuple is order-preserving, meaning the first circuit in is the first circuit bits out.
self.$i0.write_bits_be(vec);
$(self.$idx.write_bits_be(vec);)+
}
}
}
}
to_bits_tuple!((C0, 0), (C1, 1));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3), (C4, 4));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3), (C4, 4), (C5, 5));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3), (C4, 4), (C5, 5), (C6, 6));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3), (C4, 4), (C5, 5), (C6, 6), (C7, 7));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3), (C4, 4), (C5, 5), (C6, 6), (C7, 7), (C8, 8));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3), (C4, 4), (C5, 5), (C6, 6), (C7, 7), (C8, 8), (C9, 9));
to_bits_tuple!((C0, 0), (C1, 1), (C2, 2), (C3, 3), (C4, 4), (C5, 5), (C6, 6), (C7, 7), (C8, 8), (C9, 9), (C10, 10));
/********************/
/****** Boolean *****/
/********************/
impl ToBits for bool {
/// A helper method to return a concatenated list of little-endian bits.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
vec.push(*self);
}
/// A helper method to return a concatenated list of big-endian bits.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
vec.push(*self);
}
}
/********************/
/***** Integers *****/
/********************/
macro_rules! impl_bits_for_integer {
($int:ty) => {
impl ToBits for $int {
/// Returns `self` as a boolean array in little-endian order.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
let mut value = *self;
for _ in 0..<$int>::BITS {
vec.push(value & 1 == 1);
value = value.wrapping_shr(1u32);
}
}
/// Returns `self` as a boolean array in big-endian order.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
let reversed = self.reverse_bits();
reversed.write_bits_le(vec);
}
fn num_bits() -> Option<usize> {
Some(<$int>::BITS as usize)
}
}
impl FromBits for $int {
/// Reads `Self` from a boolean array in little-endian order.
#[inline]
fn from_bits_le(bits: &[bool]) -> Result<Self> {
// If the number of bits exceeds the size of the integer, ensure that the upper bits are all zero.
// Note that because the input bits are little-endian, these are the bits at the end of slice.
for bit in bits.iter().skip(<$int>::BITS as usize) {
ensure!(!bit, "upper bits are not zero");
}
// Construct the integer from the bits.
Ok(bits.iter().take(<$int>::BITS as usize).rev().fold(0, |value, bit| match bit {
true => (value.wrapping_shl(1)) ^ 1,
false => (value.wrapping_shl(1)) ^ 0,
}))
}
/// Reads `Self` from a boolean array in big-endian order.
#[inline]
fn from_bits_be(bits: &[bool]) -> Result<Self> {
// If the number of bits exceeds the size of the integer, ensure that the upper bits are all zero.
// Note that because the input bits are big-endian, these are the bits at the beginning of slice.
for bit in bits.iter().take(bits.len().saturating_sub(<$int>::BITS as usize)) {
ensure!(!bit, "upper bits are not zero");
}
// Construct the integer from the bits.
Ok(bits.iter().skip(bits.len().saturating_sub(<$int>::BITS as usize)).fold(0, |value, bit| match bit {
true => (value.wrapping_shl(1)) ^ 1,
false => (value.wrapping_shl(1)) ^ 0,
}))
}
}
};
}
impl_bits_for_integer!(u8);
impl_bits_for_integer!(u16);
impl_bits_for_integer!(u32);
impl_bits_for_integer!(u64);
impl_bits_for_integer!(u128);
impl_bits_for_integer!(i8);
impl_bits_for_integer!(i16);
impl_bits_for_integer!(i32);
impl_bits_for_integer!(i64);
impl_bits_for_integer!(i128);
/********************/
/****** String ******/
/********************/
impl ToBits for String {
/// A helper method to return a concatenated list of little-endian bits.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
// The vector is order-preserving, meaning the first byte in is the first byte bits out.
self.as_bytes().write_bits_le(vec);
}
/// A helper method to return a concatenated list of big-endian bits.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
// The vector is order-preserving, meaning the first byte in is the first byte bits out.
self.as_bytes().write_bits_be(vec);
}
}
/********************/
/****** Arrays ******/
/********************/
impl<C: ToBits> ToBits for Vec<C> {
/// A helper method to return a concatenated list of little-endian bits.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
// The vector is order-preserving, meaning the first variable in is the first variable bits out.
self.as_slice().write_bits_le(vec);
}
/// A helper method to return a concatenated list of big-endian bits.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
// The vector is order-preserving, meaning the first variable in is the first variable bits out.
self.as_slice().write_bits_be(vec);
}
}
impl<C: ToBits, const N: usize> ToBits for [C; N] {
/// A helper method to return a concatenated list of little-endian bits.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
// The slice is order-preserving, meaning the first variable in is the first variable bits out.
self.as_slice().write_bits_le(vec)
}
/// A helper method to return a concatenated list of big-endian bits.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
// The slice is order-preserving, meaning the first variable in is the first variable bits out.
self.as_slice().write_bits_be(vec)
}
}
impl<C: ToBits> ToBits for &[C] {
/// A helper method to return a concatenated list of little-endian bits.
#[inline]
fn write_bits_le(&self, vec: &mut Vec<bool>) {
if let Some(num_bits) = C::num_bits() {
vec.reserve(num_bits * self.len());
}
for elem in self.iter() {
elem.write_bits_le(vec);
}
}
/// A helper method to return a concatenated list of big-endian bits.
#[inline]
fn write_bits_be(&self, vec: &mut Vec<bool>) {
if let Some(num_bits) = C::num_bits() {
vec.reserve(num_bits * self.len());
}
for elem in self.iter() {
elem.write_bits_be(vec);
}
}
}
impl FromBits for Vec<u8> {
/// A helper method to return `Self` from a concatenated list of little-endian bits.
#[inline]
fn from_bits_le(bits: &[bool]) -> Result<Self> {
// The vector is order-preserving, meaning the first variable in is the first variable bits out.
bits.chunks(8).map(u8::from_bits_le).collect::<Result<Vec<_>>>()
}
/// A helper method to return `Self` from a concatenated list of big-endian bits.
#[inline]
fn from_bits_be(bits: &[bool]) -> Result<Self> {
// The vector is order-preserving, meaning the first variable in is the first variable bits out.
bits.chunks(8).map(u8::from_bits_be).collect::<Result<Vec<_>>>()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{TestRng, Uniform};
use anyhow::Result;
use rand::{Rng, distributions::Alphanumeric};
const ITERATIONS: u64 = 10000;
fn random_string(len: u16, rng: &mut TestRng) -> String {
rng.sample_iter(&Alphanumeric).take(len as usize).map(char::from).collect()
}
#[test]
fn test_to_bits_le_macro() {
let rng = &mut TestRng::default();
// The checker.
macro_rules! check {
($given:expr) => {{
let given = $given;
let mut expected = Vec::new();
given.iter().for_each(|elem| elem.write_bits_le(&mut expected));
let candidate = to_bits_le!(given);
assert_eq!(candidate, expected);
}};
}
// U8
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<u8>>());
// U16
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<u16>>());
// U32
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<u32>>());
// U64
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<u64>>());
// U128
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<u128>>());
// I8
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<i8>>());
// I16
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<i16>>());
// I32
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<i32>>());
// I64
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<i64>>());
// I128
check!((0..100).map(|_| Uniform::rand(rng)).collect::<Vec<i128>>());
// String
check!((0..100).map(|_| random_string(rng.gen(), rng)).collect::<Vec<String>>());
// Vec<Vec<u8>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<u8>>()).collect::<Vec<_>>());
// Vec<Vec<u16>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<u16>>()).collect::<Vec<_>>());
// Vec<Vec<u32>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<u32>>()).collect::<Vec<_>>());
// Vec<Vec<u64>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<u64>>()).collect::<Vec<_>>());
// Vec<Vec<u128>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<u128>>()).collect::<Vec<_>>());
// Vec<Vec<i8>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<i8>>()).collect::<Vec<_>>());
// Vec<Vec<i16>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<i16>>()).collect::<Vec<_>>());
// Vec<Vec<i32>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<i32>>()).collect::<Vec<_>>());
// Vec<Vec<i64>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<i64>>()).collect::<Vec<_>>());
// Vec<Vec<i128>>
check!((0..100).map(|_| (0..128).map(|_| Uniform::rand(rng)).collect::<Vec<i128>>()).collect::<Vec<_>>());
// Vec<Vec<String>>
check!(
(0..100)
.map(|_| (0..128).map(|_| random_string(rng.gen(), rng)).collect::<Vec<String>>())
.collect::<Vec<_>>()
);
}
#[test]
fn test_to_bits_le_macro_with_capacity() {
let mut expected = Vec::new();
1u8.write_bits_le(&mut expected);
2u16.write_bits_le(&mut expected);
3u32.write_bits_le(&mut expected);
4u64.write_bits_le(&mut expected);
5u128.write_bits_le(&mut expected);
6i8.write_bits_le(&mut expected);
7i16.write_bits_le(&mut expected);
8i32.write_bits_le(&mut expected);
9i64.write_bits_le(&mut expected);
10i128.write_bits_le(&mut expected);
let capacity = expected.len();
let candidate = to_bits_le![1u8, 2u16, 3u32, 4u64, 5u128, 6i8, 7i16, 8i32, 9i64, 10i128; capacity];
assert_eq!(candidate, expected);
}
#[test]
fn test_integers() -> Result<()> {
macro_rules! check_integer {
($integer:tt, $rng:expr) => {{
for _ in 0..ITERATIONS {
let expected: $integer = Uniform::rand($rng);
let bits_le = expected.to_bits_le();
assert_eq!(expected, $integer::from_bits_le(&bits_le)?);
let bits_be = expected.to_bits_be();
assert_eq!(expected, $integer::from_bits_be(&bits_be)?);
}
}};
}
let mut rng = TestRng::default();
check_integer!(u8, &mut rng);
check_integer!(u16, &mut rng);
check_integer!(u32, &mut rng);
check_integer!(u64, &mut rng);
check_integer!(u128, &mut rng);
check_integer!(i8, &mut rng);
check_integer!(i16, &mut rng);
check_integer!(i32, &mut rng);
check_integer!(i64, &mut rng);
check_integer!(i128, &mut rng);
Ok(())
}
}