Skip to content

Commit 8fc6214

Browse files
authored
Simplify (#906)
* simplify * Simplify * More * Misc fixes + simplify FS impl * clippy
1 parent 9ec3945 commit 8fc6214

File tree

5 files changed

+37
-59
lines changed

5 files changed

+37
-59
lines changed

fastcrypto-tbls/src/threshold_schnorr/avss.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,10 @@ impl Receiver {
312312
&self.random_oracle(),
313313
|shares: &SharesForNode| shares.verify(message),
314314
)?;
315-
Ok(ComplaintResponse::create(
316-
self.id,
317-
my_output.my_shares.clone(),
318-
))
315+
Ok(ComplaintResponse {
316+
responder_id: self.id,
317+
shares: my_output.my_shares.clone(),
318+
})
319319
}
320320

321321
/// 5. Upon receiving t valid responses to a complaint, the accuser can recover its shares.

fastcrypto-tbls/src/threshold_schnorr/batch_avss.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<const BATCH_SIZE: usize> ShareBatch<BATCH_SIZE> {
108108
.iter()
109109
.zip(challenge)
110110
.fold(self.blinding_share, |acc, (r_l, gamma_l)| {
111-
acc + (*r_l * gamma_l)
111+
acc + r_l * gamma_l
112112
})
113113
!= message.response_polynomial.eval(self.index).value
114114
{
@@ -407,10 +407,10 @@ impl Receiver {
407407
&self.random_oracle(),
408408
|shares: &SharesForNode<BATCH_SIZE>| shares.verify(&self.random_oracle(), message),
409409
)?;
410-
Ok(ComplaintResponse::create(
411-
self.id,
412-
my_output.my_shares.clone(),
413-
))
410+
Ok(ComplaintResponse {
411+
responder_id: self.id,
412+
shares: my_output.my_shares.clone(),
413+
})
414414
}
415415

416416
/// 5. Upon receiving t valid responses to a complaint, the accuser can recover its shares.

fastcrypto-tbls/src/threshold_schnorr/complaint.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,26 @@ impl Complaint {
4040
self.accuser_id as usize,
4141
)?;
4242

43-
let shares = match S::from_bytes(&buffer) {
44-
Ok(s) => s,
45-
Err(_) => {
46-
debug!(
47-
"Complaint by party {} is valid: C complaint failed to deserialize shares",
48-
self.accuser_id
49-
);
50-
return Ok(());
51-
}
43+
let Ok(shares) = S::from_bytes(&buffer) else {
44+
debug!(
45+
"Complaint by party {} is valid: Failed to deserialize shares",
46+
self.accuser_id
47+
);
48+
return Ok(());
5249
};
5350

54-
match verifier(&shares) {
55-
Ok(_) => {
56-
debug!(
57-
"Complaint by party {} is invalid: Shares verify correctly",
58-
self.accuser_id
59-
);
60-
Err(InvalidProof)
61-
}
62-
Err(_) => {
63-
debug!(
64-
"Complaint by party {} is valid: Shares do not verify correctly",
65-
self.accuser_id
66-
);
67-
Ok(())
68-
}
51+
if verifier(&shares).is_ok() {
52+
debug!(
53+
"Complaint by party {} is invalid: Shares verify correctly",
54+
self.accuser_id
55+
);
56+
Err(InvalidProof)
57+
} else {
58+
debug!(
59+
"Complaint by party {} is valid: Shares do not verify correctly",
60+
self.accuser_id
61+
);
62+
Ok(())
6963
}
7064
}
7165

@@ -93,12 +87,3 @@ pub struct ComplaintResponse<S> {
9387
pub(crate) responder_id: PartyId,
9488
pub(crate) shares: S,
9589
}
96-
97-
impl<S> ComplaintResponse<S> {
98-
pub(crate) fn create(responder_id: PartyId, shares: S) -> Self {
99-
ComplaintResponse {
100-
responder_id,
101-
shares,
102-
}
103-
}
104-
}

fastcrypto-tbls/src/threshold_schnorr/pascal_matrix.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<C: GroupElement> LazyPascalMatrixMultiplier<C> {
6767
/// Create a new lazy Pascal matrix iterator that will yield `height * columns.len()` elements.
6868
/// Panics if
6969
/// * `columns` is empty,
70-
/// * if the columns are not all of the same length which is at least `height`,
70+
/// * if the columns are not all of the same length that is at least `height`,
7171
/// * if `height` is zero.
7272
pub fn new(height: usize, columns: Vec<Vec<C>>) -> Self {
7373
assert!(!columns.is_empty());
@@ -93,18 +93,12 @@ impl<C: GroupElement> Iterator for LazyPascalMatrixMultiplier<C> {
9393
type Item = C;
9494

9595
fn next(&mut self) -> Option<Self::Item> {
96-
match self.current_vector.next() {
97-
Some(v) => Some(v),
98-
None => {
99-
if self.buffers.is_empty() {
100-
None
101-
} else {
102-
self.current_vector =
103-
LazyPascalVectorMultiplier::new(self.height, self.buffers.pop().unwrap());
104-
self.current_vector.next()
105-
}
106-
}
107-
}
96+
self.current_vector.next().or_else(|| {
97+
self.buffers.pop().and_then(|v| {
98+
self.current_vector = LazyPascalVectorMultiplier::new(self.height, v);
99+
self.next()
100+
})
101+
})
108102
}
109103

110104
fn size_hint(&self) -> (usize, Option<usize>) {

fastcrypto/src/groups/secp256k1.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::error::{FastCryptoError, FastCryptoResult};
77
use crate::groups::{
88
Doubling, FiatShamirChallenge, GroupElement, MultiScalarMul, Scalar as ScalarTrait,
99
};
10-
use crate::hash::{HashFunction, Sha3_512};
1110
use crate::serde_helpers::ToFromByteArray;
1211
use crate::serialize_deserialize_with_to_from_byte_array;
1312
use crate::traits::AllowedRng;
@@ -257,9 +256,9 @@ impl ToFromByteArray<SCALAR_SIZE_IN_BYTES> for Scalar {
257256

258257
impl FiatShamirChallenge for Scalar {
259258
fn fiat_shamir_reduction_to_group_element(uniform_buffer: &[u8]) -> Self {
260-
Scalar::from(Fr::from_be_bytes_mod_order(
261-
&Sha3_512::digest(uniform_buffer).digest,
262-
))
259+
// Ensure that we have enough bytes to avoid bias in the modular reduction.
260+
assert!(uniform_buffer.len() >= 48);
261+
Scalar::from(Fr::from_be_bytes_mod_order(uniform_buffer))
263262
}
264263
}
265264

0 commit comments

Comments
 (0)