Skip to content

Commit 995d2f9

Browse files
committed
chore: document tests for trivial linear relations.
Let the relation builder create relations for which no valid witness exists. It's responsibility of the user to check that the relation is satisfiable.
1 parent 5020749 commit 995d2f9

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ pub mod tests;
6767

6868
pub use fiat_shamir::Nizk;
6969
pub use linear_relation::LinearRelation;
70+
71+
#[deprecated = "Use sigma_rs::group::serialization instead"]
72+
pub use group::serialization;

src/linear_relation/canonical.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ impl<G: PrimeGroup> TryFrom<&LinearRelation<G>> for CanonicalLinearRelation<G> {
453453
if lhs_value == rhs_value {
454454
continue; // Skip processing trivially true constraints
455455
}
456+
// We know that there is no valid witness for the relation here.
457+
// return Err(InvalidInstance::new(
458+
// "Trivial constraint does not hold (LHS != RHS)",
459+
// ));
456460
} else if lhs_value == G::identity() {
457461
return Err(InvalidInstance::new("Image contains identity element"));
458462
}

src/tests/test_relations.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use ff::Field;
22
use group::prime::PrimeGroup;
3-
use rand::rngs::OsRng;
43
use rand::RngCore;
54

65
use crate::codec::Shake128DuplexSponge;
@@ -474,7 +473,7 @@ fn test_cmz_wallet_with_fee() {
474473
use group::Group;
475474
type G = bls12_381::G1Projective;
476475

477-
let mut rng = OsRng;
476+
let mut rng = rand::thread_rng();
478477

479478
// This version should fail with InvalidInstanceWitnessPair
480479
// because it uses a scalar constant directly in the equation
@@ -508,7 +507,7 @@ fn test_cmz_wallet_with_fee() {
508507

509508
// Try to convert to CanonicalLinearRelation - this should fail
510509
let nizk = relation.into_nizk(b"session_identifier").unwrap();
511-
let result = nizk.prove_batchable(&vec![n_balance, i_price, z_w_balance], &mut OsRng);
510+
let result = nizk.prove_batchable(&vec![n_balance, i_price, z_w_balance], &mut rng);
512511
assert!(result.is_ok());
513512
let proof = result.unwrap();
514513
let verify_result = nizk.verify_batchable(&proof);
@@ -518,12 +517,9 @@ fn test_cmz_wallet_with_fee() {
518517
/// Generic helper function to test both relation correctness and NIZK functionality
519518
#[test]
520519
fn test_relations() {
521-
use group::Group;
522520
type G = bls12_381::G1Projective;
523-
type RelationGenerator<G> =
524-
&'static dyn Fn(&mut OsRng) -> (CanonicalLinearRelation<G>, Vec<<G as Group>::Scalar>);
525521

526-
let instance_generators: Vec<(&str, RelationGenerator<G>)> = vec![
522+
let instance_generators: Vec<(_, &'static dyn Fn(&mut _) -> _)> = vec![
527523
("dlog", &discrete_logarithm),
528524
("shifted_dlog", &shifted_dlog),
529525
("dleq", &dleq),
@@ -541,7 +537,7 @@ fn test_relations() {
541537
];
542538

543539
for (relation_name, relation_sampler) in instance_generators.iter() {
544-
let mut rng = OsRng;
540+
let mut rng = rand::thread_rng();
545541
let (canonical_relation, witness) = relation_sampler(&mut rng);
546542

547543
// Test the NIZK protocol
@@ -555,10 +551,10 @@ fn test_relations() {
555551

556552
// Test both proof types
557553
let proof_batchable = nizk
558-
.prove_batchable(&witness, &mut OsRng)
554+
.prove_batchable(&witness, &mut rng)
559555
.unwrap_or_else(|_| panic!("Failed to create batchable proof for {relation_name}"));
560556
let proof_compact = nizk
561-
.prove_compact(&witness, &mut OsRng)
557+
.prove_compact(&witness, &mut rng)
562558
.unwrap_or_else(|_| panic!("Failed to create compact proof for {relation_name}"));
563559

564560
// Verify both proof types

src/tests/test_validation_criteria.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,18 @@ mod instance_validation {
131131

132132
let X = G::generator() * Scalar::from(4);
133133

134-
// The following relation is invalid and should trigger a fail.
134+
// The following relation is trivially invalid.
135+
// That is, we know that no witness will ever satisfy it.
136+
// In this case, we're letting the prover fail and build the relation anyways.
135137
let mut linear_relation = LinearRelation::<G>::new();
136138
let B_var = linear_relation.allocate_element();
137139
let C_var = linear_relation.allocate_eq(B_var);
138140
linear_relation.set_element(B_var, B);
139141
linear_relation.set_element(C_var, C);
140-
assert!(linear_relation.canonical().is_err());
141-
142-
// The following relation is valid and should pass.
143-
let mut linear_relation = LinearRelation::<G>::new();
144-
let B_var = linear_relation.allocate_element();
145-
let C_var = linear_relation.allocate_eq(B_var);
146-
linear_relation.set_element(B_var, B);
147-
linear_relation.set_element(C_var, B);
148142
assert!(linear_relation.canonical().is_ok());
149143

150-
// The following relation is invalid and should trigger a fail.
144+
// Also in this case, we know that no witness will ever satisfy the relation.
145+
// Also here, the relation is built even though the prover will never be able to give a valid proof for it.
151146
// X != B * pub_scalar + A * 3
152147
let mut linear_relation = LinearRelation::<G>::new();
153148
let B_var = linear_relation.allocate_element();
@@ -157,7 +152,15 @@ mod instance_validation {
157152
linear_relation.set_element(B_var, B);
158153
linear_relation.set_element(A_var, A);
159154
linear_relation.set_element(X_var, X);
160-
assert!(linear_relation.canonical().is_err());
155+
assert!(linear_relation.canonical().is_ok());
156+
157+
// The following relation is valid and should pass.
158+
let mut linear_relation = LinearRelation::<G>::new();
159+
let B_var = linear_relation.allocate_element();
160+
let C_var = linear_relation.allocate_eq(B_var);
161+
linear_relation.set_element(B_var, B);
162+
linear_relation.set_element(C_var, B);
163+
assert!(linear_relation.canonical().is_ok());
161164

162165
// The following relation is valid and should pass.
163166
// C = B * pub_scalar + A * 3

0 commit comments

Comments
 (0)