snarkvm_console_account/compute_key/
from_bits.rsuse super::*;
impl<N: Network> FromBits for ComputeKey<N> {
fn from_bits_le(bits_le: &[bool]) -> Result<Self> {
let group_size_in_bits = Group::<N>::size_in_bits();
let (pk_sig_start, pk_sig_end) = (0, group_size_in_bits);
let (pr_sig_start, pr_sig_end) = (pk_sig_end, pk_sig_end + group_size_in_bits);
let Some(pk_sig_bits) = bits_le.get(pk_sig_start..pk_sig_end) else {
bail!("Unable to recover the 'pk_sig' (LE) bits for the compute key");
};
let Some(pr_sig_bits) = bits_le.get(pr_sig_start..pr_sig_end) else {
bail!("Unable to recover the 'pr_sig' (LE) bits for the compute key");
};
Self::try_from((Group::from_bits_le(pk_sig_bits)?, Group::from_bits_le(pr_sig_bits)?))
}
fn from_bits_be(bits_be: &[bool]) -> Result<Self> {
let group_size_in_bits = Group::<N>::size_in_bits();
let (pk_sig_start, pk_sig_end) = (0, group_size_in_bits);
let (pr_sig_start, pr_sig_end) = (pk_sig_end, pk_sig_end + group_size_in_bits);
let Some(pk_sig_bits) = bits_be.get(pk_sig_start..pk_sig_end) else {
bail!("Unable to recover the 'pk_sig' (BE) bits for the compute key");
};
let Some(pr_sig_bits) = bits_be.get(pr_sig_start..pr_sig_end) else {
bail!("Unable to recover the 'pr_sig' (BE) bits for the compute key");
};
Self::try_from((Group::from_bits_be(pk_sig_bits)?, Group::from_bits_be(pr_sig_bits)?))
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::MainnetV0;
type CurrentNetwork = MainnetV0;
const ITERATIONS: usize = 100;
fn check_from_bits_le() -> Result<()> {
let rng = &mut TestRng::default();
for i in 0..ITERATIONS {
let expected = ComputeKey::<CurrentNetwork>::try_from(PrivateKey::new(rng).unwrap()).unwrap();
let given_bits = expected.to_bits_le();
assert_eq!(ComputeKey::<CurrentNetwork>::size_in_bits(), given_bits.len());
let candidate = ComputeKey::<CurrentNetwork>::from_bits_le(&given_bits)?;
assert_eq!(expected, candidate);
let candidate = [given_bits, vec![false; i]].concat();
let candidate = ComputeKey::<CurrentNetwork>::from_bits_le(&candidate)?;
assert_eq!(expected, candidate);
assert_eq!(ComputeKey::<CurrentNetwork>::size_in_bits(), candidate.to_bits_le().len());
}
Ok(())
}
fn check_from_bits_be() -> Result<()> {
let rng = &mut TestRng::default();
for i in 0..ITERATIONS {
let expected = ComputeKey::<CurrentNetwork>::try_from(PrivateKey::new(rng).unwrap()).unwrap();
let given_bits = expected.to_bits_be();
assert_eq!(ComputeKey::<CurrentNetwork>::size_in_bits(), given_bits.len());
let candidate = ComputeKey::<CurrentNetwork>::from_bits_be(&given_bits)?;
assert_eq!(expected, candidate);
let candidate = [given_bits, vec![false; i]].concat();
let candidate = ComputeKey::<CurrentNetwork>::from_bits_be(&candidate)?;
assert_eq!(expected, candidate);
assert_eq!(ComputeKey::<CurrentNetwork>::size_in_bits(), candidate.to_bits_be().len());
}
Ok(())
}
#[test]
fn test_from_bits_le() -> Result<()> {
check_from_bits_le()
}
#[test]
fn test_from_bits_be() -> Result<()> {
check_from_bits_be()
}
}