From 885c51bfc1c9efd1601084c49f863d064565b1a7 Mon Sep 17 00:00:00 2001 From: Cheng Date: Thu, 24 Feb 2022 10:59:13 +1000 Subject: [PATCH] Fixed template bug in serialization --- bit_hacks.h | 75 ----------------------------- introspection_of_standard_C_types.h | 4 +- ristretto255.h | 12 ++--- 3 files changed, 8 insertions(+), 83 deletions(-) delete mode 100644 bit_hacks.h diff --git a/bit_hacks.h b/bit_hacks.h deleted file mode 100644 index 21a4673..0000000 --- a/bit_hacks.h +++ /dev/null @@ -1,75 +0,0 @@ -#pragma once - -// We should template this to use __popcnt64 if available -// but that is premature optimization -inline uint64_t bitcount(uint64_t c) { - c = c - ((c >> 1) & 0x5555555555555555); - c = ((c >> 2) & 0x3333333333333333) + - (c & 0x3333333333333333); - c = ((c >> 4) + c) & 0x0F0F0F0F0F0F0F0F; - c = ((c >> 8) + c) & 0x00FF00FF00FF00FF; - c = ((c >> 16) + c) & 0x0000FFFF0000FFFF; - c = ((c >> 32) + c) & 0x00000000FFFFFFFF; - return c; -} - -// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog "Bit Hacks" -// Find ⌊log2⌋ -// We should template this to use lzcnt64, __builtin_clz or _BitScanReverse if available, -// but that is premature optimization. -inline auto rounded_log2(uint32_t v) { - // This algorithm extends to 64 bits, by adding a step, shrinks to sixteen bits by removing a step. - decltype(v) r{ 0 }, s; - // This redundant initialization and redundant |= of r can be eliminated, - // but eliminating it obfuscates the simplicity of the algorithm. - s = (v > 0xFFFF) << 4; v >>= s; r |= s; - s = (v > 0x00FF) << 3; v >>= s; r |= s; - s = (v > 0x000F) << 2; v >>= s; r |= s; - s = (v > 0x0003) << 1; v >>= s; r |= s; - r |= (v >> 1); - // result of ⌊log2(v)⌋ is in r - return r; -} - -// For trailing bits, consider int __builtin_ctz (unsigned int x) -// http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightLinear - -// Count the consecutive trailing zero bits -inline auto trailing_zero_bits(uint64_t v) { - unsigned int c; - if (v & 0x3F) { - v = (v ^ (v - 1)) >> 1; // Set v's trailing 0s to 1s and zero rest - for (c = 0; v; c++) { - v >>= 1; - } - } - else { - c = 1; - if ((v & 0xffffffff) == 0) { - v >>= 32; - c += 32; - } - if ((v & 0xffff) == 0) { - v >>= 16; - c += 16; - } - if ((v & 0xff) == 0){ - v >>= 8; - c += 8; - } - if ((v & 0xf) == 0){ - v >>= 4; - c += 4; - } - if ((v & 0x3) == 0) { - v >>= 2; - c += 2; - } - if ((v & 0x1) == 0) { - v >>= 1; - c += 1; - } - c -= v & 0x01; - } - return c; -} diff --git a/introspection_of_standard_C_types.h b/introspection_of_standard_C_types.h index a08edcf..75f6bcf 100644 --- a/introspection_of_standard_C_types.h +++ b/introspection_of_standard_C_types.h @@ -14,11 +14,11 @@ namespace ro { template constexpr bool is_standard_unsigned_integer = - _Is_any_of_v, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long>; + _Is_any_of_v::type, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long>; template constexpr bool is_standard_signed_integer = - _Is_any_of_v, signed char, signed short, signed int, signed long, signed long long>; + _Is_any_of_v::type, signed char, signed short, signed int, signed long, signed long long>; // A compile time test to see if desired casts work, and make sure that // undesired casts do not work diff --git a/ristretto255.h b/ristretto255.h index 6c74a6c..85f856a 100644 --- a/ristretto255.h +++ b/ristretto255.h @@ -105,11 +105,11 @@ namespace ro { } }; - // This template generates a span over const unsigned bytes for - // any range over bytes, such as a C array or an std::array + // This template generates a span over an indexable byte type, + // such as a C array or an std::array, but not pointers template < typename T> std::enable_if_t< - sizeof(std::size(std::declval())) >= sizeof(int) && + !std::is_pointer::value && sizeof(std::declval()[0]) == 1, std::span > serialize(const T& a) { @@ -453,8 +453,8 @@ namespace ristretto255 { } template < typename T> - decltype(ro::serialize(std::declval()), hsh())& - operator <<(const T& j) { + decltype(ro::serialize(std::declval::type >()), hsh())& + operator <<(const T & j) { return *this << ro::serialize(j); } @@ -468,7 +468,7 @@ namespace ristretto255 { } } - hsh& operator <<(const std::span& j) { + hsh& operator <<(const std::span j) { int i = crypto_generichash_blake2b_update( &st, &j[0],