From 7cd252c6defc01f1f199bcdff57c3fb05a64eab6 Mon Sep 17 00:00:00 2001 From: pmvr Date: Sun, 14 Jun 2020 16:30:49 +0200 Subject: [PATCH] small improvement for inversion --- README.md | 6 +++--- mpy-modules/curve25519/arithmetic.c | 18 ++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cd1acf6..02b7bf7 100644 --- a/README.md +++ b/README.md @@ -41,15 +41,15 @@ Python script `x25519.py` Python script `ed25519.py` Test 1: Length of message: 0 bytes - Computation time: 53 ms + Computation time: 52 ms Test 1 passed. Test 2: Length of message: 1 byte - Computation time: 53 ms + Computation time: 52 ms Test 2 passed. Test 3: Length of message: 2 bytes - Computation time: 53 ms + Computation time: 52 ms Test 3 passed. Test 4: Length of message: 1023 bytes diff --git a/mpy-modules/curve25519/arithmetic.c b/mpy-modules/curve25519/arithmetic.c index fb613f5..6d47298 100644 --- a/mpy-modules/curve25519/arithmetic.c +++ b/mpy-modules/curve25519/arithmetic.c @@ -104,7 +104,8 @@ STEP_4: shift_right(D); } sub9_zxy(u, u, v); - if ((u[8] & 0x80000000) == 0) { // u >= v + if ((u[8] & 0x80000000) == 0) { + // u >= v sub9_zxy(B, B, D); } else { @@ -112,16 +113,13 @@ STEP_4: sub9_zxy(v, v, u); sub9_zxy(D, D, B); } - uint32_t cmp = 0; - for (int i=0; i<9; i++) cmp |= u[i]; - if (cmp == 0) { - if (D[8] & 0x80000000) { - add9_zxy(D, D, m); // D < 0 - } - for (uint32_t i=0; i<8; i++) y[i] = D[i]; - return; + for (uint32_t i=0; i<9; i++) { + if (u[i] != 0) goto STEP_4; } - goto STEP_4; + if (D[8] & 0x80000000) { + add9_zxy(D, D, m); // D < 0 + } + for (uint32_t i=0; i<8; i++) y[i] = D[i]; }