1- use rand_core:: CryptoRngCore ;
21use 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
96use super :: ciphersuite:: Ciphersuite ;
107use crate :: {
11- protocol:: { Participant , ProtocolError } ,
128 participants:: ParticipantMap ,
9+ protocol:: { Participant , ProtocolError } ,
1310} ;
1411
1512use 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
280265impl < 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}
0 commit comments