Skip to content

Commit

Permalink
tabs -> spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip committed Nov 23, 2020
1 parent 247d594 commit d9d3ae0
Show file tree
Hide file tree
Showing 5 changed files with 1,323 additions and 1,320 deletions.
2 changes: 1 addition & 1 deletion src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::MultiscalarMul;
use digest::{XofReader, ExtendableOutputDirty, Update};
use digest::{ExtendableOutputDirty, Update, XofReader};
use sha3::{Sha3XofReader, Sha3_512, Shake256};

/// Represents a pair of base points for Pedersen commitments.
Expand Down
5 changes: 4 additions & 1 deletion src/r1cs/constraint_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ pub trait ConstraintSystem {
/// Allocate a single variable using a closure, similar to `allocate`.
/// When allocating left variable, return left variable and None.
/// When allocating right variable, return right variable and output variable.
fn allocate_single(&mut self, assignment: Option<Scalar>) -> Result<(Variable, Option<Variable>), R1CSError>;
fn allocate_single(
&mut self,
assignment: Option<Scalar>,
) -> Result<(Variable, Option<Variable>), R1CSError>;
}

/// An extension to the constraint system trait that permits randomized constraints.
Expand Down
270 changes: 135 additions & 135 deletions src/r1cs/linear_combination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,217 +10,217 @@ use hashbrown::hash_map::HashMap;
/// Represents a variable in a constraint system.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Variable {
/// Represents an external input specified by a commitment.
Committed(usize),
/// Represents the left input of a multiplication gate.
MultiplierLeft(usize),
/// Represents the right input of a multiplication gate.
MultiplierRight(usize),
/// Represents the output of a multiplication gate.
MultiplierOutput(usize),
/// Represents the constant 1.
One(),
/// Represents an external input specified by a commitment.
Committed(usize),
/// Represents the left input of a multiplication gate.
MultiplierLeft(usize),
/// Represents the right input of a multiplication gate.
MultiplierRight(usize),
/// Represents the output of a multiplication gate.
MultiplierOutput(usize),
/// Represents the constant 1.
One(),
}

impl From<Variable> for LinearCombination {
fn from(v: Variable) -> LinearCombination {
LinearCombination {
terms: vec![(v, Scalar::one())],
}
}
fn from(v: Variable) -> LinearCombination {
LinearCombination {
terms: vec![(v, Scalar::one())],
}
}
}

impl<S: Into<Scalar>> From<S> for LinearCombination {
fn from(s: S) -> LinearCombination {
LinearCombination {
terms: vec![(Variable::One(), s.into())],
}
}
fn from(s: S) -> LinearCombination {
LinearCombination {
terms: vec![(Variable::One(), s.into())],
}
}
}

// Arithmetic on variables produces linear combinations

impl Neg for Variable {
type Output = LinearCombination;
type Output = LinearCombination;

fn neg(self) -> Self::Output {
-LinearCombination::from(self)
}
fn neg(self) -> Self::Output {
-LinearCombination::from(self)
}
}

impl<L: Into<LinearCombination>> Add<L> for Variable {
type Output = LinearCombination;
type Output = LinearCombination;

fn add(self, other: L) -> Self::Output {
LinearCombination::from(self) + other.into()
}
fn add(self, other: L) -> Self::Output {
LinearCombination::from(self) + other.into()
}
}

impl<L: Into<LinearCombination>> Sub<L> for Variable {
type Output = LinearCombination;
type Output = LinearCombination;

fn sub(self, other: L) -> Self::Output {
LinearCombination::from(self) - other.into()
}
fn sub(self, other: L) -> Self::Output {
LinearCombination::from(self) - other.into()
}
}

impl<S: Into<Scalar>> Mul<S> for Variable {
type Output = LinearCombination;
type Output = LinearCombination;

fn mul(self, other: S) -> Self::Output {
LinearCombination {
terms: vec![(self, other.into())],
}
}
fn mul(self, other: S) -> Self::Output {
LinearCombination {
terms: vec![(self, other.into())],
}
}
}

// Arithmetic on scalars with variables produces linear combinations

impl Add<Variable> for Scalar {
type Output = LinearCombination;
type Output = LinearCombination;

fn add(self, other: Variable) -> Self::Output {
LinearCombination {
terms: vec![(Variable::One(), self), (other, Scalar::one())],
}
}
fn add(self, other: Variable) -> Self::Output {
LinearCombination {
terms: vec![(Variable::One(), self), (other, Scalar::one())],
}
}
}

impl Sub<Variable> for Scalar {
type Output = LinearCombination;
type Output = LinearCombination;

fn sub(self, other: Variable) -> Self::Output {
LinearCombination {
terms: vec![(Variable::One(), self), (other, -Scalar::one())],
}
}
fn sub(self, other: Variable) -> Self::Output {
LinearCombination {
terms: vec![(Variable::One(), self), (other, -Scalar::one())],
}
}
}

impl Mul<Variable> for Scalar {
type Output = LinearCombination;
type Output = LinearCombination;

fn mul(self, other: Variable) -> Self::Output {
LinearCombination {
terms: vec![(other, self)],
}
}
fn mul(self, other: Variable) -> Self::Output {
LinearCombination {
terms: vec![(other, self)],
}
}
}

/// Represents a linear combination of
/// [`Variables`](::r1cs::Variable). Each term is represented by a
/// `(Variable, Scalar)` pair.
#[derive(Clone, Debug, PartialEq)]
pub struct LinearCombination {
pub(super) terms: Vec<(Variable, Scalar)>,
pub(super) terms: Vec<(Variable, Scalar)>,
}

impl LinearCombination {
pub fn get_terms(self) -> Vec<(Variable, Scalar)> {
self.terms
}

/// Simplify linear combination by taking Variables common across terms and adding their corresponding scalars.
/// Useful when linear combinations become large. Takes ownership of linear combination as this function is useful
/// when memory is limited and the obvious action after this function call will be to free the memory held by the old linear combination
#[cfg(feature = "std")]
pub fn simplify(self) -> Self {
// Build hashmap to hold unique variables with their values.
let mut vars: HashMap<Variable, Scalar> = HashMap::new();

let terms = self.get_terms();
for (var, val) in terms {
*vars.entry(var).or_insert(Scalar::zero()) += val;
}

let mut new_lc_terms = vec![];
for (var, val) in vars {
new_lc_terms.push((var, val));
}
new_lc_terms.iter().collect()
}
pub fn get_terms(self) -> Vec<(Variable, Scalar)> {
self.terms
}

/// Simplify linear combination by taking Variables common across terms and adding their corresponding scalars.
/// Useful when linear combinations become large. Takes ownership of linear combination as this function is useful
/// when memory is limited and the obvious action after this function call will be to free the memory held by the old linear combination
#[cfg(feature = "std")]
pub fn simplify(self) -> Self {
// Build hashmap to hold unique variables with their values.
let mut vars: HashMap<Variable, Scalar> = HashMap::new();

let terms = self.get_terms();
for (var, val) in terms {
*vars.entry(var).or_insert(Scalar::zero()) += val;
}

let mut new_lc_terms = vec![];
for (var, val) in vars {
new_lc_terms.push((var, val));
}
new_lc_terms.iter().collect()
}
}

impl Default for LinearCombination {
fn default() -> Self {
LinearCombination { terms: Vec::new() }
}
fn default() -> Self {
LinearCombination { terms: Vec::new() }
}
}

impl FromIterator<(Variable, Scalar)> for LinearCombination {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (Variable, Scalar)>,
{
LinearCombination {
terms: iter.into_iter().collect(),
}
}
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (Variable, Scalar)>,
{
LinearCombination {
terms: iter.into_iter().collect(),
}
}
}

impl<'a> FromIterator<&'a (Variable, Scalar)> for LinearCombination {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = &'a (Variable, Scalar)>,
{
LinearCombination {
terms: iter.into_iter().cloned().collect(),
}
}
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = &'a (Variable, Scalar)>,
{
LinearCombination {
terms: iter.into_iter().cloned().collect(),
}
}
}

// Arithmetic on linear combinations

impl<L: Into<LinearCombination>> Add<L> for LinearCombination {
type Output = Self;
type Output = Self;

fn add(mut self, rhs: L) -> Self::Output {
self.terms.extend(rhs.into().terms.iter().cloned());
LinearCombination { terms: self.terms }
}
fn add(mut self, rhs: L) -> Self::Output {
self.terms.extend(rhs.into().terms.iter().cloned());
LinearCombination { terms: self.terms }
}
}

impl<L: Into<LinearCombination>> Sub<L> for LinearCombination {
type Output = Self;
type Output = Self;

fn sub(mut self, rhs: L) -> Self::Output {
self.terms
.extend(rhs.into().terms.iter().map(|(var, coeff)| (*var, -coeff)));
LinearCombination { terms: self.terms }
}
fn sub(mut self, rhs: L) -> Self::Output {
self.terms
.extend(rhs.into().terms.iter().map(|(var, coeff)| (*var, -coeff)));
LinearCombination { terms: self.terms }
}
}

impl Mul<LinearCombination> for Scalar {
type Output = LinearCombination;
type Output = LinearCombination;

fn mul(self, other: LinearCombination) -> Self::Output {
let out_terms = other
.terms
.into_iter()
.map(|(var, scalar)| (var, scalar * self))
.collect();
LinearCombination { terms: out_terms }
}
fn mul(self, other: LinearCombination) -> Self::Output {
let out_terms = other
.terms
.into_iter()
.map(|(var, scalar)| (var, scalar * self))
.collect();
LinearCombination { terms: out_terms }
}
}

impl Neg for LinearCombination {
type Output = Self;
type Output = Self;

fn neg(mut self) -> Self::Output {
for (_, s) in self.terms.iter_mut() {
*s = -*s
}
self
}
fn neg(mut self) -> Self::Output {
for (_, s) in self.terms.iter_mut() {
*s = -*s
}
self
}
}

impl<S: Into<Scalar>> Mul<S> for LinearCombination {
type Output = Self;

fn mul(mut self, other: S) -> Self::Output {
let other = other.into();
for (_, s) in self.terms.iter_mut() {
*s *= other
}
self
}
type Output = Self;

fn mul(mut self, other: S) -> Self::Output {
let other = other.into();
for (_, s) in self.terms.iter_mut() {
*s *= other
}
self
}
}
Loading

0 comments on commit d9d3ae0

Please sign in to comment.