Skip to content

support mcl library #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: monolithic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ ifeq ($(CURVE),BN128)
AR_LIBS += $(DEPINST)/lib/libzm.a
endif

ifeq ($(CURVE),MCL_BN128)
LIB_SRCS += \
src/algebra/curves/mcl_bn128/mcl_bn128_g1.cpp \
src/algebra/curves/mcl_bn128/mcl_bn128_g2.cpp \
src/algebra/curves/mcl_bn128/mcl_bn128_gt.cpp \
src/algebra/curves/mcl_bn128/mcl_bn128_init.cpp \
src/algebra/curves/mcl_bn128/mcl_bn128_pairing.cpp \
src/algebra/curves/mcl_bn128/mcl_bn128_pp.cpp

AR_LIBS += $(DEPINST)/lib/libmcl.a
endif

EXECUTABLES = \
src/algebra/curves/tests/test_bilinearity \
src/algebra/curves/tests/test_groups \
Expand Down
12 changes: 12 additions & 0 deletions prepare-depends.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ cd ../..
cp -rv $DEPSRC/ate-pairing/include $DEPINST/
cp -rv $DEPSRC/ate-pairing/lib $DEPINST/

# mcl library, and its dependency, xbyak and cybozulib (needed for MCL_BN128 curve)
cd $DEPSRC
[ ! -d xbyak ] && git clone git://github.com/herumi/xbyak.git
[ ! -d cybozulib ] && git clone git://github.com/herumi/cybozulib.git
[ ! -d mcl ] && git clone git://github.com/herumi/mcl.git
cd mcl
make -j
cd ../..
cp -rv $DEPSRC/mcl/include/mcl $DEPINST/include/
cp -rv $DEPSRC/cybozulib/include/cybozu $DEPINST/include/
cp -rv $DEPSRC/mcl/lib $DEPINST/

# SUPERCOP library (optimized crypto implementations, used by ADSNARK)
cd $DEPSRC
[ ! -d libsnark-supercop ] && git clone git://github.com/mbbarbosa/libsnark-supercop.git
Expand Down
22 changes: 22 additions & 0 deletions src/algebra/curves/mcl_bn128/bn_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @file
*****************************************************************************
* @author This file is part of libsnark, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#ifndef BN_UTILS_HPP_
#define BN_UTILS_HPP_
#include <vector>
#include "bn.h"

namespace libsnark {

template<typename FieldT>
void bn_batch_invert(std::vector<FieldT> &vec);

} // libsnark

#include "algebra/curves/mcl_bn128/bn_utils.tcc"

#endif // BN_UTILS_HPP_
40 changes: 40 additions & 0 deletions src/algebra/curves/mcl_bn128/bn_utils.tcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/** @file
*****************************************************************************
* @author This file is part of libsnark, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/

#ifndef BN_UTILS_TCC_
#define BN_UTILS_TCC_

namespace libsnark {

template<typename FieldT>
void bn_batch_invert(std::vector<FieldT> &vec)
{
std::vector<FieldT> prod;
prod.reserve(vec.size());

FieldT acc = 1;

for (auto el : vec)
{
assert(!el.isZero());
prod.emplace_back(acc);
FieldT::mul(acc, acc, el);
}

FieldT acc_inverse;
FieldT::inv(acc_inverse, acc);

for (long i = vec.size()-1; i >= 0; --i)
{
const FieldT& old_el = vec[i];
FieldT::mul(vec[i], acc_inverse, prod[i]);
FieldT::mul(acc_inverse, acc_inverse, old_el);
}
}

} // libsnark
#endif // FIELD_UTILS_TCC_
Loading