Skip to content

Commit 38357ed

Browse files
authored
Merge pull request #1452 from tomt1664/minisketch_patches
Minisketch upstream patches
2 parents 9748c00 + 2b5154d commit 38357ed

File tree

11 files changed

+79
-88
lines changed

11 files changed

+79
-88
lines changed

configure.ac

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,22 +1004,6 @@ AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
10041004
#include <byteswap.h>
10051005
#endif])
10061006

1007-
AC_MSG_CHECKING([for __builtin_clzl])
1008-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
1009-
(void) __builtin_clzl(0);
1010-
]])],
1011-
[ AC_MSG_RESULT([yes]); have_clzl=yes; AC_DEFINE([HAVE_BUILTIN_CLZL], [1], [Define this symbol if you have __builtin_clzl])],
1012-
[ AC_MSG_RESULT([no]); have_clzl=no;]
1013-
)
1014-
1015-
AC_MSG_CHECKING([for __builtin_clzll])
1016-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
1017-
(void) __builtin_clzll(0);
1018-
]])],
1019-
[ AC_MSG_RESULT([yes]); have_clzll=yes; AC_DEFINE([HAVE_BUILTIN_CLZLL], [1], [Define this symbol if you have __builtin_clzll])],
1020-
[ AC_MSG_RESULT([no]); have_clzll=no;]
1021-
)
1022-
10231007
dnl Check for malloc_info (for memory statistics information in getmemoryinfo)
10241008
AC_MSG_CHECKING([for getmemoryinfo])
10251009
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
@@ -1848,7 +1832,6 @@ AM_CONDITIONAL([LIQUID], [test "$liquid_build" = "yes"])
18481832

18491833
dnl for minisketch
18501834
AM_CONDITIONAL([ENABLE_CLMUL], [test "$enable_clmul" = "yes"])
1851-
AM_CONDITIONAL([HAVE_CLZ], [test "$have_clzl$have_clzll" = "yesyes"])
18521835

18531836
AC_DEFINE([CLIENT_VERSION_MAJOR], [_CLIENT_VERSION_MAJOR], [Major version])
18541837
AC_DEFINE([CLIENT_VERSION_MINOR], [_CLIENT_VERSION_MINOR], [Minor version])

src/Makefile.minisketch.include

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ LIBMINISKETCH_CPPFLAGS += -DHAVE_CLMUL
1212
MINISKETCH_LIBS += $(LIBMINISKETCH_CLMUL)
1313
endif
1414

15-
if HAVE_CLZ
16-
LIBMINISKETCH_CPPFLAGS += -DHAVE_CLZ
17-
endif
18-
1915
EXTRA_LIBRARIES += $(MINISKETCH_LIBS)
2016

2117
minisketch_libminisketch_clmul_a_SOURCES = $(MINISKETCH_FIELD_CLMUL_SOURCES_INT) $(MINISKETCH_FIELD_CLMUL_HEADERS_INT)

src/minisketch/.cirrus.yml

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ env_matrix_snippet: &ENV_MATRIX_VALGRIND
3636
TESTRUNS: 1
3737
BUILD:
3838

39-
env_matrix_snippet: &ENV_MATRIX_SAN
40-
- env:
41-
ENABLE_FIELDS: 28
42-
- env:
43-
BUILD: distcheck
44-
- env:
45-
CXXFLAGS: "-fsanitize=undefined -fno-omit-frame-pointer"
46-
LDFLAGS: "-fsanitize=undefined -fno-omit-frame-pointer"
47-
UBSAN_OPTIONS: "print_stacktrace=1:halt_on_error=1"
48-
BENCH: no
49-
5039
env_matrix_snippet: &ENV_MATRIX_SAN_VALGRIND
5140
- env:
5241
ENABLE_FIELDS: "11,64,37"
@@ -72,9 +61,9 @@ task:
7261
<< : *ENV_MATRIX_SAN_VALGRIND
7362
matrix:
7463
- env:
75-
CC: gcc
64+
CXX: g++
7665
- env:
77-
CC: clang
66+
CXX: clang++ -gdwarf-4
7867
<< : *MERGE_BASE
7968
test_script:
8069
- ./ci/cirrus.sh
@@ -92,30 +81,45 @@ task:
9281
<< : *ENV_MATRIX_VALGRIND
9382
matrix:
9483
- env:
95-
CC: i686-linux-gnu-gcc
84+
CXX: i686-linux-gnu-g++
9685
- env:
97-
CC: clang --target=i686-pc-linux-gnu -isystem /usr/i686-linux-gnu/include
86+
CXX: clang++ --target=i686-linux-gnu -gdwarf-4
87+
CXXFLAGS: -g -O2 -isystem /usr/i686-linux-gnu/include -isystem /usr/i686-linux-gnu/include/c++/10/i686-linux-gnu
9888
test_script:
9989
- ./ci/cirrus.sh
10090
<< : *CAT_LOGS
10191

10292
task:
103-
name: "x86_64: macOS Catalina"
93+
name: "arm64: macOS Monterey"
10494
macos_instance:
105-
image: catalina-base
95+
image: ghcr.io/cirruslabs/macos-monterey-base:latest
10696
env:
107-
# Cirrus gives us a fixed number of 12 virtual CPUs.
108-
MAKEFLAGS: -j13
109-
matrix:
110-
<< : *ENV_MATRIX_SAN
97+
# Cirrus gives us a fixed number of 4 virtual CPUs.
98+
MAKEFLAGS: -j5
11199
matrix:
112100
- env:
113-
CC: gcc-9
101+
CXX: g++-11
102+
# Homebrew's gcc for arm64 has no libubsan.
103+
matrix:
104+
- env:
105+
ENABLE_FIELDS: 28
106+
- env:
107+
BUILD: distcheck
114108
- env:
115-
CC: clang
109+
CXX: clang++
110+
matrix:
111+
- env:
112+
ENABLE_FIELDS: 28
113+
- env:
114+
BUILD: distcheck
115+
- env:
116+
CXXFLAGS: "-fsanitize=undefined -fno-omit-frame-pointer"
117+
LDFLAGS: "-fsanitize=undefined -fno-omit-frame-pointer"
118+
UBSAN_OPTIONS: "print_stacktrace=1:halt_on_error=1"
119+
BENCH: no
116120
brew_script:
117121
- brew update
118-
- brew install automake libtool gcc@9
122+
- brew install automake libtool gcc@11
119123
<< : *MERGE_BASE
120124
test_script:
121125
- ./ci/cirrus.sh
@@ -128,13 +132,11 @@ task:
128132
cpu: 4
129133
memory: 2G
130134
env:
131-
EXEC_CMD: qemu-s390x -L /usr/s390x-linux-gnu
135+
EXEC_CMD: qemu-s390x
132136
HOST: s390x-linux-gnu
133137
BUILD:
134138
<< : *MERGE_BASE
135139
test_script:
136-
# https://sourceware.org/bugzilla/show_bug.cgi?id=27008
137-
- rm /etc/ld.so.cache
138140
- ./ci/cirrus.sh
139141
<< : *CAT_LOGS
140142

@@ -146,6 +148,7 @@ task:
146148
memory: 2G
147149
env:
148150
EXEC_CMD: wine
151+
EXEC_EXT: .exe
149152
HOST: x86_64-w64-mingw32
150153
BUILD:
151154
<< : *MERGE_BASE

src/minisketch/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ Some improvements that are still TODO:
203203
* <a name="myfootnote4">[4]</a> Bhaskar Biswas, Vincent Herbert. *Efficient Root Finding of Polynomials over Fields of Characteristic 2.* 2009. hal-00626997. [[URL]](https://hal.archives-ouvertes.fr/hal-00626997) [[PDF]](https://hal.archives-ouvertes.fr/hal-00626997/document)
204204
* <a name="myfootnote6">[6]</a> Eppstein, David, Michael T. Goodrich, Frank Uyeda, and George Varghese. *What's the difference?: efficient set reconciliation without prior context.* ACM SIGCOMM Computer Communication Review, vol. 41, no. 4, pp. 218-229. ACM, 2011. [[PDF]](https://www.ics.uci.edu/~eppstein/pubs/EppGooUye-SIGCOMM-11.pdf)
205205
* <a name="myfootnote7">[7]</a> Goodrich, Michael T. and Michael Mitzenmacher. *Invertible bloom lookup tables.* 2011 49th Annual Allerton Conference on Communication, Control, and Computing (Allerton) (2011): 792-799. [[PDF]](https://arxiv.org/pdf/1101.2245.pdf)
206-
* <a name="myfootnote8">[8]</a> Maxwell, Gregory F. *[Blocksonly mode BW savings, the limits of efficient block xfer, and better relay](https://bitcointalk.org/index.php?topic=1377345.0)* Bitcointalk 2016, *[Technical notes on mempool synchronizing relay](https://people.xiph.org/~greg/mempool_sync_relay.txt)* #bitcoin-wizards 2016.
207-
* <a name="myfootnote9">[9]</a> Maxwell, Gregory F. *[Block network coding](https://en.bitcoin.it/wiki/User:Gmaxwell/block_network_coding)* Bitcoin Wiki 2014, *[Technical notes on efficient block xfer](https://people.xiph.org/~greg/efficient.block.xfer.txt)* #bitcoin-wizards 2015.
206+
* <a name="myfootnote8">[8]</a> Maxwell, Gregory F. *[Blocksonly mode BW savings, the limits of efficient block xfer, and better relay](https://bitcointalk.org/index.php?topic=1377345.0)* Bitcointalk 2016, *[Technical notes on mempool synchronizing relay](https://nt4tn.net/tech-notes/2016.mempool_sync_relay.txt)* #bitcoin-wizards 2016.
207+
* <a name="myfootnote9">[9]</a> Maxwell, Gregory F. *[Block network coding](https://en.bitcoin.it/wiki/User:Gmaxwell/block_network_coding)* Bitcoin Wiki 2014, *[Technical notes on efficient block xfer](https://nt4tn.net/tech-notes/201512.efficient.block.xfer.txt)* #bitcoin-wizards 2015.
208208
* <a name="myfootnote10">[10]</a> Ruffing, Tim, Moreno-Sanchez, Pedro, Aniket, Kate, *P2P Mixing and Unlinkable Bitcoin Transactions* NDSS Symposium 2017 [[URL]](https://eprint.iacr.org/2016/824) [[PDF]](https://eprint.iacr.org/2016/824.pdf)
209209
* <a name="myfootnote11">[11]</a> Y. Misky, A. Trachtenberg, R. Zippel. *Set Reconciliation with Nearly Optimal Communication Complexity.* Cornell University, 2000. [[URL]](https://ecommons.cornell.edu/handle/1813/5803) [[PDF]](https://ecommons.cornell.edu/bitstream/handle/1813/5803/2000-1813.pdf)
210210
* <a name="myfootnote12">[12]</a> Itoh, Toshiya, and Shigeo Tsujii. "A fast algorithm for computing multiplicative inverses in GF (2m) using normal bases." Information and computation 78, no. 3 (1988): 171-177. [[URL]](https://www.sciencedirect.com/science/article/pii/0890540188900247)

src/minisketch/ci/cirrus.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export LC_ALL=C
77

88
env >> test_env.log
99

10-
$CC -v || true
10+
$CXX -v || true
1111
valgrind --version || true
1212

1313
./autogen.sh
@@ -32,10 +32,10 @@ then
3232
fi
3333

3434
if [ -n "$EXEC_CMD" ]; then
35-
$EXEC_CMD ./test $TESTRUNS
36-
$EXEC_CMD ./test-verify $TESTRUNS
35+
$EXEC_CMD "./test$EXEC_EXT" $TESTRUNS
36+
$EXEC_CMD "./test-verify$EXEC_EXT" $TESTRUNS
3737
fi
3838

3939
if [ "$BENCH" = "yes" ]; then
40-
$EXEC_CMD ./bench
40+
$EXEC_CMD "./bench$EXEC_EXT"
4141
fi

src/minisketch/ci/linux-debian.Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ RUN apt-get update
88
RUN apt-get install --no-install-recommends --no-upgrade -y \
99
git ca-certificates \
1010
make automake libtool pkg-config dpkg-dev valgrind qemu-user \
11-
gcc g++ clang libc6-dbg \
11+
gcc g++ clang libclang-rt-dev libc6-dbg \
1212
gcc-i686-linux-gnu g++-i686-linux-gnu libc6-dev-i386-cross libc6-dbg:i386 \
13-
g++-s390x-linux-gnu gcc-s390x-linux-gnu libc6-dev-s390x-cross libc6-dbg:s390x \
14-
wine g++-mingw-w64-x86-64
13+
g++-s390x-linux-gnu libstdc++6:s390x gcc-s390x-linux-gnu libc6-dev-s390x-cross libc6-dbg:s390x \
14+
wine wine64 g++-mingw-w64-x86-64
1515

1616
# Run a dummy command in wine to make it set up configuration
1717
RUN wine true || true

src/minisketch/configure.ac

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ esac
104104
AX_CHECK_COMPILE_FLAG([-Wall],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wall"],,[[$CXXFLAG_WERROR]])
105105
AX_CHECK_COMPILE_FLAG([-fvisibility=hidden],[CXXFLAGS="$CXXFLAGS -fvisibility=hidden"],[],[$CXXFLAG_WERROR])
106106

107-
## Some compilers (gcc) ignore unknown -Wno-* options, but warn about all
108-
## unknown options if any other warning is produced. Test the -Wfoo case, and
109-
## set the -Wno-foo case if it works.
110-
AX_CHECK_COMPILE_FLAG([-Wshift-count-overflow],[NOWARN_CXXFLAGS="$NOWARN_CXXFLAGS -Wno-shift-count-overflow"],,[[$CXXFLAG_WERROR]])
111-
112107
if test "x$use_ccache" != "xno"; then
113108
AC_MSG_CHECKING(if ccache should be used)
114109
if test x$CCACHE = x; then
@@ -119,14 +114,10 @@ if test "x$use_ccache" != "xno"; then
119114
fi
120115
else
121116
use_ccache=yes
122-
CC="$ac_cv_path_CCACHE $CC"
123117
CXX="$ac_cv_path_CCACHE $CXX"
124118
fi
125119
AC_MSG_RESULT($use_ccache)
126120
fi
127-
if test "x$use_ccache" = "xyes"; then
128-
AX_CHECK_COMPILE_FLAG([-Qunused-arguments],[NOWARN_CXXFLAGS="$NOWARN_CXXFLAGS -Qunused-arguments"],,[[$CXXFLAG_WERROR]])
129-
fi
130121

131122
VERIFY_DEFINES=-DMINISKETCH_VERIFY
132123
RELEASE_DEFINES=

src/minisketch/include/minisketch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include <stdlib.h>
66

77
#ifdef _MSC_VER
8-
# include <compat.h>
8+
# include <BaseTsd.h>
9+
typedef SSIZE_T ssize_t;
910
#else
1011
# include <unistd.h>
1112
#endif

src/minisketch/src/bench.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,11 @@ int main(int argc, char** argv) {
6262
if (!states[0]) {
6363
printf(" -\t");
6464
} else {
65-
double total = 0.0;
6665
for (auto& state : states) {
6766
auto start = std::chrono::steady_clock::now();
6867
minisketch_decode(state, 2 * syndromes, roots.data());
6968
auto stop = std::chrono::steady_clock::now();
7069
std::chrono::duration<double> dur(stop - start);
71-
total += dur.count();
7270
benches.push_back(dur.count());
7371
}
7472
std::sort(benches.begin(), benches.end());
@@ -98,15 +96,13 @@ int main(int argc, char** argv) {
9896
if (!states[0]) {
9997
printf(" -\t");
10098
} else {
101-
double total = 0.0;
10299
for (auto& state : states) {
103100
auto start = std::chrono::steady_clock::now();
104101
for (auto val : data) {
105102
minisketch_add_uint64(state, val);
106103
}
107104
auto stop = std::chrono::steady_clock::now();
108105
std::chrono::duration<double> dur(stop - start);
109-
total += dur.count();
110106
benches.push_back(dur.count());
111107
}
112108
std::sort(benches.begin(), benches.end());

src/minisketch/src/int_utils.h

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
#ifndef _MINISKETCH_INT_UTILS_H_
88
#define _MINISKETCH_INT_UTILS_H_
99

10+
#include <stdint.h>
1011
#include <stdlib.h>
1112

1213
#include <limits>
1314
#include <algorithm>
1415
#include <type_traits>
1516

16-
#ifdef _MSC_VER
17+
#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
18+
# include <bit>
19+
#elif defined(_MSC_VER)
1720
# include <intrin.h>
1821
#endif
1922

@@ -54,11 +57,10 @@ class BitWriter {
5457
int offset = 0;
5558
unsigned char* out;
5659

57-
public:
58-
BitWriter(unsigned char* output) : out(output) {}
59-
6060
template<int BITS, typename I>
61-
inline void Write(I val) {
61+
inline void WriteInner(I val) {
62+
// We right shift by up to 8 bits below. Verify that's well defined for the type I.
63+
static_assert(std::numeric_limits<I>::digits > 8, "BitWriter::WriteInner needs I > 8 bits");
6264
int bits = BITS;
6365
if (bits + offset >= 8) {
6466
state |= ((val & ((I(1) << (8 - offset)) - 1)) << offset);
@@ -77,6 +79,19 @@ class BitWriter {
7779
offset += bits;
7880
}
7981

82+
83+
public:
84+
BitWriter(unsigned char* output) : out(output) {}
85+
86+
template<int BITS, typename I>
87+
inline void Write(I val) {
88+
// If I is smaller than an unsigned int, invoke WriteInner with argument converted to unsigned.
89+
using compute_type = typename std::conditional<
90+
(std::numeric_limits<I>::digits < std::numeric_limits<unsigned>::digits),
91+
unsigned, I>::type;
92+
return WriteInner<BITS, compute_type>(val);
93+
}
94+
8095
inline void Flush() {
8196
if (offset) {
8297
*(out++) = state;
@@ -129,17 +144,11 @@ constexpr inline I Mask() { return ((I((I(-1)) << (std::numeric_limits<I>::digit
129144
/** Compute the smallest power of two that is larger than val. */
130145
template<typename I>
131146
static inline int CountBits(I val, int max) {
132-
#ifdef HAVE_CLZ
147+
#if defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L
148+
// c++20 impl
133149
(void)max;
134-
if (val == 0) return 0;
135-
if (std::numeric_limits<unsigned>::digits >= std::numeric_limits<I>::digits) {
136-
return std::numeric_limits<unsigned>::digits - __builtin_clz(val);
137-
} else if (std::numeric_limits<unsigned long>::digits >= std::numeric_limits<I>::digits) {
138-
return std::numeric_limits<unsigned long>::digits - __builtin_clzl(val);
139-
} else {
140-
return std::numeric_limits<unsigned long long>::digits - __builtin_clzll(val);
141-
}
142-
#elif _MSC_VER
150+
return std::bit_width(val);
151+
#elif defined(_MSC_VER)
143152
(void)max;
144153
unsigned long index;
145154
unsigned char ret;
@@ -149,7 +158,17 @@ static inline int CountBits(I val, int max) {
149158
ret = _BitScanReverse64(&index, val);
150159
}
151160
if (!ret) return 0;
152-
return index;
161+
return index + 1;
162+
#elif HAVE_CLZ
163+
(void)max;
164+
if (val == 0) return 0;
165+
if (std::numeric_limits<unsigned>::digits >= std::numeric_limits<I>::digits) {
166+
return std::numeric_limits<unsigned>::digits - __builtin_clz(val);
167+
} else if (std::numeric_limits<unsigned long>::digits >= std::numeric_limits<I>::digits) {
168+
return std::numeric_limits<unsigned long>::digits - __builtin_clzl(val);
169+
} else {
170+
return std::numeric_limits<unsigned long long>::digits - __builtin_clzll(val);
171+
}
153172
#else
154173
while (max && (val >> (max - 1) == 0)) --max;
155174
return max;
@@ -175,6 +194,7 @@ class BitsInt {
175194
}
176195

177196
static constexpr inline bool IsZero(I a) { return a == 0; }
197+
static constexpr inline bool IsOne(I a) { return a == 1; }
178198
static constexpr inline I Mask(I val) { return val & MASK; }
179199
static constexpr inline I Shift(I val, int bits) { return ((val << bits) & MASK); }
180200
static constexpr inline I UnsafeShift(I val, int bits) { return (val << bits); }
@@ -233,7 +253,7 @@ template<typename I, int N, typename L, typename F> inline constexpr I GFMul(con
233253
template<typename I, typename F, int BITS, uint32_t MOD>
234254
inline I InvExtGCD(I x)
235255
{
236-
if (F::IsZero(x)) return x;
256+
if (F::IsZero(x) || F::IsOne(x)) return x;
237257
I t(0), newt(1);
238258
I r(MOD), newr = x;
239259
int rlen = BITS + 1, newrlen = F::Bits(newr, BITS);

0 commit comments

Comments
 (0)