From dc981e6abcba1d8a3efd375e04276849c73321e6 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 14 Dec 2017 16:11:24 -0700 Subject: [PATCH] Allow linear combinations to be added together. --- Cargo.toml | 2 +- src/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c48f1e0..7831318 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ homepage = "https://github.com/ebfull/bellman" license = "MIT/Apache-2.0" name = "bellman" repository = "https://github.com/ebfull/bellman" -version = "0.0.6" +version = "0.0.7" [dependencies] rand = "0.3" diff --git a/src/lib.rs b/src/lib.rs index 866fb9b..480cb96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,6 +57,30 @@ impl Sub for LinearCombination { } } +impl<'a, T: Copy, E: Engine> Add<&'a LinearCombination> for LinearCombination { + type Output = LinearCombination; + + fn add(mut self, other: &'a LinearCombination) -> LinearCombination { + for s in &other.0 { + self = self + (s.1, s.0); + } + + self + } +} + +impl<'a, T: Copy, E: Engine> Sub<&'a LinearCombination> for LinearCombination { + type Output = LinearCombination; + + fn sub(mut self, other: &'a LinearCombination) -> LinearCombination { + for s in &other.0 { + self = self - (s.1, s.0); + } + + self + } +} + #[test] fn test_lc() { use pairing::bls12_381::{Bls12, Fr}; @@ -67,6 +91,19 @@ fn test_lc() { negone.negate(); assert_eq!(a.0, vec![(0usize, Fr::one()), (1usize, Fr::one()), (2usize, Fr::one()), (3usize, negone)]); + + let x = LinearCombination::::zero() + (Fr::one(), 0usize) - (Fr::one(), 1usize); + let y = LinearCombination::::zero() + (Fr::one(), 2usize) - (Fr::one(), 3usize); + let z = x + &y - &y; + + assert_eq!(z.0, vec![ + (0usize, Fr::one()), + (1usize, negone), + (2usize, Fr::one()), + (3usize, negone), + (2usize, negone), + (3usize, Fr::one()) + ]); } /// This is an error that could occur during circuit synthesis contexts,