Allow linear combinations to be added together.

This commit is contained in:
Sean Bowe 2017-12-14 16:11:24 -07:00
parent 70a588ceee
commit dc981e6abc
2 changed files with 38 additions and 1 deletions

View File

@ -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"

View File

@ -57,6 +57,30 @@ impl<T, E: Engine> Sub<T> for LinearCombination<T, E> {
}
}
impl<'a, T: Copy, E: Engine> Add<&'a LinearCombination<T, E>> for LinearCombination<T, E> {
type Output = LinearCombination<T, E>;
fn add(mut self, other: &'a LinearCombination<T, E>) -> LinearCombination<T, E> {
for s in &other.0 {
self = self + (s.1, s.0);
}
self
}
}
impl<'a, T: Copy, E: Engine> Sub<&'a LinearCombination<T, E>> for LinearCombination<T, E> {
type Output = LinearCombination<T, E>;
fn sub(mut self, other: &'a LinearCombination<T, E>) -> LinearCombination<T, E> {
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::<usize, Bls12>::zero() + (Fr::one(), 0usize) - (Fr::one(), 1usize);
let y = LinearCombination::<usize, Bls12>::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,