@@ -272,8 +272,351 @@ public byte[] reduce(byte[] s) {
272272 return result ;
273273 }
274274
275+
276+ /**
277+ * Input:<br>
278+ * a[0]+256*a[1]+...+256^31*a[31] = a<br>
279+ * b[0]+256*b[1]+...+256^31*b[31] = b<br>
280+ * c[0]+256*c[1]+...+256^31*c[31] = c<br><br>
281+ *
282+ * Output:<br>
283+ * result[0]+256*result[1]+...+256^31*result[31] = (ab+c) mod l<br>
284+ * where l = 2^252 + 27742317777372353535851937790883648493.
285+ */
275286 public byte [] multiplyAndAdd (byte [] a , byte [] b , byte [] c ) {
276- // TODO Auto-generated method stub
277- return null ;
287+ BigInteger n2097151 = BigInteger .valueOf (2097151 );
288+
289+ long a0 = n2097151 .and (load_3 (a , 0 )).longValue ();;
290+ long a1 = n2097151 .and (load_4 (a , 2 ).shiftRight (5 )).longValue ();
291+ long a2 = n2097151 .and (load_3 (a , 5 ).shiftRight (2 )).longValue ();
292+ long a3 = n2097151 .and (load_4 (a , 7 ).shiftRight (7 )).longValue ();
293+ long a4 = n2097151 .and (load_4 (a , 10 ).shiftRight (4 )).longValue ();
294+ long a5 = n2097151 .and (load_3 (a , 13 ).shiftRight (1 )).longValue ();
295+ long a6 = n2097151 .and (load_4 (a , 15 ).shiftRight (6 )).longValue ();
296+ long a7 = n2097151 .and (load_3 (a , 18 ).shiftRight (3 )).longValue ();
297+ long a8 = n2097151 .and (load_3 (a , 21 )).longValue ();
298+ long a9 = n2097151 .and (load_4 (a , 23 ).shiftRight (5 )).longValue ();
299+ long a10 = n2097151 .and (load_3 (a , 26 ).shiftRight (2 )).longValue ();
300+ long a11 = (load_4 (a , 28 ).shiftRight (7 )).longValue ();
301+ long b0 = n2097151 .and (load_3 (b , 0 )).longValue ();;
302+ long b1 = n2097151 .and (load_4 (b , 2 ).shiftRight (5 )).longValue ();
303+ long b2 = n2097151 .and (load_3 (b , 5 ).shiftRight (2 )).longValue ();
304+ long b3 = n2097151 .and (load_4 (b , 7 ).shiftRight (7 )).longValue ();
305+ long b4 = n2097151 .and (load_4 (b , 10 ).shiftRight (4 )).longValue ();
306+ long b5 = n2097151 .and (load_3 (b , 13 ).shiftRight (1 )).longValue ();
307+ long b6 = n2097151 .and (load_4 (b , 15 ).shiftRight (6 )).longValue ();
308+ long b7 = n2097151 .and (load_3 (b , 18 ).shiftRight (3 )).longValue ();
309+ long b8 = n2097151 .and (load_3 (b , 21 )).longValue ();
310+ long b9 = n2097151 .and (load_4 (b , 23 ).shiftRight (5 )).longValue ();
311+ long b10 = n2097151 .and (load_3 (b , 26 ).shiftRight (2 )).longValue ();
312+ long b11 = (load_4 (b , 28 ).shiftRight (7 )).longValue ();
313+ long c0 = n2097151 .and (load_3 (c , 0 )).longValue ();;
314+ long c1 = n2097151 .and (load_4 (c , 2 ).shiftRight (5 )).longValue ();
315+ long c2 = n2097151 .and (load_3 (c , 5 ).shiftRight (2 )).longValue ();
316+ long c3 = n2097151 .and (load_4 (c , 7 ).shiftRight (7 )).longValue ();
317+ long c4 = n2097151 .and (load_4 (c , 10 ).shiftRight (4 )).longValue ();
318+ long c5 = n2097151 .and (load_3 (c , 13 ).shiftRight (1 )).longValue ();
319+ long c6 = n2097151 .and (load_4 (c , 15 ).shiftRight (6 )).longValue ();
320+ long c7 = n2097151 .and (load_3 (c , 18 ).shiftRight (3 )).longValue ();
321+ long c8 = n2097151 .and (load_3 (c , 21 )).longValue ();
322+ long c9 = n2097151 .and (load_4 (c , 23 ).shiftRight (5 )).longValue ();
323+ long c10 = n2097151 .and (load_3 (c , 26 ).shiftRight (2 )).longValue ();
324+ long c11 = (load_4 (c , 28 ).shiftRight (7 )).longValue ();
325+ long s0 ;
326+ long s1 ;
327+ long s2 ;
328+ long s3 ;
329+ long s4 ;
330+ long s5 ;
331+ long s6 ;
332+ long s7 ;
333+ long s8 ;
334+ long s9 ;
335+ long s10 ;
336+ long s11 ;
337+ long s12 ;
338+ long s13 ;
339+ long s14 ;
340+ long s15 ;
341+ long s16 ;
342+ long s17 ;
343+ long s18 ;
344+ long s19 ;
345+ long s20 ;
346+ long s21 ;
347+ long s22 ;
348+ long s23 ;
349+ long carry0 ;
350+ long carry1 ;
351+ long carry2 ;
352+ long carry3 ;
353+ long carry4 ;
354+ long carry5 ;
355+ long carry6 ;
356+ long carry7 ;
357+ long carry8 ;
358+ long carry9 ;
359+ long carry10 ;
360+ long carry11 ;
361+ long carry12 ;
362+ long carry13 ;
363+ long carry14 ;
364+ long carry15 ;
365+ long carry16 ;
366+ long carry17 ;
367+ long carry18 ;
368+ long carry19 ;
369+ long carry20 ;
370+ long carry21 ;
371+ long carry22 ;
372+
373+ s0 = c0 + a0 *b0 ;
374+ s1 = c1 + a0 *b1 + a1 *b0 ;
375+ s2 = c2 + a0 *b2 + a1 *b1 + a2 *b0 ;
376+ s3 = c3 + a0 *b3 + a1 *b2 + a2 *b1 + a3 *b0 ;
377+ s4 = c4 + a0 *b4 + a1 *b3 + a2 *b2 + a3 *b1 + a4 *b0 ;
378+ s5 = c5 + a0 *b5 + a1 *b4 + a2 *b3 + a3 *b2 + a4 *b1 + a5 *b0 ;
379+ s6 = c6 + a0 *b6 + a1 *b5 + a2 *b4 + a3 *b3 + a4 *b2 + a5 *b1 + a6 *b0 ;
380+ s7 = c7 + a0 *b7 + a1 *b6 + a2 *b5 + a3 *b4 + a4 *b3 + a5 *b2 + a6 *b1 + a7 *b0 ;
381+ s8 = c8 + a0 *b8 + a1 *b7 + a2 *b6 + a3 *b5 + a4 *b4 + a5 *b3 + a6 *b2 + a7 *b1 + a8 *b0 ;
382+ s9 = c9 + a0 *b9 + a1 *b8 + a2 *b7 + a3 *b6 + a4 *b5 + a5 *b4 + a6 *b3 + a7 *b2 + a8 *b1 + a9 *b0 ;
383+ s10 = c10 + a0 *b10 + a1 *b9 + a2 *b8 + a3 *b7 + a4 *b6 + a5 *b5 + a6 *b4 + a7 *b3 + a8 *b2 + a9 *b1 + a10 *b0 ;
384+ s11 = c11 + a0 *b11 + a1 *b10 + a2 *b9 + a3 *b8 + a4 *b7 + a5 *b6 + a6 *b5 + a7 *b4 + a8 *b3 + a9 *b2 + a10 *b1 + a11 *b0 ;
385+ s12 = a1 *b11 + a2 *b10 + a3 *b9 + a4 *b8 + a5 *b7 + a6 *b6 + a7 *b5 + a8 *b4 + a9 *b3 + a10 *b2 + a11 *b1 ;
386+ s13 = a2 *b11 + a3 *b10 + a4 *b9 + a5 *b8 + a6 *b7 + a7 *b6 + a8 *b5 + a9 *b4 + a10 *b3 + a11 *b2 ;
387+ s14 = a3 *b11 + a4 *b10 + a5 *b9 + a6 *b8 + a7 *b7 + a8 *b6 + a9 *b5 + a10 *b4 + a11 *b3 ;
388+ s15 = a4 *b11 + a5 *b10 + a6 *b9 + a7 *b8 + a8 *b7 + a9 *b6 + a10 *b5 + a11 *b4 ;
389+ s16 = a5 *b11 + a6 *b10 + a7 *b9 + a8 *b8 + a9 *b7 + a10 *b6 + a11 *b5 ;
390+ s17 = a6 *b11 + a7 *b10 + a8 *b9 + a9 *b8 + a10 *b7 + a11 *b6 ;
391+ s18 = a7 *b11 + a8 *b10 + a9 *b9 + a10 *b8 + a11 *b7 ;
392+ s19 = a8 *b11 + a9 *b10 + a10 *b9 + a11 *b8 ;
393+ s20 = a9 *b11 + a10 *b10 + a11 *b9 ;
394+ s21 = a10 *b11 + a11 *b10 ;
395+ s22 = a11 *b11 ;
396+ s23 = 0 ;
397+
398+ carry0 = (s0 + (1 <<20 )) >> 21 ; s1 += carry0 ; s0 -= carry0 << 21 ;
399+ carry2 = (s2 + (1 <<20 )) >> 21 ; s3 += carry2 ; s2 -= carry2 << 21 ;
400+ carry4 = (s4 + (1 <<20 )) >> 21 ; s5 += carry4 ; s4 -= carry4 << 21 ;
401+ carry6 = (s6 + (1 <<20 )) >> 21 ; s7 += carry6 ; s6 -= carry6 << 21 ;
402+ carry8 = (s8 + (1 <<20 )) >> 21 ; s9 += carry8 ; s8 -= carry8 << 21 ;
403+ carry10 = (s10 + (1 <<20 )) >> 21 ; s11 += carry10 ; s10 -= carry10 << 21 ;
404+ carry12 = (s12 + (1 <<20 )) >> 21 ; s13 += carry12 ; s12 -= carry12 << 21 ;
405+ carry14 = (s14 + (1 <<20 )) >> 21 ; s15 += carry14 ; s14 -= carry14 << 21 ;
406+ carry16 = (s16 + (1 <<20 )) >> 21 ; s17 += carry16 ; s16 -= carry16 << 21 ;
407+ carry18 = (s18 + (1 <<20 )) >> 21 ; s19 += carry18 ; s18 -= carry18 << 21 ;
408+ carry20 = (s20 + (1 <<20 )) >> 21 ; s21 += carry20 ; s20 -= carry20 << 21 ;
409+ carry22 = (s22 + (1 <<20 )) >> 21 ; s23 += carry22 ; s22 -= carry22 << 21 ;
410+
411+ carry1 = (s1 + (1 <<20 )) >> 21 ; s2 += carry1 ; s1 -= carry1 << 21 ;
412+ carry3 = (s3 + (1 <<20 )) >> 21 ; s4 += carry3 ; s3 -= carry3 << 21 ;
413+ carry5 = (s5 + (1 <<20 )) >> 21 ; s6 += carry5 ; s5 -= carry5 << 21 ;
414+ carry7 = (s7 + (1 <<20 )) >> 21 ; s8 += carry7 ; s7 -= carry7 << 21 ;
415+ carry9 = (s9 + (1 <<20 )) >> 21 ; s10 += carry9 ; s9 -= carry9 << 21 ;
416+ carry11 = (s11 + (1 <<20 )) >> 21 ; s12 += carry11 ; s11 -= carry11 << 21 ;
417+ carry13 = (s13 + (1 <<20 )) >> 21 ; s14 += carry13 ; s13 -= carry13 << 21 ;
418+ carry15 = (s15 + (1 <<20 )) >> 21 ; s16 += carry15 ; s15 -= carry15 << 21 ;
419+ carry17 = (s17 + (1 <<20 )) >> 21 ; s18 += carry17 ; s17 -= carry17 << 21 ;
420+ carry19 = (s19 + (1 <<20 )) >> 21 ; s20 += carry19 ; s19 -= carry19 << 21 ;
421+ carry21 = (s21 + (1 <<20 )) >> 21 ; s22 += carry21 ; s21 -= carry21 << 21 ;
422+
423+ s11 += s23 * 666643 ;
424+ s12 += s23 * 470296 ;
425+ s13 += s23 * 654183 ;
426+ s14 -= s23 * 997805 ;
427+ s15 += s23 * 136657 ;
428+ s16 -= s23 * 683901 ;
429+ s23 = 0 ;
430+
431+ s10 += s22 * 666643 ;
432+ s11 += s22 * 470296 ;
433+ s12 += s22 * 654183 ;
434+ s13 -= s22 * 997805 ;
435+ s14 += s22 * 136657 ;
436+ s15 -= s22 * 683901 ;
437+ s22 = 0 ;
438+
439+ s9 += s21 * 666643 ;
440+ s10 += s21 * 470296 ;
441+ s11 += s21 * 654183 ;
442+ s12 -= s21 * 997805 ;
443+ s13 += s21 * 136657 ;
444+ s14 -= s21 * 683901 ;
445+ s21 = 0 ;
446+
447+ s8 += s20 * 666643 ;
448+ s9 += s20 * 470296 ;
449+ s10 += s20 * 654183 ;
450+ s11 -= s20 * 997805 ;
451+ s12 += s20 * 136657 ;
452+ s13 -= s20 * 683901 ;
453+ s20 = 0 ;
454+
455+ s7 += s19 * 666643 ;
456+ s8 += s19 * 470296 ;
457+ s9 += s19 * 654183 ;
458+ s10 -= s19 * 997805 ;
459+ s11 += s19 * 136657 ;
460+ s12 -= s19 * 683901 ;
461+ s19 = 0 ;
462+
463+ s6 += s18 * 666643 ;
464+ s7 += s18 * 470296 ;
465+ s8 += s18 * 654183 ;
466+ s9 -= s18 * 997805 ;
467+ s10 += s18 * 136657 ;
468+ s11 -= s18 * 683901 ;
469+ s18 = 0 ;
470+
471+ carry6 = (s6 + (1 <<20 )) >> 21 ; s7 += carry6 ; s6 -= carry6 << 21 ;
472+ carry8 = (s8 + (1 <<20 )) >> 21 ; s9 += carry8 ; s8 -= carry8 << 21 ;
473+ carry10 = (s10 + (1 <<20 )) >> 21 ; s11 += carry10 ; s10 -= carry10 << 21 ;
474+ carry12 = (s12 + (1 <<20 )) >> 21 ; s13 += carry12 ; s12 -= carry12 << 21 ;
475+ carry14 = (s14 + (1 <<20 )) >> 21 ; s15 += carry14 ; s14 -= carry14 << 21 ;
476+ carry16 = (s16 + (1 <<20 )) >> 21 ; s17 += carry16 ; s16 -= carry16 << 21 ;
477+
478+ carry7 = (s7 + (1 <<20 )) >> 21 ; s8 += carry7 ; s7 -= carry7 << 21 ;
479+ carry9 = (s9 + (1 <<20 )) >> 21 ; s10 += carry9 ; s9 -= carry9 << 21 ;
480+ carry11 = (s11 + (1 <<20 )) >> 21 ; s12 += carry11 ; s11 -= carry11 << 21 ;
481+ carry13 = (s13 + (1 <<20 )) >> 21 ; s14 += carry13 ; s13 -= carry13 << 21 ;
482+ carry15 = (s15 + (1 <<20 )) >> 21 ; s16 += carry15 ; s15 -= carry15 << 21 ;
483+
484+ s5 += s17 * 666643 ;
485+ s6 += s17 * 470296 ;
486+ s7 += s17 * 654183 ;
487+ s8 -= s17 * 997805 ;
488+ s9 += s17 * 136657 ;
489+ s10 -= s17 * 683901 ;
490+ s17 = 0 ;
491+
492+ s4 += s16 * 666643 ;
493+ s5 += s16 * 470296 ;
494+ s6 += s16 * 654183 ;
495+ s7 -= s16 * 997805 ;
496+ s8 += s16 * 136657 ;
497+ s9 -= s16 * 683901 ;
498+ s16 = 0 ;
499+
500+ s3 += s15 * 666643 ;
501+ s4 += s15 * 470296 ;
502+ s5 += s15 * 654183 ;
503+ s6 -= s15 * 997805 ;
504+ s7 += s15 * 136657 ;
505+ s8 -= s15 * 683901 ;
506+ s15 = 0 ;
507+
508+ s2 += s14 * 666643 ;
509+ s3 += s14 * 470296 ;
510+ s4 += s14 * 654183 ;
511+ s5 -= s14 * 997805 ;
512+ s6 += s14 * 136657 ;
513+ s7 -= s14 * 683901 ;
514+ s14 = 0 ;
515+
516+ s1 += s13 * 666643 ;
517+ s2 += s13 * 470296 ;
518+ s3 += s13 * 654183 ;
519+ s4 -= s13 * 997805 ;
520+ s5 += s13 * 136657 ;
521+ s6 -= s13 * 683901 ;
522+ s13 = 0 ;
523+
524+ s0 += s12 * 666643 ;
525+ s1 += s12 * 470296 ;
526+ s2 += s12 * 654183 ;
527+ s3 -= s12 * 997805 ;
528+ s4 += s12 * 136657 ;
529+ s5 -= s12 * 683901 ;
530+ s12 = 0 ;
531+
532+ carry0 = (s0 + (1 <<20 )) >> 21 ; s1 += carry0 ; s0 -= carry0 << 21 ;
533+ carry2 = (s2 + (1 <<20 )) >> 21 ; s3 += carry2 ; s2 -= carry2 << 21 ;
534+ carry4 = (s4 + (1 <<20 )) >> 21 ; s5 += carry4 ; s4 -= carry4 << 21 ;
535+ carry6 = (s6 + (1 <<20 )) >> 21 ; s7 += carry6 ; s6 -= carry6 << 21 ;
536+ carry8 = (s8 + (1 <<20 )) >> 21 ; s9 += carry8 ; s8 -= carry8 << 21 ;
537+ carry10 = (s10 + (1 <<20 )) >> 21 ; s11 += carry10 ; s10 -= carry10 << 21 ;
538+
539+ carry1 = (s1 + (1 <<20 )) >> 21 ; s2 += carry1 ; s1 -= carry1 << 21 ;
540+ carry3 = (s3 + (1 <<20 )) >> 21 ; s4 += carry3 ; s3 -= carry3 << 21 ;
541+ carry5 = (s5 + (1 <<20 )) >> 21 ; s6 += carry5 ; s5 -= carry5 << 21 ;
542+ carry7 = (s7 + (1 <<20 )) >> 21 ; s8 += carry7 ; s7 -= carry7 << 21 ;
543+ carry9 = (s9 + (1 <<20 )) >> 21 ; s10 += carry9 ; s9 -= carry9 << 21 ;
544+ carry11 = (s11 + (1 <<20 )) >> 21 ; s12 += carry11 ; s11 -= carry11 << 21 ;
545+
546+ s0 += s12 * 666643 ;
547+ s1 += s12 * 470296 ;
548+ s2 += s12 * 654183 ;
549+ s3 -= s12 * 997805 ;
550+ s4 += s12 * 136657 ;
551+ s5 -= s12 * 683901 ;
552+ s12 = 0 ;
553+
554+ carry0 = s0 >> 21 ; s1 += carry0 ; s0 -= carry0 << 21 ;
555+ carry1 = s1 >> 21 ; s2 += carry1 ; s1 -= carry1 << 21 ;
556+ carry2 = s2 >> 21 ; s3 += carry2 ; s2 -= carry2 << 21 ;
557+ carry3 = s3 >> 21 ; s4 += carry3 ; s3 -= carry3 << 21 ;
558+ carry4 = s4 >> 21 ; s5 += carry4 ; s4 -= carry4 << 21 ;
559+ carry5 = s5 >> 21 ; s6 += carry5 ; s5 -= carry5 << 21 ;
560+ carry6 = s6 >> 21 ; s7 += carry6 ; s6 -= carry6 << 21 ;
561+ carry7 = s7 >> 21 ; s8 += carry7 ; s7 -= carry7 << 21 ;
562+ carry8 = s8 >> 21 ; s9 += carry8 ; s8 -= carry8 << 21 ;
563+ carry9 = s9 >> 21 ; s10 += carry9 ; s9 -= carry9 << 21 ;
564+ carry10 = s10 >> 21 ; s11 += carry10 ; s10 -= carry10 << 21 ;
565+ carry11 = s11 >> 21 ; s12 += carry11 ; s11 -= carry11 << 21 ;
566+
567+ s0 += s12 * 666643 ;
568+ s1 += s12 * 470296 ;
569+ s2 += s12 * 654183 ;
570+ s3 -= s12 * 997805 ;
571+ s4 += s12 * 136657 ;
572+ s5 -= s12 * 683901 ;
573+ s12 = 0 ;
574+
575+ carry0 = s0 >> 21 ; s1 += carry0 ; s0 -= carry0 << 21 ;
576+ carry1 = s1 >> 21 ; s2 += carry1 ; s1 -= carry1 << 21 ;
577+ carry2 = s2 >> 21 ; s3 += carry2 ; s2 -= carry2 << 21 ;
578+ carry3 = s3 >> 21 ; s4 += carry3 ; s3 -= carry3 << 21 ;
579+ carry4 = s4 >> 21 ; s5 += carry4 ; s4 -= carry4 << 21 ;
580+ carry5 = s5 >> 21 ; s6 += carry5 ; s5 -= carry5 << 21 ;
581+ carry6 = s6 >> 21 ; s7 += carry6 ; s6 -= carry6 << 21 ;
582+ carry7 = s7 >> 21 ; s8 += carry7 ; s7 -= carry7 << 21 ;
583+ carry8 = s8 >> 21 ; s9 += carry8 ; s8 -= carry8 << 21 ;
584+ carry9 = s9 >> 21 ; s10 += carry9 ; s9 -= carry9 << 21 ;
585+ carry10 = s10 >> 21 ; s11 += carry10 ; s10 -= carry10 << 21 ;
586+
587+ byte [] result = new byte [32 ];
588+ result [0 ] = (byte ) (s0 >> 0 );
589+ result [1 ] = (byte ) (s0 >> 8 );
590+ result [2 ] = (byte ) ((s0 >> 16 ) | (s1 << 5 ));
591+ result [3 ] = (byte ) (s1 >> 3 );
592+ result [4 ] = (byte ) (s1 >> 11 );
593+ result [5 ] = (byte ) ((s1 >> 19 ) | (s2 << 2 ));
594+ result [6 ] = (byte ) (s2 >> 6 );
595+ result [7 ] = (byte ) ((s2 >> 14 ) | (s3 << 7 ));
596+ result [8 ] = (byte ) (s3 >> 1 );
597+ result [9 ] = (byte ) (s3 >> 9 );
598+ result [10 ] = (byte ) ((s3 >> 17 ) | (s4 << 4 ));
599+ result [11 ] = (byte ) (s4 >> 4 );
600+ result [12 ] = (byte ) (s4 >> 12 );
601+ result [13 ] = (byte ) ((s4 >> 20 ) | (s5 << 1 ));
602+ result [14 ] = (byte ) (s5 >> 7 );
603+ result [15 ] = (byte ) ((s5 >> 15 ) | (s6 << 6 ));
604+ result [16 ] = (byte ) (s6 >> 2 );
605+ result [17 ] = (byte ) (s6 >> 10 );
606+ result [18 ] = (byte ) ((s6 >> 18 ) | (s7 << 3 ));
607+ result [19 ] = (byte ) (s7 >> 5 );
608+ result [20 ] = (byte ) (s7 >> 13 );
609+ result [21 ] = (byte ) (s8 >> 0 );
610+ result [22 ] = (byte ) (s8 >> 8 );
611+ result [23 ] = (byte ) ((s8 >> 16 ) | (s9 << 5 ));
612+ result [24 ] = (byte ) (s9 >> 3 );
613+ result [25 ] = (byte ) (s9 >> 11 );
614+ result [26 ] = (byte ) ((s9 >> 19 ) | (s10 << 2 ));
615+ result [27 ] = (byte ) (s10 >> 6 );
616+ result [28 ] = (byte ) ((s10 >> 14 ) | (s11 << 7 ));
617+ result [29 ] = (byte ) (s11 >> 1 );
618+ result [30 ] = (byte ) (s11 >> 9 );
619+ result [31 ] = (byte ) (s11 >> 17 );
620+ return result ;
278621 }
279622}
0 commit comments