Skip to content

Commit ef14fb5

Browse files
committed
Remove the redundant largrange_interpolation from compute_agg_key_from_dkg
1 parent 94224cc commit ef14fb5

File tree

3 files changed

+135
-72
lines changed

3 files changed

+135
-72
lines changed

crates/dkg/src/dkg_math.rs

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ pub fn lagrange_interpolation<C: Curve>(
206206
}
207207
// Pre-allocate vectors for batch processing
208208
let mut terms = Vec::with_capacity(k);
209+
let mut denominators = Vec::with_capacity(k);
209210

210211
for i in 0..k {
211212
let mut b = x_vec[i];
@@ -221,7 +222,13 @@ pub fn lagrange_interpolation<C: Curve>(
221222
b.mul_assign(&v);
222223
}
223224
}
224-
let li0 = a.mul(&b.invert());
225+
denominators.push(b);
226+
}
227+
228+
let inv_denominators = batch_invert::<C>(&denominators);
229+
230+
for i in 0..k {
231+
let li0 = a.mul(&inv_denominators[i]);
225232
terms.push(y_vec[i].mul_scalar(&li0));
226233
}
227234

@@ -230,10 +237,10 @@ pub fn lagrange_interpolation<C: Curve>(
230237
}
231238

232239
#[allow(clippy::assign_op_pattern)]
233-
pub fn agg_coefficients<C: Curve>(
234-
verification_vectors: &[Vec<C::Point>],
235-
ids: &[C::Scalar],
236-
) -> Vec<C::Point> {
240+
pub fn agg_coefficients<C: Curve>(verification_vectors: &[Vec<C::Point>]) -> Vec<C::Point> {
241+
if verification_vectors.is_empty() {
242+
return Vec::new();
243+
}
237244
let num_vectors = verification_vectors.len();
238245
let vector_len = verification_vectors[0].len();
239246

@@ -252,12 +259,7 @@ pub fn agg_coefficients<C: Curve>(
252259
let sum = batch_add_points::<C>(&points_to_sum);
253260
final_cfs.push(sum);
254261
}
255-
let mut final_keys = Vec::new();
256-
for id in ids.iter() {
257-
let tmp = evaluate_polynomial::<C>(&final_cfs, id);
258-
final_keys.push(tmp);
259-
}
260-
final_keys
262+
final_cfs
261263
}
262264

263265
// Optimized batch point addition function for elliptic curve operations
@@ -302,6 +304,30 @@ pub fn batch_add_points<C: Curve>(points: &[C::Point]) -> C::Point {
302304
current_points[0]
303305
}
304306

307+
fn batch_invert<C: Curve>(scalars: &[C::Scalar]) -> Vec<C::Scalar> {
308+
if scalars.is_empty() {
309+
return Vec::new();
310+
}
311+
312+
let mut products = Vec::with_capacity(scalars.len());
313+
let mut current_product = C::Scalar::from_u32(1);
314+
315+
for s in scalars {
316+
current_product = current_product.mul(s);
317+
products.push(current_product);
318+
}
319+
320+
let mut inv = products[products.len() - 1].invert();
321+
let mut result = vec![C::Scalar::from_u32(0); scalars.len()];
322+
323+
for i in (1..scalars.len()).rev() {
324+
result[i] = inv.mul(&products[i - 1]);
325+
inv = inv.mul(&scalars[i]);
326+
}
327+
result[0] = inv;
328+
result
329+
}
330+
305331
#[cfg(test)]
306332
mod tests {
307333
use crate::crypto::*;
@@ -310,6 +336,43 @@ mod tests {
310336

311337
use super::*;
312338

339+
#[test]
340+
fn test_batch_invert_basic() {
341+
let scalars = vec![
342+
BlsScalar::from_u32(2),
343+
BlsScalar::from_u32(3),
344+
BlsScalar::from_u32(4),
345+
BlsScalar::from_u32(5),
346+
];
347+
348+
let inverted = batch_invert::<BlsG1Curve>(&scalars);
349+
350+
assert_eq!(scalars.len(), inverted.len());
351+
352+
for i in 0..scalars.len() {
353+
let product = scalars[i].mul(&inverted[i]);
354+
assert_eq!(product.scalar, Scalar::one());
355+
}
356+
}
357+
358+
#[test]
359+
fn test_batch_invert_empty() {
360+
let scalars: Vec<BlsScalar> = vec![];
361+
let inverted = batch_invert::<BlsG1Curve>(&scalars);
362+
assert!(inverted.is_empty());
363+
}
364+
365+
#[test]
366+
fn test_batch_invert_single() {
367+
let scalar = BlsScalar::from_u32(42);
368+
let scalars = vec![scalar];
369+
370+
let inverted = batch_invert::<BlsG1Curve>(&scalars);
371+
372+
assert_eq!(inverted.len(), 1);
373+
assert_eq!(inverted[0].scalar, scalar.invert().scalar);
374+
}
375+
313376
#[test]
314377
fn test_verify_signature() {
315378
let data = hex::decode("2f901d5cec8722e44afd59e94d0a56bf1506a72a0a60709920aad714d1a2ece0")

crates/dkg/src/verification.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,16 @@ where
202202

203203
fn compute_agg_key_from_dkg<C: Curve>(
204204
verification_vectors: &[Vec<C::Point>],
205-
ids: &[C::Scalar],
205+
_ids: &[C::Scalar],
206206
) -> Result<C::Point, Box<dyn std::error::Error>> {
207-
let coefficients = agg_coefficients::<C>(verification_vectors, ids);
208-
lagrange_interpolation::<C>(&coefficients, ids)
207+
let coefficients = agg_coefficients::<C>(verification_vectors);
208+
if coefficients.is_empty() {
209+
return Err(Box::new(std::io::Error::new(
210+
std::io::ErrorKind::InvalidData,
211+
"no verification vectors",
212+
)));
213+
}
214+
Ok(coefficients[0])
209215
}
210216

211217
pub fn verify_generation_hashes<Setup>(
@@ -539,14 +545,8 @@ where
539545
})
540546
.collect();
541547

542-
let ids: Vec<Setup::Scalar> = sorted
543-
.iter()
544-
.enumerate()
545-
.map(|(i, _)| Setup::Scalar::from_u32((i + 1) as u32))
546-
.collect();
547-
548-
let computed_keys = agg_coefficients::<Setup::Curve>(&verification_vectors, &ids);
549-
let expected_key = evaluate_polynomial::<Setup::Curve>(&computed_keys, perpetrator_id);
548+
let computed_keys_coeffs = agg_coefficients::<Setup::Curve>(&verification_vectors);
549+
let expected_key = evaluate_polynomial::<Setup::Curve>(&computed_keys_coeffs, perpetrator_id);
550550
Setup::Point::from_bytes(&expected_key.to_bytes()).expect("Invalid pubkey")
551551
}
552552

perf/baseline.json

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,30 @@
101101
}
102102
},
103103
"bad-partial-key_bad_partial_key.json": {
104-
"total_opcodes": 25814891,
104+
"total_opcodes": 25815409,
105105
"opcodes": {
106-
"add": 9927134,
107-
"sltu": 4806127,
108-
"lw": 2914792,
109-
"mul": 1952285,
106+
"add": 9927835,
107+
"sltu": 4806121,
108+
"lw": 2914771,
109+
"mul": 1952288,
110110
"mulhu": 1896338,
111-
"sw": 1577028,
112-
"beq": 1080593,
113-
"and": 764557,
114-
"or": 233881,
115-
"sub": 174034,
116-
"sll": 64726,
111+
"sw": 1576996,
112+
"beq": 1080597,
113+
"and": 764539,
114+
"or": 233878,
115+
"sub": 174042,
116+
"sll": 64742,
117117
"srl": 64259,
118118
"sra": 61131,
119-
"jalr": 59257,
120-
"bltu": 58140,
121-
"bne": 50796,
119+
"jalr": 59250,
120+
"bltu": 58135,
121+
"bne": 50721,
122122
"xor": 45900,
123-
"auipc": 30227,
123+
"auipc": 30223,
124124
"lbu": 16666,
125125
"sb": 16076,
126-
"jal": 10686,
127-
"bgeu": 5583,
126+
"jal": 10700,
127+
"bgeu": 5526,
128128
"ecall": 1565,
129129
"blt": 1547,
130130
"lb": 1507,
@@ -154,53 +154,53 @@
154154
}
155155
},
156156
"finalization_finalization_test.json": {
157-
"total_opcodes": 60332240,
157+
"total_opcodes": 42662050,
158158
"opcodes": {
159-
"add": 16267715,
160-
"sw": 11982112,
161-
"lw": 11957780,
162-
"and": 4629027,
163-
"bltu": 3555662,
164-
"bne": 2841325,
165-
"jalr": 2691236,
166-
"sltu": 1476623,
167-
"beq": 1462263,
168-
"auipc": 1347865,
169-
"or": 772070,
170-
"ecall": 549206,
171-
"xor": 409355,
172-
"sb": 159922,
173-
"lbu": 79504,
174-
"sub": 53956,
175-
"sll": 24251,
176-
"mul": 21466,
177-
"srl": 17914,
178-
"mulhu": 8609,
179-
"bgeu": 8507,
180-
"jal": 7309,
181-
"blt": 3839,
159+
"add": 11636103,
160+
"sw": 8706072,
161+
"lw": 8602479,
162+
"and": 3015370,
163+
"bltu": 2545745,
164+
"jalr": 1905389,
165+
"bne": 1857709,
166+
"sltu": 971430,
167+
"beq": 964262,
168+
"auipc": 954151,
169+
"or": 514625,
170+
"ecall": 426369,
171+
"xor": 243173,
172+
"sb": 128164,
173+
"lbu": 58289,
174+
"sub": 44221,
175+
"sll": 23525,
176+
"mul": 19594,
177+
"srl": 15971,
178+
"bgeu": 8478,
179+
"mulhu": 8247,
180+
"jal": 5732,
181+
"blt": 2302,
182182
"slt": 1920,
183183
"lb": 1653,
184-
"sra": 1118,
184+
"sra": 1044,
185185
"sh": 22,
186186
"bge": 9,
187187
"lh": 1,
188188
"lhu": 1
189189
},
190-
"total_syscalls": 549206,
190+
"total_syscalls": 426369,
191191
"syscalls": {
192-
"bls12381_fp_mul": 240216,
193-
"bls12381_fp_add": 110012,
192+
"bls12381_fp_mul": 178786,
194193
"bls12381_fp2_add": 84845,
194+
"bls12381_fp_add": 57882,
195195
"bls12381_fp2_mul": 50257,
196196
"bls12381_fp2_sub": 35618,
197-
"bls12381_fp_sub": 24659,
197+
"bls12381_fp_sub": 15454,
198198
"bls12381_double": 2772,
199199
"bls12381_add": 220,
200-
"uint256_mul": 146,
201-
"hint_len": 135,
202-
"hint_read": 135,
203-
"enter_unconstrained": 112,
200+
"uint256_mul": 122,
201+
"hint_len": 119,
202+
"hint_read": 119,
203+
"enter_unconstrained": 96,
204204
"sha_compress": 25,
205205
"sha_extend": 25,
206206
"write": 12,

0 commit comments

Comments
 (0)