mirror of
https://github.com/arnaucube/ark-r1cs-std.git
synced 2026-01-11 00:11:29 +01:00
Rename arguments to CondSelectGadget for code clarity
This commit is contained in:
committed by
Pratyush Mishra
parent
6c0ee7ffbc
commit
23c6bb4bd0
@@ -312,14 +312,14 @@ impl<ConstraintF: PrimeField> CondSelectGadget<ConstraintF> for AllocatedBit {
|
|||||||
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
||||||
cs: CS,
|
cs: CS,
|
||||||
cond: &Boolean,
|
cond: &Boolean,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError> {
|
) -> Result<Self, SynthesisError> {
|
||||||
cond_select_helper(
|
cond_select_helper(
|
||||||
cs,
|
cs,
|
||||||
cond,
|
cond,
|
||||||
(first.value, first.variable),
|
(true_value.value, true_value.variable),
|
||||||
(second.value, second.variable),
|
(false_value.value, false_value.variable),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -827,17 +827,17 @@ impl<ConstraintF: PrimeField> CondSelectGadget<ConstraintF> for Boolean {
|
|||||||
fn conditionally_select<CS>(
|
fn conditionally_select<CS>(
|
||||||
mut cs: CS,
|
mut cs: CS,
|
||||||
cond: &Self,
|
cond: &Self,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError>
|
) -> Result<Self, SynthesisError>
|
||||||
where
|
where
|
||||||
CS: ConstraintSystem<ConstraintF>,
|
CS: ConstraintSystem<ConstraintF>,
|
||||||
{
|
{
|
||||||
match cond {
|
match cond {
|
||||||
Boolean::Constant(true) => Ok(first.clone()),
|
Boolean::Constant(true) => Ok(true_value.clone()),
|
||||||
Boolean::Constant(false) => Ok(second.clone()),
|
Boolean::Constant(false) => Ok(false_value.clone()),
|
||||||
cond @ Boolean::Not(_) => Self::conditionally_select(cs, &cond.not(), second, first),
|
cond @ Boolean::Not(_) => Self::conditionally_select(cs, &cond.not(), false_value, true_value),
|
||||||
cond @ Boolean::Is(_) => match (first, second) {
|
cond @ Boolean::Is(_) => match (true_value, false_value) {
|
||||||
(x, &Boolean::Constant(false)) => Boolean::and(cs.ns(|| "and"), cond, x).into(),
|
(x, &Boolean::Constant(false)) => Boolean::and(cs.ns(|| "and"), cond, x).into(),
|
||||||
(&Boolean::Constant(false), x) => Boolean::and(cs.ns(|| "and"), &cond.not(), x),
|
(&Boolean::Constant(false), x) => Boolean::and(cs.ns(|| "and"), &cond.not(), x),
|
||||||
(&Boolean::Constant(true), x) => Boolean::or(cs.ns(|| "or"), cond, x).into(),
|
(&Boolean::Constant(true), x) => Boolean::or(cs.ns(|| "or"), cond, x).into(),
|
||||||
|
|||||||
@@ -449,19 +449,19 @@ impl<F: PrimeField> CondSelectGadget<F> for FpGadget<F> {
|
|||||||
fn conditionally_select<CS: ConstraintSystem<F>>(
|
fn conditionally_select<CS: ConstraintSystem<F>>(
|
||||||
mut cs: CS,
|
mut cs: CS,
|
||||||
cond: &Boolean,
|
cond: &Boolean,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError> {
|
) -> Result<Self, SynthesisError> {
|
||||||
if let Boolean::Constant(cond) = *cond {
|
if let Boolean::Constant(cond) = *cond {
|
||||||
if cond {
|
if cond {
|
||||||
Ok(first.clone())
|
Ok(true_value.clone())
|
||||||
} else {
|
} else {
|
||||||
Ok(second.clone())
|
Ok(false_value.clone())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let result = Self::alloc(cs.ns(|| ""), || {
|
let result = Self::alloc(cs.ns(|| ""), || {
|
||||||
cond.get_value()
|
cond.get_value()
|
||||||
.and_then(|cond| if cond { first } else { second }.get_value())
|
.and_then(|cond| if cond { true_value } else { false_value }.get_value())
|
||||||
.get()
|
.get()
|
||||||
})?;
|
})?;
|
||||||
// a = self; b = other; c = cond;
|
// a = self; b = other; c = cond;
|
||||||
@@ -473,8 +473,8 @@ impl<F: PrimeField> CondSelectGadget<F> for FpGadget<F> {
|
|||||||
cs.enforce(
|
cs.enforce(
|
||||||
|| "conditionally_select",
|
|| "conditionally_select",
|
||||||
|_| cond.lc(one, F::one()),
|
|_| cond.lc(one, F::one()),
|
||||||
|lc| (&first.variable - &second.variable) + lc,
|
|lc| (&true_value.variable - &false_value.variable) + lc,
|
||||||
|lc| (&result.variable - &second.variable) + lc,
|
|lc| (&result.variable - &false_value.variable) + lc,
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
|
|||||||
@@ -787,20 +787,20 @@ where
|
|||||||
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
||||||
mut cs: CS,
|
mut cs: CS,
|
||||||
cond: &Boolean,
|
cond: &Boolean,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError> {
|
) -> Result<Self, SynthesisError> {
|
||||||
let c0 = Fp6Gadget::<P, ConstraintF>::conditionally_select(
|
let c0 = Fp6Gadget::<P, ConstraintF>::conditionally_select(
|
||||||
&mut cs.ns(|| "c0"),
|
&mut cs.ns(|| "c0"),
|
||||||
cond,
|
cond,
|
||||||
&first.c0,
|
&true_value.c0,
|
||||||
&second.c0,
|
&false_value.c0,
|
||||||
)?;
|
)?;
|
||||||
let c1 = Fp6Gadget::<P, ConstraintF>::conditionally_select(
|
let c1 = Fp6Gadget::<P, ConstraintF>::conditionally_select(
|
||||||
&mut cs.ns(|| "c1"),
|
&mut cs.ns(|| "c1"),
|
||||||
cond,
|
cond,
|
||||||
&first.c1,
|
&true_value.c1,
|
||||||
&second.c1,
|
&false_value.c1,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Self::new(c0, c1))
|
Ok(Self::new(c0, c1))
|
||||||
|
|||||||
@@ -581,20 +581,20 @@ impl<P: Fp2Parameters<Fp = ConstraintF>, ConstraintF: PrimeField> CondSelectGadg
|
|||||||
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
||||||
mut cs: CS,
|
mut cs: CS,
|
||||||
cond: &Boolean,
|
cond: &Boolean,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError> {
|
) -> Result<Self, SynthesisError> {
|
||||||
let c0 = FpGadget::<ConstraintF>::conditionally_select(
|
let c0 = FpGadget::<ConstraintF>::conditionally_select(
|
||||||
&mut cs.ns(|| "c0"),
|
&mut cs.ns(|| "c0"),
|
||||||
cond,
|
cond,
|
||||||
&first.c0,
|
&true_value.c0,
|
||||||
&second.c0,
|
&false_value.c0,
|
||||||
)?;
|
)?;
|
||||||
let c1 = FpGadget::<ConstraintF>::conditionally_select(
|
let c1 = FpGadget::<ConstraintF>::conditionally_select(
|
||||||
&mut cs.ns(|| "c1"),
|
&mut cs.ns(|| "c1"),
|
||||||
cond,
|
cond,
|
||||||
&first.c1,
|
&true_value.c1,
|
||||||
&second.c1,
|
&false_value.c1,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Self::new(c0, c1))
|
Ok(Self::new(c0, c1))
|
||||||
|
|||||||
@@ -861,26 +861,26 @@ where
|
|||||||
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
||||||
mut cs: CS,
|
mut cs: CS,
|
||||||
cond: &Boolean,
|
cond: &Boolean,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError> {
|
) -> Result<Self, SynthesisError> {
|
||||||
let c0 = Fp2Gadget::<P, ConstraintF>::conditionally_select(
|
let c0 = Fp2Gadget::<P, ConstraintF>::conditionally_select(
|
||||||
&mut cs.ns(|| "c0"),
|
&mut cs.ns(|| "c0"),
|
||||||
cond,
|
cond,
|
||||||
&first.c0,
|
&true_value.c0,
|
||||||
&second.c0,
|
&false_value.c0,
|
||||||
)?;
|
)?;
|
||||||
let c1 = Fp2Gadget::<P, ConstraintF>::conditionally_select(
|
let c1 = Fp2Gadget::<P, ConstraintF>::conditionally_select(
|
||||||
&mut cs.ns(|| "c1"),
|
&mut cs.ns(|| "c1"),
|
||||||
cond,
|
cond,
|
||||||
&first.c1,
|
&true_value.c1,
|
||||||
&second.c1,
|
&false_value.c1,
|
||||||
)?;
|
)?;
|
||||||
let c2 = Fp2Gadget::<P, ConstraintF>::conditionally_select(
|
let c2 = Fp2Gadget::<P, ConstraintF>::conditionally_select(
|
||||||
&mut cs.ns(|| "c2"),
|
&mut cs.ns(|| "c2"),
|
||||||
cond,
|
cond,
|
||||||
&first.c2,
|
&true_value.c2,
|
||||||
&second.c2,
|
&false_value.c2,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Self::new(c0, c1, c2))
|
Ok(Self::new(c0, c1, c2))
|
||||||
|
|||||||
@@ -339,16 +339,16 @@ where
|
|||||||
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
||||||
mut cs: CS,
|
mut cs: CS,
|
||||||
cond: &Boolean,
|
cond: &Boolean,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError> {
|
) -> Result<Self, SynthesisError> {
|
||||||
let x = F::conditionally_select(&mut cs.ns(|| "x"), cond, &first.x, &second.x)?;
|
let x = F::conditionally_select(&mut cs.ns(|| "x"), cond, &true_value.x, &false_value.x)?;
|
||||||
let y = F::conditionally_select(&mut cs.ns(|| "y"), cond, &first.y, &second.y)?;
|
let y = F::conditionally_select(&mut cs.ns(|| "y"), cond, &true_value.y, &false_value.y)?;
|
||||||
let infinity = Boolean::conditionally_select(
|
let infinity = Boolean::conditionally_select(
|
||||||
&mut cs.ns(|| "infinity"),
|
&mut cs.ns(|| "infinity"),
|
||||||
cond,
|
cond,
|
||||||
&first.infinity,
|
&true_value.infinity,
|
||||||
&second.infinity,
|
&false_value.infinity,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Self::new(x, y, infinity))
|
Ok(Self::new(x, y, infinity))
|
||||||
|
|||||||
@@ -1266,11 +1266,11 @@ where
|
|||||||
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
||||||
mut cs: CS,
|
mut cs: CS,
|
||||||
cond: &Boolean,
|
cond: &Boolean,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError> {
|
) -> Result<Self, SynthesisError> {
|
||||||
let x = F::conditionally_select(&mut cs.ns(|| "x"), cond, &first.x, &second.x)?;
|
let x = F::conditionally_select(&mut cs.ns(|| "x"), cond, &true_value.x, &false_value.x)?;
|
||||||
let y = F::conditionally_select(&mut cs.ns(|| "y"), cond, &first.y, &second.y)?;
|
let y = F::conditionally_select(&mut cs.ns(|| "y"), cond, &true_value.y, &false_value.y)?;
|
||||||
|
|
||||||
Ok(Self::new(x, y))
|
Ok(Self::new(x, y))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use crate::prelude::*;
|
|||||||
use algebra::Field;
|
use algebra::Field;
|
||||||
use r1cs_core::{ConstraintSystem, SynthesisError};
|
use r1cs_core::{ConstraintSystem, SynthesisError};
|
||||||
|
|
||||||
/// If condition is `true`, return `first`; else, select `second`.
|
/// If condition is `true`, return `true_value`; else, select `false_value`.
|
||||||
pub trait CondSelectGadget<ConstraintF: Field>
|
pub trait CondSelectGadget<ConstraintF: Field>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
@@ -10,8 +10,8 @@ where
|
|||||||
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
fn conditionally_select<CS: ConstraintSystem<ConstraintF>>(
|
||||||
cs: CS,
|
cs: CS,
|
||||||
cond: &Boolean,
|
cond: &Boolean,
|
||||||
first: &Self,
|
true_value: &Self,
|
||||||
second: &Self,
|
false_value: &Self,
|
||||||
) -> Result<Self, SynthesisError>;
|
) -> Result<Self, SynthesisError>;
|
||||||
|
|
||||||
fn cost() -> usize;
|
fn cost() -> usize;
|
||||||
|
|||||||
Reference in New Issue
Block a user