Skip to content

Commit 07cad99

Browse files
committed
fix: support more weird relations.
1 parent 1e3b856 commit 07cad99

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/linear_relation/ops.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,21 @@ mod add {
246246
}
247247
}
248248

249+
impl<G: Group> Add<Sum<Weighted<Term<G>, G::Scalar>>> for Weighted<GroupVar<G>, G::Scalar> {
250+
type Output = Sum<Weighted<Term<G>, G::Scalar>>;
251+
252+
fn add(self, rhs: Sum<Weighted<Term<G>, G::Scalar>>) -> Self::Output {
253+
let weighted_term = Weighted {
254+
term: Term {
255+
scalar: super::ScalarTerm::Unit,
256+
elem: self.term,
257+
},
258+
weight: self.weight,
259+
};
260+
rhs + weighted_term
261+
}
262+
}
263+
249264
impl<G: Group> Add<Weighted<GroupVar<G>, G::Scalar>> for Sum<Weighted<Term<G>, G::Scalar>> {
250265
type Output = Sum<Weighted<Term<G>, G::Scalar>>;
251266

src/tests/test_relations.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,23 +371,21 @@ fn cmz_wallet_spend_relation<G: PrimeGroup, R: RngCore>(
371371
}
372372

373373

374-
fn test_another_relation<G: PrimeGroup, R: RngCore>(
374+
fn nested_affine_relation<G: PrimeGroup, R: RngCore>(
375375
mut rng: &mut R,
376376
) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) {
377377
let mut instance = LinearRelation::<G>::new();
378-
let r = instance.allocate_scalar();
378+
let var_r = instance.allocate_scalar();
379379
let var_A = instance.allocate_element();
380380
let var_B = instance.allocate_element();
381381
let eq1 = instance.allocate_eq(
382-
var_A * G::Scalar::from(4)
383-
+ (r * G::Scalar::from(2) + G::Scalar::from(3)) * var_B,
382+
var_A * G::Scalar::from(4) + (var_r * G::Scalar::from(2) + G::Scalar::from(3)) * var_B,
384383
);
385384

386385
let A = G::random(&mut rng);
387386
let B = G::random(&mut rng);
388387
let r = G::Scalar::random(&mut rng);
389-
let C = A * G::Scalar::from(4)
390-
+ (r * G::Scalar::from(2) + G::Scalar::from(3)) * var_B;
388+
let C = A * G::Scalar::from(4) + B * (r * G::Scalar::from(2) + G::Scalar::from(3));
391389
instance.set_element(var_A, A);
392390
instance.set_element(var_B, B);
393391
instance.set_element(eq1, C);
@@ -478,6 +476,7 @@ fn test_relations() {
478476
"cmz_wallet_spend_relation",
479477
Box::new(cmz_wallet_spend_relation),
480478
),
479+
("nested_affine_relation", Box::new(nested_affine_relation)),
481480
];
482481

483482
for (relation_name, relation_sampler) in instance_generators.iter() {

0 commit comments

Comments
 (0)