Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl'd EqGadget, CondSelectGadget, R1CSVar for Vec<T> #93

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Pending

* [\#93](https://github.com/arkworks-rs/r1cs-std/pull/93) Implement `EqGadget`, `CondSelectGadget`, and `R1CSVar` for `Vec<T>`

### Breaking changes

- [\#86](https://github.com/arkworks-rs/r1cs-std/pull/86) Change the API for domains for coset.
Expand Down
28 changes: 28 additions & 0 deletions src/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,31 @@ impl<T: EqGadget<F> + R1CSVar<F>, F: Field> EqGadget<F> for [T] {
}
}
}

// EqGadget for Vec<T> just calls down to EqGadget for [T]
impl<T: EqGadget<F> + R1CSVar<F>, F: Field> EqGadget<F> for Vec<T> {
#[tracing::instrument(target = "r1cs", skip(self, other))]
fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError> {
self.as_slice().is_eq(other.as_slice())
}

#[tracing::instrument(target = "r1cs", skip(self, other))]
fn conditional_enforce_equal(
&self,
other: &Self,
condition: &Boolean<F>,
) -> Result<(), SynthesisError> {
self.as_slice()
.conditional_enforce_equal(other.as_slice(), condition)
}

#[tracing::instrument(target = "r1cs", skip(self, other))]
fn conditional_enforce_not_equal(
&self,
other: &Self,
should_enforce: &Boolean<F>,
) -> Result<(), SynthesisError> {
self.as_slice()
.conditional_enforce_not_equal(other.as_slice(), should_enforce)
}
}
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ impl<'a, F: Field, T: 'a + R1CSVar<F>> R1CSVar<F> for &'a T {
}
}

// R1CSVar for Vec<T> just calls down to R1CSVar for [T]
impl<F: Field, T: R1CSVar<F>> R1CSVar<F> for Vec<T> {
type Value = Vec<T::Value>;

fn cs(&self) -> ark_relations::r1cs::ConstraintSystemRef<F> {
self.as_slice().cs()
}

fn value(&self) -> Result<Self::Value, ark_relations::r1cs::SynthesisError> {
self.as_slice().value()
}
}

/// A utility trait to convert `Self` to `Result<T, SynthesisErrorA`.>
pub trait Assignment<T> {
/// Converts `self` to `Result`.
Expand Down
21 changes: 21 additions & 0 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,24 @@ where
constants: &[Self::TableConstant],
) -> Result<Self, SynthesisError>;
}

impl<ConstraintF, T> CondSelectGadget<ConstraintF> for Vec<T>
where
ConstraintF: Field,
T: CondSelectGadget<ConstraintF>,
{
#[tracing::instrument(target = "r1cs", skip(true_value, false_value))]
fn conditionally_select(
cond: &Boolean<ConstraintF>,
true_value: &Vec<T>,
false_value: &Vec<T>,
) -> Result<Vec<T>, SynthesisError> {
assert_eq!(true_value.len(), false_value.len());

true_value
.iter()
.zip(false_value.iter())
.map(|(t, f)| T::conditionally_select(cond, t, f))
.collect()
}
}