Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant-time GroupElement.cmov() #31

Merged
merged 2 commits into from
Mar 11, 2017
Merged

Constant-time GroupElement.cmov() #31

merged 2 commits into from
Mar 11, 2017

Conversation

ivmaykov
Copy link
Contributor

Bumped the pom version to 0.2.0-SNAPSHOT and implemented GroupElement.cmov() in true constant time for Ed25519 curve.

@str4d
Copy link
Owner

str4d commented Feb 25, 2017

Thanks for the PR @ivmaykov! Existing tests are indeed passing (the Travis failures are a separate test issue caused by the looped random GroupElement tests timing out). Could you please also add the following test of FieldElement.cmov() itself:

index a342f60..adb27ac 100644
--- a/test/net/i2p/crypto/eddsa/math/AbstractFieldElementTest.java
+++ b/test/net/i2p/crypto/eddsa/math/AbstractFieldElementTest.java
@@ -190,6 +190,23 @@ public abstract class AbstractFieldElementTest {
 
     // endregion
 
+    // region cmov
+
+    @Test
+    public void cmovReturnsCorrectResult() {
+        final FieldElement zero = getZeroFieldElement();
+        final FieldElement nz = getNonZeroFieldElement();
+        final FieldElement f = getRandomFieldElement();
+
+        assertThat(zero.cmov(nz, 0), is(equalTo(zero)));
+        assertThat(zero.cmov(nz, 1), is(equalTo(nz)));
+
+        assertThat(f.cmov(nz, 0), is(equalTo(f)));
+        assertThat(f.cmov(nz, 1), is(equalTo(nz)));
+    }
+
+    // endregion
+
     // region hashCode / equals
 
     @Test

I'm guessing that the existing code was not in fact constant-time because the Java compiler is more intelligent that I was giving it credit for? Lovely 😂 As annoying as a 35% decrease in speed is, the faster cmov was implemented when the underlying FieldElement code was all BigInteger-based and very slow (3d12a25, de84365), so signing is still faster than it was then. And I do lean towards constant-time-ness 😃

@str4d str4d modified the milestone: 0.2 Feb 25, 2017
@ivmaykov ivmaykov mentioned this pull request Feb 25, 2017
@ivmaykov ivmaykov force-pushed the master branch 2 times, most recently from b99c6ba to 6241ec1 Compare February 25, 2017 21:43
@ivmaykov
Copy link
Contributor Author

@str4d added test and rebased to master

@ivmaykov
Copy link
Contributor Author

@str4d yeah, when trying to implement constant-time operations, CPU branch predictor is your enemy. A for loop is basically syntactic sugar for if checks and jumps, so the branch predictor will try to optimize it and get in the way. Compiler may try to do something clever too, but I'm not sure - I haven't looked at the generated byte code.

@ivmaykov
Copy link
Contributor Author

ivmaykov commented Mar 2, 2017

@str4d any chance this will get merged soon?

@str4d
Copy link
Owner

str4d commented Mar 4, 2017

Speed tests via the I2P test harness:

Before

Testing EdDSA_SHA512_Ed25519
EdDSA_SHA512_Ed25519 key gen 1000 times: 0.259003929 ms each
EdDSA_SHA512_Ed25519 private-to-public test PASSED
EdDSA_SHA512_Ed25519 sign/verify 1000 times: 714 ms = 0.187 each sign, 0.527 each verify, 0.714 s+v

After

Testing EdDSA_SHA512_Ed25519
EdDSA_SHA512_Ed25519 key gen 1000 times: 0.338187675 ms each
EdDSA_SHA512_Ed25519 private-to-public test PASSED
EdDSA_SHA512_Ed25519 sign/verify 1000 times: 828 ms = 0.296 each sign, 0.532 each verify, 0.828 s+v

So this results in a 58% increase in time per signature, or a 36% decrease in signing speed, matching @ivmaykov's observations.

Copy link
Owner

@str4d str4d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK. A few nice-to-have comments, but I'll merge this beginning of next week if there are no further changes.

int g6 = that.t[6];
int g7 = that.t[7];
int g8 = that.t[8];
int g9 = that.t[9];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: the individual copies are probably unnecessary.

int x6 = f6 ^ g6;
int x7 = f7 ^ g7;
int x8 = f8 ^ g8;
int x9 = f9 ^ g9;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: each of these steps could possibly be rolled up, e.g.

for (int i = 0; i < 10; i++) {
    x[i] = t[i] ^ g[i];
}

See the equivalent handling in add(), subtract() and negate(). We'd need to check whether the branch predictor would interfere with the multiple sequential for loops, even though they would be fixed-length (unlike the for loop that this PR replaces, which conditionally ended on the secret b).

Even more concisely:

b = -b;
for (int i = 0; i < 10; i++) {
    x[i] = t[i] ^ g[i];
    x[i] &= b;
    f[i] ^= x[i];
}

But I'm less confident that the re-ordering of b = -b doesn't enable the compiler to make any optimisations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually makes the code a few percent faster. My guess is this is thanks to the JIT auto-vectorization of tight loops (i.e. it's able to translate the loop into SSE instructions). But I haven't confirmed it by looking at the assembly or anything like that.

@@ -70,5 +70,7 @@ public FieldElement divide(FieldElement val) {

public abstract FieldElement pow22523();

public abstract FieldElement cmov(FieldElement g, final int b);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/g/val for consistency.

@@ -105,6 +105,13 @@ public FieldElement pow22523(){
}

@Override
public FieldElement cmov(FieldElement g, int b) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/g/val for consistency.

* @return a copy of this if b == 0, or a copy of g if b == 1.
*/
@Override
public FieldElement cmov(FieldElement g, int b) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/g/val for consistency (and ditto in the comments).

@ivmaykov
Copy link
Contributor Author

ivmaykov commented Mar 5, 2017

@str4d made changes you requested

@str4d
Copy link
Owner

str4d commented Mar 11, 2017

Java bytecode for the rolled-up cmov looks sensible (definitely only doing a jump on i). Benchmark shows a 30% decrease in signing speed:

Testing EdDSA_SHA512_Ed25519
EdDSA_SHA512_Ed25519 key gen 1000 times: 0.298437416 ms each
EdDSA_SHA512_Ed25519 private-to-public test PASSED
EdDSA_SHA512_Ed25519 sign/verify 1000 times: 752 ms = 0.265 each sign, 0.487 each verify, 0.752 s+v

Looks great to me. Thanks @ivmaykov!

@str4d str4d merged commit 8497fa1 into str4d:master Mar 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants