use super::*;
impl<E: Environment> Cast<Address<E>> for Field<E> {
#[inline]
fn cast(&self) -> Address<E> {
Address::from_field(self.clone())
}
}
impl<E: Environment> Cast<Boolean<E>> for Field<E> {
#[inline]
fn cast(&self) -> Boolean<E> {
let is_one = self.is_one();
E::assert(self.is_zero().bitor(&is_one));
is_one
}
}
impl<E: Environment> Cast<Field<E>> for Field<E> {
#[inline]
fn cast(&self) -> Field<E> {
self.clone()
}
}
impl<E: Environment> Cast<Group<E>> for Field<E> {
#[inline]
fn cast(&self) -> Group<E> {
Group::from_x_coordinate(self.clone())
}
}
impl<E: Environment, I: IntegerType> Cast<Integer<E, I>> for Field<E> {
#[inline]
fn cast(&self) -> Integer<E, I> {
Integer::from_field(self.clone())
}
}
impl<E: Environment> Cast<Scalar<E>> for Field<E> {
#[inline]
fn cast(&self) -> Scalar<E> {
Scalar::from_field(self.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::Cast as _;
use console_root::{
network::Testnet3,
prelude::{One, TestRng, Uniform, Zero},
};
use snarkvm_circuit_types::environment::{count_is, count_less_than, Circuit, Eject, Inject, Mode, UpdatableCount};
use std::fmt::Debug;
const ITERATIONS: usize = 100;
fn sample_values(
i: usize,
mode: Mode,
rng: &mut TestRng,
) -> (console_root::types::Field<Testnet3>, Field<Circuit>) {
let console_value = match i {
0 => console_root::types::Field::<Testnet3>::zero(),
1 => console_root::types::Field::<Testnet3>::one(),
_ => Uniform::rand(rng),
};
let circuit_value = Field::<Circuit>::new(mode, console_value);
(console_value, circuit_value)
}
impl_check_cast!(cast, Field<Circuit>, console_root::types::Field::<Testnet3>);
#[test]
fn test_field_to_address() {
check_cast::<Address<Circuit>, console_root::types::Address<Testnet3>>(
Mode::Constant,
count_less_than!(11, 0, 0, 0),
);
check_cast::<Address<Circuit>, console_root::types::Address<Testnet3>>(Mode::Public, count_is!(4, 0, 13, 13));
check_cast::<Address<Circuit>, console_root::types::Address<Testnet3>>(Mode::Private, count_is!(4, 0, 13, 13));
}
#[test]
fn test_field_to_boolean() {
check_cast::<Boolean<Circuit>, console_root::types::Boolean<Testnet3>>(Mode::Constant, count_is!(2, 0, 0, 0));
check_cast::<Boolean<Circuit>, console_root::types::Boolean<Testnet3>>(Mode::Public, count_is!(0, 0, 5, 6));
check_cast::<Boolean<Circuit>, console_root::types::Boolean<Testnet3>>(Mode::Private, count_is!(0, 0, 5, 6));
}
#[test]
fn test_field_to_field() {
check_cast::<Field<Circuit>, console_root::types::Field<Testnet3>>(Mode::Constant, count_is!(0, 0, 0, 0));
check_cast::<Field<Circuit>, console_root::types::Field<Testnet3>>(Mode::Public, count_is!(0, 0, 0, 0));
check_cast::<Field<Circuit>, console_root::types::Field<Testnet3>>(Mode::Private, count_is!(0, 0, 0, 0));
}
#[test]
fn test_field_to_group() {
check_cast::<Group<Circuit>, console_root::types::Group<Testnet3>>(
Mode::Constant,
count_less_than!(11, 0, 0, 0),
);
check_cast::<Group<Circuit>, console_root::types::Group<Testnet3>>(Mode::Public, count_is!(4, 0, 13, 13));
check_cast::<Group<Circuit>, console_root::types::Group<Testnet3>>(Mode::Private, count_is!(4, 0, 13, 13));
}
#[test]
fn test_field_to_i8() {
check_cast::<I8<Circuit>, console_root::types::I8<Testnet3>>(Mode::Constant, count_is!(8, 0, 0, 0));
check_cast::<I8<Circuit>, console_root::types::I8<Testnet3>>(Mode::Public, count_is!(0, 0, 8, 9));
check_cast::<I8<Circuit>, console_root::types::I8<Testnet3>>(Mode::Private, count_is!(0, 0, 8, 9));
}
#[test]
fn test_field_to_i16() {
check_cast::<I16<Circuit>, console_root::types::I16<Testnet3>>(Mode::Constant, count_is!(16, 0, 0, 0));
check_cast::<I16<Circuit>, console_root::types::I16<Testnet3>>(Mode::Public, count_is!(0, 0, 16, 17));
check_cast::<I16<Circuit>, console_root::types::I16<Testnet3>>(Mode::Private, count_is!(0, 0, 16, 17));
}
#[test]
fn test_field_to_i32() {
check_cast::<I32<Circuit>, console_root::types::I32<Testnet3>>(Mode::Constant, count_is!(32, 0, 0, 0));
check_cast::<I32<Circuit>, console_root::types::I32<Testnet3>>(Mode::Public, count_is!(0, 0, 32, 33));
check_cast::<I32<Circuit>, console_root::types::I32<Testnet3>>(Mode::Private, count_is!(0, 0, 32, 33));
}
#[test]
fn test_field_to_i64() {
check_cast::<I64<Circuit>, console_root::types::I64<Testnet3>>(Mode::Constant, count_is!(64, 0, 0, 0));
check_cast::<I64<Circuit>, console_root::types::I64<Testnet3>>(Mode::Public, count_is!(0, 0, 64, 65));
check_cast::<I64<Circuit>, console_root::types::I64<Testnet3>>(Mode::Private, count_is!(0, 0, 64, 65));
}
#[test]
fn test_field_to_i128() {
check_cast::<I128<Circuit>, console_root::types::I128<Testnet3>>(Mode::Constant, count_is!(128, 0, 0, 0));
check_cast::<I128<Circuit>, console_root::types::I128<Testnet3>>(Mode::Public, count_is!(0, 0, 128, 129));
check_cast::<I128<Circuit>, console_root::types::I128<Testnet3>>(Mode::Private, count_is!(0, 0, 128, 129));
}
#[test]
fn test_field_to_scalar() {
check_cast::<Scalar<Circuit>, console_root::types::Scalar<Testnet3>>(Mode::Constant, count_is!(253, 0, 0, 0));
check_cast::<Scalar<Circuit>, console_root::types::Scalar<Testnet3>>(Mode::Public, count_is!(0, 0, 755, 759));
check_cast::<Scalar<Circuit>, console_root::types::Scalar<Testnet3>>(Mode::Private, count_is!(0, 0, 755, 759));
}
#[test]
fn test_field_to_u8() {
check_cast::<U8<Circuit>, console_root::types::U8<Testnet3>>(Mode::Constant, count_is!(8, 0, 0, 0));
check_cast::<U8<Circuit>, console_root::types::U8<Testnet3>>(Mode::Public, count_is!(0, 0, 8, 9));
check_cast::<U8<Circuit>, console_root::types::U8<Testnet3>>(Mode::Private, count_is!(0, 0, 8, 9));
}
#[test]
fn test_field_to_u16() {
check_cast::<U16<Circuit>, console_root::types::U16<Testnet3>>(Mode::Constant, count_is!(16, 0, 0, 0));
check_cast::<U16<Circuit>, console_root::types::U16<Testnet3>>(Mode::Public, count_is!(0, 0, 16, 17));
check_cast::<U16<Circuit>, console_root::types::U16<Testnet3>>(Mode::Private, count_is!(0, 0, 16, 17));
}
#[test]
fn test_field_to_u32() {
check_cast::<U32<Circuit>, console_root::types::U32<Testnet3>>(Mode::Constant, count_is!(32, 0, 0, 0));
check_cast::<U32<Circuit>, console_root::types::U32<Testnet3>>(Mode::Public, count_is!(0, 0, 32, 33));
check_cast::<U32<Circuit>, console_root::types::U32<Testnet3>>(Mode::Private, count_is!(0, 0, 32, 33));
}
#[test]
fn test_field_to_u64() {
check_cast::<U64<Circuit>, console_root::types::U64<Testnet3>>(Mode::Constant, count_is!(64, 0, 0, 0));
check_cast::<U64<Circuit>, console_root::types::U64<Testnet3>>(Mode::Public, count_is!(0, 0, 64, 65));
check_cast::<U64<Circuit>, console_root::types::U64<Testnet3>>(Mode::Private, count_is!(0, 0, 64, 65));
}
#[test]
fn test_field_to_u128() {
check_cast::<U128<Circuit>, console_root::types::U128<Testnet3>>(Mode::Constant, count_is!(128, 0, 0, 0));
check_cast::<U128<Circuit>, console_root::types::U128<Testnet3>>(Mode::Public, count_is!(0, 0, 128, 129));
check_cast::<U128<Circuit>, console_root::types::U128<Testnet3>>(Mode::Private, count_is!(0, 0, 128, 129));
}
}