Skip to content

Commit 3d12a25

Browse files
committed
Fast constant-time cmov (thx zzz)
1 parent f04a2da commit 3d12a25

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/net/i2p/crypto/eddsa/math/GroupElement.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,28 @@ static byte[] toRadix16(byte[] a) {
430430
}
431431

432432
/**
433-
* Replace this with u if b == 1.
434-
* Replace this with this if b == 0.
433+
* Constant-time conditional move.
434+
* Replaces this with u if b == 1.
435+
* Replaces this with this if b == 0.
435436
*
436437
* Method is package private only so that tests run.
437438
*
438439
* @param u
439440
* @param b in {0, 1}
440-
* @return
441+
* @return u if b == 1; this if b == 0; null otherwise.
441442
*/
442443
GroupElement cmov(GroupElement u, int b) {
443-
return precomp(curve, X.cmov(u.X, b), Y.cmov(u.Y, b), Z.cmov(u.Z, b));
444+
GroupElement ret = null;
445+
int i;
446+
for (i = 0; i < b; i++) {
447+
// Only for b == 1
448+
ret = u;
449+
}
450+
for (i = 0; i < 1-b; i++) {
451+
// Only for b == 0
452+
ret = this;
453+
}
454+
return ret;
444455
}
445456

446457
/**

0 commit comments

Comments
 (0)