use super::*;
impl<E: Environment> Ternary for Group<E> {
type Boolean = Boolean<E>;
type Output = Group<E>;
fn ternary(condition: &Self::Boolean, first: &Self, second: &Self) -> Self::Output {
let x = Ternary::ternary(condition, &first.x, &second.x);
let y = Ternary::ternary(condition, &first.y, &second.y);
Group { x, y }
}
}
impl<E: Environment> Metrics<dyn Ternary<Boolean = Boolean<E>, Output = Group<E>>> for Group<E> {
type Case = (Mode, Mode, Mode);
fn count(case: &Self::Case) -> Count {
match case {
(Mode::Constant, _, _)
| (Mode::Public, Mode::Constant, Mode::Constant)
| (Mode::Private, Mode::Constant, Mode::Constant) => Count::is(0, 0, 0, 0),
_ => Count::is(0, 0, 2, 2),
}
}
}
impl<E: Environment> OutputMode<dyn Ternary<Boolean = Boolean<E>, Output = Self>> for Group<E> {
type Case = (CircuitType<Boolean<E>>, Mode, Mode);
fn output_mode(parameter: &Self::Case) -> Mode {
match parameter.0.mode().is_constant() {
true => match ¶meter.0 {
CircuitType::Constant(constant) => match constant.eject_value() {
true => parameter.1,
false => parameter.2,
},
_ => E::halt("The constant condition is required to determine output mode."),
},
false => Mode::Private,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_circuit_environment::Circuit;
fn check_ternary(
name: &str,
expected: console::Group<<Circuit as Environment>::Network>,
condition: Boolean<Circuit>,
a: Group<Circuit>,
b: Group<Circuit>,
) {
Circuit::scope(name, || {
let case = format!("({} ? {} : {})", condition.eject_value(), a.eject_value(), b.eject_value());
let candidate = Group::ternary(&condition, &a, &b);
assert_eq!(expected, candidate.eject_value(), "{case}");
assert_count!(Ternary(Boolean, Group, Group) => Group, &(condition.eject_mode(), a.eject_mode(), b.eject_mode()));
assert_output_mode!(Ternary(Boolean, Group, Group) => Group, &(CircuitType::from(&condition), a.eject_mode(), b.eject_mode()), candidate);
});
}
#[test]
fn test_constant_condition() {
let mut rng = TestRng::default();
let first = Uniform::rand(&mut rng);
let second = Uniform::rand(&mut rng);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Constant, false);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("false ? Constant : Constant", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Constant, false);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("false ? Constant : Public", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Constant, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("false ? Public : Constant", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Constant, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("false ? Public : Public", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Constant, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("false ? Public : Private", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Constant, false);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("false ? Private : Private", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Constant, true);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("true ? Constant : Constant", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Constant, true);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("true ? Constant : Public", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Constant, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("true ? Public : Constant", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Constant, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("true ? Public : Public", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Constant, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("true ? Public : Private", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Constant, true);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("true ? Private : Private", expected, condition, a, b);
}
#[test]
fn test_public_condition_and_constant_inputs() {
let mut rng = TestRng::default();
let first = Uniform::rand(&mut rng);
let second = Uniform::rand(&mut rng);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Public, false);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("false ? Constant : Constant", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Public, true);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("true ? Constant : Constant", expected, condition, a, b);
}
#[test]
fn test_public_condition_and_mixed_inputs() {
let mut rng = TestRng::default();
let first = Uniform::rand(&mut rng);
let second = Uniform::rand(&mut rng);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Public, false);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("false ? Constant : Public", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Public, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("false ? Public : Constant", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Public, true);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("true ? Constant : Public", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Public, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("true ? Public : Constant", expected, condition, a, b);
}
#[test]
fn test_private_condition_and_constant_inputs() {
let mut rng = TestRng::default();
let first = Uniform::rand(&mut rng);
let second = Uniform::rand(&mut rng);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Private, false);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("false ? Constant : Constant", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Private, true);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("true ? Constant : Constant", expected, condition, a, b);
}
#[test]
fn test_private_condition_and_mixed_inputs() {
let mut rng = TestRng::default();
let first = Uniform::rand(&mut rng);
let second = Uniform::rand(&mut rng);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Private, false);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("false ? Constant : Public", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Private, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("false ? Public : Constant", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Private, true);
let a = Group::<Circuit>::new(Mode::Constant, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("true ? Constant : Public", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Private, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Constant, second);
check_ternary("true ? Public : Constant", expected, condition, a, b);
}
#[test]
fn test_public_condition_and_variable_inputs() {
let mut rng = TestRng::default();
let first = Uniform::rand(&mut rng);
let second = Uniform::rand(&mut rng);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Public, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("false ? Public : Public", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Public, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("false ? Public : Private", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Public, false);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("false ? Private : Public", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Public, false);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("false ? Private : Private", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Public, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("true ? Public : Public", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Public, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("true ? Public : Private", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Public, true);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("true ? Private : Public", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Public, true);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("true ? Private : Private", expected, condition, a, b);
}
#[test]
fn test_private_condition_and_variable_inputs() {
let mut rng = TestRng::default();
let first = Uniform::rand(&mut rng);
let second = Uniform::rand(&mut rng);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Private, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("false ? Public : Public", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Private, false);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("false ? Public : Private", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Private, false);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("false ? Private : Public", expected, condition, a, b);
let expected = second;
let condition = Boolean::<Circuit>::new(Mode::Private, false);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("false ? Private : Private", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Private, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("true ? Public : Public", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Private, true);
let a = Group::<Circuit>::new(Mode::Public, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("true ? Public : Private", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Private, true);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Public, second);
check_ternary("true ? Private : Public", expected, condition, a, b);
let expected = first;
let condition = Boolean::<Circuit>::new(Mode::Private, true);
let a = Group::<Circuit>::new(Mode::Private, first);
let b = Group::<Circuit>::new(Mode::Private, second);
check_ternary("true ? Private : Private", expected, condition, a, b);
}
}