use super::*;
impl<E: Environment> FromBits for Group<E> {
type Boolean = Boolean<E>;
fn from_bits_le(bits_le: &[Self::Boolean]) -> Self {
let x = Field::from_bits_le(bits_le);
Self::from_x_coordinate(x)
}
fn from_bits_be(bits_be: &[Self::Boolean]) -> Self {
let x = Field::from_bits_be(bits_be);
Self::from_x_coordinate(x)
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
const ITERATIONS: u64 = 100;
fn check_from_bits_le(mode: Mode, num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) {
let mut rng = TestRng::default();
for i in 0..ITERATIONS {
let expected = Uniform::rand(&mut rng);
let candidate = Group::<Circuit>::new(mode, expected).to_bits_le();
Circuit::scope(&format!("{mode} {i}"), || {
let candidate = Group::<Circuit>::from_bits_le(&candidate);
assert_eq!(expected, candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::reset();
}
}
fn check_from_bits_be(mode: Mode, num_constants: u64, num_public: u64, num_private: u64, num_constraints: u64) {
let mut rng = TestRng::default();
for i in 0..ITERATIONS {
let expected = Uniform::rand(&mut rng);
let candidate = Group::<Circuit>::new(mode, expected).to_bits_be();
Circuit::scope(&format!("{mode} {i}"), || {
let candidate = Group::<Circuit>::from_bits_be(&candidate);
assert_eq!(expected, candidate.eject_value());
assert_scope!(num_constants, num_public, num_private, num_constraints);
});
Circuit::reset();
}
}
#[test]
fn test_from_bits_le_constant() {
check_from_bits_le(Mode::Constant, 11, 0, 0, 0);
}
#[test]
fn test_from_bits_le_public() {
check_from_bits_le(Mode::Public, 4, 0, 267, 266);
}
#[test]
fn test_from_bits_le_private() {
check_from_bits_le(Mode::Private, 4, 0, 267, 266);
}
#[test]
fn test_from_bits_be_constant() {
check_from_bits_be(Mode::Constant, 11, 0, 0, 0);
}
#[test]
fn test_from_bits_be_public() {
check_from_bits_be(Mode::Public, 4, 0, 267, 266);
}
#[test]
fn test_from_bits_be_private() {
check_from_bits_be(Mode::Private, 4, 0, 267, 266);
}
}