Skip to content

Commit 098a162

Browse files
committed
chore: empty statements are valid and the proof is the empty string.
Change of validation criterias: if the proof is empty, it is valid for the empty statement.
1 parent 98b1ff8 commit 098a162

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

examples/schnorr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ fn create_relation(P: RistrettoPoint) -> LinearRelation<RistrettoPoint> {
3535
#[allow(non_snake_case)]
3636
fn prove(x: Scalar, P: RistrettoPoint) -> ProofResult<Vec<u8>> {
3737
let nizk = create_relation(P).into_nizk(b"sigma-rs::examples");
38-
nizk.prove_batchable(&vec![x], &mut OsRng)
38+
nizk?.prove_batchable(&vec![x], &mut OsRng)
3939
}
4040

4141
/// Verify a proof of knowledge of discrete logarithm for the given public key P
4242
#[allow(non_snake_case)]
4343
fn verify(P: RistrettoPoint, proof: &[u8]) -> ProofResult<()> {
4444
let nizk = create_relation(P).into_nizk(b"sigma-rs::examples");
45-
nizk.verify_batchable(proof)
45+
nizk?.verify_batchable(proof)
4646
}
4747

4848
#[allow(non_snake_case)]

src/linear_relation/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,11 @@ impl<G: PrimeGroup> TryFrom<&LinearRelation<G>> for CanonicalLinearRelation<G> {
681681
));
682682
}
683683

684-
if relation.linear_map.linear_combinations.is_empty() {
685-
return Err(InvalidInstance::new(
686-
"Empty relations cannot be proven"
687-
));
688-
}
684+
// if relation.linear_map.linear_combinations.is_empty() {
685+
// return Err(InvalidInstance::new(
686+
// "Empty relations cannot be proven"
687+
// ));
688+
// }
689689

690690
// If any linear combination is empty, the relation is invalid
691691
if relation
@@ -924,16 +924,14 @@ impl<G: PrimeGroup> LinearRelation<G> {
924924
/// relation.compute_image(&[x]).unwrap();
925925
///
926926
/// // Convert to NIZK with custom context
927-
/// let nizk = relation.into_nizk(b"my-protocol-v1");
927+
/// let nizk = relation.into_nizk(b"my-protocol-v1").unwrap();
928928
/// let proof = nizk.prove_batchable(&vec![x], &mut OsRng).unwrap();
929929
/// assert!(nizk.verify_batchable(&proof).is_ok());
930930
/// ```
931931
pub fn into_nizk(
932932
self,
933933
session_identifier: &[u8],
934-
) -> Nizk<SchnorrProof<G>, Shake128DuplexSponge<G>> {
935-
let schnorr =
936-
SchnorrProof::try_from(self).expect("Failed to convert LinearRelation to SchnorrProof");
937-
Nizk::new(session_identifier, schnorr)
934+
) -> Result<Nizk<SchnorrProof<G>, Shake128DuplexSponge<G>>, InvalidInstance> {
935+
Ok(Nizk::new(session_identifier, self.try_into()?))
938936
}
939937
}

src/tests/test_relations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ fn test_cmz_wallet_with_fee() {
409409
.unwrap();
410410

411411
// Try to convert to CanonicalLinearRelation - this should fail
412-
let nizk = relation.into_nizk(b"session_identifier");
412+
let nizk = relation.into_nizk(b"session_identifier").unwrap();
413413
let result = nizk.prove_batchable(&vec![n_balance, i_price, z_w_balance], &mut OsRng);
414414
assert!(result.is_ok());
415415
let proof = result.unwrap();

src/tests/test_validation_criteria.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
#[cfg(test)]
77
mod instance_validation {
8-
use crate::linear_relation::{CanonicalLinearRelation, LinearRelation};
8+
use crate::{linear_relation::{CanonicalLinearRelation, LinearRelation}, tests::spec::rng};
99
use bls12_381::{G1Projective as G, Scalar};
1010

1111
#[test]
@@ -64,14 +64,18 @@ mod instance_validation {
6464

6565
#[test]
6666
fn test_empty_instance() {
67+
let rng = &mut rand::thread_rng();
6768
// Create an empty linear relation
6869
let relation = LinearRelation::<G>::new();
6970

7071
// Try to convert empty relation to canonical form
71-
let result = CanonicalLinearRelation::try_from(&relation);
72+
let result = relation.into_nizk(b"test empty instance");
7273

73-
// Empty relations should be rejected
74-
assert!(result.is_err());
74+
// Empty relations should be accepted as valid, and the proof be the empty string.
75+
assert!(result.is_ok());
76+
let proof = result.unwrap().prove_batchable(&vec![], rng);
77+
assert!(proof.is_ok());
78+
assert!(proof.unwrap().is_empty());
7579
}
7680

7781
/// Test function with the requested LinearRelation code
@@ -134,16 +138,6 @@ mod instance_validation {
134138
let X = G::generator() * Scalar::from(4);
135139
let pub_scalar = Scalar::from(42);
136140

137-
// The following relation has no equation and should trigger a fail.
138-
let mut linear_relation = LinearRelation::<G>::new();
139-
let B_var = linear_relation.allocate_element();
140-
let A_var = linear_relation.allocate_element();
141-
142-
linear_relation.set_element(B_var, B);
143-
linear_relation.set_element(A_var, A);
144-
let result = CanonicalLinearRelation::try_from(&linear_relation);
145-
assert!(result.is_err());
146-
147141
// The following relation does not have a witness and should trigger a fail.
148142
// X = B * pub_scalar + A * 3
149143
let mut linear_relation = LinearRelation::<G>::new();

0 commit comments

Comments
 (0)