1
1
/*
2
- * Copyright (c) 2018, 2019 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2018, 2020 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
21
21
* questions.
22
22
*/
23
23
24
-
25
24
/*
26
25
* This file is used to generated optimized finite field implementations.
27
26
*/
@@ -170,6 +169,19 @@ private static List<CarryReduce> P521CrSequence() {
170
169
o521crSequence (19 ), orderFieldSmallCrSequence (19 )
171
170
);
172
171
172
+ static FieldParams O25519 = new FieldParams (
173
+ "Curve25519OrderField" , 26 , 10 , 1 , 252 ,
174
+ "1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed" ,
175
+ orderFieldCrSequence (10 ), orderFieldSmallCrSequence (10 )
176
+ );
177
+
178
+ static FieldParams O448 = new FieldParams (
179
+ "Curve448OrderField" , 28 , 16 , 1 , 446 ,
180
+ "3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3" ,
181
+ //"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3",
182
+ orderFieldCrSequence (16 ), orderFieldSmallCrSequence (16 )
183
+ );
184
+
173
185
private static List <CarryReduce > o521crSequence (int numLimbs ) {
174
186
175
187
// split the full reduce in half, with a carry in between
@@ -212,7 +224,8 @@ private static List<CarryReduce> orderFieldSmallCrSequence(int numLimbs) {
212
224
}
213
225
214
226
static final FieldParams [] ALL_FIELDS = {
215
- P256 , P384 , P521 , O256 , O384 , O521 ,
227
+ Curve25519 , Curve448 ,
228
+ P256 , P384 , P521 , O256 , O384 , O521 , O25519 , O448
216
229
};
217
230
218
231
public static class Term {
@@ -322,6 +335,11 @@ public FieldParams(String className, int bitsPerLimb, int numLimbs,
322
335
private Iterable <Term > buildTerms (BigInteger sub ) {
323
336
// split a large subtrahend into smaller terms
324
337
// that are aligned with limbs
338
+ boolean negate = false ;
339
+ if (sub .compareTo (BigInteger .ZERO ) < 0 ) {
340
+ negate = true ;
341
+ sub = sub .negate ();
342
+ }
325
343
List <Term > result = new ArrayList <Term >();
326
344
BigInteger mod = BigInteger .valueOf (1 << bitsPerLimb );
327
345
int termIndex = 0 ;
@@ -332,6 +350,9 @@ private Iterable<Term> buildTerms(BigInteger sub) {
332
350
coef = coef - (1 << bitsPerLimb );
333
351
plusOne = true ;
334
352
}
353
+ if (negate ) {
354
+ coef = 0 - coef ;
355
+ }
335
356
if (coef != 0 ) {
336
357
int pow = termIndex * bitsPerLimb ;
337
358
result .add (new Term (pow , -coef ));
@@ -619,6 +640,14 @@ private String generate(FieldParams params) throws IOException {
619
640
result .appendLine ();
620
641
result .appendLine ("}" );
621
642
643
+ StringBuilder coqTerms = new StringBuilder ("//" );
644
+ for (Term t : params .getTerms ()) {
645
+ coqTerms .append ("(" + t .getPower () + "%nat," );
646
+ coqTerms .append (t .getCoefficient () + ")::" );
647
+ }
648
+ coqTerms .append ("nil." );
649
+ result .appendLine (coqTerms .toString ());
650
+
622
651
result .appendLine ("private static BigInteger evaluateModulus() {" );
623
652
result .incrIndent ();
624
653
result .appendLine ("BigInteger result = BigInteger.valueOf(2).pow("
@@ -650,6 +679,41 @@ private String generate(FieldParams params) throws IOException {
650
679
result .decrIndent ();
651
680
result .appendLine ("}" );
652
681
682
+ result .appendLine ("@Override" );
683
+ result .appendLine ("protected void reduceIn(long[] limbs, long v, int i) {" );
684
+ result .incrIndent ();
685
+ String c = "v" ;
686
+ for (Term t : params .getTerms ()) {
687
+ int reduceBits = params .getPower () - t .getPower ();
688
+ int coefficient = -1 * t .getCoefficient ();
689
+
690
+ String x = coefficient + " * " + c ;
691
+ String accOp = "+=" ;
692
+ String temp = null ;
693
+ if (coefficient == 1 ) {
694
+ x = c ;
695
+ } else if (coefficient == -1 ) {
696
+ x = c ;
697
+ accOp = "-=" ;
698
+ } else {
699
+ temp = result .getTemporary ("long" , x );
700
+ x = temp ;
701
+ }
702
+
703
+ if (reduceBits % params .getBitsPerLimb () == 0 ) {
704
+ int pos = reduceBits / params .getBitsPerLimb ();
705
+ result .appendLine ("limbs[i - " + pos + "] " + accOp + " " + x + ";" );
706
+ } else {
707
+ int secondPos = reduceBits / params .getBitsPerLimb ();
708
+ int bitOffset = (secondPos + 1 ) * params .getBitsPerLimb () - reduceBits ;
709
+ int rightBitOffset = params .getBitsPerLimb () - bitOffset ;
710
+ result .appendLine ("limbs[i - " + (secondPos + 1 ) + "] " + accOp + " (" + x + " << " + bitOffset + ") & LIMB_MASK;" );
711
+ result .appendLine ("limbs[i - " + secondPos + "] " + accOp + " " + x + " >> " + rightBitOffset + ";" );
712
+ }
713
+ }
714
+ result .decrIndent ();
715
+ result .appendLine ("}" );
716
+
653
717
result .appendLine ("@Override" );
654
718
result .appendLine ("protected void finalCarryReduceLast(long[] limbs) {" );
655
719
result .incrIndent ();
0 commit comments