From c986e6032ba12ee8273b86e96ac1f0666b220187 Mon Sep 17 00:00:00 2001 From: CryptoForge Date: Tue, 22 May 2018 18:12:03 -0700 Subject: [PATCH 1/3] Update equihash.cpp and metrics.cpp for windows build --- src/crypto/equihash.cpp | 3 +++ src/metrics.cpp | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/crypto/equihash.cpp b/src/crypto/equihash.cpp index 0d8c46f2999..a5a0b12ea5c 100644 --- a/src/crypto/equihash.cpp +++ b/src/crypto/equihash.cpp @@ -19,6 +19,9 @@ #include "compat/endian.h" #include "crypto/equihash.h" #include "util.h" +#ifndef __linux__ +#include "compat/endian.h" +#endif #include #include diff --git a/src/metrics.cpp b/src/metrics.cpp index 6ae655811c6..dd8f7d8f279 100644 --- a/src/metrics.cpp +++ b/src/metrics.cpp @@ -16,7 +16,14 @@ #include #include #include + +#ifdef WIN32 +#include +#include +#else #include +#endif + #include void AtomicTimer::start() @@ -441,11 +448,18 @@ void ThreadShowMetricsScreen() // Get current window size if (isTTY) { + #ifdef WIN32 + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + cols = csbi.srWindow.Right - csbi.srWindow.Left + 1; + #else + cols = 80; struct winsize w; w.ws_col = 0; if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1 && w.ws_col != 0) { cols = w.ws_col; } + #endif } if (isScreen) { From a8bebf6fb4d837f694445bcad58d626a228ecae4 Mon Sep 17 00:00:00 2001 From: CryptoForge Date: Tue, 22 May 2018 18:12:55 -0700 Subject: [PATCH 2/3] Update build instructions for windows compile --- configure.ac | 11 +++++------ depends/hosts/mingw32.mk | 6 ++++-- depends/packages/bdb.mk | 3 ++- depends/packages/packages.mk | 4 ++-- depends/packages/zeromq.mk | 3 ++- src/Makefile.am | 20 ++++++++++---------- zcutil/build-win.sh | 17 +++++++++++++++++ 7 files changed, 42 insertions(+), 22 deletions(-) create mode 100755 zcutil/build-win.sh diff --git a/configure.ac b/configure.ac index f9c0f124698..147dc84bea6 100644 --- a/configure.ac +++ b/configure.ac @@ -454,10 +454,9 @@ if test x$use_hardening != xno; then HARDENED_CPPFLAGS="$HARDENED_CPPFLAGS -D_FORTIFY_SOURCE=2" ],[AC_MSG_ERROR(Cannot enable -D_FORTIFY_SOURCE=2)]) - AX_CHECK_LINK_FLAG([[-Wl,-z,relro]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,relro"],[AC_MSG_ERROR(Cannot enable RELRO)]) - AX_CHECK_LINK_FLAG([[-Wl,-z,now]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,now"],[AC_MSG_ERROR(Cannot enable BIND_NOW)]) - if test x$TARGET_OS != xwindows; then + AX_CHECK_LINK_FLAG([[-Wl,-z,relro]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,relro"],[AC_MSG_ERROR(Cannot enable RELRO)]) + AX_CHECK_LINK_FLAG([[-Wl,-z,now]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-z,now"],[AC_MSG_ERROR(Cannot enable BIND_NOW)]) # All windows code is PIC, forcing it on just adds useless compile warnings AX_CHECK_COMPILE_FLAG([-fPIE],[PIE_FLAGS="-fPIE"],[AC_MSG_ERROR(Cannot enable -fPIE)]) AX_CHECK_LINK_FLAG([[-pie]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -pie"],[AC_MSG_ERROR(Cannot enable -pie)]) @@ -905,7 +904,7 @@ case $host in ;; esac -echo +echo echo "Options used to compile and link:" echo " with wallet = $enable_wallet" echo " with rust = $enable_rust" @@ -914,7 +913,7 @@ echo " with zmq = $use_zmq" echo " with test = $use_tests" echo " debug enabled = $enable_debug" echo " werror = $enable_werror" -echo +echo echo " target os = $TARGET_OS" echo " build os = $BUILD_OS" echo @@ -925,4 +924,4 @@ echo " CXX = $CXX" echo " CXXFLAGS = $CXXFLAGS" echo " LDFLAGS = $LDFLAGS" echo " ARFLAGS = $ARFLAGS" -echo +echo diff --git a/depends/hosts/mingw32.mk b/depends/hosts/mingw32.mk index dbfb62fdcf9..65ab1702b44 100644 --- a/depends/hosts/mingw32.mk +++ b/depends/hosts/mingw32.mk @@ -1,5 +1,7 @@ -mingw32_CFLAGS=-pipe -mingw32_CXXFLAGS=$(mingw32_CFLAGS) +mingw32_CC=x86_64-w64-mingw32-gcc-posix +mingw32_CXX=x86_64-w64-mingw32-g++-posix +mingw32_CFLAGS=-pipe -std=c11 +mingw32_CXXFLAGS=$(mingw32_CFLAGS) -std=c++11 mingw32_release_CFLAGS=-O2 mingw32_release_CXXFLAGS=$(mingw32_release_CFLAGS) diff --git a/depends/packages/bdb.mk b/depends/packages/bdb.mk index 1134e94d012..941c6444203 100644 --- a/depends/packages/bdb.mk +++ b/depends/packages/bdb.mk @@ -14,7 +14,8 @@ endef define $(package)_preprocess_cmds sed -i.old 's/__atomic_compare_exchange\\(/__atomic_compare_exchange_db(/' src/dbinc/atomic.h && \ - sed -i.old 's/atomic_init/atomic_init_db/' src/dbinc/atomic.h src/mp/mp_region.c src/mp/mp_mvcc.c src/mp/mp_fget.c src/mutex/mut_method.c src/mutex/mut_tas.c + sed -i.old 's/atomic_init/atomic_init_db/' src/dbinc/atomic.h src/mp/mp_region.c src/mp/mp_mvcc.c src/mp/mp_fget.c src/mutex/mut_method.c src/mutex/mut_tas.c && \ + sed -i "s/WinIoCtl.h/winioctl.h/g" src/dbinc/win_db.h endef define $(package)_config_cmds diff --git a/depends/packages/packages.mk b/depends/packages/packages.mk index 296323c8cd3..f1730aed3ae 100644 --- a/depends/packages/packages.mk +++ b/depends/packages/packages.mk @@ -1,7 +1,7 @@ -rust_packages := rust librustzcash +#rust_packages := rust librustzcash proton_packages := proton zcash_packages := libgmp libsodium packages := boost openssl libevent zeromq $(zcash_packages) googletest native_packages := native_ccache -wallet_packages=bdb +wallet_packages := bdb diff --git a/depends/packages/zeromq.mk b/depends/packages/zeromq.mk index 3cca06dae08..7ea78ebc640 100644 --- a/depends/packages/zeromq.mk +++ b/depends/packages/zeromq.mk @@ -3,9 +3,10 @@ $(package)_version=4.2.1 $(package)_download_path=https://github.com/zeromq/libzmq/releases/download/v$($(package)_version)/ $(package)_file_name=$(package)-$($(package)_version).tar.gz $(package)_sha256_hash=27d1e82a099228ee85a7ddb2260f40830212402c605a4a10b5e5498a7e0e9d03 +$(package)_build_env+=CFLAGS="$($(package)_cflags) -Wall -Wno-pedantic-ms-format -DLIBCZMQ_EXPORTS -DZMQ_DEFINED_STDINT -lzmq -lws2_32 -liphlpapi -lrpcrt4" define $(package)_set_vars - $(package)_config_opts=--without-documentation --disable-shared --disable-curve + $(package)_config_opts=--without-documentation --disable-shared --disable-curve --host=$(host) $(package)_config_opts_linux=--with-pic $(package)_cxxflags=-std=c++11 endef diff --git a/src/Makefile.am b/src/Makefile.am index 44b4d8281e2..f607470cd2c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -23,8 +23,8 @@ BITCOIN_CONFIG_INCLUDES=-I$(builddir)/config BITCOIN_INCLUDES=-I$(builddir) -I$(builddir)/obj $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS) $(CRYPTO_CFLAGS) $(SSL_CFLAGS) BITCOIN_INCLUDES += -I$(srcdir)/secp256k1/include -BITCOIN_INCLUDES += -I$(srcdir)/snark -BITCOIN_INCLUDES += -I$(srcdir)/snark/libsnark +BITCOIN_INCLUDES += -I$(srcdir)/snark/build/include +BITCOIN_INCLUDES += -I$(srcdir)/snark/build/include/libsnark BITCOIN_INCLUDES += -I$(srcdir)/univalue/include LIBBITCOIN_SERVER=libbitcoin_server.a @@ -34,24 +34,24 @@ LIBBITCOIN_CLI=libbitcoin_cli.a LIBBITCOIN_UTIL=libbitcoin_util.a LIBBITCOIN_CRYPTO=crypto/libbitcoin_crypto.a LIBSECP256K1=secp256k1/libsecp256k1.la -LIBSNARK=snark/libsnark.a +LIBSNARK=snark/build/lib/libsnark.a LIBUNIVALUE=univalue/libunivalue.la LIBZCASH=libzcash.a $(LIBSECP256K1): $(wildcard secp256k1/src/*) $(wildcard secp256k1/include/*) $(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C $(@D) $(@F) -LIBSNARK_CXXFLAGS = -fPIC -DBINARY_OUTPUT -DNO_PT_COMPRESSION=1 -fstack-protector-all -LIBSNARK_CONFIG_FLAGS = CURVE=ALT_BN128 NO_PROCPS=1 NO_DOCS=1 STATIC=1 NO_SUPERCOP=1 FEATUREFLAGS=-DMONTGOMERY_OUTPUT NO_COPY_DEPINST=1 NO_COMPILE_LIBGTEST=1 -if HAVE_OPENMP -LIBSNARK_CONFIG_FLAGS += MULTICORE=1 -endif +-include collate-libsnark +collate-libsnark: $(LIBSNARK) + +LIBSNARK_CXXFLAGS = -fPIC -DBINARY_OUTPUT -DNO_PT_COMPRESSION=1 +LIBSNARK_CONFIG_FLAGS = CURVE=ALT_BN128 MULTICORE=1 NO_PROCPS=1 NO_DOCS=1 STATIC=1 NO_SUPERCOP=1 FEATUREFLAGS=-DMONTGOMERY_OUTPUT NO_COPY_DEPINST=1 $(LIBSNARK): $(wildcard snark/src/*) - $(AM_V_at) CXXFLAGS="$(LIBSNARK_CXXFLAGS)" $(MAKE) $(AM_MAKEFLAGS) -C snark/ DEPINST="$(LIBSNARK_DEPINST)" $(LIBSNARK_CONFIG_FLAGS) OPTFLAGS="-O2 -march=x86-64" + $(AM_V_at) CXXFLAGS="$(LIBSNARK_CXXFLAGS)" $(MAKE) $(AM_MAKEFLAGS) -C snark/ install PREFIX=$(srcdir)/build DEPINST="$(LIBSNARK_DEPINST)" $(LIBSNARK_CONFIG_FLAGS) OPTFLAGS="-O2 -march=x86-64" libsnark-tests: $(wildcard snark/src/*) - $(AM_V_at) CXXFLAGS="$(LIBSNARK_CXXFLAGS)" $(MAKE) $(AM_MAKEFLAGS) -C snark/ check DEPINST="$(LIBSNARK_DEPINST)" $(LIBSNARK_CONFIG_FLAGS) OPTFLAGS="-O2 -march=x86-64" + $(AM_V_at) CXXFLAGS="$(LIBSNARK_CXXFLAGS)" $(MAKE) $(AM_MAKEFLAGS) -C snark/ check PREFIX=$(srcdir)/build DEPINST="$(LIBSNARK_DEPINST)" $(LIBSNARK_CONFIG_FLAGS) OPTFLAGS="-O2 -march=x86-64" $(LIBUNIVALUE): $(wildcard univalue/lib/*) $(AM_V_at)$(MAKE) $(AM_MAKEFLAGS) -C univalue/ diff --git a/zcutil/build-win.sh b/zcutil/build-win.sh new file mode 100755 index 00000000000..753726d24be --- /dev/null +++ b/zcutil/build-win.sh @@ -0,0 +1,17 @@ +#!/bin/bash +HOST=x86_64-w64-mingw32 +CXX=x86_64-w64-mingw32-g++-posix +CC=x86_64-w64-mingw32-gcc-posix +PREFIX="$(pwd)/depends/$HOST" + +set -eu -o pipefail + +set -x +cd "$(dirname "$(readlink -f "$0")")/.." + +cd depends/ && make HOST=$HOST V=1 NO_QT=1 && cd ../ +./autogen.sh +CONFIG_SITE=$PWD/depends/x86_64-w64-mingw32/share/config.site CXXFLAGS+="-DPTW32_STATIC_LIB -DCURVE_ALT_BN128 -fopenmp -pthread" ./configure --prefix="${PREFIX}" --host=x86_64-w64-mingw32 --enable-static --disable-shared --disable-zmq --disable-rust +sed -i 's/-lboost_system-mt /-lboost_system-mt-s /' configure +cd src/ +CC="${CC}" CXX="${CXX}" make V=1 -j4 zcashd.exe zcash-cli.exe zcash-tx.exe From 3a01c9f368ed26a5b146790d863dc21575127ec1 Mon Sep 17 00:00:00 2001 From: CryptoForge Date: Tue, 22 May 2018 18:13:31 -0700 Subject: [PATCH 3/3] Update libsnark for windows and mac build --- .../algebra/curves/alt_bn128/alt_bn128_g1.cpp | 11 +-- .../algebra/curves/alt_bn128/alt_bn128_g1.hpp | 8 +- .../algebra/curves/alt_bn128/alt_bn128_g2.cpp | 11 +-- .../algebra/curves/alt_bn128/alt_bn128_g2.hpp | 8 +- .../curves/alt_bn128/alt_bn128_pairing.cpp | 6 +- .../libsnark/algebra/curves/curve_utils.tcc | 2 +- .../domains/basic_radix2_domain.tcc | 11 +-- .../domains/basic_radix2_domain_aux.tcc | 15 ++-- .../evaluation_domain/evaluation_domain.tcc | 13 ++-- .../algebra/exponentiation/exponentiation.hpp | 2 +- .../algebra/exponentiation/exponentiation.tcc | 4 +- src/snark/libsnark/algebra/fields/bigint.hpp | 4 +- src/snark/libsnark/algebra/fields/bigint.tcc | 27 +++---- .../libsnark/algebra/fields/field_utils.hpp | 8 +- .../libsnark/algebra/fields/field_utils.tcc | 43 +++++------ src/snark/libsnark/algebra/fields/fp.hpp | 42 +++++------ src/snark/libsnark/algebra/fields/fp.tcc | 38 +++++----- .../algebra/fields/fp12_2over3over2.hpp | 4 +- .../algebra/fields/fp12_2over3over2.tcc | 10 +-- src/snark/libsnark/algebra/fields/fp2.hpp | 8 +- src/snark/libsnark/algebra/fields/fp2.tcc | 8 +- .../libsnark/algebra/fields/fp6_3over2.hpp | 4 +- .../libsnark/algebra/fields/fp6_3over2.tcc | 4 +- .../algebra/fields/tests/test_bigint.cpp | 7 +- .../scalar_multiplication/kc_multiexp.tcc | 4 +- .../scalar_multiplication/multiexp.tcc | 29 +++---- .../algebra/scalar_multiplication/wnaf.hpp | 2 +- .../algebra/scalar_multiplication/wnaf.tcc | 18 ++--- .../common/data_structures/merkle_tree.tcc | 18 ++--- .../common/data_structures/sparse_vector.hpp | 14 ++-- .../common/data_structures/sparse_vector.tcc | 36 ++++----- src/snark/libsnark/common/profiling.cpp | 75 ++++++++++++------- src/snark/libsnark/common/profiling.hpp | 10 +-- src/snark/libsnark/common/serialization.tcc | 3 +- src/snark/libsnark/common/utils.cpp | 28 +++---- src/snark/libsnark/common/utils.hpp | 17 +++-- src/snark/libsnark/common/utils.tcc | 8 ++ .../gadgetlib1/gadgets/basic_gadgets.tcc | 40 +++++----- .../gadgets/hashes/sha256/sha256_aux.tcc | 2 +- .../hashes/sha256/sha256_components.hpp | 4 +- .../hashes/sha256/sha256_components.tcc | 6 +- .../merkle_authentication_path_variable.tcc | 4 +- .../merkle_tree_check_read_gadget.tcc | 4 +- .../merkle_tree_check_update_gadget.hpp | 1 + .../merkle_tree_check_update_gadget.tcc | 4 +- src/snark/libsnark/gadgetlib1/pb_variable.hpp | 4 +- src/snark/libsnark/gadgetlib1/pb_variable.tcc | 4 +- src/snark/libsnark/relations/variable.hpp | 2 +- .../examples/run_r1cs_ppzksnark.tcc | 2 +- .../r1cs_ppzksnark/r1cs_ppzksnark.tcc | 14 ++-- 50 files changed, 346 insertions(+), 305 deletions(-) diff --git a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp index bf7f43d6f57..7f329d6594d 100644 --- a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp +++ b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.cpp @@ -6,16 +6,17 @@ *****************************************************************************/ #include "algebra/curves/alt_bn128/alt_bn128_g1.hpp" +#include "common/assert_except.hpp" namespace libsnark { #ifdef PROFILE_OP_COUNTS -long long alt_bn128_G1::add_cnt = 0; -long long alt_bn128_G1::dbl_cnt = 0; +int64_t alt_bn128_G1::add_cnt = 0; +int64_t alt_bn128_G1::dbl_cnt = 0; #endif -std::vector alt_bn128_G1::wnaf_window_table; -std::vector alt_bn128_G1::fixed_base_exp_window_table; +std::vector alt_bn128_G1::wnaf_window_table; +std::vector alt_bn128_G1::fixed_base_exp_window_table; alt_bn128_G1 alt_bn128_G1::G1_zero; alt_bn128_G1 alt_bn128_G1::G1_one; @@ -256,7 +257,7 @@ alt_bn128_G1 alt_bn128_G1::add(const alt_bn128_G1 &other) const alt_bn128_G1 alt_bn128_G1::mixed_add(const alt_bn128_G1 &other) const { #ifdef DEBUG - assert(other.is_special()); + assert_except(other.is_special()); #endif // handle special cases having to do with O diff --git a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.hpp b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.hpp index da11a2e8c4b..567f2fa3f93 100644 --- a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.hpp +++ b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g1.hpp @@ -20,11 +20,11 @@ std::istream& operator>>(std::istream &, alt_bn128_G1&); class alt_bn128_G1 { public: #ifdef PROFILE_OP_COUNTS - static long long add_cnt; - static long long dbl_cnt; + static int64_t add_cnt; + static int64_t dbl_cnt; #endif - static std::vector wnaf_window_table; - static std::vector fixed_base_exp_window_table; + static std::vector wnaf_window_table; + static std::vector fixed_base_exp_window_table; static alt_bn128_G1 G1_zero; static alt_bn128_G1 G1_one; diff --git a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.cpp b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.cpp index c4152e4374d..98f4710443d 100644 --- a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.cpp +++ b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.cpp @@ -6,16 +6,17 @@ *****************************************************************************/ #include "algebra/curves/alt_bn128/alt_bn128_g2.hpp" +#include "common/assert_except.hpp" namespace libsnark { #ifdef PROFILE_OP_COUNTS -long long alt_bn128_G2::add_cnt = 0; -long long alt_bn128_G2::dbl_cnt = 0; +int64_t alt_bn128_G2::add_cnt = 0; +int64_t alt_bn128_G2::dbl_cnt = 0; #endif -std::vector alt_bn128_G2::wnaf_window_table; -std::vector alt_bn128_G2::fixed_base_exp_window_table; +std::vector alt_bn128_G2::wnaf_window_table; +std::vector alt_bn128_G2::fixed_base_exp_window_table; alt_bn128_G2 alt_bn128_G2::G2_zero; alt_bn128_G2 alt_bn128_G2::G2_one; @@ -266,7 +267,7 @@ alt_bn128_G2 alt_bn128_G2::add(const alt_bn128_G2 &other) const alt_bn128_G2 alt_bn128_G2::mixed_add(const alt_bn128_G2 &other) const { #ifdef DEBUG - assert(other.is_special()); + assert_except(other.is_special()); #endif // handle special cases having to do with O diff --git a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.hpp b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.hpp index a996a2d1a10..57bad1a4bdd 100644 --- a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.hpp +++ b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_g2.hpp @@ -20,11 +20,11 @@ std::istream& operator>>(std::istream &, alt_bn128_G2&); class alt_bn128_G2 { public: #ifdef PROFILE_OP_COUNTS - static long long add_cnt; - static long long dbl_cnt; + static int64_t add_cnt; + static int64_t dbl_cnt; #endif - static std::vector wnaf_window_table; - static std::vector fixed_base_exp_window_table; + static std::vector wnaf_window_table; + static std::vector fixed_base_exp_window_table; static alt_bn128_G2 G2_zero; static alt_bn128_G2 G2_one; diff --git a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_pairing.cpp b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_pairing.cpp index db556c5b287..07b6a8c7188 100644 --- a/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_pairing.cpp +++ b/src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_pairing.cpp @@ -324,7 +324,7 @@ alt_bn128_ate_G2_precomp alt_bn128_ate_precompute_G2(const alt_bn128_G2& Q) bool found_one = false; alt_bn128_ate_ell_coeffs c; - for (long i = loop_count.max_bits(); i >= 0; --i) + for (int64_t i = loop_count.max_bits(); i >= 0; --i) { const bool bit = loop_count.test_bit(i); if (!found_one) @@ -378,7 +378,7 @@ alt_bn128_Fq12 alt_bn128_ate_miller_loop(const alt_bn128_ate_G1_precomp &prec_P, const bigint &loop_count = alt_bn128_ate_loop_count; alt_bn128_ate_ell_coeffs c; - for (long i = loop_count.max_bits(); i >= 0; --i) + for (int64_t i = loop_count.max_bits(); i >= 0; --i) { const bool bit = loop_count.test_bit(i); if (!found_one) @@ -432,7 +432,7 @@ alt_bn128_Fq12 alt_bn128_ate_double_miller_loop(const alt_bn128_ate_G1_precomp & size_t idx = 0; const bigint &loop_count = alt_bn128_ate_loop_count; - for (long i = loop_count.max_bits(); i >= 0; --i) + for (int64_t i = loop_count.max_bits(); i >= 0; --i) { const bool bit = loop_count.test_bit(i); if (!found_one) diff --git a/src/snark/libsnark/algebra/curves/curve_utils.tcc b/src/snark/libsnark/algebra/curves/curve_utils.tcc index 251d75d8ba1..38140cd48af 100644 --- a/src/snark/libsnark/algebra/curves/curve_utils.tcc +++ b/src/snark/libsnark/algebra/curves/curve_utils.tcc @@ -16,7 +16,7 @@ GroupT scalar_mul(const GroupT &base, const bigint &scalar) GroupT result = GroupT::zero(); bool found_one = false; - for (long i = scalar.max_bits() - 1; i >= 0; --i) + for (int64_t i = scalar.max_bits() - 1; i >= 0; --i) { if (found_one) { diff --git a/src/snark/libsnark/algebra/evaluation_domain/domains/basic_radix2_domain.tcc b/src/snark/libsnark/algebra/evaluation_domain/domains/basic_radix2_domain.tcc index d315e8319e4..16dcd54fc56 100644 --- a/src/snark/libsnark/algebra/evaluation_domain/domains/basic_radix2_domain.tcc +++ b/src/snark/libsnark/algebra/evaluation_domain/domains/basic_radix2_domain.tcc @@ -15,15 +15,16 @@ #define BASIC_RADIX2_DOMAIN_TCC_ #include "algebra/evaluation_domain/domains/basic_radix2_domain_aux.hpp" +#include "common/assert_except.hpp" namespace libsnark { template basic_radix2_domain::basic_radix2_domain(const size_t m) : evaluation_domain(m) { - assert(m > 1); + assert_except(m > 1); const size_t logm = log2(m); - assert(logm <= (FieldT::s)); + assert_except(logm <= (FieldT::s)); omega = get_root_of_unity(m); } @@ -32,7 +33,7 @@ template void basic_radix2_domain::FFT(std::vector &a) { enter_block("Execute FFT"); - assert(a.size() == this->m); + assert_except(a.size() == this->m); _basic_radix2_FFT(a, omega); leave_block("Execute FFT"); } @@ -41,7 +42,7 @@ template void basic_radix2_domain::iFFT(std::vector &a) { enter_block("Execute inverse FFT"); - assert(a.size() == this->m); + assert_except(a.size() == this->m); _basic_radix2_FFT(a, omega.inverse()); const FieldT sconst = FieldT(a.size()).inverse(); @@ -91,7 +92,7 @@ FieldT basic_radix2_domain::compute_Z(const FieldT &t) template void basic_radix2_domain::add_poly_Z(const FieldT &coeff, std::vector &H) { - assert(H.size() == this->m+1); + assert_except(H.size() == this->m+1); H[this->m] += coeff; H[0] -= coeff; } diff --git a/src/snark/libsnark/algebra/evaluation_domain/domains/basic_radix2_domain_aux.tcc b/src/snark/libsnark/algebra/evaluation_domain/domains/basic_radix2_domain_aux.tcc index 138b82dbc10..f42e42498dc 100644 --- a/src/snark/libsnark/algebra/evaluation_domain/domains/basic_radix2_domain_aux.tcc +++ b/src/snark/libsnark/algebra/evaluation_domain/domains/basic_radix2_domain_aux.tcc @@ -21,6 +21,7 @@ #include "algebra/fields/field_utils.hpp" #include "common/profiling.hpp" #include "common/utils.hpp" +#include "common/assert_except.hpp" namespace libsnark { @@ -38,7 +39,7 @@ template void _basic_serial_radix2_FFT(std::vector &a, const FieldT &omega) { const size_t n = a.size(), logn = log2(n); - assert(n == (1u << logn)); + assert_except(n == (1u << logn)); /* swapping in place (from Storer's book) */ for (size_t k = 0; k < n; ++k) @@ -74,11 +75,11 @@ void _basic_serial_radix2_FFT(std::vector &a, const FieldT &omega) template void _basic_parallel_radix2_FFT_inner(std::vector &a, const FieldT &omega, const size_t log_cpus) { - const size_t num_cpus = 1ul< &a, const FieldT &omeg std::vector > tmp(num_cpus); for (size_t j = 0; j < num_cpus; ++j) { - tmp[j].resize(1ul<<(log_m-log_cpus), FieldT::zero()); + tmp[j].resize(UINT64_C(1)<<(log_m-log_cpus), FieldT::zero()); } #ifdef MULTICORE @@ -102,7 +103,7 @@ void _basic_parallel_radix2_FFT_inner(std::vector &a, const FieldT &omeg const FieldT omega_step = omega^(j<<(log_m - log_cpus)); FieldT elt = FieldT::one(); - for (size_t i = 0; i < 1ul<<(log_m - log_cpus); ++i) + for (size_t i = 0; i < UINT64_C(1)<<(log_m - log_cpus); ++i) { for (size_t s = 0; s < num_cpus; ++s) { @@ -135,7 +136,7 @@ void _basic_parallel_radix2_FFT_inner(std::vector &a, const FieldT &omeg #endif for (size_t i = 0; i < num_cpus; ++i) { - for (size_t j = 0; j < 1ul<<(log_m - log_cpus); ++j) + for (size_t j = 0; j < UINT64_C(1)<<(log_m - log_cpus); ++j) { // now: i = idx >> (log_m - log_cpus) and j = idx % (1u << (log_m - log_cpus)), for idx = ((i<<(log_m-log_cpus))+j) % (1u << log_m) a[(j< _basic_radix2_lagrange_coeffs(const size_t m, const FieldT & return std::vector(1, FieldT::one()); } - assert(m == (1u << log2(m))); + assert_except(m == (1u << log2(m))); const FieldT omega = get_root_of_unity(m); diff --git a/src/snark/libsnark/algebra/evaluation_domain/evaluation_domain.tcc b/src/snark/libsnark/algebra/evaluation_domain/evaluation_domain.tcc index 8e3ea7a625b..8e7ae0816d5 100644 --- a/src/snark/libsnark/algebra/evaluation_domain/evaluation_domain.tcc +++ b/src/snark/libsnark/algebra/evaluation_domain/evaluation_domain.tcc @@ -22,15 +22,16 @@ #include #include "algebra/fields/field_utils.hpp" #include "algebra/evaluation_domain/domains/basic_radix2_domain.hpp" +#include "common/assert_except.hpp" namespace libsnark { template std::shared_ptr > get_evaluation_domain(const size_t min_size) { - assert(min_size > 1); + assert_except(min_size > 1); const size_t log_min_size = log2(min_size); - assert(log_min_size <= (FieldT::s+1)); + assert_except(log_min_size <= (FieldT::s+1)); std::shared_ptr > result; if (min_size == (1u << log_min_size)) @@ -54,9 +55,9 @@ std::shared_ptr > get_evaluation_domain(const size_t m } else { - const size_t big = 1ul<<(log2(min_size)-1); + const size_t big = UINT64_C(1)<<(log2(min_size)-1); const size_t small = min_size - big; - const size_t rounded_small = (1ul< > get_evaluation_domain(const size_t m template FieldT lagrange_eval(const size_t m, const std::vector &domain, const FieldT &t, const size_t idx) { - assert(m == domain.size()); - assert(idx < m); + assert_except(m == domain.size()); + assert_except(idx < m); FieldT num = FieldT::one(); FieldT denom = FieldT::one(); diff --git a/src/snark/libsnark/algebra/exponentiation/exponentiation.hpp b/src/snark/libsnark/algebra/exponentiation/exponentiation.hpp index a8a2c925cbf..836ebf0021e 100644 --- a/src/snark/libsnark/algebra/exponentiation/exponentiation.hpp +++ b/src/snark/libsnark/algebra/exponentiation/exponentiation.hpp @@ -22,7 +22,7 @@ template FieldT power(const FieldT &base, const bigint &exponent); template -FieldT power(const FieldT &base, const unsigned long exponent); +FieldT power(const FieldT &base, const uint64_t exponent); } // libsnark diff --git a/src/snark/libsnark/algebra/exponentiation/exponentiation.tcc b/src/snark/libsnark/algebra/exponentiation/exponentiation.tcc index dd557eb1233..7ac3bf5d383 100644 --- a/src/snark/libsnark/algebra/exponentiation/exponentiation.tcc +++ b/src/snark/libsnark/algebra/exponentiation/exponentiation.tcc @@ -25,7 +25,7 @@ FieldT power(const FieldT &base, const bigint &exponent) bool found_one = false; - for (long i = exponent.max_bits() - 1; i >= 0; --i) + for (int64_t i = exponent.max_bits() - 1; i >= 0; --i) { if (found_one) { @@ -43,7 +43,7 @@ FieldT power(const FieldT &base, const bigint &exponent) } template -FieldT power(const FieldT &base, const unsigned long exponent) +FieldT power(const FieldT &base, const uint64_t exponent) { return power(base, bigint<1>(exponent)); } diff --git a/src/snark/libsnark/algebra/fields/bigint.hpp b/src/snark/libsnark/algebra/fields/bigint.hpp index ff00dd5cf4a..dc47a7efc7b 100644 --- a/src/snark/libsnark/algebra/fields/bigint.hpp +++ b/src/snark/libsnark/algebra/fields/bigint.hpp @@ -33,7 +33,7 @@ class bigint { mp_limb_t data[n] = {0}; bigint() = default; - bigint(const unsigned long x); /// Initalize from a small integer + bigint(const uint64_t x); /// Initalize from a small integer bigint(const char* s); /// Initialize from a string containing an integer in decimal notation bigint(const mpz_t r); /// Initialize from MPZ element @@ -46,7 +46,7 @@ class bigint { size_t max_bits() const { return n * GMP_NUMB_BITS; } size_t num_bits() const; - unsigned long as_ulong() const; /* return the last limb of the integer */ + uint64_t as_ulong() const; /* return the last limb of the integer */ void to_mpz(mpz_t r) const; bool test_bit(const std::size_t bitno) const; diff --git a/src/snark/libsnark/algebra/fields/bigint.tcc b/src/snark/libsnark/algebra/fields/bigint.tcc index c1777ad4666..575792be8f1 100644 --- a/src/snark/libsnark/algebra/fields/bigint.tcc +++ b/src/snark/libsnark/algebra/fields/bigint.tcc @@ -13,13 +13,14 @@ #include #include #include "sodium.h" +#include "common/assert_except.hpp" namespace libsnark { template -bigint::bigint(const unsigned long x) /// Initalize from a small integer +bigint::bigint(const uint64_t x) /// Initalize from a small integer { - static_assert(ULONG_MAX <= GMP_NUMB_MAX, "unsigned long does not fit in a GMP limb"); + static_assert(UINT64_MAX <= GMP_NUMB_MAX, "uint64_t does not fit in a GMP limb"); this->data[0] = x; } @@ -31,12 +32,12 @@ bigint::bigint(const char* s) /// Initialize from a string containing an inte for (size_t i = 0; i < l; ++i) { - assert(s[i] >= '0' && s[i] <= '9'); + assert_except(s[i] >= '0' && s[i] <= '9'); s_copy[i] = s[i] - '0'; } mp_size_t limbs_written = mpn_set_str(this->data, s_copy, l, 10); - assert(limbs_written <= n); + assert_except(limbs_written <= n); delete[] s_copy; } @@ -53,7 +54,7 @@ bigint::bigint(const mpz_t r) /// Initialize from MPZ element mpz_fdiv_q_2exp(k, k, GMP_NUMB_BITS); } - assert(mpz_sgn(k) == 0); + assert_except(mpz_sgn(k) == 0); mpz_clear(k); } @@ -105,7 +106,7 @@ template size_t bigint::num_bits() const { /* - for (long i = max_bits(); i >= 0; --i) + for (int64_t i = max_bits(); i >= 0; --i) { if (this->test_bit(i)) { @@ -115,7 +116,7 @@ size_t bigint::num_bits() const return 0; */ - for (long i = n-1; i >= 0; --i) + for (int64_t i = n-1; i >= 0; --i) { mp_limb_t x = this->data[i]; if (x == 0) @@ -124,14 +125,14 @@ size_t bigint::num_bits() const } else { - return ((i+1) * GMP_NUMB_BITS) - __builtin_clzl(x); + return ((i+1) * GMP_NUMB_BITS) - __builtin_clzll(x); } } return 0; } template -unsigned long bigint::as_ulong() const +uint64_t bigint::as_ulong() const { return this->data[0]; } @@ -186,7 +187,7 @@ inline void bigint::div_qr(bigint& quotient, bigint& remainder, const bigint& dividend, const bigint& divisor) { static_assert(n >= d, "dividend must not be smaller than divisor for bigint::div_qr"); - assert(divisor.data[d-1] != 0); + assert_except(divisor.data[d-1] != 0); mpn_tdiv_qr(quotient.data, remainder.data, 0, dividend.data, n, divisor.data, d); } @@ -223,7 +224,7 @@ inline bool bigint::operator>(const bigint& other) const template bigint& bigint::randomize() { - assert(GMP_NUMB_BITS == sizeof(mp_limb_t) * 8); + assert_except(GMP_NUMB_BITS == sizeof(mp_limb_t) * 8); randombytes_buf(this->data, sizeof(mp_limb_t) * n); @@ -262,12 +263,12 @@ std::istream& operator>>(std::istream &in, bigint &b) for (size_t i = 0; i < l; ++i) { - assert(s[i] >= '0' && s[i] <= '9'); + assert_except(s[i] >= '0' && s[i] <= '9'); s_copy[i] = s[i] - '0'; } mp_size_t limbs_written = mpn_set_str(b.data, s_copy, l, 10); - assert(limbs_written <= n); + assert_except(limbs_written <= n); delete[] s_copy; #endif diff --git a/src/snark/libsnark/algebra/fields/field_utils.hpp b/src/snark/libsnark/algebra/fields/field_utils.hpp index a07ecfe284e..8358e48b37e 100644 --- a/src/snark/libsnark/algebra/fields/field_utils.hpp +++ b/src/snark/libsnark/algebra/fields/field_utils.hpp @@ -16,13 +16,13 @@ namespace libsnark { // returns root of unity of order n (for n a power of 2), if one exists template -FieldT get_root_of_unity(const size_t n); +FieldT get_root_of_unity(const uint64_t n); template -std::vector pack_int_vector_into_field_element_vector(const std::vector &v, const size_t w); +std::vector pack_int_vector_into_field_element_vector(const std::vector &v, const uint64_t w); template -std::vector pack_bit_vector_into_field_element_vector(const bit_vector &v, const size_t chunk_bits); +std::vector pack_bit_vector_into_field_element_vector(const bit_vector &v, const uint64_t chunk_bits); template std::vector pack_bit_vector_into_field_element_vector(const bit_vector &v); @@ -37,7 +37,7 @@ template bit_vector convert_field_element_to_bit_vector(const FieldT &el); template -bit_vector convert_field_element_to_bit_vector(const FieldT &el, const size_t bitcount); +bit_vector convert_field_element_to_bit_vector(const FieldT &el, const uint64_t bitcount); template FieldT convert_bit_vector_to_field_element(const bit_vector &v); diff --git a/src/snark/libsnark/algebra/fields/field_utils.tcc b/src/snark/libsnark/algebra/fields/field_utils.tcc index 13197b226f5..a0928605d59 100644 --- a/src/snark/libsnark/algebra/fields/field_utils.tcc +++ b/src/snark/libsnark/algebra/fields/field_utils.tcc @@ -11,6 +11,7 @@ #define FIELD_UTILS_TCC_ #include "common/utils.hpp" +#include "common/assert_except.hpp" namespace libsnark { @@ -21,14 +22,14 @@ FieldT coset_shift() } template -FieldT get_root_of_unity(const size_t n) +FieldT get_root_of_unity(const uint64_t n) { - const size_t logn = log2(n); - assert(n == (1u << logn)); - assert(logn <= FieldT::s); + const uint64_t logn = log2(n); + assert_except(n == (1u << logn)); + assert_except(logn <= FieldT::s); FieldT omega = FieldT::root_of_unity; - for (size_t i = FieldT::s; i > logn; --i) + for (uint64_t i = FieldT::s; i > logn; --i) { omega *= omega; } @@ -37,21 +38,21 @@ FieldT get_root_of_unity(const size_t n) } template -std::vector pack_int_vector_into_field_element_vector(const std::vector &v, const size_t w) +std::vector pack_int_vector_into_field_element_vector(const std::vector &v, const uint64_t w) { - const size_t chunk_bits = FieldT::capacity(); - const size_t repacked_size = div_ceil(v.size() * w, chunk_bits); + const uint64_t chunk_bits = FieldT::capacity(); + const uint64_t repacked_size = div_ceil(v.size() * w, chunk_bits); std::vector result(repacked_size); - for (size_t i = 0; i < repacked_size; ++i) + for (uint64_t i = 0; i < repacked_size; ++i) { bigint b; - for (size_t j = 0; j < chunk_bits; ++j) + for (uint64_t j = 0; j < chunk_bits; ++j) { - const size_t word_index = (i * chunk_bits + j) / w; - const size_t pos_in_word = (i * chunk_bits + j) % w; - const size_t word_or_0 = (word_index < v.size() ? v[word_index] : 0); - const size_t bit = (word_or_0 >> pos_in_word) & 1; + const uint64_t word_index = (i * chunk_bits + j) / w; + const uint64_t pos_in_word = (i * chunk_bits + j) % w; + const uint64_t word_or_0 = (word_index < v.size() ? v[word_index] : 0); + const uint64_t bit = (word_or_0 >> pos_in_word) & 1; b.data[j / GMP_NUMB_BITS] |= bit << (j % GMP_NUMB_BITS); } @@ -62,11 +63,11 @@ std::vector pack_int_vector_into_field_element_vector(const std::vector< } template -std::vector pack_bit_vector_into_field_element_vector(const bit_vector &v, const size_t chunk_bits) +std::vector pack_bit_vector_into_field_element_vector(const bit_vector &v, const uint64_t chunk_bits) { - assert(chunk_bits <= FieldT::capacity()); + assert_except(chunk_bits <= FieldT::capacity()); - const size_t repacked_size = div_ceil(v.size(), chunk_bits); + const uint64_t repacked_size = div_ceil(v.size(), chunk_bits); std::vector result(repacked_size); for (size_t i = 0; i < repacked_size; ++i) @@ -131,7 +132,7 @@ bit_vector convert_field_element_to_bit_vector(const FieldT &el) } template -bit_vector convert_field_element_to_bit_vector(const FieldT &el, const size_t bitcount) +bit_vector convert_field_element_to_bit_vector(const FieldT &el, const uint64_t bitcount) { bit_vector result = convert_field_element_to_bit_vector(el); result.resize(bitcount); @@ -142,7 +143,7 @@ bit_vector convert_field_element_to_bit_vector(const FieldT &el, const size_t bi template FieldT convert_bit_vector_to_field_element(const bit_vector &v) { - assert(v.size() <= FieldT::size_in_bits()); + assert_except(v.size() <= FieldT::size_in_bits()); FieldT res = FieldT::zero(); FieldT c = FieldT::one(); @@ -164,14 +165,14 @@ void batch_invert(std::vector &vec) for (auto el : vec) { - assert(!el.is_zero()); + assert_except(!el.is_zero()); prod.emplace_back(acc); acc = acc * el; } FieldT acc_inverse = acc.inverse(); - for (long i = vec.size()-1; i >= 0; --i) + for (int64_t i = vec.size()-1; i >= 0; --i) { const FieldT old_el = vec[i]; vec[i] = acc_inverse * prod[i]; diff --git a/src/snark/libsnark/algebra/fields/fp.hpp b/src/snark/libsnark/algebra/fields/fp.hpp index a4986833c30..384e5747ccd 100644 --- a/src/snark/libsnark/algebra/fields/fp.hpp +++ b/src/snark/libsnark/algebra/fields/fp.hpp @@ -44,15 +44,15 @@ class Fp_model { static const mp_size_t num_limbs = n; static const constexpr bigint& mod = modulus; #ifdef PROFILE_OP_COUNTS - static long long add_cnt; - static long long sub_cnt; - static long long mul_cnt; - static long long sqr_cnt; - static long long inv_cnt; + static int64_t add_cnt; + static int64_t sub_cnt; + static int64_t mul_cnt; + static int64_t sqr_cnt; + static int64_t inv_cnt; #endif - static size_t num_bits; + static uint64_t num_bits; static bigint euler; // (modulus-1)/2 - static size_t s; // modulus = 2^s * t + 1 + static uint64_t s; // modulus = 2^s * t + 1 static bigint t; // with t odd static bigint t_minus_1_over_2; // (t-1)/2 static Fp_model nqr; // a quadratic nonresidue @@ -67,9 +67,9 @@ class Fp_model { Fp_model() {}; Fp_model(const bigint &b); - Fp_model(const long x, const bool is_unsigned=false); + Fp_model(const int64_t x, const bool is_unsigned=false); - void set_ulong(const unsigned long x); + void set_ulong(const uint64_t x); void mul_reduce(const bigint &other); @@ -82,7 +82,7 @@ class Fp_model { /* Return the last limb of the standard representation of the field element. E.g. on 64-bit architectures Fp(123).as_ulong() and Fp(2^64+123).as_ulong() would both return 123. */ - unsigned long as_ulong() const; + uint64_t as_ulong() const; bool operator==(const Fp_model& other) const; bool operator!=(const Fp_model& other) const; @@ -93,7 +93,7 @@ class Fp_model { Fp_model& operator+=(const Fp_model& other); Fp_model& operator-=(const Fp_model& other); Fp_model& operator*=(const Fp_model& other); - Fp_model& operator^=(const unsigned long pow); + Fp_model& operator^=(const uint64_t pow); template Fp_model& operator^=(const bigint &pow); @@ -107,12 +107,12 @@ class Fp_model { Fp_model inverse() const; Fp_model sqrt() const; // HAS TO BE A SQUARE (else does not terminate) - Fp_model operator^(const unsigned long pow) const; + Fp_model operator^(const uint64_t pow) const; template Fp_model operator^(const bigint &pow) const; - static size_t size_in_bits() { return num_bits; } - static size_t capacity() { return num_bits - 1; } + static uint64_t size_in_bits() { return num_bits; } + static uint64_t capacity() { return num_bits - 1; } static bigint field_char() { return modulus; } static Fp_model zero(); @@ -125,29 +125,29 @@ class Fp_model { #ifdef PROFILE_OP_COUNTS template& modulus> -long long Fp_model::add_cnt = 0; +int64_t Fp_model::add_cnt = 0; template& modulus> -long long Fp_model::sub_cnt = 0; +int64_t Fp_model::sub_cnt = 0; template& modulus> -long long Fp_model::mul_cnt = 0; +int64_t Fp_model::mul_cnt = 0; template& modulus> -long long Fp_model::sqr_cnt = 0; +int64_t Fp_model::sqr_cnt = 0; template& modulus> -long long Fp_model::inv_cnt = 0; +int64_t Fp_model::inv_cnt = 0; #endif template& modulus> -size_t Fp_model::num_bits; +uint64_t Fp_model::num_bits; template& modulus> bigint Fp_model::euler; template& modulus> -size_t Fp_model::s; +uint64_t Fp_model::s; template& modulus> bigint Fp_model::t; diff --git a/src/snark/libsnark/algebra/fields/fp.tcc b/src/snark/libsnark/algebra/fields/fp.tcc index 566e99324a8..3d220e2e370 100644 --- a/src/snark/libsnark/algebra/fields/fp.tcc +++ b/src/snark/libsnark/algebra/fields/fp.tcc @@ -173,13 +173,13 @@ void Fp_model::mul_reduce(const bigint &other) /* calculate res = res + k * mod * b^i */ mp_limb_t carryout = mpn_addmul_1(res+i, modulus.data, n, k); carryout = mpn_add_1(res+n+i, res+n+i, n-i, carryout); - assert(carryout == 0); + assert_except(carryout == 0); } if (mpn_cmp(res+n, modulus.data, n) >= 0) { const mp_limb_t borrow = mpn_sub(res+n, res+n, n, modulus.data, n); - assert(borrow == 0); + assert_except(borrow == 0); } mpn_copyi(this->mont_repr.data, res+n, n); @@ -194,7 +194,7 @@ Fp_model::Fp_model(const bigint &b) } template& modulus> -Fp_model::Fp_model(const long x, const bool is_unsigned) +Fp_model::Fp_model(const int64_t x, const bool is_unsigned) { if (is_unsigned || x >= 0) { @@ -203,14 +203,14 @@ Fp_model::Fp_model(const long x, const bool is_unsigned) else { const mp_limb_t borrow = mpn_sub_1(this->mont_repr.data, modulus.data, n, -x); - assert(borrow == 0); + assert_except(borrow == 0); } mul_reduce(Rsquared); } template& modulus> -void Fp_model::set_ulong(const unsigned long x) +void Fp_model::set_ulong(const uint64_t x) { this->mont_repr.clear(); this->mont_repr.data[0] = x; @@ -237,7 +237,7 @@ bigint Fp_model::as_bigint() const } template& modulus> -unsigned long Fp_model::as_ulong() const +uint64_t Fp_model::as_ulong() const { return this->as_bigint().as_ulong(); } @@ -391,7 +391,7 @@ Fp_model& Fp_model::operator+=(const Fp_model& if (carry || mpn_cmp(scratch, modulus.data, n) >= 0) { const mp_limb_t borrow = mpn_sub(scratch, scratch, n+1, modulus.data, n); - assert(borrow == 0); + assert_except(borrow == 0); } mpn_copyi(this->mont_repr.data, scratch, n); @@ -483,7 +483,7 @@ Fp_model& Fp_model::operator-=(const Fp_model& } const mp_limb_t borrow = mpn_sub(scratch, scratch, n+1, other.mont_repr.data, n); - assert(borrow == 0); + assert_except(borrow == 0); mpn_copyi(this->mont_repr.data, scratch, n); } @@ -502,7 +502,7 @@ Fp_model& Fp_model::operator*=(const Fp_model& } template& modulus> -Fp_model& Fp_model::operator^=(const unsigned long pow) +Fp_model& Fp_model::operator^=(const uint64_t pow) { (*this) = power >(*this, pow); return (*this); @@ -538,7 +538,7 @@ Fp_model Fp_model::operator*(const Fp_model& ot } template& modulus> -Fp_model Fp_model::operator^(const unsigned long pow) const +Fp_model Fp_model::operator^(const uint64_t pow) const { Fp_model r(*this); return (r ^= pow); @@ -626,7 +626,7 @@ Fp_model& Fp_model::invert() this->inv_cnt++; #endif - assert(!this->is_zero()); + assert_except(!this->is_zero()); bigint g; /* gp should have room for vn = n limbs */ @@ -637,7 +637,7 @@ Fp_model& Fp_model::invert() /* computes gcd(u, v) = g = u*s + v*t, so s*u will be 1 (mod v) */ const mp_size_t gn = mpn_gcdext(g.data, s, &sn, this->mont_repr.data, n, v.data, n); - assert(gn == 1 && g.data[0] == 1); /* inverse exists */ + assert_except(gn == 1 && g.data[0] == 1); /* inverse exists */ mp_limb_t q; /* division result fits into q, as sn <= n+1 */ /* sn < 0 indicates negative sn; will fix up later */ @@ -658,7 +658,7 @@ Fp_model& Fp_model::invert() if (sn < 0) { const mp_limb_t borrow = mpn_sub_n(this->mont_repr.data, modulus.data, this->mont_repr.data, n); - assert(borrow == 0); + assert_except(borrow == 0); } mul_reduce(Rcubed); @@ -684,13 +684,13 @@ Fp_model Fp_model::random_element() /// returns random el r.mont_repr.randomize(); /* clear all bits higher than MSB of modulus */ - size_t bitno = GMP_NUMB_BITS * n - 1; + uint64_t bitno = GMP_NUMB_BITS * n - 1; while (modulus.test_bit(bitno) == false) { - const std::size_t part = bitno/GMP_NUMB_BITS; - const std::size_t bit = bitno - (GMP_NUMB_BITS*part); + const uint64_t part = bitno/GMP_NUMB_BITS; + const uint64_t bit = bitno - (GMP_NUMB_BITS*part); - r.mont_repr.data[part] &= ~(1ul< Fp_model::sqrt() const Fp_model one = Fp_model::one(); - size_t v = Fp_model::s; + uint64_t v = Fp_model::s; Fp_model z = Fp_model::nqr_to_t; Fp_model w = (*this)^Fp_model::t_minus_1_over_2; Fp_model x = (*this) * w; @@ -734,7 +734,7 @@ Fp_model Fp_model::sqrt() const while (b != one) { - size_t m = 0; + uint64_t m = 0; Fp_model b2m = b; while (b2m != one) { diff --git a/src/snark/libsnark/algebra/fields/fp12_2over3over2.hpp b/src/snark/libsnark/algebra/fields/fp12_2over3over2.hpp index 1de9d88b45c..69d057a0593 100644 --- a/src/snark/libsnark/algebra/fields/fp12_2over3over2.hpp +++ b/src/snark/libsnark/algebra/fields/fp12_2over3over2.hpp @@ -66,7 +66,7 @@ class Fp12_2over3over2_model { Fp12_2over3over2_model squared_karatsuba() const; Fp12_2over3over2_model squared_complex() const; Fp12_2over3over2_model inverse() const; - Fp12_2over3over2_model Frobenius_map(unsigned long power) const; + Fp12_2over3over2_model Frobenius_map(uint64_t power) const; Fp12_2over3over2_model unitary_inverse() const; Fp12_2over3over2_model cyclotomic_squared() const; @@ -78,7 +78,7 @@ class Fp12_2over3over2_model { Fp12_2over3over2_model cyclotomic_exp(const bigint &exponent) const; static bigint base_field_char() { return modulus; } - static size_t extension_degree() { return 12; } + static uint64_t extension_degree() { return 12; } friend std::ostream& operator<< (std::ostream &out, const Fp12_2over3over2_model &el); friend std::istream& operator>> (std::istream &in, Fp12_2over3over2_model &el); diff --git a/src/snark/libsnark/algebra/fields/fp12_2over3over2.tcc b/src/snark/libsnark/algebra/fields/fp12_2over3over2.tcc index 2fbc0b649ad..d985d687f00 100644 --- a/src/snark/libsnark/algebra/fields/fp12_2over3over2.tcc +++ b/src/snark/libsnark/algebra/fields/fp12_2over3over2.tcc @@ -156,7 +156,7 @@ Fp12_2over3over2_model Fp12_2over3over2_model::inverse() c } template& modulus> -Fp12_2over3over2_model Fp12_2over3over2_model::Frobenius_map(unsigned long power) const +Fp12_2over3over2_model Fp12_2over3over2_model::Frobenius_map(uint64_t power) const { return Fp12_2over3over2_model(c0.Frobenius_map(power), Frobenius_coeffs_c1[power % 12] * c1.Frobenius_map(power)); @@ -339,16 +339,16 @@ Fp12_2over3over2_model Fp12_2over3over2_model::cyclotomic Fp12_2over3over2_model res = Fp12_2over3over2_model::one(); bool found_one = false; - for (long i = m-1; i >= 0; --i) + for (int64_t i = m-1; i >= 0; --i) { - for (long j = GMP_NUMB_BITS - 1; j >= 0; --j) + for (int64_t j = GMP_NUMB_BITS - 1; j >= 0; --j) { if (found_one) { res = res.cyclotomic_squared(); } - if (exponent.data[i] & (1ul<>(std::istream& in, std::vector> s; char b; diff --git a/src/snark/libsnark/algebra/fields/fp2.hpp b/src/snark/libsnark/algebra/fields/fp2.hpp index f07726918cd..f194f5ec5a0 100644 --- a/src/snark/libsnark/algebra/fields/fp2.hpp +++ b/src/snark/libsnark/algebra/fields/fp2.hpp @@ -37,7 +37,7 @@ class Fp2_model { typedef Fp_model my_Fp; static bigint<2*n> euler; // (modulus^2-1)/2 - static size_t s; // modulus^2 = 2^s * t + 1 + static uint64_t s; // modulus^2 = 2^s * t + 1 static bigint<2*n> t; // with t odd static bigint<2*n> t_minus_1_over_2; // (t-1)/2 static my_Fp non_residue; // X^4-non_residue irreducible over Fp; used for constructing Fp2 = Fp[X] / (X^2 - non_residue) @@ -66,7 +66,7 @@ class Fp2_model { Fp2_model operator-() const; Fp2_model squared() const; // default is squared_complex Fp2_model inverse() const; - Fp2_model Frobenius_map(unsigned long power) const; + Fp2_model Frobenius_map(uint64_t power) const; Fp2_model sqrt() const; // HAS TO BE A SQUARE (else does not terminate) Fp2_model squared_karatsuba() const; Fp2_model squared_complex() const; @@ -74,7 +74,7 @@ class Fp2_model { template Fp2_model operator^(const bigint &other) const; - static size_t size_in_bits() { return 2*my_Fp::size_in_bits(); } + static uint64_t size_in_bits() { return 2*my_Fp::size_in_bits(); } static bigint base_field_char() { return modulus; } friend std::ostream& operator<< (std::ostream &out, const Fp2_model &el); @@ -94,7 +94,7 @@ template& modulus> bigint<2*n> Fp2_model::euler; template& modulus> -size_t Fp2_model::s; +uint64_t Fp2_model::s; template& modulus> bigint<2*n> Fp2_model::t; diff --git a/src/snark/libsnark/algebra/fields/fp2.tcc b/src/snark/libsnark/algebra/fields/fp2.tcc index 1632a04c79e..84aa3035cb5 100644 --- a/src/snark/libsnark/algebra/fields/fp2.tcc +++ b/src/snark/libsnark/algebra/fields/fp2.tcc @@ -136,7 +136,7 @@ Fp2_model Fp2_model::inverse() const } template& modulus> -Fp2_model Fp2_model::Frobenius_map(unsigned long power) const +Fp2_model Fp2_model::Frobenius_map(uint64_t power) const { return Fp2_model(c0, Frobenius_coeffs_c1[power % 2] * c1); @@ -151,7 +151,7 @@ Fp2_model Fp2_model::sqrt() const Fp2_model one = Fp2_model::one(); - size_t v = Fp2_model::s; + unsigned long long v = Fp2_model::s; Fp2_model z = Fp2_model::nqr_to_t; Fp2_model w = (*this)^Fp2_model::t_minus_1_over_2; Fp2_model x = (*this) * w; @@ -175,7 +175,7 @@ Fp2_model Fp2_model::sqrt() const while (b != one) { - size_t m = 0; + unsigned long long m = 0; Fp2_model b2m = b; while (b2m != one) { @@ -239,7 +239,7 @@ std::istream& operator>>(std::istream& in, std::vector > & { v.clear(); - size_t s; + unsigned long long s; in >> s; char b; diff --git a/src/snark/libsnark/algebra/fields/fp6_3over2.hpp b/src/snark/libsnark/algebra/fields/fp6_3over2.hpp index 335d61c5341..8a888f0cfba 100644 --- a/src/snark/libsnark/algebra/fields/fp6_3over2.hpp +++ b/src/snark/libsnark/algebra/fields/fp6_3over2.hpp @@ -63,7 +63,7 @@ class Fp6_3over2_model { Fp6_3over2_model operator-() const; Fp6_3over2_model squared() const; Fp6_3over2_model inverse() const; - Fp6_3over2_model Frobenius_map(unsigned long power) const; + Fp6_3over2_model Frobenius_map(uint64_t power) const; static my_Fp2 mul_by_non_residue(const my_Fp2 &elt); @@ -71,7 +71,7 @@ class Fp6_3over2_model { Fp6_3over2_model operator^(const bigint &other) const; static bigint base_field_char() { return modulus; } - static size_t extension_degree() { return 6; } + static uint64_t extension_degree() { return 6; } friend std::ostream& operator<< (std::ostream &out, const Fp6_3over2_model &el); friend std::istream& operator>> (std::istream &in, Fp6_3over2_model &el); diff --git a/src/snark/libsnark/algebra/fields/fp6_3over2.tcc b/src/snark/libsnark/algebra/fields/fp6_3over2.tcc index f4fffde04a7..de9b83d115d 100644 --- a/src/snark/libsnark/algebra/fields/fp6_3over2.tcc +++ b/src/snark/libsnark/algebra/fields/fp6_3over2.tcc @@ -149,7 +149,7 @@ Fp6_3over2_model Fp6_3over2_model::inverse() const } template& modulus> -Fp6_3over2_model Fp6_3over2_model::Frobenius_map(unsigned long power) const +Fp6_3over2_model Fp6_3over2_model::Frobenius_map(uint64_t power) const { return Fp6_3over2_model(c0.Frobenius_map(power), Frobenius_coeffs_c1[power % 6] * c1.Frobenius_map(power), @@ -194,7 +194,7 @@ std::istream& operator>>(std::istream& in, std::vector> s; char b; diff --git a/src/snark/libsnark/algebra/fields/tests/test_bigint.cpp b/src/snark/libsnark/algebra/fields/tests/test_bigint.cpp index d2da59e736a..3d427f1cea1 100644 --- a/src/snark/libsnark/algebra/fields/tests/test_bigint.cpp +++ b/src/snark/libsnark/algebra/fields/tests/test_bigint.cpp @@ -13,7 +13,7 @@ using namespace libsnark; TEST(algebra, bigint) { - static_assert(ULONG_MAX == 0xFFFFFFFFFFFFFFFFul, "unsigned long not 64-bit"); + static_assert(UINT64_MAX == 0xFFFFFFFFFFFFFFFFul, "unsigned long not 64-bit"); static_assert(GMP_NUMB_BITS == 64, "GMP limb not 64-bit"); const char *b1_decimal = "76749407"; @@ -22,11 +22,11 @@ TEST(algebra, bigint) const char *b2_binary = "0000000000000000000000000000010101111101101000000110100001011010" "1101101010001001000001101000101000100110011001110001111110100010"; - bigint<1> b0 = bigint<1>(0ul); + bigint<1> b0 = bigint<1>(UINT64_C(0)); bigint<1> b1 = bigint<1>(b1_decimal); bigint<2> b2 = bigint<2>(b2_decimal); - EXPECT_EQ(b0.as_ulong(), 0ul); + EXPECT_EQ(b0.as_ulong(), UINT64_C(0)); EXPECT_TRUE(b0.is_zero()); EXPECT_EQ(b1.as_ulong(), 76749407ul); EXPECT_FALSE(b1.is_zero()); @@ -94,4 +94,3 @@ TEST(algebra, bigint) // TODO: test serialization } - diff --git a/src/snark/libsnark/algebra/scalar_multiplication/kc_multiexp.tcc b/src/snark/libsnark/algebra/scalar_multiplication/kc_multiexp.tcc index e9c08d4bcea..605203347af 100644 --- a/src/snark/libsnark/algebra/scalar_multiplication/kc_multiexp.tcc +++ b/src/snark/libsnark/algebra/scalar_multiplication/kc_multiexp.tcc @@ -8,6 +8,8 @@ #ifndef KC_MULTIEXP_TCC_ #define KC_MULTIEXP_TCC_ +#include "common/assert_except.hpp" + namespace libsnark { template @@ -50,7 +52,7 @@ knowledge_commitment kc_multi_exp_with_mixed_addition(const knowledge_co while (index_it != vec.indices.end() && *index_it < max_idx) { const size_t scalar_position = (*index_it) - min_idx; - assert(scalar_position < scalar_length); + assert_except(scalar_position < scalar_length); const FieldT scalar = *(scalar_start + scalar_position); diff --git a/src/snark/libsnark/algebra/scalar_multiplication/multiexp.tcc b/src/snark/libsnark/algebra/scalar_multiplication/multiexp.tcc index a6b14c4dfa4..5dd19a651ab 100644 --- a/src/snark/libsnark/algebra/scalar_multiplication/multiexp.tcc +++ b/src/snark/libsnark/algebra/scalar_multiplication/multiexp.tcc @@ -22,6 +22,7 @@ #include "common/profiling.hpp" #include "common/utils.hpp" +#include "common/assert_except.hpp" #include "algebra/scalar_multiplication/wnaf.hpp" namespace libsnark { @@ -40,7 +41,7 @@ public: #if defined(__x86_64__) && defined(USE_ASM) if (n == 3) { - long res; + int64_t res; __asm__ ("// check for overflow \n\t" "mov $0, %[res] \n\t" @@ -58,7 +59,7 @@ public: } else if (n == 4) { - long res; + int64_t res; __asm__ ("// check for overflow \n\t" "mov $0, %[res] \n\t" @@ -77,7 +78,7 @@ public: } else if (n == 5) { - long res; + int64_t res; __asm__ ("// check for overflow \n\t" "mov $0, %[res] \n\t" @@ -119,7 +120,7 @@ T naive_exp(typename std::vector::const_iterator vec_start, bigint scalar_bigint = scalar_it->as_bigint(); result = result + opt_window_wnaf_exp(*vec_it, scalar_bigint, scalar_bigint.num_bits()); } - assert(scalar_it == scalar_end); + assert_except(scalar_it == scalar_end); return result; } @@ -139,7 +140,7 @@ T naive_plain_exp(typename std::vector::const_iterator vec_start, { result = result + (*scalar_it) * (*vec_it); } - assert(scalar_it == scalar_end); + assert_except(scalar_it == scalar_end); return result; } @@ -185,15 +186,15 @@ T multi_exp_inner(typename std::vector::const_iterator vec_start, opt_q.emplace_back(ordered_exponent(i, scalar_it->as_bigint())); } std::make_heap(opt_q.begin(),opt_q.end()); - assert(scalar_it == scalar_end); + assert_except(scalar_it == scalar_end); if (vec_len != odd_vec_len) { g.emplace_back(T::zero()); - opt_q.emplace_back(ordered_exponent(odd_vec_len - 1, bigint(0ul))); + opt_q.emplace_back(ordered_exponent(odd_vec_len - 1, bigint(UINT64_C(0)))); } - assert(g.size() % 2 == 1); - assert(opt_q.size() == g.size()); + assert_except(g.size() % 2 == 1); + assert_except(opt_q.size() == g.size()); T opt_result = T::zero(); @@ -214,7 +215,7 @@ T multi_exp_inner(typename std::vector::const_iterator vec_start, const size_t bbits = b.r.num_bits(); const size_t limit = (abits-bbits >= 20 ? 20 : abits-bbits); - if (bbits < 1ul<::const_iterator vec_star const size_t chunks, const bool use_multiexp) { - assert(std::distance(vec_start, vec_end) == std::distance(scalar_start, scalar_end)); + assert_except(std::distance(vec_start, vec_end) == std::distance(scalar_start, scalar_end)); enter_block("Process scalar vector"); auto value_it = vec_start; auto scalar_it = scalar_start; @@ -389,7 +390,7 @@ size_t get_exp_window_size(const size_t num_scalars) #endif } size_t window = 1; - for (long i = T::fixed_base_exp_window_table.size()-1; i >= 0; --i) + for (int64_t i = T::fixed_base_exp_window_table.size()-1; i >= 0; --i) { #ifdef DEBUG if (!inhibit_profiling_info) @@ -420,9 +421,9 @@ window_table get_window_table(const size_t scalar_size, const size_t window, const T &g) { - const size_t in_window = 1ul< -std::vector find_wnaf(const size_t window_size, const bigint &scalar); +std::vector find_wnaf(const size_t window_size, const bigint &scalar); /** * In additive notation, use wNAF exponentiation (with the given window size) to compute scalar * base. diff --git a/src/snark/libsnark/algebra/scalar_multiplication/wnaf.tcc b/src/snark/libsnark/algebra/scalar_multiplication/wnaf.tcc index a5e47e8e2e4..4f2e4072c7d 100644 --- a/src/snark/libsnark/algebra/scalar_multiplication/wnaf.tcc +++ b/src/snark/libsnark/algebra/scalar_multiplication/wnaf.tcc @@ -17,15 +17,15 @@ namespace libsnark { template -std::vector find_wnaf(const size_t window_size, const bigint &scalar) +std::vector find_wnaf(const size_t window_size, const bigint &scalar) { const size_t length = scalar.max_bits(); // upper bound - std::vector res(length+1); + std::vector res(length+1); bigint c = scalar; - long j = 0; + int64_t j = 0; while (!c.is_zero()) { - long u; + int64_t u; if ((c.data[0] & 1) == 1) { u = c.data[0] % (1u << (window_size+1)); @@ -59,11 +59,11 @@ std::vector find_wnaf(const size_t window_size, const bigint &scalar) template T fixed_window_wnaf_exp(const size_t window_size, const T &base, const bigint &scalar) { - std::vector naf = find_wnaf(window_size, scalar); - std::vector table(1ul<<(window_size-1)); + std::vector naf = find_wnaf(window_size, scalar); + std::vector table(UINT64_C(1)<<(window_size-1)); T tmp = base; T dbl = base.dbl(); - for (size_t i = 0; i < 1ul<<(window_size-1); ++i) + for (size_t i = 0; i < UINT64_C(1)<<(window_size-1); ++i) { table[i] = tmp; tmp = tmp + dbl; @@ -71,7 +71,7 @@ T fixed_window_wnaf_exp(const size_t window_size, const T &base, const bigint T res = T::zero(); bool found_nonzero = false; - for (long i = naf.size()-1; i >= 0; --i) + for (int64_t i = naf.size()-1; i >= 0; --i) { if (found_nonzero) { @@ -99,7 +99,7 @@ template T opt_window_wnaf_exp(const T &base, const bigint &scalar, const size_t scalar_bits) { size_t best = 0; - for (long i = T::wnaf_window_table.size() - 1; i >= 0; --i) + for (int64_t i = T::wnaf_window_table.size() - 1; i >= 0; --i) { if (scalar_bits >= T::wnaf_window_table[i]) { diff --git a/src/snark/libsnark/common/data_structures/merkle_tree.tcc b/src/snark/libsnark/common/data_structures/merkle_tree.tcc index 281700b3317..ce28b124fb1 100644 --- a/src/snark/libsnark/common/data_structures/merkle_tree.tcc +++ b/src/snark/libsnark/common/data_structures/merkle_tree.tcc @@ -66,14 +66,14 @@ merkle_tree::merkle_tree(const size_t depth, assert(log2(contents_as_vector.size()) <= depth); for (size_t address = 0; address < contents_as_vector.size(); ++address) { - const size_t idx = address + (1ul< 0; --layer) { @@ -100,13 +100,13 @@ merkle_tree::merkle_tree(const size_t depth, if (!contents.empty()) { - assert(contents.rbegin()->first < 1ul<first < UINT64_C(1)<first; const bit_vector value = it->second; - const size_t idx = address + (1ul<::set_value(const size_t address, const bit_vector &value) { assert(log2(address) <= depth); - size_t idx = address + (1ul<::get_path(con { typename HashT::merkle_authentication_path_type result(depth); assert(log2(address) <= depth); - size_t idx = address + (1ul< 0; --layer) { @@ -209,7 +209,7 @@ typename HashT::merkle_authentication_path_type merkle_tree::get_path(con auto it = hashes.find(sibling_idx); if (layer == depth) { - auto it2 = values.find(sibling_idx - ((1ul<second); result[layer-1].resize(digest_size); } @@ -227,7 +227,7 @@ typename HashT::merkle_authentication_path_type merkle_tree::get_path(con template void merkle_tree::dump() const { - for (size_t i = 0; i < 1ul< ", i); diff --git a/src/snark/libsnark/common/data_structures/sparse_vector.hpp b/src/snark/libsnark/common/data_structures/sparse_vector.hpp index 8b134f42e39..20f1bc2673f 100644 --- a/src/snark/libsnark/common/data_structures/sparse_vector.hpp +++ b/src/snark/libsnark/common/data_structures/sparse_vector.hpp @@ -32,9 +32,9 @@ std::istream& operator>>(std::istream &in, sparse_vector &v); template struct sparse_vector { - std::vector indices; + std::vector indices; std::vector values; - size_t domain_size_ = 0; + uint64_t domain_size_ = 0; sparse_vector() = default; sparse_vector(const sparse_vector &other) = default; @@ -44,7 +44,7 @@ struct sparse_vector { sparse_vector& operator=(const sparse_vector &other) = default; sparse_vector& operator=(sparse_vector &&other) = default; - T operator[](const size_t idx) const; + T operator[](const uint64_t idx) const; bool operator==(const sparse_vector &other) const; bool operator==(const std::vector &other) const; @@ -52,15 +52,15 @@ struct sparse_vector { bool is_valid() const; bool empty() const; - size_t domain_size() const; // return domain_size_ - size_t size() const; // return the number of indices (representing the number of non-zero entries) - size_t size_in_bits() const; // return the number bits needed to store the sparse vector + uint64_t domain_size() const; // return domain_size_ + uint64_t size() const; // return the number of indices (representing the number of non-zero entries) + uint64_t size_in_bits() const; // return the number bits needed to store the sparse vector /* return a pair consisting of the accumulated value and the sparse vector of non-accumuated values */ template std::pair > accumulate(const typename std::vector::const_iterator &it_begin, const typename std::vector::const_iterator &it_end, - const size_t offset) const; + const uint64_t offset) const; friend std::ostream& operator<< (std::ostream &out, const sparse_vector &v); friend std::istream& operator>> (std::istream &in, sparse_vector &v); diff --git a/src/snark/libsnark/common/data_structures/sparse_vector.tcc b/src/snark/libsnark/common/data_structures/sparse_vector.tcc index cfc5d755990..906ed16d9f1 100644 --- a/src/snark/libsnark/common/data_structures/sparse_vector.tcc +++ b/src/snark/libsnark/common/data_structures/sparse_vector.tcc @@ -29,7 +29,7 @@ sparse_vector::sparse_vector(std::vector &&v) : } template -T sparse_vector::operator[](const size_t idx) const +T sparse_vector::operator[](const uint64_t idx) const { auto it = std::lower_bound(indices.begin(), indices.end(), idx); return (it != indices.end() && *it == idx) ? values[it - indices.begin()] : T(); @@ -43,7 +43,7 @@ bool sparse_vector::operator==(const sparse_vector &other) const return false; } - size_t this_pos = 0, other_pos = 0; + uint64_t this_pos = 0, other_pos = 0; while (this_pos < this->indices.size() && other_pos < other.indices.size()) { if (this->indices[this_pos] == other.indices[other_pos]) @@ -103,8 +103,8 @@ bool sparse_vector::operator==(const std::vector &other) const return false; } - size_t j = 0; - for (size_t i = 0; i < other.size(); ++i) + uint64_t j = 0; + for (uint64_t i = 0; i < other.size(); ++i) { if (this->indices[j] == i) { @@ -134,7 +134,7 @@ bool sparse_vector::is_valid() const return false; } - for (size_t i = 0; i + 1 < indices.size(); ++i) + for (uint64_t i = 0; i + 1 < indices.size(); ++i) { if (indices[i] >= indices[i+1]) { @@ -157,42 +157,42 @@ bool sparse_vector::empty() const } template -size_t sparse_vector::domain_size() const +uint64_t sparse_vector::domain_size() const { return domain_size_; } template -size_t sparse_vector::size() const +uint64_t sparse_vector::size() const { return indices.size(); } template -size_t sparse_vector::size_in_bits() const +uint64_t sparse_vector::size_in_bits() const { - return indices.size() * (sizeof(size_t) * 8 + T::size_in_bits()); + return indices.size() * (sizeof(uint64_t) * 8 + T::size_in_bits()); } template template std::pair > sparse_vector::accumulate(const typename std::vector::const_iterator &it_begin, const typename std::vector::const_iterator &it_end, - const size_t offset) const + const uint64_t offset) const { // TODO: does not really belong here. - const size_t chunks = 1; + const uint64_t chunks = 1; const bool use_multiexp = true; T accumulated_value = T::zero(); sparse_vector resulting_vector; resulting_vector.domain_size_ = domain_size_; - const size_t range_len = it_end - it_begin; + const uint64_t range_len = it_end - it_begin; bool in_block = false; - size_t first_pos = -1, last_pos = -1; // g++ -flto emits unitialized warning, even though in_block guards for such cases. + uint64_t first_pos = -1, last_pos = -1; // g++ -flto emits unitialized warning, even though in_block guards for such cases. - for (size_t i = 0; i < indices.size(); ++i) + for (uint64_t i = 0; i < indices.size(); ++i) { const bool matching_pos = (offset <= indices[i] && indices[i] < offset + range_len); // printf("i = %zu, pos[i] = %zu, offset = %zu, w_size = %zu\n", i, indices[i], offset, w_size); @@ -265,7 +265,7 @@ std::ostream& operator<<(std::ostream& out, const sparse_vector &v) { out << v.domain_size_ << "\n"; out << v.indices.size() << "\n"; - for (const size_t& i : v.indices) + for (const uint64_t& i : v.indices) { out << i << "\n"; } @@ -285,11 +285,11 @@ std::istream& operator>>(std::istream& in, sparse_vector &v) in >> v.domain_size_; consume_newline(in); - size_t s; + uint64_t s; in >> s; consume_newline(in); v.indices.resize(s); - for (size_t i = 0; i < s; ++i) + for (uint64_t i = 0; i < s; ++i) { in >> v.indices[i]; consume_newline(in); @@ -300,7 +300,7 @@ std::istream& operator>>(std::istream& in, sparse_vector &v) consume_newline(in); v.values.reserve(s); - for (size_t i = 0; i < s; ++i) + for (uint64_t i = 0; i < s; ++i) { T t; in >> t; diff --git a/src/snark/libsnark/common/profiling.cpp b/src/snark/libsnark/common/profiling.cpp index d227203a0fa..e4a2146f186 100644 --- a/src/snark/libsnark/common/profiling.cpp +++ b/src/snark/libsnark/common/profiling.cpp @@ -26,27 +26,44 @@ #include #endif +#ifdef __MACH__ // required to build on MacOS +#include +#include +#include +#include +#endif + namespace libsnark { -long long get_nsec_time() +int64_t get_nsec_time() { auto timepoint = std::chrono::high_resolution_clock::now(); return std::chrono::duration_cast(timepoint.time_since_epoch()).count(); } /* Return total CPU time consumsed by all threads of the process, in nanoseconds. */ -long long get_nsec_cpu_time() +int64_t get_nsec_cpu_time() { ::timespec ts; + #ifdef __MACH__ + clock_serv_t cclock; + mach_timespec_t mts; + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + ts.tv_sec = mts.tv_sec; + ts.tv_nsec = mts.tv_nsec; + #else if ( ::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) ) throw ::std::runtime_error("clock_gettime(CLOCK_PROCESS_CPUTIME_ID) failed"); // If we expected this to work, don't silently ignore failures, because that would hide the problem and incur an unnecessarily system-call overhead. So if we ever observe this exception, we should probably add a suitable #ifdef . //TODO: clock_gettime(CLOCK_PROCESS_CPUTIME_ID) is not supported by native Windows. What about Cygwin? Should we #ifdef on CLOCK_PROCESS_CPUTIME_ID or on __linux__? + #endif return ts.tv_sec * 1000000000ll + ts.tv_nsec; } -long long start_time, last_time; -long long start_cpu_time, last_cpu_time; +int64_t start_time, last_time; +int64_t start_cpu_time, last_cpu_time; void start_profiling() { @@ -57,20 +74,20 @@ void start_profiling() } std::map invocation_counts; -std::map enter_times; -std::map last_times; -std::map cumulative_times; +std::map enter_times; +std::map last_times; +std::map cumulative_times; //TODO: Instead of analogous maps for time and cpu_time, use a single struct-valued map -std::map enter_cpu_times; -std::map last_cpu_times; -std::map, long long> op_counts; -std::map, long long> cumulative_op_counts; // ((msg, data_point), value) +std::map enter_cpu_times; +std::map last_cpu_times; +std::map, int64_t> op_counts; +std::map, int64_t> cumulative_op_counts; // ((msg, data_point), value) // TODO: Convert op_counts and cumulative_op_counts from pair to structs size_t indentation = 0; std::vector block_names; -std::list > op_data_points = { +std::list > op_data_points = { #ifdef PROFILE_OP_COUNTS std::make_pair("Fradd", &Fr::add_cnt), std::make_pair("Frsub", &Fr::sub_cnt), @@ -98,7 +115,7 @@ void clear_profiling_counters() cumulative_times.clear(); } -void print_cumulative_time_entry(const std::string &key, const long long factor) +void print_cumulative_time_entry(const std::string &key, const int64_t factor) { const double total_ms = (cumulative_times.at(key) * 1e-6); const size_t cnt = invocation_counts.at(key); @@ -106,7 +123,7 @@ void print_cumulative_time_entry(const std::string &key, const long long factor) printf(" %-45s: %12.5fms = %lld * %0.5fms (%zu invocations, %0.5fms = %lld * %0.5fms per invocation)\n", key.c_str(), total_ms, factor, total_ms/factor, cnt, avg_ms, factor, avg_ms/factor); } -void print_cumulative_times(const long long factor) +void print_cumulative_times(const int64_t factor) { printf("Dumping times:\n"); for (auto& kv : cumulative_times) @@ -155,7 +172,7 @@ void print_op_profiling(const std::string &msg) printf("(opcounts) = ("); bool first = true; - for (std::pair p : op_data_points) + for (std::pair p : op_data_points) { if (!first) { @@ -171,14 +188,14 @@ void print_op_profiling(const std::string &msg) #endif } -static void print_times_from_last_and_start(long long now, long long last, - long long cpu_now, long long cpu_last) +static void print_times_from_last_and_start(int64_t now, int64_t last, + int64_t cpu_now, int64_t cpu_last) { - long long time_from_start = now - start_time; - long long time_from_last = now - last; + int64_t time_from_start = now - start_time; + int64_t time_from_last = now - last; - long long cpu_time_from_start = cpu_now - start_cpu_time; - long long cpu_time_from_last = cpu_now - cpu_last; + int64_t cpu_time_from_start = cpu_now - start_cpu_time; + int64_t cpu_time_from_last = cpu_now - cpu_last; if (time_from_last != 0) { double parallelism_from_last = 1.0 * cpu_time_from_last / time_from_last; @@ -199,8 +216,8 @@ void print_time(const char* msg) return; } - long long now = get_nsec_time(); - long long cpu_now = get_nsec_cpu_time(); + int64_t now = get_nsec_time(); + int64_t cpu_now = get_nsec_cpu_time(); printf("%-35s\t", msg); print_times_from_last_and_start(now, last_time, cpu_now, last_cpu_time); @@ -231,7 +248,7 @@ void print_indent() void op_profiling_enter(const std::string &msg) { - for (std::pair p : op_data_points) + for (std::pair p : op_data_points) { op_counts[std::make_pair(msg, p.first)] = *(p.second); } @@ -245,9 +262,9 @@ void enter_block(const std::string &msg, const bool indent) } block_names.emplace_back(msg); - long long t = get_nsec_time(); + int64_t t = get_nsec_time(); enter_times[msg] = t; - long long cpu_t = get_nsec_cpu_time(); + int64_t cpu_t = get_nsec_cpu_time(); enter_cpu_times[msg] = cpu_t; if (inhibit_profiling_info) @@ -288,15 +305,15 @@ void leave_block(const std::string &msg, const bool indent) ++invocation_counts[msg]; - long long t = get_nsec_time(); + int64_t t = get_nsec_time(); last_times[msg] = (t - enter_times[msg]); cumulative_times[msg] += (t - enter_times[msg]); - long long cpu_t = get_nsec_cpu_time(); + int64_t cpu_t = get_nsec_cpu_time(); last_cpu_times[msg] = (cpu_t - enter_cpu_times[msg]); #ifdef PROFILE_OP_COUNTS - for (std::pair p : op_data_points) + for (std::pair p : op_data_points) { cumulative_op_counts[std::make_pair(msg, p.first)] += *(p.second)-op_counts[std::make_pair(msg, p.first)]; } diff --git a/src/snark/libsnark/common/profiling.hpp b/src/snark/libsnark/common/profiling.hpp index 9619117f4b7..4a496107b37 100644 --- a/src/snark/libsnark/common/profiling.hpp +++ b/src/snark/libsnark/common/profiling.hpp @@ -22,7 +22,7 @@ namespace libsnark { void start_profiling(); -long long get_nsec_time(); +int64_t get_nsec_time(); void print_time(const char* msg); void print_header(const char* msg); @@ -31,13 +31,13 @@ void print_indent(); extern bool inhibit_profiling_info; extern bool inhibit_profiling_counters; extern std::map invocation_counts; -extern std::map last_times; -extern std::map cumulative_times; +extern std::map last_times; +extern std::map cumulative_times; void clear_profiling_counters(); -void print_cumulative_time_entry(const std::string &key, const long long factor=1); -void print_cumulative_times(const long long factor=1); +void print_cumulative_time_entry(const std::string &key, const int64_t factor=1); +void print_cumulative_times(const int64_t factor=1); void print_cumulative_op_counts(const bool only_fq=false); void enter_block(const std::string &msg, const bool indent=true); diff --git a/src/snark/libsnark/common/serialization.tcc b/src/snark/libsnark/common/serialization.tcc index 398f978500d..7983c9f259f 100644 --- a/src/snark/libsnark/common/serialization.tcc +++ b/src/snark/libsnark/common/serialization.tcc @@ -17,6 +17,7 @@ #include #include #include "common/utils.hpp" +#include "common/assert_except.hpp" namespace libsnark { @@ -69,7 +70,7 @@ T reserialize(const T &obj) ss << obj; T tmp; ss >> tmp; - assert(obj == tmp); + assert_except(obj == tmp); return tmp; } diff --git a/src/snark/libsnark/common/utils.cpp b/src/snark/libsnark/common/utils.cpp index dd114fdf0d5..5bcaf82527b 100644 --- a/src/snark/libsnark/common/utils.cpp +++ b/src/snark/libsnark/common/utils.cpp @@ -15,11 +15,11 @@ namespace libsnark { -size_t log2(size_t n) +uint64_t log2(uint64_t n) /* returns ceil(log2(n)), so 1ul< 1) { @@ -30,10 +30,10 @@ size_t log2(size_t n) return r; } -size_t bitreverse(size_t n, const size_t l) +uint64_t bitreverse(uint64_t n, const uint64_t l) { - size_t r = 0; - for (size_t k = 0; k < l; ++k) + uint64_t r = 0; + for (uint64_t k = 0; k < l; ++k) { r = (r << 1) | (n & 1); n >>= 1; @@ -41,20 +41,20 @@ size_t bitreverse(size_t n, const size_t l) return r; } -bit_vector int_list_to_bits(const std::initializer_list &l, const size_t wordsize) +bit_vector int_list_to_bits(const std::initializer_list &l, const size_t wordsize) { bit_vector res(wordsize*l.size()); - for (size_t i = 0; i < l.size(); ++i) + for (uint64_t i = 0; i < l.size(); ++i) { - for (size_t j = 0; j < wordsize; ++j) + for (uint64_t j = 0; j < wordsize; ++j) { - res[i*wordsize + j] = (*(l.begin()+i) & (1ul<<(wordsize-1-j))); + res[i*wordsize + j] = (*(l.begin()+i) & (UINT64_C(1)<<(wordsize-1-j))); } } return res; } -long long div_ceil(long long x, long long y) +int64_t div_ceil(int64_t x, int64_t y) { return (x + (y-1)) / y; } @@ -68,7 +68,7 @@ bool is_little_endian() std::string FORMAT(const std::string &prefix, const char* format, ...) { - const static size_t MAX_FMT = 256; + const static uint64_t MAX_FMT = 256; char buf[MAX_FMT]; va_list args; va_start(args, format); @@ -81,7 +81,7 @@ std::string FORMAT(const std::string &prefix, const char* format, ...) void serialize_bit_vector(std::ostream &out, const bit_vector &v) { out << v.size() << "\n"; - for (size_t i = 0; i < v.size(); ++i) + for (uint64_t i = 0; i < v.size(); ++i) { out << v[i] << "\n"; } @@ -89,10 +89,10 @@ void serialize_bit_vector(std::ostream &out, const bit_vector &v) void deserialize_bit_vector(std::istream &in, bit_vector &v) { - size_t size; + uint64_t size; in >> size; v.resize(size); - for (size_t i = 0; i < size; ++i) + for (uint64_t i = 0; i < size; ++i) { bool b; in >> b; diff --git a/src/snark/libsnark/common/utils.hpp b/src/snark/libsnark/common/utils.hpp index d7d9e894739..6879f8023a4 100644 --- a/src/snark/libsnark/common/utils.hpp +++ b/src/snark/libsnark/common/utils.hpp @@ -21,13 +21,13 @@ namespace libsnark { typedef std::vector bit_vector; /// returns ceil(log2(n)), so 1ul< &l, const size_t wordsize); -long long div_ceil(long long x, long long y); +uint64_t bitreverse(uint64_t n, const uint64_t l); +bit_vector int_list_to_bits(const std::initializer_list &l, const uint64_t wordsize); +int64_t div_ceil(int64_t x, int64_t y); bool is_little_endian(); @@ -46,8 +46,13 @@ void UNUSED(Types&&...) {} void serialize_bit_vector(std::ostream &out, const bit_vector &v); void deserialize_bit_vector(std::istream &in, bit_vector &v); +#ifdef __APPLE__ template -size_t size_in_bits(const std::vector &v); +unsigned long size_in_bits(const std::vector &v); +#else +template +uint64_t size_in_bits(const std::vector &v); +#endif #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0])) diff --git a/src/snark/libsnark/common/utils.tcc b/src/snark/libsnark/common/utils.tcc index f97178f8cc5..4afdc8b3d81 100644 --- a/src/snark/libsnark/common/utils.tcc +++ b/src/snark/libsnark/common/utils.tcc @@ -12,11 +12,19 @@ namespace libsnark { +#ifdef __APPLE__ +template +unsigned long size_in_bits(const std::vector &v) +{ + return v.size() * T::size_in_bits(); +} +#else template size_t size_in_bits(const std::vector &v) { return v.size() * T::size_in_bits(); } +#endif } // libsnark diff --git a/src/snark/libsnark/gadgetlib1/gadgets/basic_gadgets.tcc b/src/snark/libsnark/gadgetlib1/gadgets/basic_gadgets.tcc index 213b1906f29..bcd2f2c7225 100644 --- a/src/snark/libsnark/gadgetlib1/gadgets/basic_gadgets.tcc +++ b/src/snark/libsnark/gadgetlib1/gadgets/basic_gadgets.tcc @@ -275,11 +275,11 @@ void test_disjunction_gadget(const size_t n) disjunction_gadget d(pb, inputs, output, "d"); d.generate_r1cs_constraints(); - for (size_t w = 0; w < 1ul< c(pb, inputs, output, "c"); c.generate_r1cs_constraints(); - for (size_t w = 0; w < 1ul< cmp(pb, n, A, B, less, less_or_eq, "cmp"); cmp.generate_r1cs_constraints(); - for (size_t a = 0; a < 1ul< g(pb, A, B, result, "g"); g.generate_r1cs_constraints(); - for (size_t i = 0; i < 1ul<::generate_r1cs_witness() { /* assumes that idx can be fit in ulong; true for our purposes for now */ const bigint valint = this->pb.val(index).as_bigint(); - unsigned long idx = valint.as_ulong(); + uint64_t idx = valint.as_ulong(); const bigint arrsize(arr.size()); if (idx >= arr.size() || mpn_cmp(valint.data, arrsize.data, FieldT::num_limbs) >= 0) @@ -619,7 +619,7 @@ void test_loose_multiplexing_gadget(const size_t n) protoboard pb; pb_variable_array arr; - arr.allocate(pb, 1ul< index, result, success_flag; index.allocate(pb, "index"); result.allocate(pb, "result"); @@ -628,20 +628,20 @@ void test_loose_multiplexing_gadget(const size_t n) loose_multiplexing_gadget g(pb, arr, index, result, success_flag, "g"); g.generate_r1cs_constraints(); - for (size_t i = 0; i < 1ul<::generate_r1cs_witness() { for (size_t i = 0; i < 32; ++i) { - const long v = (this->pb.lc_val(X[i]) + this->pb.lc_val(Y[i]) + this->pb.lc_val(Z[i])).as_ulong(); + const int64_t v = (this->pb.lc_val(X[i]) + this->pb.lc_val(Y[i]) + this->pb.lc_val(Z[i])).as_ulong(); this->pb.val(result_bits[i]) = FieldT(v / 2); } diff --git a/src/snark/libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.hpp b/src/snark/libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.hpp index c2f31e3af35..13bbc075c9b 100644 --- a/src/snark/libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.hpp +++ b/src/snark/libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.hpp @@ -78,7 +78,7 @@ class sha256_round_function_gadget : public gadget { pb_linear_combination_array g; pb_linear_combination_array h; pb_variable W; - long K; + int64_t K; pb_linear_combination_array new_a; pb_linear_combination_array new_e; @@ -92,7 +92,7 @@ class sha256_round_function_gadget : public gadget { const pb_linear_combination_array &g, const pb_linear_combination_array &h, const pb_variable &W, - const long &K, + const int64_t &K, const pb_linear_combination_array &new_a, const pb_linear_combination_array &new_e, const std::string &annotation_prefix); diff --git a/src/snark/libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.tcc b/src/snark/libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.tcc index e8f233a544d..b0e00638812 100644 --- a/src/snark/libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.tcc +++ b/src/snark/libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.tcc @@ -16,7 +16,7 @@ namespace libsnark { -const unsigned long SHA256_K[64] = { +const uint64_t SHA256_K[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, @@ -27,7 +27,7 @@ const unsigned long SHA256_K[64] = { 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; -const unsigned long SHA256_H[8] = { +const uint64_t SHA256_H[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; @@ -149,7 +149,7 @@ sha256_round_function_gadget::sha256_round_function_gadget(protoboard &g, const pb_linear_combination_array &h, const pb_variable &W, - const long &K, + const int64_t &K, const pb_linear_combination_array &new_a, const pb_linear_combination_array &new_e, const std::string &annotation_prefix) : diff --git a/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_authentication_path_variable.tcc b/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_authentication_path_variable.tcc index d773051ab91..b3d805d8edb 100644 --- a/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_authentication_path_variable.tcc +++ b/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_authentication_path_variable.tcc @@ -41,7 +41,7 @@ void merkle_authentication_path_variable::generate_r1cs_witness(c for (size_t i = 0; i < tree_depth; ++i) { - if (address & (1ul << (tree_depth-1-i))) + if (address & (UINT64_C(1) << (tree_depth-1-i))) { left_digests[i].generate_r1cs_witness(path[i]); } @@ -58,7 +58,7 @@ merkle_authentication_path merkle_authentication_path_variable::g merkle_authentication_path result; for (size_t i = 0; i < tree_depth; ++i) { - if (address & (1ul << (tree_depth-1-i))) + if (address & (UINT64_C(1) << (tree_depth-1-i))) { result.emplace_back(left_digests[i].get_digest()); } diff --git a/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_read_gadget.tcc b/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_read_gadget.tcc index 6002a5886d7..2fde4f68cbf 100644 --- a/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_read_gadget.tcc +++ b/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_read_gadget.tcc @@ -144,10 +144,10 @@ void test_merkle_tree_check_read_gadget() bit_vector address_bits; size_t address = 0; - for (long level = tree_depth-1; level >= 0; --level) + for (int64_t level = tree_depth-1; level >= 0; --level) { const bool computed_is_right = (std::rand() % 2); - address |= (computed_is_right ? 1ul << (tree_depth-1-level) : 0); + address |= (computed_is_right ? UINT64_C(1) << (tree_depth-1-level) : 0); address_bits.push_back(computed_is_right); bit_vector other(digest_len); std::generate(other.begin(), other.end(), [&]() { return std::rand() % 2; }); diff --git a/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.hpp b/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.hpp index 6ec0ca11ff6..51e13ecb8ff 100644 --- a/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.hpp +++ b/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.hpp @@ -19,6 +19,7 @@ #include "common/data_structures/merkle_tree.hpp" #include "gadgetlib1/gadget.hpp" +/*#include "gadgetlib1/gadgets/hashes/crh_gadget.hpp"*/ #include "gadgetlib1/gadgets/hashes/hash_io.hpp" #include "gadgetlib1/gadgets/hashes/digest_selector_gadget.hpp" #include "gadgetlib1/gadgets/merkle_tree/merkle_authentication_path_variable.hpp" diff --git a/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.tcc b/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.tcc index 1ac08edbbe8..3e73904c10d 100644 --- a/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.tcc +++ b/src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.tcc @@ -197,10 +197,10 @@ void test_merkle_tree_check_update_gadget() bit_vector address_bits; size_t address = 0; - for (long level = tree_depth-1; level >= 0; --level) + for (int64_t level = tree_depth-1; level >= 0; --level) { const bool computed_is_right = (std::rand() % 2); - address |= (computed_is_right ? 1ul << (tree_depth-1-level) : 0); + address |= (computed_is_right ? UINT64_C(1) << (tree_depth-1-level) : 0); address_bits.push_back(computed_is_right); bit_vector other(digest_len); std::generate(other.begin(), other.end(), [&]() { return std::rand() % 2; }); diff --git a/src/snark/libsnark/gadgetlib1/pb_variable.hpp b/src/snark/libsnark/gadgetlib1/pb_variable.hpp index fdf64d0140f..a6c71748d71 100644 --- a/src/snark/libsnark/gadgetlib1/pb_variable.hpp +++ b/src/snark/libsnark/gadgetlib1/pb_variable.hpp @@ -59,7 +59,7 @@ class pb_variable_array : private std::vector > void fill_with_field_elements(protoboard &pb, const std::vector& vals) const; void fill_with_bits(protoboard &pb, const bit_vector& bits) const; - void fill_with_bits_of_ulong(protoboard &pb, const unsigned long i) const; + void fill_with_bits_of_ulong(protoboard &pb, const uint64_t i) const; void fill_with_bits_of_field_element(protoboard &pb, const FieldT &r) const; std::vector get_vals(const protoboard &pb) const; @@ -120,7 +120,7 @@ class pb_linear_combination_array : private std::vector &pb, const std::vector& vals) const; void fill_with_bits(protoboard &pb, const bit_vector& bits) const; - void fill_with_bits_of_ulong(protoboard &pb, const unsigned long i) const; + void fill_with_bits_of_ulong(protoboard &pb, const uint64_t i) const; void fill_with_bits_of_field_element(protoboard &pb, const FieldT &r) const; std::vector get_vals(const protoboard &pb) const; diff --git a/src/snark/libsnark/gadgetlib1/pb_variable.tcc b/src/snark/libsnark/gadgetlib1/pb_variable.tcc index b36b3f8d788..77c9f13f892 100644 --- a/src/snark/libsnark/gadgetlib1/pb_variable.tcc +++ b/src/snark/libsnark/gadgetlib1/pb_variable.tcc @@ -65,7 +65,7 @@ void pb_variable_array::fill_with_bits_of_field_element(protoboard -void pb_variable_array::fill_with_bits_of_ulong(protoboard &pb, const unsigned long i) const +void pb_variable_array::fill_with_bits_of_ulong(protoboard &pb, const uint64_t i) const { this->fill_with_bits_of_field_element(pb, FieldT(i, true)); } @@ -232,7 +232,7 @@ void pb_linear_combination_array::fill_with_bits_of_field_element(protob } template -void pb_linear_combination_array::fill_with_bits_of_ulong(protoboard &pb, const unsigned long i) const +void pb_linear_combination_array::fill_with_bits_of_ulong(protoboard &pb, const uint64_t i) const { this->fill_with_bits_of_field_element(pb, FieldT(i)); } diff --git a/src/snark/libsnark/relations/variable.hpp b/src/snark/libsnark/relations/variable.hpp index a9a1449b81e..c63f57b423a 100644 --- a/src/snark/libsnark/relations/variable.hpp +++ b/src/snark/libsnark/relations/variable.hpp @@ -26,7 +26,7 @@ namespace libsnark { * Mnemonic typedefs. */ typedef size_t var_index_t; -typedef long integer_coeff_t; +typedef int64_t integer_coeff_t; /** * Forward declaration. diff --git a/src/snark/libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/examples/run_r1cs_ppzksnark.tcc b/src/snark/libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/examples/run_r1cs_ppzksnark.tcc index 00af6fe25e3..9bc87586910 100644 --- a/src/snark/libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/examples/run_r1cs_ppzksnark.tcc +++ b/src/snark/libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/examples/run_r1cs_ppzksnark.tcc @@ -83,7 +83,7 @@ bool run_r1cs_ppzksnark(const r1cs_example > &example, } print_header("R1CS ppzkSNARK Prover"); - r1cs_ppzksnark_proof proof = r1cs_ppzksnark_prover(keypair.pk, example.primary_input, example.auxiliary_input, example.constraint_system); + r1cs_ppzksnark_proof proof = r1cs_ppzksnark_prover(keypair.pk, example.primary_input, example.auxiliary_input); printf("\n"); print_indent(); print_mem("after prover"); if (test_serialization) diff --git a/src/snark/libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.tcc b/src/snark/libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.tcc index 84db9fc1a3b..83757ee9f6d 100644 --- a/src/snark/libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.tcc +++ b/src/snark/libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.tcc @@ -443,9 +443,9 @@ knowledge_commitment r1cs_compute_proof_kc(const qap_witness > & #endif #ifdef MULTICORE - const size_t chunks = omp_get_max_threads(); // to override, set OMP_NUM_THREADS env var or call omp_set_num_threads() + const uint64_t chunks = omp_get_max_threads(); // to override, set OMP_NUM_THREADS env var or call omp_set_num_threads() #else - const size_t chunks = 1; + const uint64_t chunks = 1; #endif returnval = returnval + kc_multi_exp_with_mixed_addition >( @@ -471,9 +471,9 @@ G1 r1cs_compute_proof_K(const qap_witness> &qap_wit, const G1_vecto #endif #ifdef MULTICORE - const size_t chunks = omp_get_max_threads(); // to override, set OMP_NUM_THREADS env var or call omp_set_num_threads() + const uint64_t chunks = omp_get_max_threads(); // to override, set OMP_NUM_THREADS env var or call omp_set_num_threads() #else - const size_t chunks = 1; + const uint64_t chunks = 1; #endif G1 g_K = K_query[0] + zk_shift; @@ -500,9 +500,9 @@ G1 r1cs_compute_proof_H(const qap_witness > &qap_wit, const G1_vect #endif #ifdef MULTICORE - const size_t chunks = omp_get_max_threads(); // to override, set OMP_NUM_THREADS env var or call omp_set_num_threads() + const uint64_t chunks = omp_get_max_threads(); // to override, set OMP_NUM_THREADS env var or call omp_set_num_threads() #else - const size_t chunks = 1; + const uint64_t chunks = 1; #endif g_H = g_H + multi_exp, Fr >( @@ -685,7 +685,7 @@ bool r1cs_ppzksnark_online_verifier_weak_IC(const r1cs_ppzksnark_processed_verif const accumulation_vector > accumulated_IC = pvk.encoded_IC_query.template accumulate_chunk >(primary_input.begin(), primary_input.end(), 0); const G1 &acc = accumulated_IC.first; - + if (!proof.is_well_formed()) { return false;