use super::*;
impl<N: Network> ToBits for ProgramID<N> {
fn to_bits_le(&self) -> Vec<bool> {
(&self).to_bits_le()
}
fn to_bits_be(&self) -> Vec<bool> {
(&self).to_bits_be()
}
}
impl<N: Network> ToBits for &ProgramID<N> {
fn to_bits_le(&self) -> Vec<bool> {
let mut bits = self.name().to_bits_le();
bits.extend(self.network().to_bits_le());
bits
}
fn to_bits_be(&self) -> Vec<bool> {
let mut bits = self.name().to_bits_be();
bits.extend(self.network().to_bits_be());
bits
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::data::identifier::tests::sample_lowercase_identifier_as_string;
use snarkvm_console_network::Testnet3;
type CurrentNetwork = Testnet3;
const ITERATIONS: usize = 100;
#[test]
fn test_to_bits_le() -> Result<()> {
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let expected_name_string = sample_lowercase_identifier_as_string::<CurrentNetwork>(&mut rng)?;
let expected_name_field = Field::<CurrentNetwork>::from_bits_le(&expected_name_string.to_bits_le())?;
let expected_network_field = Field::<CurrentNetwork>::from_bits_le(&"aleo".to_string().to_bits_le())?;
let mut expected_bits = expected_name_field.to_bits_le()[..expected_name_string.len() * 8].to_vec();
expected_bits.extend(&expected_network_field.to_bits_le()[..4 * 8]);
let candidate = ProgramID::<CurrentNetwork>::from_str(&format!("{expected_name_string}.aleo"))?;
assert_eq!(
expected_name_field.to_bits_le()[..expected_name_string.len() * 8],
candidate.name().to_bits_le()
);
assert_eq!(expected_network_field.to_bits_le()[..4 * 8], candidate.network().to_bits_le());
assert_eq!(expected_bits, candidate.to_bits_le());
}
Ok(())
}
#[test]
fn test_to_bits_be() -> Result<()> {
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let expected_name_string = sample_lowercase_identifier_as_string::<CurrentNetwork>(&mut rng)?;
let expected_name_field = Field::<CurrentNetwork>::from_bits_le(&expected_name_string.to_bits_le())?;
let expected_network_field = Field::<CurrentNetwork>::from_bits_le(&"aleo".to_string().to_bits_le())?;
let mut expected_bits = expected_name_field.to_bits_le()[..expected_name_string.len() * 8]
.iter()
.rev()
.copied()
.collect::<Vec<_>>();
expected_bits.extend(expected_network_field.to_bits_le()[..4 * 8].iter().rev().copied());
let candidate = ProgramID::<CurrentNetwork>::from_str(&format!("{expected_name_string}.aleo"))?;
assert_eq!(
expected_name_field.to_bits_le()[..expected_name_string.len() * 8]
.iter()
.rev()
.copied()
.collect::<Vec<_>>(),
candidate.name().to_bits_be()
);
assert_eq!(
expected_network_field.to_bits_le()[..4 * 8].iter().rev().copied().collect::<Vec<_>>(),
candidate.network().to_bits_be()
);
assert_eq!(expected_bits, candidate.to_bits_be());
}
Ok(())
}
}