/ console / types / integers / src / to_bits.rs
to_bits.rs
  1  // Copyright (c) 2025-2026 ACDC Network
  2  // This file is part of the alphavm library.
  3  //
  4  // Alpha Chain | Delta Chain Protocol
  5  // International Monetary Graphite.
  6  //
  7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
  8  // They built world-class ZK infrastructure. We installed the EASY button.
  9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
 10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
 11  //
 12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
 13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
 14  // No rights reserved. No permission required. No warranty. No refunds.
 15  //
 16  // https://creativecommons.org/publicdomain/zero/1.0/
 17  // SPDX-License-Identifier: CC0-1.0
 18  
 19  use super::*;
 20  
 21  impl<E: Environment, I: IntegerType> ToBits for Integer<E, I> {
 22      /// Outputs the little-endian bit representation of `self` *without* trailing zeros.
 23      fn write_bits_le(&self, vec: &mut Vec<bool>) {
 24          (**self).write_bits_le(vec);
 25      }
 26  
 27      /// Outputs the big-endian bit representation of `self` *without* leading zeros.
 28      fn write_bits_be(&self, vec: &mut Vec<bool>) {
 29          (**self).write_bits_be(vec);
 30      }
 31  }
 32  
 33  #[cfg(test)]
 34  mod tests {
 35      use super::*;
 36      use alphavm_console_network_environment::Console;
 37  
 38      type CurrentEnvironment = Console;
 39  
 40      const ITERATIONS: u64 = 10_000;
 41  
 42      fn check_to_bits_le<I: IntegerType>(rng: &mut TestRng) {
 43          for _ in 0..ITERATIONS {
 44              // Sample a random value.
 45              let integer: Integer<CurrentEnvironment, I> = Uniform::rand(rng);
 46  
 47              let candidate = integer.to_bits_le();
 48              assert_eq!(Integer::<CurrentEnvironment, I>::size_in_bits(), candidate.len());
 49  
 50              for (expected, candidate) in (*integer).to_bits_le().iter().zip_eq(&candidate) {
 51                  assert_eq!(expected, candidate);
 52              }
 53          }
 54      }
 55  
 56      fn check_to_bits_be<I: IntegerType>(rng: &mut TestRng) {
 57          for _ in 0..ITERATIONS {
 58              // Sample a random value.
 59              let integer: Integer<CurrentEnvironment, I> = Uniform::rand(rng);
 60  
 61              let candidate = integer.to_bits_be();
 62              assert_eq!(Integer::<CurrentEnvironment, I>::size_in_bits(), candidate.len());
 63  
 64              for (expected, candidate) in (*integer).to_bits_be().iter().zip_eq(&candidate) {
 65                  assert_eq!(expected, candidate);
 66              }
 67          }
 68      }
 69  
 70      #[test]
 71      fn test_to_bits_le() {
 72          let mut rng = TestRng::default();
 73  
 74          check_to_bits_le::<u8>(&mut rng);
 75          check_to_bits_le::<u16>(&mut rng);
 76          check_to_bits_le::<u32>(&mut rng);
 77          check_to_bits_le::<u64>(&mut rng);
 78          check_to_bits_le::<u128>(&mut rng);
 79  
 80          check_to_bits_le::<i8>(&mut rng);
 81          check_to_bits_le::<i16>(&mut rng);
 82          check_to_bits_le::<i32>(&mut rng);
 83          check_to_bits_le::<i64>(&mut rng);
 84          check_to_bits_le::<i128>(&mut rng);
 85      }
 86  
 87      #[test]
 88      fn test_to_bits_be() {
 89          let mut rng = TestRng::default();
 90  
 91          check_to_bits_be::<u8>(&mut rng);
 92          check_to_bits_be::<u16>(&mut rng);
 93          check_to_bits_be::<u32>(&mut rng);
 94          check_to_bits_be::<u64>(&mut rng);
 95          check_to_bits_be::<u128>(&mut rng);
 96  
 97          check_to_bits_be::<i8>(&mut rng);
 98          check_to_bits_be::<i16>(&mut rng);
 99          check_to_bits_be::<i32>(&mut rng);
100          check_to_bits_be::<i64>(&mut rng);
101          check_to_bits_be::<i128>(&mut rng);
102      }
103  }