From 22c4feb174df59cc4ebcef981d223621087a7d89 Mon Sep 17 00:00:00 2001 From: "John M. Schanck" Date: Tue, 1 Sep 2020 16:18:21 -0400 Subject: [PATCH] NTRU Prime: avoid unsigned unary negation --- crypto_kem/sntrup653/avx2/crypto_core_invsntrup653.c | 2 +- crypto_kem/sntrup653/clean/crypto_core_inv3sntrup653.c | 2 +- crypto_kem/sntrup653/clean/crypto_core_wforcesntrup653.c | 2 +- crypto_kem/sntrup761/avx2/crypto_core_invsntrup761.c | 2 +- crypto_kem/sntrup761/clean/crypto_core_inv3sntrup761.c | 2 +- crypto_kem/sntrup761/clean/crypto_core_wforcesntrup761.c | 2 +- crypto_kem/sntrup857/avx2/crypto_core_invsntrup857.c | 2 +- crypto_kem/sntrup857/clean/crypto_core_inv3sntrup857.c | 2 +- crypto_kem/sntrup857/clean/crypto_core_wforcesntrup857.c | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crypto_kem/sntrup653/avx2/crypto_core_invsntrup653.c b/crypto_kem/sntrup653/avx2/crypto_core_invsntrup653.c index be775232..c7807e1c 100644 --- a/crypto_kem/sntrup653/avx2/crypto_core_invsntrup653.c +++ b/crypto_kem/sntrup653/avx2/crypto_core_invsntrup653.c @@ -16,7 +16,7 @@ static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ } diff --git a/crypto_kem/sntrup653/clean/crypto_core_inv3sntrup653.c b/crypto_kem/sntrup653/clean/crypto_core_inv3sntrup653.c index bb4e752c..ef09babe 100644 --- a/crypto_kem/sntrup653/clean/crypto_core_inv3sntrup653.c +++ b/crypto_kem/sntrup653/clean/crypto_core_inv3sntrup653.c @@ -15,7 +15,7 @@ static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ } diff --git a/crypto_kem/sntrup653/clean/crypto_core_wforcesntrup653.c b/crypto_kem/sntrup653/clean/crypto_core_wforcesntrup653.c index 80e55851..4c3e616b 100644 --- a/crypto_kem/sntrup653/clean/crypto_core_wforcesntrup653.c +++ b/crypto_kem/sntrup653/clean/crypto_core_wforcesntrup653.c @@ -14,7 +14,7 @@ typedef int8 small; static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ } diff --git a/crypto_kem/sntrup761/avx2/crypto_core_invsntrup761.c b/crypto_kem/sntrup761/avx2/crypto_core_invsntrup761.c index 7ef48607..b8dcfa5e 100644 --- a/crypto_kem/sntrup761/avx2/crypto_core_invsntrup761.c +++ b/crypto_kem/sntrup761/avx2/crypto_core_invsntrup761.c @@ -16,7 +16,7 @@ static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ } diff --git a/crypto_kem/sntrup761/clean/crypto_core_inv3sntrup761.c b/crypto_kem/sntrup761/clean/crypto_core_inv3sntrup761.c index 8db3bb2a..99342bce 100644 --- a/crypto_kem/sntrup761/clean/crypto_core_inv3sntrup761.c +++ b/crypto_kem/sntrup761/clean/crypto_core_inv3sntrup761.c @@ -15,7 +15,7 @@ static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ } diff --git a/crypto_kem/sntrup761/clean/crypto_core_wforcesntrup761.c b/crypto_kem/sntrup761/clean/crypto_core_wforcesntrup761.c index a60b5405..01dc9ddd 100644 --- a/crypto_kem/sntrup761/clean/crypto_core_wforcesntrup761.c +++ b/crypto_kem/sntrup761/clean/crypto_core_wforcesntrup761.c @@ -14,7 +14,7 @@ typedef int8 small; static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ } diff --git a/crypto_kem/sntrup857/avx2/crypto_core_invsntrup857.c b/crypto_kem/sntrup857/avx2/crypto_core_invsntrup857.c index 4ca429cd..bc0fd1b4 100644 --- a/crypto_kem/sntrup857/avx2/crypto_core_invsntrup857.c +++ b/crypto_kem/sntrup857/avx2/crypto_core_invsntrup857.c @@ -16,7 +16,7 @@ static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ } diff --git a/crypto_kem/sntrup857/clean/crypto_core_inv3sntrup857.c b/crypto_kem/sntrup857/clean/crypto_core_inv3sntrup857.c index 60f313ed..6ea2adcb 100644 --- a/crypto_kem/sntrup857/clean/crypto_core_inv3sntrup857.c +++ b/crypto_kem/sntrup857/clean/crypto_core_inv3sntrup857.c @@ -15,7 +15,7 @@ static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ } diff --git a/crypto_kem/sntrup857/clean/crypto_core_wforcesntrup857.c b/crypto_kem/sntrup857/clean/crypto_core_wforcesntrup857.c index b7d5b1d8..174ab471 100644 --- a/crypto_kem/sntrup857/clean/crypto_core_wforcesntrup857.c +++ b/crypto_kem/sntrup857/clean/crypto_core_wforcesntrup857.c @@ -14,7 +14,7 @@ typedef int8 small; static int int16_nonzero_mask(int16 x) { uint16 u = (uint16) x; /* 0, else 1...65535 */ uint32 v = u; /* 0, else 1...65535 */ - v = -v; /* 0, else 2^32-65535...2^32-1 */ + v = ~v + 1; /* 0, else 2^32-65535...2^32-1 */ v >>= 31; /* 0, else 1 */ return -(int) v; /* 0, else -1 */ }