Skip to content

Commit e3221fa

Browse files
armfazhmmaker
andauthored
tests: refactoring test directory (#100)
Co-authored-by: Michele Orrù <[email protected]>
1 parent 8945b33 commit e3221fa

23 files changed

+258
-249
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: 17 additions & 7 deletions
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;
@@ -23,7 +23,11 @@ pub trait Codec {
2323
type Challenge;
2424

2525
/// Generates an empty codec that can be identified by a domain separator.
26-
fn new(protocol_identifier: &[u8], session_identifier: &[u8], instance_label: &[u8]) -> Self;
26+
fn new(
27+
protocol_identifier: &[u8; 64],
28+
session_identifier: &[u8],
29+
instance_label: &[u8],
30+
) -> Self;
2731

2832
/// Allows for precomputed initialization of the codec with a specific IV.
2933
fn from_iv(iv: [u8; 64]) -> Self;
@@ -65,12 +69,11 @@ fn length_to_bytes(x: usize) -> [u8; WORD_SIZE] {
6569
/// This function computes a deterministic IV from the protocol identifier,
6670
/// session identifier, and instance label using the specified duplex sponge.
6771
pub fn compute_iv<H: DuplexSpongeInterface>(
68-
protocol_id: &[u8],
72+
protocol_id: &[u8; 64],
6973
session_id: &[u8],
7074
instance_label: &[u8],
7175
) -> [u8; 64] {
7276
let mut tmp = H::new([0u8; 64]);
73-
tmp.absorb(&length_to_bytes(protocol_id.len()));
7477
tmp.absorb(protocol_id);
7578
tmp.absorb(&length_to_bytes(session_id.len()));
7679
tmp.absorb(session_id);
@@ -86,9 +89,16 @@ where
8689
{
8790
type Challenge = G::Scalar;
8891

89-
fn new(protocol_id: &[u8], session_id: &[u8], instance_label: &[u8]) -> Self {
90-
let iv = compute_iv::<H>(protocol_id, session_id, instance_label);
91-
Self::from_iv(iv)
92+
fn new(protocol_id: &[u8; 64], session_id: &[u8], instance_label: &[u8]) -> Self {
93+
let mut hasher = H::new(*protocol_id);
94+
hasher.absorb(&length_to_bytes(session_id.len()));
95+
hasher.absorb(session_id);
96+
hasher.absorb(&length_to_bytes(instance_label.len()));
97+
hasher.absorb(instance_label);
98+
Self {
99+
hasher,
100+
_marker: core::marker::PhantomData,
101+
}
92102
}
93103

94104
fn from_iv(iv: [u8; 64]) -> Self {

src/composition.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ impl<G: PrimeGroup + ConstantTimeEq + ConditionallySelectable> SigmaProtocol
556556
}
557557
}
558558

559-
fn protocol_identifier(&self) -> impl AsRef<[u8]> {
559+
fn protocol_identifier(&self) -> [u8; 64] {
560560
let mut hasher = Sha3_256::new();
561561

562562
match self {
@@ -569,19 +569,21 @@ impl<G: PrimeGroup + ConstantTimeEq + ConditionallySelectable> SigmaProtocol
569569
let mut hasher = Sha3_256::new();
570570
hasher.update([1u8; 32]);
571571
for p in protocols {
572-
hasher.update(p.protocol_identifier());
572+
hasher.update(p.protocol_identifier().as_ref());
573573
}
574574
}
575575
ComposedRelation::Or(protocols) => {
576576
let mut hasher = Sha3_256::new();
577577
hasher.update([2u8; 32]);
578578
for p in protocols {
579-
hasher.update(p.protocol_identifier());
579+
hasher.update(p.protocol_identifier().as_ref());
580580
}
581581
}
582582
}
583583

584-
hasher.finalize()
584+
let mut protocol_id = [0u8; 64];
585+
protocol_id[..32].clone_from_slice(&hasher.finalize());
586+
protocol_id
585587
}
586588

587589
fn serialize_response(&self, response: &Self::Response) -> Vec<u8> {

src/fiat_shamir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ where
6868
/// A new [`Nizk`] that can generate and verify non-interactive proofs.
6969
pub fn new(session_identifier: &[u8], interactive_proof: P) -> Self {
7070
let hash_state = C::new(
71-
interactive_proof.protocol_identifier().as_ref(),
71+
&interactive_proof.protocol_identifier(),
7272
session_identifier,
7373
interactive_proof.instance_label().as_ref(),
7474
);

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/schnorr_protocol.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,11 @@ impl<G: PrimeGroup> SigmaProtocol for CanonicalLinearRelation<G> {
233233
self.label()
234234
}
235235

236-
fn protocol_identifier(&self) -> impl AsRef<[u8]> {
237-
b"draft-zkproof-fiat-shamir"
236+
fn protocol_identifier(&self) -> [u8; 64] {
237+
const PROTOCOL_ID: &[u8; 32] = b"ietf sigma proof linear relation";
238+
let mut protocol_id = [0; 64];
239+
protocol_id[..32].clone_from_slice(PROTOCOL_ID);
240+
protocol_id
238241
}
239242
}
240243

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/spec/vectors/testSigmaProtocols.json

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

src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub trait SigmaProtocol {
9898
/// Deserializes a response from bytes.
9999
fn deserialize_response(&self, data: &[u8]) -> Result<Self::Response, Error>;
100100

101-
fn protocol_identifier(&self) -> impl AsRef<[u8]>;
101+
fn protocol_identifier(&self) -> [u8; 64];
102102

103103
fn instance_label(&self) -> impl AsRef<[u8]>;
104104
}

0 commit comments

Comments
 (0)