Skip to content

Commit

Permalink
Merge pull request #206 from clearmatics/refactor-libsnark-helpers
Browse files Browse the repository at this point in the history
Refactor libsnark helpers [depends on #200]
  • Loading branch information
AntoineRondelet committed May 1, 2020
2 parents 26424bb + 6d57f4e commit f8c528e
Show file tree
Hide file tree
Showing 143 changed files with 3,086 additions and 2,761 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,3 @@ jobs:
submodules: recursive
- name: Execute
run: CI_CHECK_FORMAT=1 CI_USE_DOCKER=1 CI_CONFIG=${{ matrix.config }} scripts/ci build

build-linux-release-pghr13:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
with:
submodules: recursive
- name: Execute
run: CI_USE_DOCKER=1 CI_CONFIG=Release CI_ZKSNARK=PGHR13 scripts/ci build
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ set(ZETH_VERSION_MINOR 3)
# Configure a header file to pass some of the CMake settings
# to the source code
configure_file(
"${PROJECT_SOURCE_DIR}/zethConfig.h.in"
"${PROJECT_BINARY_DIR}/zethConfig.h"
"${PROJECT_SOURCE_DIR}/zeth_config.h.in"
"${PROJECT_BINARY_DIR}/zeth_config.h"
)

# Flags and compilation options to chose the type of zksnark
Expand Down
2 changes: 1 addition & 1 deletion depends/libsnark
19 changes: 6 additions & 13 deletions libzeth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,14 @@ string(TOLOWER ${ZKSNARK} ZKSNARK_NAME)
file(
GLOB_RECURSE
ZETH_SOURCE
types/**.?pp types/**.tcc

core/**.?pp core/**.tcc
snarks/**.?pp snarks/**.tcc
serialization/**.?pp serialization/**.tcc
circuits/**.?pp circuits/**.tcc
circuit_wrapper.???
commitments/**.?pp commitments/**.tcc
libsnark_helpers/**.?pp libsnark_helpers/**.tcc
snarks/${ZKSNARK_NAME}/core/**.???
snarks/${ZKSNARK_NAME}/api/**.???
# We only implement the MPC for Groth16 for now
snarks/groth16/mpc/**.???
snarks_alias.hpp
include_libsnark.hpp
util.?pp util.tcc
util_api.?pp util_api.tcc
zeth.h
mpc/**.?pp mpc/**.tcc
)

add_library(
zeth

Expand Down
8 changes: 4 additions & 4 deletions libzeth/circuits/binary_operation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#ifndef __ZETH_CIRCUITS_BINARY_OPERATION_HPP__
#define __ZETH_CIRCUITS_BINARY_OPERATION_HPP__

#include "libzeth/circuits/circuits_utils.hpp"
#include "libzeth/types/bits.hpp"
#include "libzeth/circuits/circuit_utils.hpp"
#include "libzeth/core/bits.hpp"
#include "math.h"

#include <libsnark/gadgetlib1/gadget.hpp>
Expand Down Expand Up @@ -45,7 +45,6 @@ template<typename FieldT> class xor_gadget : public libsnark::gadget<FieldT>
template<typename FieldT>
class xor_constant_gadget : public libsnark::gadget<FieldT>
{

private:
const libsnark::pb_variable_array<FieldT> a;
const libsnark::pb_variable_array<FieldT> b;
Expand Down Expand Up @@ -119,6 +118,7 @@ class double_bit32_sum_eq_gadget : public libsnark::gadget<FieldT>
};

} // namespace libzeth

#include "libzeth/circuits/binary_operation.tcc"

#endif // __ZETH_CIRCUITS_BINARY_OPERATION_HPP__
#endif // __ZETH_CIRCUITS_BINARY_OPERATION_HPP__
30 changes: 15 additions & 15 deletions libzeth/circuits/binary_operation.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#ifndef __ZETH_CIRCUITS_BINARY_OPERATION_TCC__
#define __ZETH_CIRCUITS_BINARY_OPERATION_TCC__

#include "libzeth/circuits/circuits_utils.hpp"
#include "libzeth/types/bits.hpp"
#include "libzeth/circuits/circuit_utils.hpp"
#include "libzeth/core/bits.hpp"

#include <libsnark/gadgetlib1/gadget.hpp>
#include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>
Expand All @@ -25,7 +25,7 @@ xor_gadget<FieldT>::xor_gadget(
{
assert(a.size() == b.size());
assert(b.size() == res.size());
};
}

template<typename FieldT> void xor_gadget<FieldT>::generate_r1cs_constraints()
{
Expand All @@ -37,7 +37,7 @@ template<typename FieldT> void xor_gadget<FieldT>::generate_r1cs_constraints()
2 * a[i], b[i], a[i] + b[i] - res[i]),
FMT(this->annotation_prefix, " xored_bits_%zu", i));
}
};
}

template<typename FieldT> void xor_gadget<FieldT>::generate_r1cs_witness()
{
Expand All @@ -49,7 +49,7 @@ template<typename FieldT> void xor_gadget<FieldT>::generate_r1cs_witness()
this->pb.val(res[i]) = this->pb.val(a[i]) + this->pb.val(b[i]);
}
}
};
}

template<typename FieldT>
xor_constant_gadget<FieldT>::xor_constant_gadget(
Expand All @@ -68,7 +68,7 @@ xor_constant_gadget<FieldT>::xor_constant_gadget(
assert(a.size() == b.size());
assert(b.size() == c.size());
assert(c.size() == res.size());
};
}

template<typename FieldT>
void xor_constant_gadget<FieldT>::generate_r1cs_constraints()
Expand Down Expand Up @@ -97,7 +97,7 @@ void xor_constant_gadget<FieldT>::generate_r1cs_constraints()
b[i] * (FieldT("1") - FieldT("2") * c[i])),
FMT(this->annotation_prefix, " rotated_xored_bits_%zu", i));
}
};
}

template<typename FieldT>
void xor_constant_gadget<FieldT>::generate_r1cs_witness()
Expand All @@ -116,7 +116,7 @@ void xor_constant_gadget<FieldT>::generate_r1cs_witness()
this->pb.val(res[i]) = FieldT("1");
}
}
};
}

template<typename FieldT>
xor_rot_gadget<FieldT>::xor_rot_gadget(
Expand All @@ -134,7 +134,7 @@ xor_rot_gadget<FieldT>::xor_rot_gadget(
{
assert(a.size() == b.size());
assert(b.size() == res.size());
};
}

template<typename FieldT>
void xor_rot_gadget<FieldT>::generate_r1cs_constraints()
Expand All @@ -146,7 +146,7 @@ void xor_rot_gadget<FieldT>::generate_r1cs_constraints()
2 * a[i], b[i], a[i] + b[i] - res[(i + shift) % a.size()]),
FMT(this->annotation_prefix, " rotated_xored_bits_%zu", i));
}
};
}

template<typename FieldT> void xor_rot_gadget<FieldT>::generate_r1cs_witness()
{
Expand All @@ -160,7 +160,7 @@ template<typename FieldT> void xor_rot_gadget<FieldT>::generate_r1cs_witness()
this->pb.val(a[i]) + this->pb.val(b[i]);
}
}
};
}

template<typename FieldT>
double_bit32_sum_eq_gadget<FieldT>::double_bit32_sum_eq_gadget(
Expand All @@ -174,7 +174,7 @@ double_bit32_sum_eq_gadget<FieldT>::double_bit32_sum_eq_gadget(
assert(a.size() == 32);
assert(a.size() == b.size());
assert(a.size() == res.size());
};
}

template<typename FieldT>
void double_bit32_sum_eq_gadget<FieldT>::generate_r1cs_constraints(
Expand Down Expand Up @@ -246,7 +246,7 @@ void double_bit32_sum_eq_gadget<FieldT>::generate_r1cs_constraints(
(left_side - packed_addition(res) - pow(2, 32)),
0),
FMT(this->annotation_prefix, " sum_equal_sum_constraint"));
};
}

template<typename FieldT>
void double_bit32_sum_eq_gadget<FieldT>::generate_r1cs_witness()
Expand All @@ -260,8 +260,8 @@ void double_bit32_sum_eq_gadget<FieldT>::generate_r1cs_witness()

bits32 left_side_acc = binary_addition<32>(a_bits32, b_bits32, false);
res.fill_with_bits(this->pb, get_vector_from_bits32(left_side_acc));
};
}

} // namespace libzeth

#endif // __ZETH_CIRCUITS_BINARY_OPERATION_TCC__
#endif // __ZETH_CIRCUITS_BINARY_OPERATION_TCC__
7 changes: 4 additions & 3 deletions libzeth/circuits/blake2s/blake2s.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include "libzeth/circuits/binary_operation.hpp"
#include "libzeth/circuits/blake2s/blake2s_comp.hpp"
#include "libzeth/circuits/circuits_utils.hpp"
#include "libzeth/types/bits.hpp"
#include "libzeth/util.hpp"
#include "libzeth/circuits/circuit_utils.hpp"
#include "libzeth/core/bits.hpp"
#include "libzeth/core/utils.hpp"

#include <libsnark/gadgetlib1/gadget.hpp>
#include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>
Expand Down Expand Up @@ -165,6 +165,7 @@ template<typename FieldT> class BLAKE2s_256 : public libsnark::gadget<FieldT>
};

} // namespace libzeth

#include "libzeth/circuits/blake2s/blake2s.tcc"

#endif // __ZETH_CIRCUITS_BLAKE2S_HPP__
2 changes: 2 additions & 0 deletions libzeth/circuits/blake2s/blake2s.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace libzeth
{

static const size_t BYTE_LEN = 8;

/// This gadget implements the interface of the HashT template
template<typename FieldT>
BLAKE2s_256<FieldT>::BLAKE2s_256(
Expand Down
6 changes: 3 additions & 3 deletions libzeth/circuits/blake2s/blake2s_comp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include "libzeth/circuits/binary_operation.hpp"
#include "libzeth/circuits/blake2s/g_primitive.hpp"
#include "libzeth/circuits/circuits_utils.hpp"
#include "libzeth/types/bits.hpp"
#include "libzeth/util.hpp"
#include "libzeth/circuits/circuit_utils.hpp"
#include "libzeth/core/bits.hpp"
#include "libzeth/core/utils.hpp"

#include <libsnark/gadgetlib1/gadget.hpp>
#include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>
Expand Down
7 changes: 4 additions & 3 deletions libzeth/circuits/blake2s/g_primitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#define __ZETH_CIRCUITS_G_PRIMITIVE_HPP__

#include "libzeth/circuits/binary_operation.hpp"
#include "libzeth/circuits/circuits_utils.hpp"
#include "libzeth/types/bits.hpp"
#include "libzeth/circuits/circuit_utils.hpp"
#include "libzeth/core/bits.hpp"

#include <libsnark/gadgetlib1/gadget.hpp>
#include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>
Expand Down Expand Up @@ -69,6 +69,7 @@ template<typename FieldT> class g_primitive : public libsnark::gadget<FieldT>
};

} // namespace libzeth

#include "libzeth/circuits/blake2s/g_primitive.tcc"

#endif // __ZETH_CIRCUITS_G_PRIMITIVE_HPP__
#endif // __ZETH_CIRCUITS_G_PRIMITIVE_HPP__
2 changes: 1 addition & 1 deletion libzeth/circuits/blake2s/g_primitive.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ template<typename FieldT> void g_primitive<FieldT>::generate_r1cs_witness()

} // namespace libzeth

#endif // __ZETH_CIRCUITS_G_PRIMITIVE_TCC__
#endif // __ZETH_CIRCUITS_G_PRIMITIVE_TCC__
11 changes: 6 additions & 5 deletions libzeth/circuit_types.hpp → libzeth/circuits/circuit_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//
// SPDX-License-Identifier: LGPL-3.0+

#ifndef __ZETH_CIRCUIT_TYPES_HPP__
#define __ZETH_CIRCUIT_TYPES_HPP__
#ifndef __ZETH_CIRCUITS_CIRCUIT_TYPES_HPP__
#define __ZETH_CIRCUITS_CIRCUIT_TYPES_HPP__

#include "libzeth/circuit_wrapper.hpp"
#include "libzeth/circuits/blake2s/blake2s.hpp"
#include "libzeth/include_libsnark.hpp"
#include "libzeth/circuits/circuit_wrapper.hpp"
#include "libzeth/circuits/mimc/mimc_mp.hpp"
#include "libzeth/core/include_libsnark.hpp"

// Types that must be common across all executable, defined once here. Outside
// of tests, these should not be set anywhere else in the code. Do not include
Expand All @@ -30,4 +31,4 @@ using HashTreeT = MiMC_mp_gadget<FieldT>;

} // namespace libzeth

#endif // __ZETH_CIRCUIT_TYPES_HPP__
#endif // __ZETH_CIRCUITS_CIRCUIT_TYPES_HPP__
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: LGPL-3.0+

#include "libzeth/circuits/circuits_utils.hpp"
#include "libzeth/circuits/circuit_utils.hpp"

namespace libzeth
{
Expand All @@ -25,4 +25,4 @@ std::vector<bool> convert_to_binary(size_t n)
return res;
}

} // namespace libzeth
} // namespace libzeth
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//
// SPDX-License-Identifier: LGPL-3.0+

#ifndef __ZETH_CIRCUITS_CIRCUITS_UTILS_HPP__
#define __ZETH_CIRCUITS_CIRCUITS_UTILS_HPP__
#ifndef __ZETH_CIRCUITS_CIRCUIT_UTILS_HPP__
#define __ZETH_CIRCUITS_CIRCUIT_UTILS_HPP__

#include "libzeth/types/bits.hpp"
#include "libzeth/core/bits.hpp"

#include <libsnark/gadgetlib1/pb_variable.hpp>

Expand All @@ -22,6 +22,7 @@ libsnark::pb_variable_array<FieldT> from_bits(
std::vector<bool> bits, const libsnark::pb_variable<FieldT> &ZERO);

} // namespace libzeth
#include "libzeth/circuits/circuits_utils.tcc"

#endif // __ZETH_CIRCUITS_CIRCUITS_UTILS_HPP__
#include "libzeth/circuits/circuit_utils.tcc"

#endif // __ZETH_CIRCUITS_CIRCUIT_UTILS_HPP__
File renamed without changes.
Loading

0 comments on commit f8c528e

Please sign in to comment.