Skip to content

Commit 5c5f42c

Browse files
committed
Add x-coord decoding method to ECPoint
1 parent 153b533 commit 5c5f42c

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

JCMathLib/src/opencrypto/jcmathlib/ECPoint.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,50 @@ public void negate() {
424424
ech.unlock(ech.uncompressed_point_arr1);
425425
PM.check(PM.TRAP_ECPOINT_NEGATE_5);
426426
}
427-
427+
428+
/**
429+
* Restore point from X coordinate. Stores one of the two results into this point.
430+
*
431+
* @param xCoord byte array containing the X coordinate
432+
* @param xOffset offset in the byte array
433+
* @param xLen length of the X coordinate
434+
*/
435+
public void from_x(byte[] xCoord, short xOffset, short xLen) {
436+
ech.fnc_from_x_x.lock();
437+
ech.fnc_from_x_x.set_size(xLen);
438+
ech.fnc_from_x_x.from_byte_array(xLen, (short) 0, xCoord, xOffset);
439+
from_x(ech.fnc_from_x_x);
440+
ech.fnc_from_x_x.unlock();
441+
}
442+
443+
/**
444+
* Restore point from X coordinate. Stores one of the two results into this point.
445+
*
446+
* @param x the x coordinate
447+
*/
448+
private void from_x(Bignat x) {
449+
//Y^2 = X^3 + XA + B = x(x^2+A)+B
450+
ech.fnc_from_x_y_sq.lock();
451+
ech.fnc_from_x_y_sq.clone(x);
452+
ech.fnc_from_x_y_sq.mod_exp(Bignat_Helper.TWO, this.theCurve.pBN);
453+
ech.fnc_from_x_y_sq.mod_add(this.theCurve.aBN, this.theCurve.pBN);
454+
ech.fnc_from_x_y_sq.mod_mult(ech.fnc_from_x_y_sq, x, this.theCurve.pBN);
455+
ech.fnc_from_x_y_sq.mod_add(this.theCurve.bBN, this.theCurve.pBN);
456+
ech.fnc_from_x_y.lock();
457+
ech.fnc_from_x_y.clone(ech.fnc_from_x_y_sq);
458+
ech.fnc_from_x_y_sq.unlock();
459+
ech.fnc_from_x_y.sqrt_FP(this.theCurve.pBN);
460+
461+
// Construct public key with <x, y_1>
462+
ech.lock(ech.uncompressed_point_arr1);
463+
ech.uncompressed_point_arr1[0] = 0x04;
464+
x.prepend_zeros(this.theCurve.COORD_SIZE, ech.uncompressed_point_arr1, (short) 1);
465+
ech.fnc_from_x_y.prepend_zeros(this.theCurve.COORD_SIZE, ech.uncompressed_point_arr1, (short) (1 + theCurve.COORD_SIZE));
466+
ech.fnc_from_x_y.unlock();
467+
this.setW(ech.uncompressed_point_arr1, (short) 0, theCurve.POINT_SIZE);
468+
ech.unlock(ech.uncompressed_point_arr1);
469+
}
470+
428471
/**
429472
* Compares this and provided point for equality. The comparison is made using hash of both values to prevent leak of position of mismatching byte.
430473
* @param other second point for comparison

JCMathLib/src/opencrypto/jcmathlib/ECPoint_Helper.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
*
9-
* @author Petr Svenda
9+
* @author Petr Svenda
1010
*/
1111
public class ECPoint_Helper extends Base_Helper {
1212
// Selected constants missing from older JC API specs
@@ -19,11 +19,11 @@ public class ECPoint_Helper extends Base_Helper {
1919
* set automatically after successful allocation of required engines
2020
*/
2121
public boolean FLAG_FAST_EC_MULT_VIA_KA = false;
22-
22+
2323
byte[] uncompressed_point_arr1;
2424
byte[] fnc_isEqual_hashArray;
2525
byte[] fnc_multiplication_resultArray;
26-
26+
2727
// These Bignats are just pointing to some helperEC_BN_? so reasonable naming is preserved yet no need to actually allocated whole Bignat object
2828
Bignat fnc_add_x_r; // frequent write
2929
Bignat fnc_add_y_r; // frequent write
@@ -33,21 +33,25 @@ public class ECPoint_Helper extends Base_Helper {
3333
Bignat fnc_add_lambda; // write mod_mul (but only final result)
3434
Bignat fnc_add_nominator; // frequent write
3535
Bignat fnc_add_denominator; // frequent write
36-
36+
3737
Bignat fnc_multiplication_x; // result write
3838
Bignat fnc_multiplication_y_sq; // frequent write
3939
Bignat fnc_multiplication_scalar; // write once, read
4040
Bignat fnc_multiplication_y1; // mostly just read, write inside sqrt_FP
4141
Bignat fnc_multiplication_y2; // mostly just read, result write
4242
Bignat fnc_negate_yBN; // mostly just read, result write
43-
43+
44+
Bignat fnc_from_x_x;
45+
Bignat fnc_from_x_y_sq;
46+
Bignat fnc_from_x_y;
47+
4448
KeyAgreement fnc_multiplication_x_keyAgreement;
4549
Signature fnc_SignVerifyECDSA_signEngine;
4650
MessageDigest fnc_isEqual_hashEngine;
47-
51+
4852
public ECPoint_Helper(ResourceManager rm) {
4953
super(rm);
50-
54+
5155
FLAG_FAST_EC_MULT_VIA_KA = false; // set true only if succesfully allocated and tested below
5256
try {
5357
//fnc_multiplication_x_keyAgreement = KeyAgreement.getInstance(KeyAgreement.ALG_EC_SVDP_DHC, false);
@@ -57,9 +61,9 @@ public ECPoint_Helper(ResourceManager rm) {
5761
fnc_SignVerifyECDSA_signEngine = Signature.getInstance(Signature_ALG_ECDSA_SHA_256, false);
5862
FLAG_FAST_EC_MULT_VIA_KA = true;
5963
} catch (Exception ignored) {
60-
} // Discard any exception
64+
} // Discard any exception
6165
}
62-
66+
6367
void initialize() {
6468
// Important: assignment of helper BNs is made according to two criteria:
6569
// 1. Correctness: same BN must not be assigned to overlapping operations (guarded by lock/unlock)
@@ -74,21 +78,23 @@ void initialize() {
7478
fnc_add_nominator = rm.helperEC_BN_B;
7579
fnc_add_denominator = rm.helperEC_BN_C;
7680
fnc_add_lambda = rm.helperEC_BN_A;
77-
81+
7882
fnc_multiplication_scalar = rm.helperEC_BN_F;
7983
fnc_multiplication_x = rm.helperEC_BN_B;
8084
fnc_multiplication_y_sq = rm.helperEC_BN_C;
8185
fnc_multiplication_y1 = rm.helperEC_BN_D;
8286
fnc_multiplication_y2 = rm.helperEC_BN_B;
8387
fnc_multiplication_resultArray = rm.helper_BN_array1;
84-
88+
8589
fnc_negate_yBN = rm.helperEC_BN_C;
86-
90+
91+
Bignat fnc_from_x_x;
92+
Bignat fnc_from_x_y_sq;
93+
Bignat fnc_from_x_y;
94+
8795
fnc_isEqual_hashArray = rm.helper_hashArray;
8896
fnc_isEqual_hashEngine = rm.hashEngine;
8997

9098
uncompressed_point_arr1 = rm.helper_uncompressed_point_arr1;
91-
9299
}
93-
94100
}

0 commit comments

Comments
 (0)