Skip to content

Commit d2b5a2d

Browse files
committed
Auto merge of zcash#3502 - wo01:fix-num_bits, r=bitcartel
Fix assertion failure in circuit.merkle_tree_gadget_weirdness test on Windows zcash-gtest.exe fails ``` [ RUN ] circuit.merkle_tree_gadget_weirdness Assertion failed! Program: C:\zcash-gtest.exe File: ./snark/libsnark/gadgetlib1/gadgets/basic_gadgets.tcc, Line 50 Expression: this->pb.lc_val(packed).as_bigint().num_bits() <= bits.size() ``` The argument type of `__builtin_clzl()` function is unsigned long. So, we need to replace `__builtin_clzl` with `__builtin_clzll`. (The argument type of `__builtin_clzll()` is unsigned long long (64bit)).
2 parents 396bdaf + 3786db4 commit d2b5a2d

File tree

14 files changed

+30
-29
lines changed

14 files changed

+30
-29
lines changed

src/snark/libsnark/algebra/curves/alt_bn128/alt_bn128_pairing.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ alt_bn128_ate_G2_precomp alt_bn128_ate_precompute_G2(const alt_bn128_G2& Q)
324324
bool found_one = false;
325325
alt_bn128_ate_ell_coeffs c;
326326

327-
for (long i = loop_count.max_bits(); i >= 0; --i)
327+
for (int64_t i = loop_count.max_bits(); i >= 0; --i)
328328
{
329329
const bool bit = loop_count.test_bit(i);
330330
if (!found_one)
@@ -378,7 +378,7 @@ alt_bn128_Fq12 alt_bn128_ate_miller_loop(const alt_bn128_ate_G1_precomp &prec_P,
378378
const bigint<alt_bn128_Fr::num_limbs> &loop_count = alt_bn128_ate_loop_count;
379379
alt_bn128_ate_ell_coeffs c;
380380

381-
for (long i = loop_count.max_bits(); i >= 0; --i)
381+
for (int64_t i = loop_count.max_bits(); i >= 0; --i)
382382
{
383383
const bool bit = loop_count.test_bit(i);
384384
if (!found_one)
@@ -432,7 +432,7 @@ alt_bn128_Fq12 alt_bn128_ate_double_miller_loop(const alt_bn128_ate_G1_precomp &
432432
size_t idx = 0;
433433

434434
const bigint<alt_bn128_Fr::num_limbs> &loop_count = alt_bn128_ate_loop_count;
435-
for (long i = loop_count.max_bits(); i >= 0; --i)
435+
for (int64_t i = loop_count.max_bits(); i >= 0; --i)
436436
{
437437
const bool bit = loop_count.test_bit(i);
438438
if (!found_one)

src/snark/libsnark/algebra/curves/curve_utils.tcc

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ GroupT scalar_mul(const GroupT &base, const bigint<m> &scalar)
1616
GroupT result = GroupT::zero();
1717

1818
bool found_one = false;
19-
for (long i = scalar.max_bits() - 1; i >= 0; --i)
19+
for (int64_t i = scalar.max_bits() - 1; i >= 0; --i)
2020
{
2121
if (found_one)
2222
{

src/snark/libsnark/algebra/exponentiation/exponentiation.tcc

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ FieldT power(const FieldT &base, const bigint<m> &exponent)
2525

2626
bool found_one = false;
2727

28-
for (long i = exponent.max_bits() - 1; i >= 0; --i)
28+
for (int64_t i = exponent.max_bits() - 1; i >= 0; --i)
2929
{
3030
if (found_one)
3131
{

src/snark/libsnark/algebra/fields/bigint.tcc

+4-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ template<mp_size_t n>
105105
size_t bigint<n>::num_bits() const
106106
{
107107
/*
108-
for (long i = max_bits(); i >= 0; --i)
108+
for (int64_t i = max_bits(); i >= 0; --i)
109109
{
110110
if (this->test_bit(i))
111111
{
@@ -115,7 +115,7 @@ size_t bigint<n>::num_bits() const
115115
116116
return 0;
117117
*/
118-
for (long i = n-1; i >= 0; --i)
118+
for (int64_t i = n-1; i >= 0; --i)
119119
{
120120
mp_limb_t x = this->data[i];
121121
if (x == 0)
@@ -124,7 +124,8 @@ size_t bigint<n>::num_bits() const
124124
}
125125
else
126126
{
127-
return ((i+1) * GMP_NUMB_BITS) - __builtin_clzl(x);
127+
static_assert(GMP_NUMB_MAX <= ULLONG_MAX, "coercing limb to unsigned long long might truncate");
128+
return ((i+1) * GMP_NUMB_BITS) - __builtin_clzll(x);
128129
}
129130
}
130131
return 0;

src/snark/libsnark/algebra/fields/field_utils.tcc

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void batch_invert(std::vector<FieldT> &vec)
171171

172172
FieldT acc_inverse = acc.inverse();
173173

174-
for (long i = vec.size()-1; i >= 0; --i)
174+
for (int64_t i = vec.size()-1; i >= 0; --i)
175175
{
176176
const FieldT old_el = vec[i];
177177
vec[i] = acc_inverse * prod[i];

src/snark/libsnark/algebra/fields/fp.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Fp_model {
6767

6868
Fp_model() {};
6969
Fp_model(const bigint<n> &b);
70-
Fp_model(const long x, const bool is_unsigned=false);
70+
Fp_model(const int64_t x, const bool is_unsigned=false);
7171

7272
void set_uint64(const uint64_t x);
7373

src/snark/libsnark/algebra/fields/fp.tcc

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Fp_model<n,modulus>::Fp_model(const bigint<n> &b)
194194
}
195195

196196
template<mp_size_t n, const bigint<n>& modulus>
197-
Fp_model<n,modulus>::Fp_model(const long x, const bool is_unsigned)
197+
Fp_model<n,modulus>::Fp_model(const int64_t x, const bool is_unsigned)
198198
{
199199
if (is_unsigned || x >= 0)
200200
{
@@ -690,7 +690,7 @@ Fp_model<n, modulus> Fp_model<n,modulus>::random_element() /// returns random el
690690
const std::size_t part = bitno/GMP_NUMB_BITS;
691691
const std::size_t bit = bitno - (GMP_NUMB_BITS*part);
692692

693-
r.mont_repr.data[part] &= ~(((mp_limb_t) 1)<<bit);
693+
r.mont_repr.data[part] &= ~(UINT64_C(1)<<bit);
694694

695695
bitno--;
696696
}

src/snark/libsnark/algebra/fields/fp12_2over3over2.tcc

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ Fp12_2over3over2_model<n, modulus> Fp12_2over3over2_model<n,modulus>::cyclotomic
339339
Fp12_2over3over2_model<n,modulus> res = Fp12_2over3over2_model<n,modulus>::one();
340340

341341
bool found_one = false;
342-
for (long i = m-1; i >= 0; --i)
342+
for (int64_t i = m-1; i >= 0; --i)
343343
{
344-
for (long j = GMP_NUMB_BITS - 1; j >= 0; --j)
344+
for (int64_t j = GMP_NUMB_BITS - 1; j >= 0; --j)
345345
{
346346
if (found_one)
347347
{

src/snark/libsnark/algebra/scalar_multiplication/multiexp.tcc

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public:
4040
#if defined(__x86_64__) && defined(USE_ASM)
4141
if (n == 3)
4242
{
43-
long res;
43+
int64_t res;
4444
__asm__
4545
("// check for overflow \n\t"
4646
"mov $0, %[res] \n\t"
@@ -58,7 +58,7 @@ public:
5858
}
5959
else if (n == 4)
6060
{
61-
long res;
61+
int64_t res;
6262
__asm__
6363
("// check for overflow \n\t"
6464
"mov $0, %[res] \n\t"
@@ -77,7 +77,7 @@ public:
7777
}
7878
else if (n == 5)
7979
{
80-
long res;
80+
int64_t res;
8181
__asm__
8282
("// check for overflow \n\t"
8383
"mov $0, %[res] \n\t"
@@ -389,7 +389,7 @@ size_t get_exp_window_size(const size_t num_scalars)
389389
#endif
390390
}
391391
size_t window = 1;
392-
for (long i = T::fixed_base_exp_window_table.size()-1; i >= 0; --i)
392+
for (int64_t i = T::fixed_base_exp_window_table.size()-1; i >= 0; --i)
393393
{
394394
#ifdef DEBUG
395395
if (!inhibit_profiling_info)

src/snark/libsnark/algebra/scalar_multiplication/wnaf.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace libsnark {
1818
* Find the wNAF representation of the given scalar relative to the given window size.
1919
*/
2020
template<mp_size_t n>
21-
std::vector<long> find_wnaf(const size_t window_size, const bigint<n> &scalar);
21+
std::vector<int64_t> find_wnaf(const size_t window_size, const bigint<n> &scalar);
2222

2323
/**
2424
* In additive notation, use wNAF exponentiation (with the given window size) to compute scalar * base.

src/snark/libsnark/algebra/scalar_multiplication/wnaf.tcc

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
namespace libsnark {
1818

1919
template<mp_size_t n>
20-
std::vector<long> find_wnaf(const size_t window_size, const bigint<n> &scalar)
20+
std::vector<int64_t> find_wnaf(const size_t window_size, const bigint<n> &scalar)
2121
{
2222
const size_t length = scalar.max_bits(); // upper bound
23-
std::vector<long> res(length+1);
23+
std::vector<int64_t> res(length+1);
2424
bigint<n> c = scalar;
25-
long j = 0;
25+
int64_t j = 0;
2626
while (!c.is_zero())
2727
{
28-
long u;
28+
int64_t u;
2929
if ((c.data[0] & 1) == 1)
3030
{
3131
u = c.data[0] % (1u << (window_size+1));
@@ -59,7 +59,7 @@ std::vector<long> find_wnaf(const size_t window_size, const bigint<n> &scalar)
5959
template<typename T, mp_size_t n>
6060
T fixed_window_wnaf_exp(const size_t window_size, const T &base, const bigint<n> &scalar)
6161
{
62-
std::vector<long> naf = find_wnaf(window_size, scalar);
62+
std::vector<int64_t> naf = find_wnaf(window_size, scalar);
6363
std::vector<T> table(UINT64_C(1)<<(window_size-1));
6464
T tmp = base;
6565
T dbl = base.dbl();
@@ -71,7 +71,7 @@ T fixed_window_wnaf_exp(const size_t window_size, const T &base, const bigint<n>
7171

7272
T res = T::zero();
7373
bool found_nonzero = false;
74-
for (long i = naf.size()-1; i >= 0; --i)
74+
for (int64_t i = naf.size()-1; i >= 0; --i)
7575
{
7676
if (found_nonzero)
7777
{
@@ -99,7 +99,7 @@ template<typename T, mp_size_t n>
9999
T opt_window_wnaf_exp(const T &base, const bigint<n> &scalar, const size_t scalar_bits)
100100
{
101101
size_t best = 0;
102-
for (long i = T::wnaf_window_table.size() - 1; i >= 0; --i)
102+
for (int64_t i = T::wnaf_window_table.size() - 1; i >= 0; --i)
103103
{
104104
if (scalar_bits >= T::wnaf_window_table[i])
105105
{

src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_read_gadget.tcc

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void test_merkle_tree_check_read_gadget()
144144
bit_vector address_bits;
145145

146146
size_t address = 0;
147-
for (long level = tree_depth-1; level >= 0; --level)
147+
for (int64_t level = tree_depth-1; level >= 0; --level)
148148
{
149149
const bool computed_is_right = (std::rand() % 2);
150150
address |= (computed_is_right ? UINT64_C(1) << (tree_depth-1-level) : 0);

src/snark/libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.tcc

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void test_merkle_tree_check_update_gadget()
197197
bit_vector address_bits;
198198

199199
size_t address = 0;
200-
for (long level = tree_depth-1; level >= 0; --level)
200+
for (int64_t level = tree_depth-1; level >= 0; --level)
201201
{
202202
const bool computed_is_right = (std::rand() % 2);
203203
address |= (computed_is_right ? UINT64_C(1) << (tree_depth-1-level) : 0);

src/snark/libsnark/relations/variable.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace libsnark {
2626
* Mnemonic typedefs.
2727
*/
2828
typedef size_t var_index_t;
29-
typedef long integer_coeff_t;
29+
typedef int64_t integer_coeff_t;
3030

3131
/**
3232
* Forward declaration.

0 commit comments

Comments
 (0)