Skip to content

Commit c7d7924

Browse files
Improve arithmetic checks and update verifier API (#63)
This PR checks for an arithmetic side effect [lint](https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects), which can help prevent unintended behavior. It also updates the API such that proof verification now returns `Result<(), ProofError>` instead of `bool`. This simplifies the implementation and seems like a safer design. BREAKING CHANGE: Updates the API relating to proof verification.
1 parent eb7d198 commit c7d7924

File tree

8 files changed

+79
-73
lines changed

8 files changed

+79
-73
lines changed

benches/triptych.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const BATCH_SIZES: [usize; 1] = [2];
2929

3030
// Generate a batch of witnesses, statements, and transcripts
3131
#[allow(non_snake_case)]
32+
#[allow(clippy::arithmetic_side_effects)]
3233
fn generate_data<R: CryptoRngCore>(
3334
params: &Arc<Parameters>,
3435
b: usize,
@@ -166,7 +167,7 @@ fn verify_proof(c: &mut Criterion) {
166167
|| transcripts[0].clone(),
167168
|t| {
168169
// Verify the proof
169-
assert!(proof.verify(&statements[0], t));
170+
assert!(proof.verify(&statements[0], t).is_ok());
170171
},
171172
BatchSize::SmallInput,
172173
)
@@ -209,7 +210,7 @@ fn verify_batch_proof(c: &mut Criterion) {
209210
|| transcripts.clone(),
210211
|t| {
211212
// Verify the proofs in a batch
212-
assert!(Proof::verify_batch(&statements, &proofs, t));
213+
assert!(Proof::verify_batch(&statements, &proofs, t).is_ok());
213214
},
214215
BatchSize::SmallInput,
215216
)

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arithmetic-side-effects-allowed = [ "curve25519_dalek::Scalar", "curve25519_dalek::RistrettoPoint" ]

lints.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ deny = [
4343
'clippy::cast_possible_truncation',
4444
'clippy::cast_possible_wrap',
4545
'clippy::cast_precision-loss',
46-
'clippy::cast_sign_loss'
46+
'clippy::cast_sign_loss',
47+
48+
# Mathematical mistakes
49+
'clippy::arithmetic_side_effects',
4750
]
4851

4952
warn = [

src/gray.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@ impl GrayIterator {
5454
// Get a base-`N` decomposition
5555
let mut base_N = Vec::with_capacity(M as usize);
5656
for _ in 0..M {
57-
base_N.push(v % N);
58-
v /= N;
57+
// These are always defined since `N > 0`
58+
base_N.push(v.checked_rem(N)?);
59+
v = v.checked_div(N)?;
5960
}
6061

6162
// Now get the Gray decomposition from the base-`N` decomposition
6263
let mut shift = 0;
6364
let mut digits = vec![0; M as usize];
6465

6566
for i in (0..M).rev() {
66-
digits[i as usize] = (base_N[i as usize] + shift) % N;
67-
shift = shift + N - digits[i as usize];
67+
digits[i as usize] = (base_N[i as usize].checked_add(shift)?).checked_rem(N)?;
68+
shift = shift.checked_add(N)?.checked_sub(digits[i as usize])?;
6869
}
6970

7071
Some(digits)
@@ -128,7 +129,7 @@ impl Iterator for GrayIterator {
128129
#[allow(non_snake_case)]
129130
fn next(&mut self) -> Option<Self::Item> {
130131
if self.i == 0 {
131-
self.i += 1;
132+
self.i = 1;
132133
return Some((0, 0, 0));
133134
}
134135

@@ -150,7 +151,7 @@ impl Iterator for GrayIterator {
150151
let new = next[index];
151152

152153
// Update the state
153-
self.i += 1;
154+
self.i = self.i.checked_add(1)?;
154155
self.last = next;
155156

156157
Some((index, old, new))

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
//! let proof = Proof::prove(&witness, &statement, &mut transcript.clone()).unwrap();
104104
//!
105105
//! // The proof should verify against the same statement and transcript
106-
//! assert!(proof.verify(&statement, &mut transcript));
106+
//! assert!(proof.verify(&statement, &mut transcript).is_ok());
107107
//! # }
108108
//! ```
109109

src/parameters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Parameters {
100100
hasher.update(&m.to_le_bytes());
101101
let mut hasher_xof = hasher.finalize_xof();
102102
let mut CommitmentG_bytes = [0u8; 64];
103-
let CommitmentG = (0..n * m)
103+
let CommitmentG = (0..n.checked_mul(m).ok_or(ParameterError::InvalidParameter)?)
104104
.map(|_| {
105105
hasher_xof.fill(&mut CommitmentG_bytes);
106106
RistrettoPoint::from_uniform_bytes(&CommitmentG_bytes)

0 commit comments

Comments
 (0)