From 9048def7c7495d2e458df80bc5d7affae362ba70 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 3 Dec 2014 18:10:52 +0100 Subject: [PATCH] Avoid undefined shift behaviour --- src/scalar_4x64_impl.h | 2 +- src/scalar_8x32_impl.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scalar_4x64_impl.h b/src/scalar_4x64_impl.h index afa7a540181..ef4438d522e 100644 --- a/src/scalar_4x64_impl.h +++ b/src/scalar_4x64_impl.h @@ -95,7 +95,7 @@ static void secp256k1_scalar_add(secp256k1_scalar_t *r, const secp256k1_scalar_t static void secp256k1_scalar_add_bit(secp256k1_scalar_t *r, unsigned int bit) { VERIFY_CHECK(bit < 256); - uint128_t t = (uint128_t)r->d[0] + (((uint64_t)((bit >> 6) == 0)) << bit); + uint128_t t = (uint128_t)r->d[0] + (((uint64_t)((bit >> 6) == 0)) << (bit & 0x3F)); r->d[0] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64; t += (uint128_t)r->d[1] + (((uint64_t)((bit >> 6) == 1)) << (bit & 0x3F)); r->d[1] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64; diff --git a/src/scalar_8x32_impl.h b/src/scalar_8x32_impl.h index 0f82bfbb0b6..810cd6160be 100644 --- a/src/scalar_8x32_impl.h +++ b/src/scalar_8x32_impl.h @@ -133,7 +133,7 @@ static void secp256k1_scalar_add(secp256k1_scalar_t *r, const secp256k1_scalar_t static void secp256k1_scalar_add_bit(secp256k1_scalar_t *r, unsigned int bit) { VERIFY_CHECK(bit < 256); - uint64_t t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << bit); + uint64_t t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F)); r->d[0] = t & 0xFFFFFFFFULL; t >>= 32; t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F)); r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;