Skip to content

Commit 6bd837a

Browse files
committed
feat: impl Add for Sum<Weighted>
1 parent 426ead0 commit 6bd837a

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/linear_relation/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,10 +761,7 @@ where
761761
/// let proof = nizk.prove_batchable(&vec![x], &mut OsRng).unwrap();
762762
/// assert!(nizk.verify_batchable(&proof).is_ok());
763763
/// ```
764-
pub fn into_nizk(
765-
self,
766-
session_identifier: &[u8],
767-
) -> Nizk<SchnorrProof<G>, ShakeCodec<G>>
764+
pub fn into_nizk(self, session_identifier: &[u8]) -> Nizk<SchnorrProof<G>, ShakeCodec<G>>
768765
where
769766
G: group::GroupEncoding,
770767
{

src/linear_relation/ops.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,41 @@ mod add {
217217
self + Self::from(rhs)
218218
}
219219
}
220+
221+
impl<G: Group> Add<Weighted<GroupVar<G>, G::Scalar>> for Term<G> {
222+
type Output = Sum<Weighted<Term<G>, G::Scalar>>;
223+
224+
fn add(self, rhs: Weighted<GroupVar<G>, G::Scalar>) -> Self::Output {
225+
Sum(vec![
226+
Weighted {
227+
term: self,
228+
weight: G::Scalar::ONE,
229+
},
230+
Weighted {
231+
term: Term {
232+
scalar: super::ScalarTerm::Unit,
233+
elem: rhs.term,
234+
},
235+
weight: rhs.weight,
236+
},
237+
])
238+
}
239+
}
240+
241+
impl<G: Group> Add<Weighted<GroupVar<G>, G::Scalar>> for Sum<Weighted<Term<G>, G::Scalar>> {
242+
type Output = Sum<Weighted<Term<G>, G::Scalar>>;
243+
244+
fn add(mut self, rhs: Weighted<GroupVar<G>, G::Scalar>) -> Self::Output {
245+
self.0.push(Weighted {
246+
term: Term {
247+
scalar: super::ScalarTerm::Unit,
248+
elem: rhs.term,
249+
},
250+
weight: rhs.weight,
251+
});
252+
self
253+
}
254+
}
220255
}
221256

222257
mod mul {

src/tests/composition.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ fn composition_proof_correct() {
8383
let protocol = Protocol::And(vec![or_protocol1, simple_protocol1, and_protocol1]);
8484
let witness = ProtocolWitness::And(vec![or_witness1, simple_witness1, and_witness1]);
8585

86-
let nizk =
87-
Nizk::<Protocol<RistrettoPoint>, ShakeCodec<G>>::new(domain_sep, protocol);
86+
let nizk = Nizk::<Protocol<RistrettoPoint>, ShakeCodec<G>>::new(domain_sep, protocol);
8887

8988
// Batchable and compact proofs
9089
let proof_batchable_bytes = nizk.prove_batchable(&witness, &mut rng).unwrap();

src/tests/test_utils.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,18 @@ pub fn bbs_blind_commitment_computation<G: Group + GroupEncoding>(
184184
assert!(vec![C] == relation.linear_map.evaluate(&witness).unwrap());
185185
(relation, witness)
186186
}
187+
188+
/// Test function with the requested LinearRelation code
189+
#[allow(non_snake_case)]
190+
pub fn test_linear_relation_example<G: Group + GroupEncoding>() -> LinearRelation<G> {
191+
use ff::Field;
192+
193+
let mut sigma__lr = LinearRelation::<G>::new();
194+
let x = sigma__lr.allocate_scalar();
195+
let B = sigma__lr.allocate_element();
196+
let _sigma__eq1 =
197+
sigma__lr.allocate_eq((x + (-<<G as Group>::Scalar as Field>::ONE)) * B + (-B));
198+
199+
sigma__lr
200+
}
201+

0 commit comments

Comments
 (0)