Skip to content

Commit 4ec00e1

Browse files
authored
refactor: morphism construction and more concise definition of statements (#12)
1 parent 608b20c commit 4ec00e1

23 files changed

+772
-822
lines changed

.github/workflows/docs.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ jobs:
3030
steps:
3131
- name: git clone
3232
uses: actions/checkout@v4
33-
- name: install toolchain
34-
uses: dtolnay/rust-toolchain@nightly
35-
- name: cargo doc
33+
- name: Install Rust toolchain
34+
uses: actions-rs/toolchain@v1
35+
with:
36+
toolchain: nightly
37+
- name: cargo +nightly doc
3638
run: RUSTDOCFLAGS="-Z unstable-options --enable-index-page" cargo doc --all-features
3739
- name: page configuration
3840
uses: actions/configure-pages@v3

.github/workflows/lint-fmt.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
- name: Install Rust toolchain
2222
uses: actions-rs/toolchain@v1
2323
with:
24-
toolchain: stable
2524
components: rustfmt
2625

2726
- name: Run cargo fmt
@@ -59,7 +58,6 @@ jobs:
5958
- uses: actions-rs/toolchain@v1
6059
with:
6160
profile: minimal
62-
toolchain: stable
6361
override: true
6462
components: clippy
6563
- uses: actions-rs/cargo@v1

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ jobs:
3939
components: rustfmt, clippy
4040

4141
- name: Build (nightly)
42-
run: cargo build --all-features --verbose
42+
run: cargo +${{ matrix.toolchain }} build --all-features --verbose
4343
- name: Run tests (nightly)
44-
run: cargo test --all-features --verbose
44+
run: cargo +${{ matrix.toolchain }} test --all-features --verbose

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ curve25519-dalek = { version = "4", default-features = false, features = ["serde
3535
hex = "0.4"
3636
json = "0.12.4"
3737
sha2 = "0.10"
38+
sigma-rs = { path = ".", features = ["test-utils"] }
3839
subtle = "2.6.1"
40+
41+
[profile.dev]
42+
# Makes tests run much faster at the cost of slightly longer builds and worse debug info.
43+
opt-level = 1
44+
45+
[features]
46+
# TODO: Remove this feature, and either move tests into `src` or the morphism definitions into a separate crate.
47+
test-utils = []

rust-toolchain

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.86

src/codec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ pub mod shake_codec;
33
pub mod r#trait;
44

55
pub use keccak_codec::{ByteSchnorrCodec, KeccakDuplexSponge};
6-
pub use r#trait::Codec;
76
pub use shake_codec::ShakeCodec;
7+
pub use r#trait::Codec;

src/codec/shake_codec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use ff::PrimeField;
1515
use group::{Group, GroupEncoding};
1616
use sha3::{
17-
digest::{ExtendableOutput, Update, XofReader},
1817
Shake128,
18+
digest::{ExtendableOutput, Update, XofReader},
1919
};
2020

2121
use crate::codec::r#trait::Codec;

src/errors.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
//! - Mismatched parameters during batch verification.
99
//! - Unimplemented methods.
1010
//! - Group element or scalar serialization failures.
11-
use thiserror::Error;
11+
12+
use crate::group_morphism::GroupVar;
13+
1214
/// An error during proving or verification, such as a verification failure.
13-
#[derive(Debug, Error)]
14-
pub enum ProofError {
15+
#[non_exhaustive]
16+
#[derive(Debug, thiserror::Error)]
17+
pub enum Error {
1518
/// Something is wrong with the proof, causing a verification failure.
1619
#[error("Verification failed.")]
1720
VerificationFailure,
@@ -21,4 +24,7 @@ pub enum ProofError {
2124
/// Serialization of a group element/scalar has failed.
2225
#[error("Serialization of a group element/scalar failed.")]
2326
GroupSerializationFailure,
27+
/// Uninitialized group element variable.
28+
#[error("Uninitialized group element variable {var:?}")]
29+
UnassignedGroupVar { var: GroupVar },
2430
}

src/fiat_shamir.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! - `G`: the group used for commitments and operations ([`Group`] trait).
1515
1616
use crate::codec::Codec;
17-
use crate::errors::ProofError;
17+
use crate::errors::Error;
1818
use crate::traits::{CompactProtocol, SigmaProtocol};
1919

2020
use group::{Group, GroupEncoding};
@@ -50,6 +50,7 @@ where
5050
pub sigmap: P,
5151
}
5252

53+
// TODO: Write a serialization of the morphism to the transcript.
5354
impl<P, C, G> NISigmaProtocol<P, C, G>
5455
where
5556
G: Group + GroupEncoding,
@@ -93,7 +94,7 @@ where
9394
&self,
9495
witness: &P::Witness,
9596
rng: &mut (impl RngCore + CryptoRng),
96-
) -> Result<Transcript<P>, ProofError> {
97+
) -> Result<Transcript<P>, Error> {
9798
let mut codec = self.hash_state.clone();
9899

99100
let (commitment, prover_state) = self.sigmap.prover_commit(witness, rng)?;
@@ -131,7 +132,7 @@ where
131132
commitment: &P::Commitment,
132133
challenge: &P::Challenge,
133134
response: &P::Response,
134-
) -> Result<(), ProofError> {
135+
) -> Result<(), Error> {
135136
let mut codec = self.hash_state.clone();
136137

137138
// Commitment data for expected challenge generation
@@ -144,7 +145,7 @@ where
144145
// Verification of the proof
145146
match *challenge == expected_challenge {
146147
true => self.sigmap.verifier(commitment, challenge, response),
147-
false => Err(ProofError::VerificationFailure),
148+
false => Err(Error::VerificationFailure),
148149
}
149150
}
150151
/// Generates a batchable, serialized non-interactive proof.
@@ -162,7 +163,7 @@ where
162163
&self,
163164
witness: &P::Witness,
164165
rng: &mut (impl RngCore + CryptoRng),
165-
) -> Result<Vec<u8>, ProofError> {
166+
) -> Result<Vec<u8>, Error> {
166167
let (commitment, challenge, response) = self.prove(witness, rng)?;
167168
Ok(self
168169
.sigmap
@@ -183,7 +184,7 @@ where
183184
/// - Returns `ProofError::VerificationFailure` if:
184185
/// - The challenge doesn't match the recomputed one from the commitment.
185186
/// - The response fails verification under the Sigma protocol.
186-
pub fn verify_batchable(&self, proof: &[u8]) -> Result<(), ProofError> {
187+
pub fn verify_batchable(&self, proof: &[u8]) -> Result<(), Error> {
187188
let (commitment, response) = self.sigmap.deserialize_batchable(proof).unwrap();
188189

189190
let mut codec = self.hash_state.clone();
@@ -223,7 +224,7 @@ where
223224
&self,
224225
witness: &P::Witness,
225226
rng: &mut (impl RngCore + CryptoRng),
226-
) -> Result<Vec<u8>, ProofError> {
227+
) -> Result<Vec<u8>, Error> {
227228
let (commitment, challenge, response) = self.prove(witness, rng)?;
228229
Ok(self
229230
.sigmap
@@ -246,7 +247,7 @@ where
246247
/// - Returns `ProofError::VerificationFailure` if:
247248
/// - Deserialization fails.
248249
/// - The recomputed commitment or response is invalid under the Sigma protocol.
249-
pub fn verify_compact(&self, proof: &[u8]) -> Result<(), ProofError> {
250+
pub fn verify_compact(&self, proof: &[u8]) -> Result<(), Error> {
250251
let (challenge, response) = self.sigmap.deserialize_compact(proof).unwrap();
251252
// Compute the commitments
252253
let commitment = self.sigmap.get_commitment(&challenge, &response)?;

0 commit comments

Comments
 (0)