Skip to content

Commit f067c09

Browse files
committed
Refactoring test directory.
Ensures all tests run with the cargo test command, also in CI.
1 parent 8945b33 commit f067c09

File tree

18 files changed

+154
-156
lines changed

18 files changed

+154
-156
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ jobs:
2424
- name: Build
2525
run: cargo build --verbose
2626
- name: Run tests
27-
run: cargo test --verbose
27+
run: cargo test --verbose --no-fail-fast
2828
- name: Run benchmark test
2929
# Run the msm benchmark, just to ensure it isn't broken.
3030
run: cargo bench --bench msm -- --quick
3131

3232
no-std-check:
3333
runs-on: ubuntu-latest
34-
34+
3535
steps:
3636
- uses: actions/checkout@v3
3737
- name: Install Rust toolchain
@@ -76,4 +76,4 @@ jobs:
7676
- name: Build no_std (nightly)
7777
run: cargo +${{ matrix.toolchain }} build --no-default-features --verbose
7878
- name: Run tests (nightly)
79-
run: cargo +${{ matrix.toolchain }} test --all-features --verbose
79+
run: cargo +${{ matrix.toolchain }} test --all-features --verbose --no-fail-fast

src/codec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Encoding and decoding utilities for Fiat-Shamir and group operations.
22
33
use crate::duplex_sponge::DuplexSpongeInterface;
4-
pub use crate::duplex_sponge::{keccak::KeccakDuplexSponge, shake::ShakeDuplexSponge};
4+
use crate::duplex_sponge::{keccak::KeccakDuplexSponge, shake::ShakeDuplexSponge};
55
use alloc::vec;
66
use ff::PrimeField;
77
use group::prime::PrimeGroup;

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ extern crate alloc;
7272
pub mod codec;
7373
pub mod composition;
7474
pub mod errors;
75+
pub mod group;
7576
pub mod linear_relation;
7677
pub mod traits;
7778

7879
pub(crate) mod duplex_sponge;
7980
pub(crate) mod fiat_shamir;
80-
pub(crate) mod group;
8181
pub(crate) mod schnorr_protocol;
8282

83-
#[cfg(test)]
84-
pub mod tests;
85-
83+
pub use duplex_sponge::{
84+
keccak::KeccakDuplexSponge, shake::ShakeDuplexSponge, DuplexSpongeInterface,
85+
};
8686
pub use fiat_shamir::Nizk;
8787
pub use group::msm::VariableMultiScalarMul;
8888
pub use linear_relation::LinearRelation;

src/tests/mod.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/tests/spec/mod.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/tests/test_relations.rs renamed to tests/relations/mod.rs

Lines changed: 7 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use ff::Field;
22
use group::prime::PrimeGroup;
33
use rand::RngCore;
44

5-
use crate::codec::Shake128DuplexSponge;
6-
use crate::fiat_shamir::Nizk;
7-
use crate::linear_relation::{CanonicalLinearRelation, LinearRelation, Sum};
5+
use sigma_proofs::linear_relation::{CanonicalLinearRelation, LinearRelation, Sum};
86

97
/// LinearMap for knowledge of a discrete logarithm relative to a fixed basepoint.
108
#[allow(non_snake_case)]
@@ -351,7 +349,7 @@ pub fn weird_linear_combination<G: PrimeGroup, R: RngCore>(
351349
(instance, witness)
352350
}
353351

354-
fn simple_subtractions<G: PrimeGroup, R: RngCore>(
352+
pub fn simple_subtractions<G: PrimeGroup, R: RngCore>(
355353
mut rng: &mut R,
356354
) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) {
357355
let x = G::Scalar::random(&mut rng);
@@ -370,7 +368,7 @@ fn simple_subtractions<G: PrimeGroup, R: RngCore>(
370368
(instance, witness)
371369
}
372370

373-
fn subtractions_with_shift<G: PrimeGroup, R: RngCore>(
371+
pub fn subtractions_with_shift<G: PrimeGroup, R: RngCore>(
374372
rng: &mut R,
375373
) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) {
376374
let B = G::generator();
@@ -390,7 +388,7 @@ fn subtractions_with_shift<G: PrimeGroup, R: RngCore>(
390388
}
391389

392390
#[allow(non_snake_case)]
393-
fn cmz_wallet_spend_relation<G: PrimeGroup, R: RngCore>(
391+
pub fn cmz_wallet_spend_relation<G: PrimeGroup, R: RngCore>(
394392
mut rng: &mut R,
395393
) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) {
396394
// Simulate the wallet spend relation from cmz
@@ -435,7 +433,7 @@ fn cmz_wallet_spend_relation<G: PrimeGroup, R: RngCore>(
435433
(instance, witness)
436434
}
437435

438-
fn nested_affine_relation<G: PrimeGroup, R: RngCore>(
436+
pub fn nested_affine_relation<G: PrimeGroup, R: RngCore>(
439437
mut rng: &mut R,
440438
) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) {
441439
let mut instance = LinearRelation::<G>::new();
@@ -459,7 +457,7 @@ fn nested_affine_relation<G: PrimeGroup, R: RngCore>(
459457
(instance, witness)
460458
}
461459

462-
fn pedersen_commitment_equality<G: PrimeGroup, R: RngCore>(
460+
pub fn pedersen_commitment_equality<G: PrimeGroup, R: RngCore>(
463461
rng: &mut R,
464462
) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) {
465463
let mut instance = LinearRelation::new();
@@ -482,7 +480,7 @@ fn pedersen_commitment_equality<G: PrimeGroup, R: RngCore>(
482480
(instance.canonical().unwrap(), witness)
483481
}
484482

485-
fn elgamal_subtraction<G: PrimeGroup, R: RngCore>(
483+
pub fn elgamal_subtraction<G: PrimeGroup, R: RngCore>(
486484
rng: &mut R,
487485
) -> (CanonicalLinearRelation<G>, Vec<G::Scalar>) {
488486
let mut instance = LinearRelation::new();
@@ -509,105 +507,3 @@ fn elgamal_subtraction<G: PrimeGroup, R: RngCore>(
509507

510508
(instance.canonical().unwrap(), witness)
511509
}
512-
513-
#[test]
514-
fn test_cmz_wallet_with_fee() {
515-
use group::Group;
516-
type G = bls12_381::G1Projective;
517-
518-
let mut rng = rand::thread_rng();
519-
520-
// This version should fail with InvalidInstanceWitnessPair
521-
// because it uses a scalar constant directly in the equation
522-
let P_W = G::random(&mut rng);
523-
let A = G::random(&mut rng);
524-
525-
let n_balance = <G as Group>::Scalar::random(&mut rng);
526-
let i_price = <G as Group>::Scalar::random(&mut rng);
527-
let _fee = <G as Group>::Scalar::from(5u64);
528-
let z_w_balance = <G as Group>::Scalar::random(&mut rng);
529-
530-
let mut relation = LinearRelation::<G>::new();
531-
532-
let var_n_balance = relation.allocate_scalar();
533-
let var_i_price = relation.allocate_scalar();
534-
let var_z_w_balance = relation.allocate_scalar();
535-
536-
let var_P_W = relation.allocate_element();
537-
let var_A = relation.allocate_element();
538-
539-
// This equation has a scalar constant (fee) which causes the error
540-
let _var_C = relation.allocate_eq(
541-
(var_n_balance + var_i_price + <G as Group>::Scalar::from(5)) * var_P_W
542-
+ var_z_w_balance * var_A,
543-
);
544-
545-
relation.set_elements([(var_P_W, P_W), (var_A, A)]);
546-
relation
547-
.compute_image(&[n_balance, i_price, z_w_balance])
548-
.unwrap();
549-
550-
// Try to convert to CanonicalLinearRelation - this should fail
551-
let nizk = relation.into_nizk(b"session_identifier").unwrap();
552-
let result = nizk.prove_batchable(&vec![n_balance, i_price, z_w_balance], &mut rng);
553-
assert!(result.is_ok());
554-
let proof = result.unwrap();
555-
let verify_result = nizk.verify_batchable(&proof);
556-
assert!(verify_result.is_ok());
557-
}
558-
559-
/// Generic helper function to test both relation correctness and NIZK functionality
560-
#[test]
561-
fn test_relations() {
562-
type G = bls12_381::G1Projective;
563-
564-
let instance_generators: Vec<(_, &'static dyn Fn(&mut _) -> _)> = vec![
565-
("dlog", &discrete_logarithm),
566-
("shifted_dlog", &shifted_dlog),
567-
("dleq", &dleq),
568-
("shifted_dleq", &shifted_dleq),
569-
("pedersen_commitment", &pedersen_commitment),
570-
("twisted_pedersen_commitment", &twisted_pedersen_commitment),
571-
("pedersen_commitment_dleq", &pedersen_commitment_equality),
572-
("bbs_blind_commitment", &bbs_blind_commitment),
573-
("test_range", &test_range),
574-
("weird_linear_combination", &weird_linear_combination),
575-
("simple_subtractions", &simple_subtractions),
576-
("subtractions_with_shift", &subtractions_with_shift),
577-
("cmz_wallet_spend_relation", &cmz_wallet_spend_relation),
578-
("nested_affine_relation", &nested_affine_relation),
579-
("elgamal_public_subtract", &elgamal_subtraction),
580-
];
581-
582-
for (relation_name, relation_sampler) in instance_generators.iter() {
583-
let mut rng = rand::thread_rng();
584-
let (canonical_relation, witness) = relation_sampler(&mut rng);
585-
586-
// Test the NIZK protocol
587-
let domain_sep = format!("test-fiat-shamir-{relation_name}")
588-
.as_bytes()
589-
.to_vec();
590-
let nizk = Nizk::<CanonicalLinearRelation<G>, Shake128DuplexSponge<G>>::new(
591-
&domain_sep,
592-
canonical_relation,
593-
);
594-
595-
// Test both proof types
596-
let proof_batchable = nizk
597-
.prove_batchable(&witness, &mut rng)
598-
.unwrap_or_else(|_| panic!("Failed to create batchable proof for {relation_name}"));
599-
let proof_compact = nizk
600-
.prove_compact(&witness, &mut rng)
601-
.unwrap_or_else(|_| panic!("Failed to create compact proof for {relation_name}"));
602-
603-
// Verify both proof types
604-
assert!(
605-
nizk.verify_batchable(&proof_batchable).is_ok(),
606-
"Batchable proof verification failed for {relation_name}"
607-
);
608-
assert!(
609-
nizk.verify_compact(&proof_compact).is_ok(),
610-
"Compact proof verification failed for {relation_name}"
611-
);
612-
}
613-
}

src/tests/spec/bls12_381.rs renamed to tests/spec/bls12_381.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use num_bigint::BigUint;
66
use rand::{CryptoRng, Rng};
77
use subtle::CtOption;
88

9-
use crate::tests::spec::random::{SInput, SRandom};
9+
use crate::spec::random::{SInput, SRandom};
1010

1111
impl SInput for G1Projective {
1212
fn scalar_from_hex_be(hex_str: &str) -> Option<Self::Scalar> {

src/tests/spec/custom_schnorr_protocol.rs renamed to tests/spec/custom_schnorr_protocol.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use group::prime::PrimeGroup;
22
use rand::{CryptoRng, Rng};
33

4-
use crate::errors::Error;
5-
use crate::linear_relation::{CanonicalLinearRelation, LinearRelation};
6-
use crate::tests::spec::random::SRandom;
7-
use crate::traits::{SigmaProtocol, SigmaProtocolSimulator};
4+
use crate::spec::random::SRandom;
5+
use sigma_proofs::errors::Error;
6+
use sigma_proofs::linear_relation::{CanonicalLinearRelation, LinearRelation};
7+
use sigma_proofs::traits::{SigmaProtocol, SigmaProtocolSimulator};
88

99
pub struct DeterministicSchnorrProof<G: PrimeGroup>(pub CanonicalLinearRelation<G>);
1010

tests/spec/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod bls12_381;
2+
pub(crate) mod custom_schnorr_protocol;
3+
mod random;
4+
pub(crate) mod rng;
File renamed without changes.

0 commit comments

Comments
 (0)