use super::*;
impl<E: Environment> Ternary for Field<E> {
type Boolean = Boolean<E>;
type Output = Self;
fn ternary(condition: &Self::Boolean, first: &Self, second: &Self) -> Self::Output {
if condition.is_constant() {
match condition.eject_value() {
true => first.clone(),
false => second.clone(),
}
}
else if first.is_constant() && second.is_constant() {
let not_condition = Field::from_boolean(&!condition);
let condition = Field::from_boolean(condition);
(condition * first) + (not_condition * second)
}
else {
let witness = witness!(|condition, first, second| match condition {
true => first,
false => second,
});
E::enforce(|| (condition, (first - second), (&witness - second)));
witness
}
}
}
impl<E: Environment> Metrics<dyn Ternary<Boolean = Boolean<E>, Output = Field<E>>> for Field<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, 1, 1),
}
}
}
impl<E: Environment> OutputMode<dyn Ternary<Boolean = Boolean<E>, Output = Field<E>>> for Field<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(circuit) => match circuit.eject_value() {
true => parameter.1,
false => parameter.2,
},
_ => E::halt("Circuit 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::Field<<Circuit as Environment>::Network>,
condition: Boolean<Circuit>,
a: Field<Circuit>,
b: Field<Circuit>,
) {
Circuit::scope(name, || {
let case = format!("({} ? {} : {})", condition.eject_value(), a.eject_value(), b.eject_value());
let candidate = Field::ternary(&condition, &a, &b);
assert_eq!(expected, candidate.eject_value(), "{case}");
assert_count!(Ternary(Boolean, Field, Field) => Field, &(condition.eject_mode(), a.eject_mode(), b.eject_mode()));
assert_output_mode!(Ternary(Boolean, Field, Field) => Field, &(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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Constant, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Public, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<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 = Field::<Circuit>::new(Mode::Private, first);
let b = Field::<Circuit>::new(Mode::Private, second);
check_ternary("true ? Private : Private", expected, condition, a, b);
}
}