Skip to content

Commit d3c55de

Browse files
nougzarmmmaker
andauthored
Improve usage of variables in formatting macros (#67)
Signed-off-by: Michele Orrù <[email protected]> Co-authored-by: Michele Orrù <[email protected]>
1 parent aa70587 commit d3c55de

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
lines changed

src/tests/spec/test_vectors.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ fn test_spec_testvectors() {
5151
// Verify the ciphersuite is supported
5252
assert!(
5353
supported_ciphersuites.contains_key(&vector.ciphersuite),
54-
"Unsupported ciphersuite '{}' in test vector {}",
55-
vector.ciphersuite,
56-
test_name
54+
"Unsupported ciphersuite '{}' in test vector {test_name}",
55+
vector.ciphersuite
5756
);
5857

5958
// Parse the statement from the test vector
@@ -71,8 +70,7 @@ fn test_spec_testvectors() {
7170
assert_eq!(
7271
parsed_instance.label(),
7372
vector.statement,
74-
"parsed statement doesn't match original for {}",
75-
test_name
73+
"parsed statement doesn't match original for {test_name}"
7674
);
7775

7876
// Create NIZK with the session_id from the test vector
@@ -90,8 +88,7 @@ fn test_spec_testvectors() {
9088
assert_eq!(
9189
computed_iv,
9290
vector.iv.as_slice(),
93-
"Computed IV doesn't match test vector IV for {}",
94-
test_name
91+
"Computed IV doesn't match test vector IV for {test_name}"
9592
);
9693

9794
// Generate proof with the proof generation RNG
@@ -101,70 +98,66 @@ fn test_spec_testvectors() {
10198
// Verify the proof matches
10299
assert_eq!(
103100
proof_bytes, vector.proof,
104-
"proof bytes for test vector {} do not match",
105-
test_name
101+
"proof bytes for test vector {test_name} do not match"
106102
);
107103

108104
// Verify the proof is valid
109105
let verified = nizk.verify_batchable(&proof_bytes).is_ok();
110106
assert!(
111107
verified,
112-
"Fiat-Shamir Schnorr proof verification failed for {}",
113-
test_name
108+
"Fiat-Shamir Schnorr proof verification failed for {test_name}"
114109
);
115110
}
116111
}
117112

118113
fn extract_vectors_new(path: &str) -> Result<HashMap<String, TestVector>, String> {
119114
use std::collections::HashMap;
120115

121-
let content =
122-
fs::read_to_string(path).map_err(|e| format!("Unable to read JSON file: {}", e))?;
123-
let root: JsonValue =
124-
json::parse(&content).map_err(|e| format!("JSON parsing error: {}", e))?;
116+
let content = fs::read_to_string(path).map_err(|e| format!("Unable to read JSON file: {e}"))?;
117+
let root: JsonValue = json::parse(&content).map_err(|e| format!("JSON parsing error: {e}"))?;
125118

126119
let mut vectors = HashMap::new();
127120

128121
for (name, obj) in root.entries() {
129122
let ciphersuite = obj["Ciphersuite"]
130123
.as_str()
131-
.ok_or_else(|| format!("Ciphersuite field not found for {}", name))?
124+
.ok_or_else(|| format!("Ciphersuite field not found for {name}"))?
132125
.to_string();
133126

134127
let session_id = Vec::from_hex(
135128
obj["SessionId"]
136129
.as_str()
137-
.ok_or_else(|| format!("SessionId field not found for {}", name))?,
130+
.ok_or_else(|| format!("SessionId field not found for {name}"))?,
138131
)
139-
.map_err(|e| format!("Invalid hex in SessionId for {}: {}", name, e))?;
132+
.map_err(|e| format!("Invalid hex in SessionId for {name}: {e}"))?;
140133

141134
let statement = Vec::from_hex(
142135
obj["Statement"]
143136
.as_str()
144-
.ok_or_else(|| format!("Statement field not found for {}", name))?,
137+
.ok_or_else(|| format!("Statement field not found for {name}"))?,
145138
)
146-
.map_err(|e| format!("Invalid hex in Statement for {}: {}", name, e))?;
139+
.map_err(|e| format!("Invalid hex in Statement for {name}: {e}"))?;
147140

148141
let witness = Vec::from_hex(
149142
obj["Witness"]
150143
.as_str()
151-
.ok_or_else(|| format!("Witness field not found for {}", name))?,
144+
.ok_or_else(|| format!("Witness field not found for {name}"))?,
152145
)
153-
.map_err(|e| format!("Invalid hex in Witness for {}: {}", name, e))?;
146+
.map_err(|e| format!("Invalid hex in Witness for {name}: {e}"))?;
154147

155148
let iv = Vec::from_hex(
156149
obj["IV"]
157150
.as_str()
158-
.ok_or_else(|| format!("IV field not found for {}", name))?,
151+
.ok_or_else(|| format!("IV field not found for {name}"))?,
159152
)
160-
.map_err(|e| format!("Invalid hex in IV for {}: {}", name, e))?;
153+
.map_err(|e| format!("Invalid hex in IV for {name}: {e}"))?;
161154

162155
let proof = Vec::from_hex(
163156
obj["Proof"]
164157
.as_str()
165-
.ok_or_else(|| format!("Proof field not found for {}", name))?,
158+
.ok_or_else(|| format!("Proof field not found for {name}"))?,
166159
)
167-
.map_err(|e| format!("Invalid hex in Proof for {}: {}", name, e))?;
160+
.map_err(|e| format!("Invalid hex in Proof for {name}: {e}"))?;
168161

169162
vectors.insert(
170163
name.to_string(),

src/tests/test_relations.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -482,31 +482,27 @@ fn test_relations() {
482482

483483
// Test the NIZK protocol
484484
let protocol = SchnorrProof(canonical_relation);
485-
let domain_sep = format!("test-fiat-shamir-{}", relation_name)
485+
let domain_sep = format!("test-fiat-shamir-{relation_name}")
486486
.as_bytes()
487487
.to_vec();
488488
let nizk = Nizk::<SchnorrProof<G>, Shake128DuplexSponge<G>>::new(&domain_sep, protocol);
489489

490490
// Test both proof types
491491
let proof_batchable = nizk.prove_batchable(&witness, &mut OsRng).expect(&format!(
492-
"Failed to create batchable proof for {}",
493-
relation_name
492+
"Failed to create batchable proof for {relation_name}"
494493
));
495494
let proof_compact = nizk.prove_compact(&witness, &mut OsRng).expect(&format!(
496-
"Failed to create compact proof for {}",
497-
relation_name
495+
"Failed to create compact proof for {relation_name}"
498496
));
499497

500498
// Verify both proof types
501499
assert!(
502500
nizk.verify_batchable(&proof_batchable).is_ok(),
503-
"Batchable proof verification failed for {}",
504-
relation_name
501+
"Batchable proof verification failed for {relation_name}"
505502
);
506503
assert!(
507504
nizk.verify_compact(&proof_compact).is_ok(),
508-
"Compact proof verification failed for {}",
509-
relation_name
505+
"Compact proof verification failed for {relation_name}"
510506
);
511507
}
512508
}

src/tests/test_validation_criteria.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ mod proof_validation {
249249
// Verification should fail
250250
assert!(
251251
nizk.verify_batchable(&proof).is_err(),
252-
"Proof verification should fail with bit {} flipped at position {}",
253-
bit,
254-
pos
252+
"Proof verification should fail with bit {bit} flipped at position {pos}"
255253
);
256254

257255
// Restore original byte
@@ -282,8 +280,7 @@ mod proof_validation {
282280
// Verification should fail
283281
assert!(
284282
nizk.verify_batchable(&proof).is_err(),
285-
"Proof verification should fail with {} bytes appended",
286-
size
283+
"Proof verification should fail with {size} bytes appended"
287284
);
288285

289286
// Restore original proof
@@ -311,8 +308,7 @@ mod proof_validation {
311308
// Verification should fail
312309
assert!(
313310
nizk.verify_batchable(&prepended_proof).is_err(),
314-
"Proof verification should fail with {} bytes prepended",
315-
size
311+
"Proof verification should fail with {size} bytes prepended"
316312
);
317313
}
318314
}
@@ -334,8 +330,7 @@ mod proof_validation {
334330
// Verification should fail
335331
assert!(
336332
nizk.verify_batchable(truncated_proof).is_err(),
337-
"Proof verification should fail with {} bytes truncated",
338-
size
333+
"Proof verification should fail with {size} bytes truncated"
339334
);
340335
}
341336
}
@@ -446,7 +441,7 @@ mod proof_validation {
446441
);
447442
}
448443
Err(e) => {
449-
println!("Proof generation failed as expected: {:?}", e);
444+
println!("Proof generation failed as expected: {e:?}");
450445
}
451446
}
452447
}

0 commit comments

Comments
 (0)