NTRU Prime: avoid unsigned unary negation

This commit is contained in:
John M. Schanck 2020-09-01 16:18:21 -04:00 committed by Kris Kwiatkowski
parent 191aed977f
commit 4883bb44c0
9 changed files with 9 additions and 9 deletions

View File

@ -16,7 +16,7 @@
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }

View File

@ -15,7 +15,7 @@
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }

View File

@ -14,7 +14,7 @@ typedef int8 small;
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }

View File

@ -16,7 +16,7 @@
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }

View File

@ -15,7 +15,7 @@
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }

View File

@ -14,7 +14,7 @@ typedef int8 small;
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }

View File

@ -16,7 +16,7 @@
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }

View File

@ -15,7 +15,7 @@
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }

View File

@ -14,7 +14,7 @@ typedef int8 small;
static int int16_nonzero_mask(int16 x) { static int int16_nonzero_mask(int16 x) {
uint16 u = (uint16) x; /* 0, else 1...65535 */ uint16 u = (uint16) x; /* 0, else 1...65535 */
uint32 v = u; /* 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 */ v >>= 31; /* 0, else 1 */
return -(int) v; /* 0, else -1 */ return -(int) v; /* 0, else -1 */
} }