snarkvm_circuit_program/data/identifier/
from_bits.rsuse super::*;
impl<A: Aleo> FromBits for Identifier<A> {
type Boolean = Boolean<A>;
fn from_bits_le(bits_le: &[Self::Boolean]) -> Self {
debug_assert!(bits_le.len() <= A::BaseField::size_in_bits(), "Identifier exceeds the maximum bits allowed");
let field = Field::from_bits_le(bits_le);
let num_bytes = match console::Identifier::<A::Network>::from_bits_le(&bits_le.eject_value()) {
Ok(console_identifier) => console_identifier.size_in_bits() / 8,
Err(error) => A::halt(format!("Failed to recover an identifier from bits: {error}")),
};
let max_bytes = A::BaseField::size_in_data_bits() / 8; match num_bytes as usize <= max_bytes {
true => Self(field, num_bytes),
false => A::halt("Identifier exceeds the maximum capacity allowed"),
}
}
fn from_bits_be(bits_be: &[Self::Boolean]) -> Self {
Self::from_bits_le(bits_be.iter().rev().cloned().collect::<Vec<_>>().as_slice())
}
}
#[cfg(all(test, feature = "console"))]
mod tests {
use super::*;
use crate::{Circuit, data::identifier::tests::sample_console_identifier};
use anyhow::Result;
const ITERATIONS: u64 = 100;
fn check_from_bits_le(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) -> Result<()> {
for _ in 0..ITERATIONS {
let console_identifier = sample_console_identifier::<Circuit>()?;
let circuit_bits: Vec<_> = Inject::constant(console_identifier.to_bits_le());
Circuit::scope("Identifier FromBits", || {
let candidate = Identifier::<Circuit>::from_bits_le(&circuit_bits);
assert_eq!(Mode::Constant, candidate.eject_mode());
assert_eq!(console_identifier, candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::reset();
}
Ok(())
}
fn check_from_bits_be(num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) -> Result<()> {
for _ in 0..ITERATIONS {
let console_identifier = sample_console_identifier::<Circuit>()?;
let circuit_bits: Vec<_> = Inject::constant(console_identifier.to_bits_be());
Circuit::scope("Identifier FromBits", || {
let candidate = Identifier::<Circuit>::from_bits_be(&circuit_bits);
assert_eq!(Mode::Constant, candidate.eject_mode());
assert_eq!(console_identifier, candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::reset();
}
Ok(())
}
#[test]
fn test_from_bits_le() -> Result<()> {
check_from_bits_le(0, 0, 0, 0)
}
#[test]
fn test_from_bits_be() -> Result<()> {
check_from_bits_be(0, 0, 0, 0)
}
}