@@ -336,6 +336,42 @@ Mulmod(a,b){
336336 return ( a * b ) % this . order ;
337337}
338338
339+ //operations are not constant time, not required as aggregation is a public function
340+ Partial_sig_agg ( psigs , session_ctx ) {
341+ let sessionV = this . Get_session_values ( session_ctx ) ; //(Q, gacc, tacc, b, R, e)
342+
343+
344+ let Q = sessionV [ 0 ] ; //aggnonce
345+ let tacc = sessionV [ 2 ] ;
346+
347+ let e = sessionV [ 5 ] ;
348+
349+ let s = BigInt ( 0 ) ;
350+ let u = psigs . length ;
351+ for ( let i = 0 ; i < u ; i ++ ) {
352+ let s_i = int_from_bytes ( psigs [ i ] )
353+ if ( s_i > this . order ) {
354+ return false ;
355+ }
356+ s = ( s + s_i ) % this . order ;
357+ }
358+ let g = BigInt ( 1 ) ;
359+ if ( this . curve . Has_even_y ( Q ) == false )
360+ g = this . order - g ; //n-1
361+
362+
363+ s = ( s + e * g * tacc ) % this . order ;
364+ s = int_to_bytes ( s , 32 ) ;
365+
366+ let R = this . curve . GetX ( sessionV [ 4 ] ) ;
367+ console . log ( "R=" , R ) ;
368+ console . log ( "from " , sessionV [ 4 ] ) ;
369+ console . log ( "s=" , s , s . length ) ;
370+
371+ return Buffer . concat ( [ R , s ] ) ;
372+
373+ }
374+
339375//partial signature
340376//secnonce: 2 nonces + kpub
341377//sk: 32 bytes
@@ -369,20 +405,22 @@ Psign(secnonce, sk, session_ctx){
369405 let secnonce_pk = secnonce . slice ( 64 , 64 + this . RawBytesSize ) ; //pk is part of secnonce, 32 or 33 bytes
370406 let Q3 = this . curve . PointDecompress ( secnonce_pk ) ;
371407
408+
372409 //todo test x equality
373410 if ( this . curve . EqualsX ( P , Q3 ) == false ) {
374411 return false ; //wrong public key
375412 }
376413
377414 let a = this . Get_session_key_agg_coeff ( session_ctx [ 1 ] , secnonce . slice ( 64 , 64 + this . RawBytesSize ) ) ;
378-
415+
416+
379417 let g = BigInt ( '0x1' ) ;
380418 if ( this . curve . Has_even_y ( Q ) == false ) { //this line ensures the compatibility with requirement that aggregated key is even in verification
381419 g = this . order - g ; //n-1
382420
383421 }
384422 let d = this . Mulmod ( g , gacc ) ; //d = (g * gacc * d_) % n
385- d = this . Mulmod ( d , d_ ) ;
423+ d = this . Mulmod ( d , d_ ) ; //g*gacc*d
386424 let s = ( k1 + this . Mulmod ( b , k2 ) ) % this . order ; //
387425 s = ( s + this . Mulmod ( this . Mulmod ( e , a ) , d ) ) % this . order ;
388426
@@ -393,70 +431,51 @@ Psign(secnonce, sk, session_ctx){
393431 }
394432
395433
396- //operations are not constant time, not required as aggregation is a public function
397- Partial_sig_agg ( psigs , session_ctx ) {
398- let sessionV = this . Get_session_values ( session_ctx ) ; //(Q, gacc, tacc, b, R, e)
399-
400-
401- let Q = sessionV [ 0 ] ; //aggnonce
402- let tacc = sessionV [ 2 ] ;
403-
404- let e = sessionV [ 5 ] ;
405-
406- let s = BigInt ( 0 ) ;
407- let u = psigs . length ;
408- for ( let i = 0 ; i < u ; i ++ ) {
409- let s_i = int_from_bytes ( psigs [ i ] )
410- if ( s_i > this . order ) {
411- return false ;
412- }
413- s = ( s + s_i ) % this . order ;
414- }
415- let g = BigInt ( 1 ) ;
416- if ( this . curve . Has_even_y ( Q ) == false )
417- g = this . order - g ; //n-1
418-
419-
420- s = ( s + e * g * tacc ) % this . order ;
421- s = int_to_bytes ( s , 32 ) ;
422-
423- let R = this . curve . GetX ( sessionV [ 4 ] ) ;
424- console . log ( "R=" , R ) ;
425- console . log ( "from " , sessionV [ 4 ] ) ;
426- console . log ( "s=" , s , s . length ) ;
427-
428- return Buffer . concat ( [ R , s ] ) ;
429-
430- }
431-
432434/********************************************************************************************/
433435/* VERIFICATIONS*/
434436/********************************************************************************************/
435437
436438//verify one of the partial signature provided by a participant
437439Psig_verify ( psig , pubnonce , pk , session_ctx ) {
438- let sessionV = this . Get_session_values ( session_ctx ) ; //(Q, gacc, tacc , b, R, e)
440+ let sessionV = this . Get_session_values ( session_ctx ) ; //(Q, gacc, _ , b, R, e)
439441 let s = int_from_bytes ( psig ) ;
442+ console . log ( "psig:" , psig ) ;
443+ let Q = sessionV [ 0 ] ;
444+ let gacc = sessionV [ 1 ] ;
445+ let b = sessionV [ 3 ] ;
446+ let R = sessionV [ 4 ] ;
447+ let e = sessionV [ 5 ] ;
448+
449+
440450 let R_s1 = this . curve . PointDecompress ( pubnonce . slice ( 0 , this . RawBytesSize ) ) ;
441451 let R_s2 = this . curve . PointDecompress ( pubnonce . slice ( this . RawBytesSize , 2 * this . RawBytesSize ) ) ;
442-
452+
443453 let Re_s_ = R_s1 . add ( R_s2 . multiply ( b ) ) ;
454+
455+ let Re_s = Re_s_ ;
456+
457+ if ( this . curve . Has_even_y ( R ) == false )
458+ {
459+ Re_s = Re_s . negate ( ) ; //forced to even point
460+ }
461+ let P = this . curve . PointDecompress ( pk ) ; //partial input public key
444462
445- let Re_s = this . curve . PointCompressXonly ( Re_s_ ) ; //forced to even point
463+ let a = this . Get_session_key_agg_coeff ( session_ctx [ 1 ] , pk ) ; //session_ctx[1]=pubkeys
446464
447465
448- a = key_agg_coeff ( session_ctx [ 1 ] , pk ) ;
449466 let g = BigInt ( 1 ) ;
450- if ( has_even_y ( Q ) == false )
451- g = secp256k1 . CURVE . n - g ; //n-1
452- let P = ProjectivePoint . fromHex ( pk ) ; //partial input public key
467+ if ( this . curve . Has_even_y ( Q ) == false ) {
468+ g = this . order - g ; //n-1
469+ }
453470
454471 g = ( g * gacc ) % this . order ;
455472
456- let G = secp256k1 . ProjectivePoint . BASE ;
473+ let G = this . curve . GetBase ( ) ;
457474 let P1 = ( G . multiply ( s ) ) ;
458- let P2 = Re_s . add ( P . multiply ( ( e * a * g ) % this . order ) ) ;
459475
476+ let tmp = this . Mulmod ( e , a ) ;
477+ tmp = this . Mulmod ( tmp , g ) ; //e*a*g % n
478+ let P2 = ( Re_s . add ( P . multiply ( tmp ) ) ) ;
460479
461480 return ( P1 . equals ( P2 ) ) ;
462481}
0 commit comments