src/snark patches for windows build
This commit is contained in:
@@ -275,11 +275,11 @@ void test_disjunction_gadget(const size_t n)
|
||||
disjunction_gadget<FieldT> d(pb, inputs, output, "d");
|
||||
d.generate_r1cs_constraints();
|
||||
|
||||
for (size_t w = 0; w < 1ul<<n; ++w)
|
||||
for (size_t w = 0; w < UINT64_C(1)<<n; ++w)
|
||||
{
|
||||
for (size_t j = 0; j < n; ++j)
|
||||
{
|
||||
pb.val(inputs[j]) = FieldT((w & (1ul<<j)) ? 1 : 0);
|
||||
pb.val(inputs[j]) = FieldT((w & (UINT64_C(1)<<j)) ? 1 : 0);
|
||||
}
|
||||
|
||||
d.generate_r1cs_witness();
|
||||
@@ -366,11 +366,11 @@ void test_conjunction_gadget(const size_t n)
|
||||
conjunction_gadget<FieldT> c(pb, inputs, output, "c");
|
||||
c.generate_r1cs_constraints();
|
||||
|
||||
for (size_t w = 0; w < 1ul<<n; ++w)
|
||||
for (size_t w = 0; w < UINT64_C(1)<<n; ++w)
|
||||
{
|
||||
for (size_t j = 0; j < n; ++j)
|
||||
{
|
||||
pb.val(inputs[j]) = (w & (1ul<<j)) ? FieldT::one() : FieldT::zero();
|
||||
pb.val(inputs[j]) = (w & (UINT64_C(1)<<j)) ? FieldT::one() : FieldT::zero();
|
||||
}
|
||||
|
||||
c.generate_r1cs_witness();
|
||||
@@ -378,13 +378,13 @@ void test_conjunction_gadget(const size_t n)
|
||||
#ifdef DEBUG
|
||||
printf("positive test for %zu\n", w);
|
||||
#endif
|
||||
assert(pb.val(output) == (w == (1ul<<n) - 1 ? FieldT::one() : FieldT::zero()));
|
||||
assert(pb.val(output) == (w == (UINT64_C(1)<<n) - 1 ? FieldT::one() : FieldT::zero()));
|
||||
assert(pb.is_satisfied());
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("negative test for %zu\n", w);
|
||||
#endif
|
||||
pb.val(output) = (w == (1ul<<n) - 1 ? FieldT::zero() : FieldT::one());
|
||||
pb.val(output) = (w == (UINT64_C(1)<<n) - 1 ? FieldT::zero() : FieldT::one());
|
||||
assert(!pb.is_satisfied());
|
||||
}
|
||||
|
||||
@@ -454,9 +454,9 @@ void test_comparison_gadget(const size_t n)
|
||||
comparison_gadget<FieldT> cmp(pb, n, A, B, less, less_or_eq, "cmp");
|
||||
cmp.generate_r1cs_constraints();
|
||||
|
||||
for (size_t a = 0; a < 1ul<<n; ++a)
|
||||
for (size_t a = 0; a < UINT64_C(1)<<n; ++a)
|
||||
{
|
||||
for (size_t b = 0; b < 1ul<<n; ++b)
|
||||
for (size_t b = 0; b < UINT64_C(1)<<n; ++b)
|
||||
{
|
||||
pb.val(A) = FieldT(a);
|
||||
pb.val(B) = FieldT(b);
|
||||
@@ -523,16 +523,16 @@ void test_inner_product_gadget(const size_t n)
|
||||
inner_product_gadget<FieldT> g(pb, A, B, result, "g");
|
||||
g.generate_r1cs_constraints();
|
||||
|
||||
for (size_t i = 0; i < 1ul<<n; ++i)
|
||||
for (size_t i = 0; i < UINT64_C(1)<<n; ++i)
|
||||
{
|
||||
for (size_t j = 0; j < 1ul<<n; ++j)
|
||||
for (size_t j = 0; j < UINT64_C(1)<<n; ++j)
|
||||
{
|
||||
size_t correct = 0;
|
||||
for (size_t k = 0; k < n; ++k)
|
||||
{
|
||||
pb.val(A[k]) = (i & (1ul<<k) ? FieldT::one() : FieldT::zero());
|
||||
pb.val(B[k]) = (j & (1ul<<k) ? FieldT::one() : FieldT::zero());
|
||||
correct += ((i & (1ul<<k)) && (j & (1ul<<k)) ? 1 : 0);
|
||||
pb.val(A[k]) = (i & (UINT64_C(1)<<k) ? FieldT::one() : FieldT::zero());
|
||||
pb.val(B[k]) = (j & (UINT64_C(1)<<k) ? FieldT::one() : FieldT::zero());
|
||||
correct += ((i & (UINT64_C(1)<<k)) && (j & (UINT64_C(1)<<k)) ? 1 : 0);
|
||||
}
|
||||
|
||||
g.generate_r1cs_witness();
|
||||
@@ -587,7 +587,7 @@ void loose_multiplexing_gadget<FieldT>::generate_r1cs_witness()
|
||||
{
|
||||
/* assumes that idx can be fit in ulong; true for our purposes for now */
|
||||
const bigint<FieldT::num_limbs> valint = this->pb.val(index).as_bigint();
|
||||
unsigned long idx = valint.as_ulong();
|
||||
uint64_t idx = valint.as_ulong();
|
||||
const bigint<FieldT::num_limbs> 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<FieldT> pb;
|
||||
|
||||
pb_variable_array<FieldT> arr;
|
||||
arr.allocate(pb, 1ul<<n, "arr");
|
||||
arr.allocate(pb, UINT64_C(1)<<n, "arr");
|
||||
pb_variable<FieldT> 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<FieldT> g(pb, arr, index, result, success_flag, "g");
|
||||
g.generate_r1cs_constraints();
|
||||
|
||||
for (size_t i = 0; i < 1ul<<n; ++i)
|
||||
for (size_t i = 0; i < UINT64_C(1)<<n; ++i)
|
||||
{
|
||||
pb.val(arr[i]) = FieldT((19*i) % (1ul<<n));
|
||||
pb.val(arr[i]) = FieldT((19*i) % (UINT64_C(1)<<n));
|
||||
}
|
||||
|
||||
for (int idx = -1; idx <= (int)(1ul<<n); ++idx)
|
||||
for (int idx = -1; idx <= (int)(UINT64_C(1)<<n); ++idx)
|
||||
{
|
||||
pb.val(index) = FieldT(idx);
|
||||
g.generate_r1cs_witness();
|
||||
|
||||
if (0 <= idx && idx <= (int)(1ul<<n) - 1)
|
||||
if (0 <= idx && idx <= (int)(UINT64_C(1)<<n) - 1)
|
||||
{
|
||||
printf("demuxing element %d (in bounds)\n", idx);
|
||||
assert(pb.val(result) == FieldT((19*idx) % (1ul<<n)));
|
||||
assert(pb.val(result) == FieldT((19*idx) % (UINT64_C(1)<<n)));
|
||||
assert(pb.val(success_flag) == FieldT::one());
|
||||
assert(pb.is_satisfied());
|
||||
pb.val(result) -= FieldT::one();
|
||||
|
||||
@@ -285,7 +285,7 @@ void majority_gadget<FieldT>::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);
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ public:
|
||||
pb_linear_combination_array<FieldT> g;
|
||||
pb_linear_combination_array<FieldT> h;
|
||||
pb_variable<FieldT> W;
|
||||
long K;
|
||||
int64_t K;
|
||||
pb_linear_combination_array<FieldT> new_a;
|
||||
pb_linear_combination_array<FieldT> new_e;
|
||||
|
||||
@@ -92,7 +92,7 @@ public:
|
||||
const pb_linear_combination_array<FieldT> &g,
|
||||
const pb_linear_combination_array<FieldT> &h,
|
||||
const pb_variable<FieldT> &W,
|
||||
const long &K,
|
||||
const int64_t &K,
|
||||
const pb_linear_combination_array<FieldT> &new_a,
|
||||
const pb_linear_combination_array<FieldT> &new_e,
|
||||
const std::string &annotation_prefix);
|
||||
|
||||
@@ -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<FieldT>::sha256_round_function_gadget(protoboard<Fi
|
||||
const pb_linear_combination_array<FieldT> &g,
|
||||
const pb_linear_combination_array<FieldT> &h,
|
||||
const pb_variable<FieldT> &W,
|
||||
const long &K,
|
||||
const int64_t &K,
|
||||
const pb_linear_combination_array<FieldT> &new_a,
|
||||
const pb_linear_combination_array<FieldT> &new_e,
|
||||
const std::string &annotation_prefix) :
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
#include "common/profiling.hpp"
|
||||
#include "gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace libsnark;
|
||||
|
||||
template<typename FieldT>
|
||||
@@ -37,10 +35,10 @@ void test_two_to_one()
|
||||
f.generate_r1cs_witness();
|
||||
output.generate_r1cs_witness(hash_bv);
|
||||
|
||||
EXPECT_TRUE(pb.is_satisfied());
|
||||
assert(pb.is_satisfied());
|
||||
}
|
||||
|
||||
TEST(gadgetlib1, sha256)
|
||||
int main(void)
|
||||
{
|
||||
start_profiling();
|
||||
default_ec_pp::init_public_params();
|
||||
|
||||
@@ -41,7 +41,7 @@ void merkle_authentication_path_variable<FieldT, HashT>::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<FieldT, HashT>::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());
|
||||
}
|
||||
|
||||
@@ -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; });
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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; });
|
||||
|
||||
@@ -5,36 +5,44 @@
|
||||
* @copyright MIT license (see LICENSE file)
|
||||
*****************************************************************************/
|
||||
|
||||
#include "algebra/curves/alt_bn128/alt_bn128_pp.hpp"
|
||||
#ifdef CURVE_BN128
|
||||
#include "algebra/curves/bn128/bn128_pp.hpp"
|
||||
#endif
|
||||
#include "algebra/curves/edwards/edwards_pp.hpp"
|
||||
#include "algebra/curves/mnt/mnt4/mnt4_pp.hpp"
|
||||
#include "algebra/curves/mnt/mnt6/mnt6_pp.hpp"
|
||||
#include "gadgetlib1/gadgets/merkle_tree/merkle_tree_check_read_gadget.hpp"
|
||||
#include "gadgetlib1/gadgets/merkle_tree/merkle_tree_check_update_gadget.hpp"
|
||||
#include "gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace libsnark;
|
||||
|
||||
template<typename ppT>
|
||||
void test_all_merkle_tree_gadgets()
|
||||
{
|
||||
typedef Fr<ppT> FieldT;
|
||||
test_merkle_tree_check_read_gadget<FieldT, CRH_with_bit_out_gadget<FieldT> >();
|
||||
test_merkle_tree_check_read_gadget<FieldT, sha256_two_to_one_hash_gadget<FieldT> >();
|
||||
|
||||
test_merkle_tree_check_update_gadget<FieldT, CRH_with_bit_out_gadget<FieldT> >();
|
||||
test_merkle_tree_check_update_gadget<FieldT, sha256_two_to_one_hash_gadget<FieldT> >();
|
||||
}
|
||||
|
||||
TEST(gadgetlib1, merkle_tree)
|
||||
int main(void)
|
||||
{
|
||||
start_profiling();
|
||||
|
||||
alt_bn128_pp::init_public_params();
|
||||
test_all_merkle_tree_gadgets<alt_bn128_pp>();
|
||||
|
||||
#ifdef CURVE_BN128 // BN128 has fancy dependencies so it may be disabled
|
||||
bn128_pp::init_public_params();
|
||||
test_all_merkle_tree_gadgets<bn128_pp>();
|
||||
#endif
|
||||
|
||||
edwards_pp::init_public_params();
|
||||
test_all_merkle_tree_gadgets<edwards_pp>();
|
||||
|
||||
mnt4_pp::init_public_params();
|
||||
test_all_merkle_tree_gadgets<mnt4_pp>();
|
||||
|
||||
mnt6_pp::init_public_params();
|
||||
test_all_merkle_tree_gadgets<mnt6_pp>();
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ public:
|
||||
|
||||
void fill_with_field_elements(protoboard<FieldT> &pb, const std::vector<FieldT>& vals) const;
|
||||
void fill_with_bits(protoboard<FieldT> &pb, const bit_vector& bits) const;
|
||||
void fill_with_bits_of_ulong(protoboard<FieldT> &pb, const unsigned long i) const;
|
||||
void fill_with_bits_of_ulong(protoboard<FieldT> &pb, const uint64_t i) const;
|
||||
void fill_with_bits_of_field_element(protoboard<FieldT> &pb, const FieldT &r) const;
|
||||
|
||||
std::vector<FieldT> get_vals(const protoboard<FieldT> &pb) const;
|
||||
@@ -120,7 +120,7 @@ public:
|
||||
|
||||
void fill_with_field_elements(protoboard<FieldT> &pb, const std::vector<FieldT>& vals) const;
|
||||
void fill_with_bits(protoboard<FieldT> &pb, const bit_vector& bits) const;
|
||||
void fill_with_bits_of_ulong(protoboard<FieldT> &pb, const unsigned long i) const;
|
||||
void fill_with_bits_of_ulong(protoboard<FieldT> &pb, const uint64_t i) const;
|
||||
void fill_with_bits_of_field_element(protoboard<FieldT> &pb, const FieldT &r) const;
|
||||
|
||||
std::vector<FieldT> get_vals(const protoboard<FieldT> &pb) const;
|
||||
|
||||
@@ -65,7 +65,7 @@ void pb_variable_array<FieldT>::fill_with_bits_of_field_element(protoboard<Field
|
||||
}
|
||||
|
||||
template<typename FieldT>
|
||||
void pb_variable_array<FieldT>::fill_with_bits_of_ulong(protoboard<FieldT> &pb, const unsigned long i) const
|
||||
void pb_variable_array<FieldT>::fill_with_bits_of_ulong(protoboard<FieldT> &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<FieldT>::fill_with_bits_of_field_element(protob
|
||||
}
|
||||
|
||||
template<typename FieldT>
|
||||
void pb_linear_combination_array<FieldT>::fill_with_bits_of_ulong(protoboard<FieldT> &pb, const unsigned long i) const
|
||||
void pb_linear_combination_array<FieldT>::fill_with_bits_of_ulong(protoboard<FieldT> &pb, const uint64_t i) const
|
||||
{
|
||||
this->fill_with_bits_of_field_element(pb, FieldT(i));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user