Skip to content

Commit 3bcc578

Browse files
author
Simon Rastikian
committed
Cargo fmt
1 parent 8f0d46f commit 3bcc578

31 files changed

+501
-626
lines changed

src/crypto/commit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use sha2::{Digest, Sha256};
2-
use serde::{Deserialize, Serialize};
31
use rand_core::CryptoRngCore;
2+
use serde::{Deserialize, Serialize};
3+
use sha2::{Digest, Sha256};
44

55
use crate::serde::encode_writer;
66

src/crypto/hash.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ pub fn domain_separate_hash<T: Serialize>(domain_separator: u32, data: &T) -> Ha
3131
hash(&preimage)
3232
}
3333

34-
35-
3634
#[cfg(test)]
3735
mod test_scalar_hash {
3836
use elliptic_curve::{ops::Reduce, Curve, CurveArithmetic};

src/crypto/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
pub mod commit;
21
pub mod ciphersuite;
2+
pub mod commit;
33
pub mod hash;
44
pub mod polynomials;
5+
pub mod proofs;
56
pub mod random;
6-
pub mod proofs;

src/crypto/polynomials.rs

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
use rand_core::CryptoRngCore;
21
use frost_core::{
3-
Scalar,
4-
Group, Field,
5-
keys::CoefficientCommitment,
6-
serialization::SerializableScalar,
2+
keys::CoefficientCommitment, serialization::SerializableScalar, Field, Group, Scalar,
73
};
4+
use rand_core::CryptoRngCore;
85

96
use super::ciphersuite::Ciphersuite;
107
use crate::{
11-
protocol::{Participant, ProtocolError},
128
participants::ParticipantMap,
9+
protocol::{Participant, ProtocolError},
1310
};
1411

1512
use std::ops::Add;
1613

17-
pub struct Polynomial<C:Ciphersuite>(Vec<Scalar<C>>);
14+
pub struct Polynomial<C: Ciphersuite>(Vec<Scalar<C>>);
1815

19-
impl <C: Ciphersuite> Polynomial<C>{
16+
impl<C: Ciphersuite> Polynomial<C> {
2017
/// Constructs the polynomial out of scalars
2118
/// The first scalar (coefficients[0]) is the constant term
2219
pub fn new(coefficients: Vec<Scalar<C>>) -> Self {
@@ -28,7 +25,7 @@ impl <C: Ciphersuite> Polynomial<C>{
2825
}
2926

3027
/// Outputs the degree of the polynomial
31-
pub fn degree(&self) -> usize{
28+
pub fn degree(&self) -> usize {
3229
let mut degree = self.0.len();
3330
// loop as long as the higher terms are zero
3431
while degree > 0 && self.0[degree - 1] == <C::Group as Group>::Field::zero() {
@@ -49,7 +46,7 @@ impl <C: Ciphersuite> Polynomial<C>{
4946
degree: usize,
5047
rng: &mut impl CryptoRngCore,
5148
) -> Self {
52-
let poly_size = degree+1;
49+
let poly_size = degree + 1;
5350
let mut coefficients = Vec::with_capacity(poly_size);
5451
// insert the secret share if exists
5552
let secret = secret.unwrap_or_else(|| <C::Group as Group>::Field::random(rng));
@@ -71,11 +68,8 @@ impl <C: Ciphersuite> Polynomial<C>{
7168
/// at the point using Horner's method.
7269
/// Implements [`polynomial_evaluate`] from the spec:
7370
/// https://datatracker.ietf.org/doc/html/rfc9591#name-additional-polynomial-opera
74-
pub fn eval_on_point(
75-
&self,
76-
point: Scalar<C>,
77-
) -> SerializableScalar<C> {
78-
if point == <C::Group as Group>::Field::zero(){
71+
pub fn eval_on_point(&self, point: Scalar<C>) -> SerializableScalar<C> {
72+
if point == <C::Group as Group>::Field::zero() {
7973
self.eval_on_zero()
8074
} else {
8175
let mut value = <<C::Group as Group>::Field>::zero();
@@ -84,18 +78,16 @@ impl <C: Ciphersuite> Polynomial<C>{
8478
value = value * point;
8579
}
8680
value = value
87-
+ *self.0
81+
+ *self
82+
.0
8883
.first()
8984
.expect("coefficients must have at least one element");
9085
SerializableScalar(value)
9186
}
9287
}
9388

9489
/// Evaluates a polynomial on the identifier of a participant
95-
pub fn eval_on_participant(
96-
&self,
97-
participant: Participant,
98-
) -> SerializableScalar<C> {
90+
pub fn eval_on_participant(&self, participant: Participant) -> SerializableScalar<C> {
9991
let id = participant.scalar::<C>();
10092
self.eval_on_point(id)
10193
}
@@ -111,7 +103,7 @@ impl <C: Ciphersuite> Polynomial<C>{
111103
let eval = poly.eval_on_participant(participant);
112104
result_vec.push(eval);
113105
}
114-
match result_vec.try_into(){
106+
match result_vec.try_into() {
115107
Ok(arr) => arr,
116108
Err(_) => panic!("Internal error: Vec did not match expected array size"),
117109
}
@@ -122,21 +114,21 @@ impl <C: Ciphersuite> Polynomial<C>{
122114
pub fn eval_interpolation(
123115
signingshares_map: &ParticipantMap<'_, SerializableScalar<C>>,
124116
point: Option<&Scalar<C>>,
125-
)-> Result<SerializableScalar<C>, ProtocolError>{
117+
) -> Result<SerializableScalar<C>, ProtocolError> {
126118
let mut interpolation = <<C::Group as Group>::Field>::zero();
127-
let identifiers: Vec<Scalar<C>> = signingshares_map
128-
.participants()
129-
.iter()
130-
.map(|p| p.scalar::<C>())
131-
.collect();
132-
let shares = signingshares_map.into_refs_or_none()
133-
.ok_or(ProtocolError::InvalidInterpolationArguments)?;
119+
let identifiers: Vec<Scalar<C>> = signingshares_map
120+
.participants()
121+
.iter()
122+
.map(|p| p.scalar::<C>())
123+
.collect();
124+
let shares = signingshares_map
125+
.into_refs_or_none()
126+
.ok_or(ProtocolError::InvalidInterpolationArguments)?;
134127

135128
// Compute the Lagrange coefficients
136129
for (id, share) in identifiers.iter().zip(shares) {
137130
// would raise error if not enough shares or identifiers
138-
let lagrange_coefficient =
139-
compute_lagrange_coefficient::<C>(&identifiers, id, point)?;
131+
let lagrange_coefficient = compute_lagrange_coefficient::<C>(&identifiers, id, point)?;
140132

141133
// Compute y = f(point) via polynomial interpolation of these points of f
142134
interpolation = interpolation + (lagrange_coefficient.0 * share.0);
@@ -147,13 +139,13 @@ impl <C: Ciphersuite> Polynomial<C>{
147139

148140
/// Commits to a polynomial returning a sequence of group coefficients
149141
/// Creates a commitment vector of coefficients * G
150-
pub fn commit_polynomial(
151-
&self,
152-
) -> PolynomialCommitment<C> {
142+
pub fn commit_polynomial(&self) -> PolynomialCommitment<C> {
153143
// Computes the multiplication of every coefficient of p with the generator G
154-
let coef_commitment = self.0.iter().map(
155-
|c| CoefficientCommitment::new(<C::Group as Group>::generator() * *c)
156-
).collect();
144+
let coef_commitment = self
145+
.0
146+
.iter()
147+
.map(|c| CoefficientCommitment::new(<C::Group as Group>::generator() * *c))
148+
.collect();
157149
PolynomialCommitment::new(coef_commitment)
158150
}
159151

@@ -169,24 +161,21 @@ impl <C: Ciphersuite> Polynomial<C>{
169161
/// Extends the Polynomial with an extra value as a constant
170162
/// Used usually after sending a smaller polynomial to prevent serialization from
171163
/// failing if the constant term is the identity
172-
pub fn extend_with_zero(&self) -> Self{
164+
pub fn extend_with_zero(&self) -> Self {
173165
let mut coeffcommitment = vec![<C::Group as Group>::Field::zero()];
174166
coeffcommitment.extend(self.get_coefficients());
175167
Polynomial::new(coeffcommitment)
176168
}
177-
178169
}
179170

180-
181-
182171
/******************* Polynomial Commitment *******************/
183172
/// Contains the commited coefficients of a polynomial i.e. coeff * G
184173
#[derive(Clone, serde::Serialize, serde::Deserialize)]
185174
#[serde(bound = "C: Ciphersuite")]
186-
pub struct PolynomialCommitment<C:Ciphersuite>(Vec<CoefficientCommitment<C>>);
175+
pub struct PolynomialCommitment<C: Ciphersuite>(Vec<CoefficientCommitment<C>>);
187176

188-
impl <C: Ciphersuite> PolynomialCommitment<C>{
189-
pub fn new(coefcommitments: Vec<CoefficientCommitment<C>>)-> Self{
177+
impl<C: Ciphersuite> PolynomialCommitment<C> {
178+
pub fn new(coefcommitments: Vec<CoefficientCommitment<C>>) -> Self {
190179
PolynomialCommitment(coefcommitments)
191180
}
192181

@@ -196,7 +185,7 @@ impl <C: Ciphersuite> PolynomialCommitment<C>{
196185
}
197186

198187
/// Outputs the degree of the commited polynomial
199-
pub fn degree(&self) -> usize{
188+
pub fn degree(&self) -> usize {
200189
let mut degree = self.0.len();
201190
// loop as long as the higher terms are zero
202191
while degree > 0 && self.0[degree - 1].value() == <C::Group as Group>::identity() {
@@ -211,11 +200,10 @@ impl <C: Ciphersuite> PolynomialCommitment<C>{
211200

212201
/// Evaluates the commited polynomial on zero
213202
/// In other words, outputs the constant term
214-
pub fn eval_on_zero(&self) -> CoefficientCommitment<C>{
203+
pub fn eval_on_zero(&self) -> CoefficientCommitment<C> {
215204
self.0[0]
216205
}
217206

218-
219207
/// Evaluates the commited polynomial at a specific value
220208
pub fn eval_on_point(&self, point: Scalar<C>) -> CoefficientCommitment<C> {
221209
let mut out = C::Group::identity();
@@ -237,17 +225,16 @@ impl <C: Ciphersuite> PolynomialCommitment<C>{
237225
identifiers: &Vec<Scalar<C>>,
238226
shares: &Vec<CoefficientCommitment<C>>,
239227
point: Option<&Scalar<C>>,
240-
) -> Result<CoefficientCommitment<C>, ProtocolError>{
228+
) -> Result<CoefficientCommitment<C>, ProtocolError> {
241229
let mut interpolation = <C::Group as Group>::identity();
242-
if identifiers.len() != shares.len(){
243-
return Err(ProtocolError::InvalidInterpolationArguments)
230+
if identifiers.len() != shares.len() {
231+
return Err(ProtocolError::InvalidInterpolationArguments);
244232
};
245233

246234
// Compute the Lagrange coefficients
247235
for (id, share) in identifiers.iter().zip(shares) {
248236
// would raise error if not enough shares or identifiers
249-
let lagrange_coefficient =
250-
compute_lagrange_coefficient::<C>(&identifiers, id, point)?;
237+
let lagrange_coefficient = compute_lagrange_coefficient::<C>(&identifiers, id, point)?;
251238

252239
// Compute y = g^f(point) via polynomial interpolation of these points of f
253240
interpolation = interpolation + (share.value() * lagrange_coefficient.0);
@@ -259,13 +246,12 @@ impl <C: Ciphersuite> PolynomialCommitment<C>{
259246
/// Extends the Commited Polynomial with an extra value as a constant
260247
/// Used usually after sending a smaller polynomial to prevent serialization from
261248
/// failing if the constant term is the identity
262-
pub fn extend_with_identity(&self) -> Self{
249+
pub fn extend_with_identity(&self) -> Self {
263250
let mut coeffcommitment = vec![CoefficientCommitment::<C>::new(C::Group::identity())];
264251
coeffcommitment.extend(self.get_coefficients());
265252
PolynomialCommitment::new(coeffcommitment)
266253
}
267254

268-
269255
/// Set the constant value of this polynomial to a new scalar
270256
pub fn set_constant(&mut self, v: CoefficientCommitment<C>) {
271257
if self.0.is_empty() {
@@ -274,7 +260,6 @@ impl <C: Ciphersuite> PolynomialCommitment<C>{
274260
self.0[0] = v
275261
}
276262
}
277-
278263
}
279264

280265
impl<C: Ciphersuite> Add for &PolynomialCommitment<C> {
@@ -303,15 +288,14 @@ pub fn compute_lagrange_coefficient<C: Ciphersuite>(
303288
let mut num = <<C::Group as Group>::Field>::one();
304289
let mut den = <<C::Group as Group>::Field>::one();
305290

306-
if points_set.len() <= 1 || !points_set.contains(i){
291+
if points_set.len() <= 1 || !points_set.contains(i) {
307292
// returns error if there is not enough points to interpolate
308293
// or if i is not in the set of points
309-
return Err(ProtocolError::InvalidInterpolationArguments)
294+
return Err(ProtocolError::InvalidInterpolationArguments);
310295
}
311296
if let Some(x) = x {
312297
for j in points_set.iter() {
313298
if *i == *j {
314-
315299
continue;
316300
}
317301
num = num * (*x - *j);
@@ -330,6 +314,6 @@ pub fn compute_lagrange_coefficient<C: Ciphersuite>(
330314

331315
// raises error if the denominator is null, i.e., the set contains duplicates
332316
let den = <<C::Group as Group>::Field>::invert(&den)
333-
.map_err(|_| ProtocolError::InvalidInterpolationArguments)?;
317+
.map_err(|_| ProtocolError::InvalidInterpolationArguments)?;
334318
Ok(SerializableScalar(num * den))
335319
}

src/crypto/proofs/dlog.rs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
use crate::{
2-
crypto::ciphersuite::{
3-
Ciphersuite,
4-
Element,
5-
},
6-
};
7-
use frost_core::{
8-
Group,
9-
Field,
10-
serialization::SerializableScalar,
11-
};
12-
13-
use super::{
14-
encode_point,
15-
strobe_transcript::Transcript,
16-
};
1+
use crate::crypto::ciphersuite::{Ciphersuite, Element};
2+
use frost_core::{serialization::SerializableScalar, Field, Group};
3+
4+
use super::{encode_point, strobe_transcript::Transcript};
175
use rand_core::CryptoRngCore;
186

197
/// The label we use for hashing the statement.
@@ -38,32 +26,32 @@ impl<'a, C: Ciphersuite> Statement<'a, C> {
3826
}
3927

4028
/// Encode into Vec<u8>: some sort of serialization
41-
fn encode(&self) -> Vec<u8>{
42-
let mut enc = Vec::new();
29+
fn encode(&self) -> Vec<u8> {
30+
let mut enc = Vec::new();
4331
enc.extend_from_slice(b"statement:");
4432

45-
match <C::Group as Group>::serialize(self.public){
33+
match <C::Group as Group>::serialize(self.public) {
4634
Ok(ser) => {
4735
enc.extend_from_slice(b"public:");
4836
enc.extend_from_slice(ser.as_ref().into());
49-
},
50-
_=> panic!("Expected non-identity element"),
37+
}
38+
_ => panic!("Expected non-identity element"),
5139
};
5240
enc
5341
}
5442
}
5543

5644
/// The private witness for this proof.
5745
/// This holds the scalar the prover needs to know.
58-
#[derive(Clone, Copy, serde::Serialize, serde::Deserialize)]
46+
#[derive(Clone, Copy, serde::Serialize, serde::Deserialize)]
5947
pub struct Witness<C: Ciphersuite> {
6048
pub x: SerializableScalar<C>,
6149
}
6250

6351
/// Represents a proof of the statement.
6452
#[derive(Clone, serde::Serialize, serde::Deserialize)]
6553
#[serde(bound = "C: Ciphersuite")]
66-
pub struct Proof<C:Ciphersuite> {
54+
pub struct Proof<C: Ciphersuite> {
6755
e: SerializableScalar<C>,
6856
s: SerializableScalar<C>,
6957
}
@@ -83,10 +71,7 @@ pub fn prove<'a, C: Ciphersuite>(
8371
let k = <C::Group as Group>::Field::random(rng);
8472
let big_k = statement.phi(&SerializableScalar(k));
8573

86-
transcript.message(
87-
COMMITMENT_LABEL,
88-
&encode_point::<C>(&big_k),
89-
);
74+
transcript.message(COMMITMENT_LABEL, &encode_point::<C>(&big_k));
9075
let mut rng = transcript.challenge_then_build_rng(CHALLENGE_LABEL);
9176
let e = <C::Group as Group>::Field::random(&mut rng);
9277

@@ -110,10 +95,7 @@ pub fn verify<C: Ciphersuite>(
11095

11196
let big_k: Element<C> = statement.phi(&proof.s) - *statement.public * proof.e.0;
11297

113-
transcript.message(
114-
COMMITMENT_LABEL,
115-
&encode_point::<C>(&big_k),
116-
);
98+
transcript.message(COMMITMENT_LABEL, &encode_point::<C>(&big_k));
11799
let mut rng = transcript.challenge_then_build_rng(CHALLENGE_LABEL);
118100
let e = <C::Group as Group>::Field::random(&mut rng);
119101

@@ -135,7 +117,9 @@ mod test {
135117
let statement = Statement::<Secp256K1Sha256> {
136118
public: &(ProjectivePoint::GENERATOR * x),
137119
};
138-
let witness = Witness { x: SerializableScalar::<Secp256K1Sha256>(x) };
120+
let witness = Witness {
121+
x: SerializableScalar::<Secp256K1Sha256>(x),
122+
};
139123

140124
let transcript = Transcript::new(b"protocol");
141125

0 commit comments

Comments
 (0)