aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/pubkey
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-12-31 14:32:06 -0500
committerJack Lloyd <[email protected]>2017-12-31 14:32:43 -0500
commit342596073420efc7000a161f69e5a6928d6a2e53 (patch)
tree42753e232352119aeb2bcab0d111b80452e44a85 /src/lib/pubkey
parent987662117c2b98b12fdbe8e8ab3eaf8c93c748e3 (diff)
Resolve undefined behavior in Ed25519 code
Use multiplications instead of shifting directly.
Diffstat (limited to 'src/lib/pubkey')
-rw-r--r--src/lib/pubkey/ed25519/ed25519_fe.cpp222
-rw-r--r--src/lib/pubkey/ed25519/ge.cpp3
-rw-r--r--src/lib/pubkey/ed25519/sc_muladd.cpp233
-rw-r--r--src/lib/pubkey/ed25519/sc_reduce.cpp142
4 files changed, 313 insertions, 287 deletions
diff --git a/src/lib/pubkey/ed25519/ed25519_fe.cpp b/src/lib/pubkey/ed25519/ed25519_fe.cpp
index 067c9dc18..5aa515e9b 100644
--- a/src/lib/pubkey/ed25519/ed25519_fe.cpp
+++ b/src/lib/pubkey/ed25519/ed25519_fe.cpp
@@ -277,71 +277,74 @@ FE_25519 FE_25519::mul(const FE_25519& f, const FE_25519& g)
|h1| <= (1.65*1.65*2^51*(1+1+19+19+19+19+19+19+19+19))
i.e. |h1| <= 1.7*2^59; narrower ranges for h3, h5, h7, h9
*/
+ const int64_t X24 = (1 << 24);
+ const int64_t X25 = (1 << 25);
+ const int64_t X26 = (1 << 26);
- carry0 = (h0 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry0 = (h0 + X25) >> 26;
h1 += carry0;
- h0 -= carry0 << 26;
- carry4 = (h4 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h0 -= carry0 * X26;
+ carry4 = (h4 + X25) >> 26;
h5 += carry4;
- h4 -= carry4 << 26;
+ h4 -= carry4 * X26;
/* |h0| <= 2^25 */
/* |h4| <= 2^25 */
/* |h1| <= 1.71*2^59 */
/* |h5| <= 1.71*2^59 */
- carry1 = (h1 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry1 = (h1 + X24) >> 25;
h2 += carry1;
- h1 -= carry1 << 25;
- carry5 = (h5 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h1 -= carry1 * X25;
+ carry5 = (h5 + X24) >> 25;
h6 += carry5;
- h5 -= carry5 << 25;
+ h5 -= carry5 * X25;
/* |h1| <= 2^24; from now on fits into int32 */
/* |h5| <= 2^24; from now on fits into int32 */
/* |h2| <= 1.41*2^60 */
/* |h6| <= 1.41*2^60 */
- carry2 = (h2 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry2 = (h2 + X25) >> 26;
h3 += carry2;
- h2 -= carry2 << 26;
- carry6 = (h6 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h2 -= carry2 * X26;
+ carry6 = (h6 + X25) >> 26;
h7 += carry6;
- h6 -= carry6 << 26;
+ h6 -= carry6 * X26;
/* |h2| <= 2^25; from now on fits into int32 unchanged */
/* |h6| <= 2^25; from now on fits into int32 unchanged */
/* |h3| <= 1.71*2^59 */
/* |h7| <= 1.71*2^59 */
- carry3 = (h3 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry3 = (h3 + X24) >> 25;
h4 += carry3;
- h3 -= carry3 << 25;
- carry7 = (h7 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h3 -= carry3 * X25;
+ carry7 = (h7 + X24) >> 25;
h8 += carry7;
- h7 -= carry7 << 25;
+ h7 -= carry7 * X25;
/* |h3| <= 2^24; from now on fits into int32 unchanged */
/* |h7| <= 2^24; from now on fits into int32 unchanged */
/* |h4| <= 1.72*2^34 */
/* |h8| <= 1.41*2^60 */
- carry4 = (h4 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry4 = (h4 + X25) >> 26;
h5 += carry4;
- h4 -= carry4 << 26;
- carry8 = (h8 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h4 -= carry4 * X26;
+ carry8 = (h8 + X25) >> 26;
h9 += carry8;
- h8 -= carry8 << 26;
+ h8 -= carry8 * X26;
/* |h4| <= 2^25; from now on fits into int32 unchanged */
/* |h8| <= 2^25; from now on fits into int32 unchanged */
/* |h5| <= 1.01*2^24 */
/* |h9| <= 1.71*2^59 */
- carry9 = (h9 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry9 = (h9 + X24) >> 25;
h0 += carry9 * 19;
- h9 -= carry9 << 25;
+ h9 -= carry9 * X25;
/* |h9| <= 2^24; from now on fits into int32 unchanged */
/* |h0| <= 1.1*2^39 */
- carry0 = (h0 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry0 = (h0 + X25) >> 26;
h1 += carry0;
- h0 -= carry0 << 26;
+ h0 -= carry0 * X26;
/* |h0| <= 2^25; from now on fits into int32 unchanged */
/* |h1| <= 1.01*2^24 */
@@ -366,6 +369,10 @@ See fe_mul.c for discussion of implementation strategy.
//static
FE_25519 FE_25519::sqr_iter(const FE_25519& f, size_t iter)
{
+ const int64_t X24 = (1 << 24);
+ const int64_t X25 = (1 << 25);
+ const int64_t X26 = (1 << 26);
+
int32_t f0 = f[0];
int32_t f1 = f[1];
int32_t f2 = f[2];
@@ -471,47 +478,47 @@ FE_25519 FE_25519::sqr_iter(const FE_25519& f, size_t iter)
int64_t carry8;
int64_t carry9;
- carry0 = (h0 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry0 = (h0 + X25) >> 26;
h1 += carry0;
- h0 -= carry0 << 26;
- carry4 = (h4 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h0 -= carry0 * X26;
+ carry4 = (h4 + X25) >> 26;
h5 += carry4;
- h4 -= carry4 << 26;
+ h4 -= carry4 * X26;
- carry1 = (h1 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry1 = (h1 + X24) >> 25;
h2 += carry1;
- h1 -= carry1 << 25;
- carry5 = (h5 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h1 -= carry1 * X25;
+ carry5 = (h5 + X24) >> 25;
h6 += carry5;
- h5 -= carry5 << 25;
+ h5 -= carry5 * X25;
- carry2 = (h2 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry2 = (h2 + X25) >> 26;
h3 += carry2;
- h2 -= carry2 << 26;
- carry6 = (h6 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h2 -= carry2 * X26;
+ carry6 = (h6 + X25) >> 26;
h7 += carry6;
- h6 -= carry6 << 26;
+ h6 -= carry6 * X26;
- carry3 = (h3 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry3 = (h3 + X24) >> 25;
h4 += carry3;
- h3 -= carry3 << 25;
- carry7 = (h7 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h3 -= carry3 * X25;
+ carry7 = (h7 + X24) >> 25;
h8 += carry7;
- h7 -= carry7 << 25;
+ h7 -= carry7 * X25;
- carry4 = (h4 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry4 = (h4 + X25) >> 26;
h5 += carry4;
- h4 -= carry4 << 26;
- carry8 = (h8 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h4 -= carry4 * X26;
+ carry8 = (h8 + X25) >> 26;
h9 += carry8;
- h8 -= carry8 << 26;
+ h8 -= carry8 * X26;
- carry9 = (h9 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry9 = (h9 + X24) >> 25;
h0 += carry9 * 19;
- h9 -= carry9 << 25;
- carry0 = (h0 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h9 -= carry9 * X25;
+ carry0 = (h0 + X25) >> 26;
h1 += carry0;
- h0 -= carry0 << 26;
+ h0 -= carry0 * X26;
f0 = h0;
f1 = h1;
@@ -546,6 +553,10 @@ See fe_mul.c for discussion of implementation strategy.
//static
FE_25519 FE_25519::sqr2(const FE_25519& f)
{
+ const int64_t X24 = (1 << 24);
+ const int64_t X25 = (1 << 25);
+ const int64_t X26 = (1 << 26);
+
int32_t f0 = f[0];
int32_t f1 = f[1];
int32_t f2 = f[2];
@@ -656,48 +667,48 @@ FE_25519 FE_25519::sqr2(const FE_25519& f)
h8 += h8;
h9 += h9;
- carry0 = (h0 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry0 = (h0 + X25) >> 26;
h1 += carry0;
- h0 -= carry0 << 26;
- carry4 = (h4 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h0 -= carry0 * X26;
+ carry4 = (h4 + X25) >> 26;
h5 += carry4;
- h4 -= carry4 << 26;
+ h4 -= carry4 * X26;
- carry1 = (h1 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry1 = (h1 + X24) >> 25;
h2 += carry1;
- h1 -= carry1 << 25;
- carry5 = (h5 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h1 -= carry1 * X25;
+ carry5 = (h5 + X24) >> 25;
h6 += carry5;
- h5 -= carry5 << 25;
+ h5 -= carry5 * X25;
- carry2 = (h2 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry2 = (h2 + X25) >> 26;
h3 += carry2;
- h2 -= carry2 << 26;
- carry6 = (h6 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h2 -= carry2 * X26;
+ carry6 = (h6 + X25) >> 26;
h7 += carry6;
- h6 -= carry6 << 26;
+ h6 -= carry6 * X26;
- carry3 = (h3 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry3 = (h3 + X24) >> 25;
h4 += carry3;
- h3 -= carry3 << 25;
- carry7 = (h7 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h3 -= carry3 * X25;
+ carry7 = (h7 + X24) >> 25;
h8 += carry7;
- h7 -= carry7 << 25;
+ h7 -= carry7 * X25;
- carry4 = (h4 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry4 = (h4 + X25) >> 26;
h5 += carry4;
- h4 -= carry4 << 26;
- carry8 = (h8 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h4 -= carry4 * X26;
+ carry8 = (h8 + X25) >> 26;
h9 += carry8;
- h8 -= carry8 << 26;
+ h8 -= carry8 * X26;
- carry9 = (h9 + (static_cast<int64_t>(1) << 24)) >> 25;
+ carry9 = (h9 + X24) >> 25;
h0 += carry9 * 19;
- h9 -= carry9 << 25;
+ h9 -= carry9 * X25;
- carry0 = (h0 + (static_cast<int64_t>(1) << 25)) >> 26;
+ carry0 = (h0 + X25) >> 26;
h1 += carry0;
- h0 -= carry0 << 26;
+ h0 -= carry0 * X26;
return FE_25519(h0, h1, h2, h3, h4, h5, h6, h7, h8, h9);
}
@@ -708,6 +719,10 @@ Ignores top bit of h.
void FE_25519::from_bytes(const uint8_t s[32])
{
+ const int64_t X24 = (1 << 24);
+ const int64_t X25 = (1 << 25);
+ const int64_t X26 = (1 << 26);
+
int64_t h0 = load_4(s);
int64_t h1 = load_3(s + 4) << 6;
int64_t h2 = load_3(s + 7) << 5;
@@ -719,37 +734,37 @@ void FE_25519::from_bytes(const uint8_t s[32])
int64_t h8 = load_3(s + 26) << 4;
int64_t h9 = (load_3(s + 29) & 0x7fffff) << 2;
- const int64_t carry9 = (h9 + (static_cast<int64_t>(1) << 24)) >> 25;
+ const int64_t carry9 = (h9 + X24) >> 25;
h0 += carry9 * 19;
- h9 -= carry9 << 25;
- const int64_t carry1 = (h1 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h9 -= carry9 * X25;
+ const int64_t carry1 = (h1 + X24) >> 25;
h2 += carry1;
- h1 -= carry1 << 25;
- const int64_t carry3 = (h3 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h1 -= carry1 * X25;
+ const int64_t carry3 = (h3 + X24) >> 25;
h4 += carry3;
- h3 -= carry3 << 25;
- const int64_t carry5 = (h5 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h3 -= carry3 * X25;
+ const int64_t carry5 = (h5 + X24) >> 25;
h6 += carry5;
- h5 -= carry5 << 25;
- const int64_t carry7 = (h7 + (static_cast<int64_t>(1) << 24)) >> 25;
+ h5 -= carry5 * X25;
+ const int64_t carry7 = (h7 + X24) >> 25;
h8 += carry7;
- h7 -= carry7 << 25;
+ h7 -= carry7 * X25;
- const int64_t carry0 = (h0 + (static_cast<int64_t>(1) << 25)) >> 26;
+ const int64_t carry0 = (h0 + X25) >> 26;
h1 += carry0;
- h0 -= carry0 << 26;
- const int64_t carry2 = (h2 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h0 -= carry0 * X26;
+ const int64_t carry2 = (h2 + X25) >> 26;
h3 += carry2;
- h2 -= carry2 << 26;
- const int64_t carry4 = (h4 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h2 -= carry2 * X26;
+ const int64_t carry4 = (h4 + X25) >> 26;
h5 += carry4;
- h4 -= carry4 << 26;
- const int64_t carry6 = (h6 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h4 -= carry4 * X26;
+ const int64_t carry6 = (h6 + X25) >> 26;
h7 += carry6;
- h6 -= carry6 << 26;
- const int64_t carry8 = (h8 + (static_cast<int64_t>(1) << 25)) >> 26;
+ h6 -= carry6 * X26;
+ const int64_t carry8 = (h8 + X25) >> 26;
h9 += carry8;
- h8 -= carry8 << 26;
+ h8 -= carry8 * X26;
m_fe[0] = h0;
m_fe[1] = h1;
@@ -790,6 +805,9 @@ so floor(2^(-255)(h + 19 2^(-25) h9 + 2^(-1))) = q.
void FE_25519::to_bytes(uint8_t s[32]) const
{
+ const int64_t X25 = (1 << 25);
+ const int64_t X26 = (1 << 26);
+
int32_t h0 = m_fe[0];
int32_t h1 = m_fe[1];
int32_t h2 = m_fe[2];
@@ -830,33 +848,33 @@ void FE_25519::to_bytes(uint8_t s[32]) const
carry0 = h0 >> 26;
h1 += carry0;
- h0 -= carry0 << 26;
+ h0 -= carry0 * X26;
carry1 = h1 >> 25;
h2 += carry1;
- h1 -= carry1 << 25;
+ h1 -= carry1 * X25;
carry2 = h2 >> 26;
h3 += carry2;
- h2 -= carry2 << 26;
+ h2 -= carry2 * X26;
carry3 = h3 >> 25;
h4 += carry3;
- h3 -= carry3 << 25;
+ h3 -= carry3 * X25;
carry4 = h4 >> 26;
h5 += carry4;
- h4 -= carry4 << 26;
+ h4 -= carry4 * X26;
carry5 = h5 >> 25;
h6 += carry5;
- h5 -= carry5 << 25;
+ h5 -= carry5 * X25;
carry6 = h6 >> 26;
h7 += carry6;
- h6 -= carry6 << 26;
+ h6 -= carry6 * X26;
carry7 = h7 >> 25;
h8 += carry7;
- h7 -= carry7 << 25;
+ h7 -= carry7 * X25;
carry8 = h8 >> 26;
h9 += carry8;
- h8 -= carry8 << 26;
+ h8 -= carry8 * X26;
carry9 = h9 >> 25;
- h9 -= carry9 << 25;
+ h9 -= carry9 * X25;
/* h10 = carry9 */
/*
diff --git a/src/lib/pubkey/ed25519/ge.cpp b/src/lib/pubkey/ed25519/ge.cpp
index a8cf593e8..4773602db 100644
--- a/src/lib/pubkey/ed25519/ge.cpp
+++ b/src/lib/pubkey/ed25519/ge.cpp
@@ -9,6 +9,7 @@
*/
#include <botan/internal/ed25519_internal.h>
+#include <assert.h>
namespace Botan {
@@ -2028,7 +2029,7 @@ inline void select(ge_precomp* t,
int8_t b)
{
uint8_t bnegative = negative(b);
- uint8_t babs = b - (((-bnegative) & b) << 1);
+ uint8_t babs = b - (((-bnegative) & b) * 2);
ge_precomp_0(t);
diff --git a/src/lib/pubkey/ed25519/sc_muladd.cpp b/src/lib/pubkey/ed25519/sc_muladd.cpp
index e4dcf3401..83863eeb4 100644
--- a/src/lib/pubkey/ed25519/sc_muladd.cpp
+++ b/src/lib/pubkey/ed25519/sc_muladd.cpp
@@ -25,6 +25,9 @@ Output:
void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c)
{
+ const int64_t X20 = (1 << 20);
+ const int64_t X21 = (1 << 21);
+
int64_t a0 = 2097151 & load_3(a);
int64_t a1 = 2097151 & (load_4(a + 2) >> 5);
int64_t a2 = 2097151 & (load_3(a + 5) >> 2);
@@ -134,76 +137,76 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c)
s22 = a11*b11;
s23 = 0;
- carry0 = (s0 + (1<<20)) >> 21;
+ carry0 = (s0 + X20) >> 21;
s1 += carry0;
- s0 -= carry0 << 21;
- carry2 = (s2 + (1<<20)) >> 21;
+ s0 -= carry0 * X21;
+ carry2 = (s2 + X20) >> 21;
s3 += carry2;
- s2 -= carry2 << 21;
- carry4 = (s4 + (1<<20)) >> 21;
+ s2 -= carry2 * X21;
+ carry4 = (s4 + X20) >> 21;
s5 += carry4;
- s4 -= carry4 << 21;
- carry6 = (s6 + (1<<20)) >> 21;
+ s4 -= carry4 * X21;
+ carry6 = (s6 + X20) >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
- carry8 = (s8 + (1<<20)) >> 21;
+ s6 -= carry6 * X21;
+ carry8 = (s8 + X20) >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
- carry10 = (s10 + (1<<20)) >> 21;
+ s8 -= carry8 * X21;
+ carry10 = (s10 + X20) >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
- carry12 = (s12 + (1<<20)) >> 21;
+ s10 -= carry10 * X21;
+ carry12 = (s12 + X20) >> 21;
s13 += carry12;
- s12 -= carry12 << 21;
- carry14 = (s14 + (1<<20)) >> 21;
+ s12 -= carry12 * X21;
+ carry14 = (s14 + X20) >> 21;
s15 += carry14;
- s14 -= carry14 << 21;
- carry16 = (s16 + (1<<20)) >> 21;
+ s14 -= carry14 * X21;
+ carry16 = (s16 + X20) >> 21;
s17 += carry16;
- s16 -= carry16 << 21;
- carry18 = (s18 + (1<<20)) >> 21;
+ s16 -= carry16 * X21;
+ carry18 = (s18 + X20) >> 21;
s19 += carry18;
- s18 -= carry18 << 21;
- carry20 = (s20 + (1<<20)) >> 21;
+ s18 -= carry18 * X21;
+ carry20 = (s20 + X20) >> 21;
s21 += carry20;
- s20 -= carry20 << 21;
- carry22 = (s22 + (1<<20)) >> 21;
+ s20 -= carry20 * X21;
+ carry22 = (s22 + X20) >> 21;
s23 += carry22;
- s22 -= carry22 << 21;
+ s22 -= carry22 * X21;
- carry1 = (s1 + (1<<20)) >> 21;
+ carry1 = (s1 + X20) >> 21;
s2 += carry1;
- s1 -= carry1 << 21;
- carry3 = (s3 + (1<<20)) >> 21;
+ s1 -= carry1 * X21;
+ carry3 = (s3 + X20) >> 21;
s4 += carry3;
- s3 -= carry3 << 21;
- carry5 = (s5 + (1<<20)) >> 21;
+ s3 -= carry3 * X21;
+ carry5 = (s5 + X20) >> 21;
s6 += carry5;
- s5 -= carry5 << 21;
- carry7 = (s7 + (1<<20)) >> 21;
+ s5 -= carry5 * X21;
+ carry7 = (s7 + X20) >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
- carry9 = (s9 + (1<<20)) >> 21;
+ s7 -= carry7 * X21;
+ carry9 = (s9 + X20) >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
- carry11 = (s11 + (1<<20)) >> 21;
+ s9 -= carry9 * X21;
+ carry11 = (s11 + X20) >> 21;
s12 += carry11;
- s11 -= carry11 << 21;
- carry13 = (s13 + (1<<20)) >> 21;
+ s11 -= carry11 * X21;
+ carry13 = (s13 + X20) >> 21;
s14 += carry13;
- s13 -= carry13 << 21;
- carry15 = (s15 + (1<<20)) >> 21;
+ s13 -= carry13 * X21;
+ carry15 = (s15 + X20) >> 21;
s16 += carry15;
- s15 -= carry15 << 21;
- carry17 = (s17 + (1<<20)) >> 21;
+ s15 -= carry15 * X21;
+ carry17 = (s17 + X20) >> 21;
s18 += carry17;
- s17 -= carry17 << 21;
- carry19 = (s19 + (1<<20)) >> 21;
+ s17 -= carry17 * X21;
+ carry19 = (s19 + X20) >> 21;
s20 += carry19;
- s19 -= carry19 << 21;
- carry21 = (s21 + (1<<20)) >> 21;
+ s19 -= carry19 * X21;
+ carry21 = (s21 + X20) >> 21;
s22 += carry21;
- s21 -= carry21 << 21;
+ s21 -= carry21 * X21;
s11 += s23 * 666643;
s12 += s23 * 470296;
@@ -253,40 +256,40 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c)
s11 -= s18 * 683901;
s18 = 0;
- carry6 = (s6 + (1<<20)) >> 21;
+ carry6 = (s6 + X20) >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
- carry8 = (s8 + (1<<20)) >> 21;
+ s6 -= carry6 * X21;
+ carry8 = (s8 + X20) >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
- carry10 = (s10 + (1<<20)) >> 21;
+ s8 -= carry8 * X21;
+ carry10 = (s10 + X20) >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
- carry12 = (s12 + (1<<20)) >> 21;
+ s10 -= carry10 * X21;
+ carry12 = (s12 + X20) >> 21;
s13 += carry12;
- s12 -= carry12 << 21;
- carry14 = (s14 + (1<<20)) >> 21;
+ s12 -= carry12 * X21;
+ carry14 = (s14 + X20) >> 21;
s15 += carry14;
- s14 -= carry14 << 21;
- carry16 = (s16 + (1<<20)) >> 21;
+ s14 -= carry14 * X21;
+ carry16 = (s16 + X20) >> 21;
s17 += carry16;
- s16 -= carry16 << 21;
+ s16 -= carry16 * X21;
- carry7 = (s7 + (1<<20)) >> 21;
+ carry7 = (s7 + X20) >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
- carry9 = (s9 + (1<<20)) >> 21;
+ s7 -= carry7 * X21;
+ carry9 = (s9 + X20) >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
- carry11 = (s11 + (1<<20)) >> 21;
+ s9 -= carry9 * X21;
+ carry11 = (s11 + X20) >> 21;
s12 += carry11;
- s11 -= carry11 << 21;
- carry13 = (s13 + (1<<20)) >> 21;
+ s11 -= carry11 * X21;
+ carry13 = (s13 + X20) >> 21;
s14 += carry13;
- s13 -= carry13 << 21;
- carry15 = (s15 + (1<<20)) >> 21;
+ s13 -= carry13 * X21;
+ carry15 = (s15 + X20) >> 21;
s16 += carry15;
- s15 -= carry15 << 21;
+ s15 -= carry15 * X21;
s5 += s17 * 666643;
s6 += s17 * 470296;
@@ -336,43 +339,43 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c)
s5 -= s12 * 683901;
s12 = 0;
- carry0 = (s0 + (1<<20)) >> 21;
+ carry0 = (s0 + X20) >> 21;
s1 += carry0;
- s0 -= carry0 << 21;
- carry2 = (s2 + (1<<20)) >> 21;
+ s0 -= carry0 * X21;
+ carry2 = (s2 + X20) >> 21;
s3 += carry2;
- s2 -= carry2 << 21;
- carry4 = (s4 + (1<<20)) >> 21;
+ s2 -= carry2 * X21;
+ carry4 = (s4 + X20) >> 21;
s5 += carry4;
- s4 -= carry4 << 21;
- carry6 = (s6 + (1<<20)) >> 21;
+ s4 -= carry4 * X21;
+ carry6 = (s6 + X20) >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
- carry8 = (s8 + (1<<20)) >> 21;
+ s6 -= carry6 * X21;
+ carry8 = (s8 + X20) >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
- carry10 = (s10 + (1<<20)) >> 21;
+ s8 -= carry8 * X21;
+ carry10 = (s10 + X20) >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
+ s10 -= carry10 * X21;
- carry1 = (s1 + (1<<20)) >> 21;
+ carry1 = (s1 + X20) >> 21;
s2 += carry1;
- s1 -= carry1 << 21;
- carry3 = (s3 + (1<<20)) >> 21;
+ s1 -= carry1 * X21;
+ carry3 = (s3 + X20) >> 21;
s4 += carry3;
- s3 -= carry3 << 21;
- carry5 = (s5 + (1<<20)) >> 21;
+ s3 -= carry3 * X21;
+ carry5 = (s5 + X20) >> 21;
s6 += carry5;
- s5 -= carry5 << 21;
- carry7 = (s7 + (1<<20)) >> 21;
+ s5 -= carry5 * X21;
+ carry7 = (s7 + X20) >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
- carry9 = (s9 + (1<<20)) >> 21;
+ s7 -= carry7 * X21;
+ carry9 = (s9 + X20) >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
- carry11 = (s11 + (1<<20)) >> 21;
+ s9 -= carry9 * X21;
+ carry11 = (s11 + X20) >> 21;
s12 += carry11;
- s11 -= carry11 << 21;
+ s11 -= carry11 * X21;
s0 += s12 * 666643;
s1 += s12 * 470296;
@@ -384,40 +387,40 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c)
carry0 = s0 >> 21;
s1 += carry0;
- s0 -= carry0 << 21;
+ s0 -= carry0 * X21;
carry1 = s1 >> 21;
s2 += carry1;
- s1 -= carry1 << 21;
+ s1 -= carry1 * X21;
carry2 = s2 >> 21;
s3 += carry2;
- s2 -= carry2 << 21;
+ s2 -= carry2 * X21;
carry3 = s3 >> 21;
s4 += carry3;
- s3 -= carry3 << 21;
+ s3 -= carry3 * X21;
carry4 = s4 >> 21;
s5 += carry4;
- s4 -= carry4 << 21;
+ s4 -= carry4 * X21;
carry5 = s5 >> 21;
s6 += carry5;
- s5 -= carry5 << 21;
+ s5 -= carry5 * X21;
carry6 = s6 >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
+ s6 -= carry6 * X21;
carry7 = s7 >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
+ s7 -= carry7 * X21;
carry8 = s8 >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
+ s8 -= carry8 * X21;
carry9 = s9 >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
+ s9 -= carry9 * X21;
carry10 = s10 >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
+ s10 -= carry10 * X21;
carry11 = s11 >> 21;
s12 += carry11;
- s11 -= carry11 << 21;
+ s11 -= carry11 * X21;
s0 += s12 * 666643;
s1 += s12 * 470296;
@@ -429,37 +432,37 @@ void sc_muladd(uint8_t* s, const uint8_t* a, const uint8_t* b, const uint8_t* c)
carry0 = s0 >> 21;
s1 += carry0;
- s0 -= carry0 << 21;
+ s0 -= carry0 * X21;
carry1 = s1 >> 21;
s2 += carry1;
- s1 -= carry1 << 21;
+ s1 -= carry1 * X21;
carry2 = s2 >> 21;
s3 += carry2;
- s2 -= carry2 << 21;
+ s2 -= carry2 * X21;
carry3 = s3 >> 21;
s4 += carry3;
- s3 -= carry3 << 21;
+ s3 -= carry3 * X21;
carry4 = s4 >> 21;
s5 += carry4;
- s4 -= carry4 << 21;
+ s4 -= carry4 * X21;
carry5 = s5 >> 21;
s6 += carry5;
- s5 -= carry5 << 21;
+ s5 -= carry5 * X21;
carry6 = s6 >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
+ s6 -= carry6 * X21;
carry7 = s7 >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
+ s7 -= carry7 * X21;
carry8 = s8 >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
+ s8 -= carry8 * X21;
carry9 = s9 >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
+ s9 -= carry9 * X21;
carry10 = s10 >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
+ s10 -= carry10 * X21;
s[0] = s0 >> 0;
s[1] = s0 >> 8;
diff --git a/src/lib/pubkey/ed25519/sc_reduce.cpp b/src/lib/pubkey/ed25519/sc_reduce.cpp
index 93dcc6de8..5765c7dac 100644
--- a/src/lib/pubkey/ed25519/sc_reduce.cpp
+++ b/src/lib/pubkey/ed25519/sc_reduce.cpp
@@ -24,6 +24,10 @@ Output:
void sc_reduce(uint8_t* s)
{
+ const int64_t X20 = (1 << 20);
+ const int64_t X21 = (1 << 21);
+
+
int64_t s0 = 2097151 & load_3(s);
int64_t s1 = 2097151 & (load_4(s + 2) >> 5);
int64_t s2 = 2097151 & (load_3(s + 5) >> 2);
@@ -114,40 +118,40 @@ void sc_reduce(uint8_t* s)
s11 -= s18 * 683901;
s18 = 0;
- carry6 = (s6 + (1<<20)) >> 21;
+ carry6 = (s6 + X20) >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
- carry8 = (s8 + (1<<20)) >> 21;
+ s6 -= carry6 * X21;
+ carry8 = (s8 + X20) >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
- carry10 = (s10 + (1<<20)) >> 21;
+ s8 -= carry8 * X21;
+ carry10 = (s10 + X20) >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
- carry12 = (s12 + (1<<20)) >> 21;
+ s10 -= carry10 * X21;
+ carry12 = (s12 + X20) >> 21;
s13 += carry12;
- s12 -= carry12 << 21;
- carry14 = (s14 + (1<<20)) >> 21;
+ s12 -= carry12 * X21;
+ carry14 = (s14 + X20) >> 21;
s15 += carry14;
- s14 -= carry14 << 21;
- carry16 = (s16 + (1<<20)) >> 21;
+ s14 -= carry14 * X21;
+ carry16 = (s16 + X20) >> 21;
s17 += carry16;
- s16 -= carry16 << 21;
+ s16 -= carry16 * X21;
- carry7 = (s7 + (1<<20)) >> 21;
+ carry7 = (s7 + X20) >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
- carry9 = (s9 + (1<<20)) >> 21;
+ s7 -= carry7 * X21;
+ carry9 = (s9 + X20) >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
- carry11 = (s11 + (1<<20)) >> 21;
+ s9 -= carry9 * X21;
+ carry11 = (s11 + X20) >> 21;
s12 += carry11;
- s11 -= carry11 << 21;
- carry13 = (s13 + (1<<20)) >> 21;
+ s11 -= carry11 * X21;
+ carry13 = (s13 + X20) >> 21;
s14 += carry13;
- s13 -= carry13 << 21;
- carry15 = (s15 + (1<<20)) >> 21;
+ s13 -= carry13 * X21;
+ carry15 = (s15 + X20) >> 21;
s16 += carry15;
- s15 -= carry15 << 21;
+ s15 -= carry15 * X21;
s5 += s17 * 666643;
s6 += s17 * 470296;
@@ -197,43 +201,43 @@ void sc_reduce(uint8_t* s)
s5 -= s12 * 683901;
s12 = 0;
- carry0 = (s0 + (1<<20)) >> 21;
+ carry0 = (s0 + X20) >> 21;
s1 += carry0;
- s0 -= carry0 << 21;
- carry2 = (s2 + (1<<20)) >> 21;
+ s0 -= carry0 * X21;
+ carry2 = (s2 + X20) >> 21;
s3 += carry2;
- s2 -= carry2 << 21;
- carry4 = (s4 + (1<<20)) >> 21;
+ s2 -= carry2 * X21;
+ carry4 = (s4 + X20) >> 21;
s5 += carry4;
- s4 -= carry4 << 21;
- carry6 = (s6 + (1<<20)) >> 21;
+ s4 -= carry4 * X21;
+ carry6 = (s6 + X20) >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
- carry8 = (s8 + (1<<20)) >> 21;
+ s6 -= carry6 * X21;
+ carry8 = (s8 + X20) >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
- carry10 = (s10 + (1<<20)) >> 21;
+ s8 -= carry8 * X21;
+ carry10 = (s10 + X20) >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
+ s10 -= carry10 * X21;
- carry1 = (s1 + (1<<20)) >> 21;
+ carry1 = (s1 + X20) >> 21;
s2 += carry1;
- s1 -= carry1 << 21;
- carry3 = (s3 + (1<<20)) >> 21;
+ s1 -= carry1 * X21;
+ carry3 = (s3 + X20) >> 21;
s4 += carry3;
- s3 -= carry3 << 21;
- carry5 = (s5 + (1<<20)) >> 21;
+ s3 -= carry3 * X21;
+ carry5 = (s5 + X20) >> 21;
s6 += carry5;
- s5 -= carry5 << 21;
- carry7 = (s7 + (1<<20)) >> 21;
+ s5 -= carry5 * X21;
+ carry7 = (s7 + X20) >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
- carry9 = (s9 + (1<<20)) >> 21;
+ s7 -= carry7 * X21;
+ carry9 = (s9 + X20) >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
- carry11 = (s11 + (1<<20)) >> 21;
+ s9 -= carry9 * X21;
+ carry11 = (s11 + X20) >> 21;
s12 += carry11;
- s11 -= carry11 << 21;
+ s11 -= carry11 * X21;
s0 += s12 * 666643;
s1 += s12 * 470296;
@@ -245,40 +249,40 @@ void sc_reduce(uint8_t* s)
carry0 = s0 >> 21;
s1 += carry0;
- s0 -= carry0 << 21;
+ s0 -= carry0 * X21;
carry1 = s1 >> 21;
s2 += carry1;
- s1 -= carry1 << 21;
+ s1 -= carry1 * X21;
carry2 = s2 >> 21;
s3 += carry2;
- s2 -= carry2 << 21;
+ s2 -= carry2 * X21;
carry3 = s3 >> 21;
s4 += carry3;
- s3 -= carry3 << 21;
+ s3 -= carry3 * X21;
carry4 = s4 >> 21;
s5 += carry4;
- s4 -= carry4 << 21;
+ s4 -= carry4 * X21;
carry5 = s5 >> 21;
s6 += carry5;
- s5 -= carry5 << 21;
+ s5 -= carry5 * X21;
carry6 = s6 >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
+ s6 -= carry6 * X21;
carry7 = s7 >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
+ s7 -= carry7 * X21;
carry8 = s8 >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
+ s8 -= carry8 * X21;
carry9 = s9 >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
+ s9 -= carry9 * X21;
carry10 = s10 >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
+ s10 -= carry10 * X21;
carry11 = s11 >> 21;
s12 += carry11;
- s11 -= carry11 << 21;
+ s11 -= carry11 * X21;
s0 += s12 * 666643;
s1 += s12 * 470296;
@@ -290,37 +294,37 @@ void sc_reduce(uint8_t* s)
carry0 = s0 >> 21;
s1 += carry0;
- s0 -= carry0 << 21;
+ s0 -= carry0 * X21;
carry1 = s1 >> 21;
s2 += carry1;
- s1 -= carry1 << 21;
+ s1 -= carry1 * X21;
carry2 = s2 >> 21;
s3 += carry2;
- s2 -= carry2 << 21;
+ s2 -= carry2 * X21;
carry3 = s3 >> 21;
s4 += carry3;
- s3 -= carry3 << 21;
+ s3 -= carry3 * X21;
carry4 = s4 >> 21;
s5 += carry4;
- s4 -= carry4 << 21;
+ s4 -= carry4 * X21;
carry5 = s5 >> 21;
s6 += carry5;
- s5 -= carry5 << 21;
+ s5 -= carry5 * X21;
carry6 = s6 >> 21;
s7 += carry6;
- s6 -= carry6 << 21;
+ s6 -= carry6 * X21;
carry7 = s7 >> 21;
s8 += carry7;
- s7 -= carry7 << 21;
+ s7 -= carry7 * X21;
carry8 = s8 >> 21;
s9 += carry8;
- s8 -= carry8 << 21;
+ s8 -= carry8 * X21;
carry9 = s9 >> 21;
s10 += carry9;
- s9 -= carry9 << 21;
+ s9 -= carry9 * X21;
carry10 = s10 >> 21;
s11 += carry10;
- s10 -= carry10 << 21;
+ s10 -= carry10 * X21;
s[0] = s0 >> 0;
s[1] = s0 >> 8;