Sobre
a portable and fast pairing-based cryptography library
Segurança e privacidadeCrescimento medido de estrelas
- em 24 h
- 0
- em 7 dias
- 0
- em 15 dias
- +1
- em 1 mês
- Coletando histórico
Variações líquidas em períodos sobrepostos, não totais diários. Medições ausentes ficam em branco. Estrelas indicam interesse, não qualidade.
README.md
Originalmcl
A portable and fast pairing-based cryptography library.
Abstract
mcl is a library for pairing-based cryptography, which supports the optimal Ate pairing over BN curves and BLS12-381 curves.
News
- v4.46 improve the performance of squareRoot, hashAndMapTo{G1, G2}, pairing on BN_SNARK1, multi-thread version of msm.
- v4.45
MCL_USE_GRUMPKIN=1fixes the curve to BN_SNARK1 and Grumpkin. The initialization of another curve or modulus (e.g.,initPairing(BN254),initCurve<G1>(MCL_SECP256K1),Fp::init(p)) fails and does not change the current state (see api.md).MCL_USE_GRUMPKIN=2(MCL_FP_BIT=MCL_FR_BIT=384) fixes the curve to BN382 (MCL_BN382=MCL_BN381_1) and Grumpkin382, the 2-cycle partner of BN382.initGrumpkin()callsinitPairingof the fixed curve, and fails if theMCL_USE_GRUMPKINof the caller is not equal to that of the library.- fix getStr() writing one byte past the buffer
- v4.44
mulVeckeeps its work buffers and reuses them in the next call to reduce page faults (effective mainly on Windows).mcl::ec::freeMulVecBuffer()/mclBn_freeMulVecBuffer()frees them (see api.md).
parallelForThreadtakes the tasks dynamically, so that a slow thread does not make the others wait.- Add
Fp::setArrayRaw.mklib.batsupports-256and-grumpkin.
- v4.43
mulVec(MSM) runs in parallel by a parallel-for registered withmcl::ec::setParallelFor(see api.md).mulVecMTandmillerLoopVecMTuse the threads of the OS by default; OpenMP is no longer used (MCL_USE_OMPis removed).- Add the Grumpkin curve (class
GG).initGrumpkin()afterinitPairing(BN_SNARK1); add / mul / mulVec (MSM) / serialization (no pairing, no hash-to-curve).- Available only with
MCL_USE_GRUMPKIN=1(requiresMCL_FP_BIT=256).
- Available only with
- Improve the performance of MSM ({G1,G2}::mulVec) since v4.30 about 1.5-2.3x (w/ AVX-512 IFMA), 1.3-1.7x (w/o AVX-512), and 1.2-1.8x (Apple M4).
- Improve the performance of hashAndMapTo{G1,G2} by using SHA-NI for SHA-256.
- Improve the performance of finalExp by about 12% and pairing by about 8%.
- Improve the performance of pairing on Apple M4 (aarch64) by about 14%.
- Fix memory leak of some operations on Windows
- Remove unintended G1::isValidOrder on BN curve. This improves the performance of deserialization of a point of G1.
Version v4 includes breaking changes.
- v4.30 re-adds support for Haswell or older CPUs, but this is likely to be removed again in roughly a year.
- BMI2 (mulx) and ADX (adox, adcx) are used if the CPU supports them (Broadwell (2014) or later);
MCL_CPU=noadxforces the portable code.
- BMI2 (mulx) and ADX (adox, adcx) are used if the CPU supports them (Broadwell (2014) or later);
- v4.00 - v4.20 do not support Haswell or older CPUs.
Version v3 includes breaking changes to lib/dll specifications.
- The default
mcl.{a,lib}supports up to 384 bits for the field Fp over which the elliptic curve is defined, and up to 256 bits for the order field Fr of the elliptic curve (MCL_FP_BIT=384,MCL_FR_BIT=256).MCL_FP_BITandMCL_FR_BITmust be at most 384 (MCL_BINT_MAX_BITininclude/mcl/config.hpp; the generated low-level functions support up to 384-bit multiplication), so the initialization of a curve over a larger prime such as BN462 fails. To enlarge the limit (up to 576 for the x64 assembly), setMCL_BINT_MAX_BITininclude/mcl/config.hppandBINT_MUL_N(=MCL_BINT_MAX_BIT/ 64) inMakefileto the same size, and regenerate the low-level functions bymake update_all_asm(requires LLVM) andmake header. - The arguments of the Fp/Fr initialization function have been changed.
mclbn***.{a,lib}has been merged into mcl.{a,lib} and removed.
Support architecture
- x86-64 Windows + Visual Studio 2022 (or later)
- x86, x86-64 Linux + gcc/clang
- x86-64, M1 macOS
- ARM / ARM64 Linux
- WebAssembly : see mcl-wasm
- Android : see mcl-android
- iPhone
- s390x(systemz)
- install llvm and clang, and
make UPDATE_ASM=1once.
- install llvm and clang, and
- (maybe any platform to be supported by LLVM)
Support curves
- BN curve : p(z) = 36z^4 + 36z^3 + 24z^2 + 6z + 1.
- BN254 : a BN curve over the 254-bit prime p(z) where z = -(2^62 + 2^55 + 1).
- BN_SNARK1 : a BN curve over a 254-bit prime p such that n := p + 1 - t has high 2-adicity.
- BN382 (= BN381_1) : a BN curve over the 382-bit prime p(z) where z = -(2^94 + 2^76 + 2^72 + 1).
- BN462 : a BN curve over the 462-bit prime p(z) where z = 2^114 + 2^101 - 2^14 - 1.
- BLS12_381
- BLS12_377
- Grumpkin : y^2 = x^3 - 17 over the Fr of BN_SNARK1 (the 2-cycle with BN_SNARK1, class
GG, the group operations only, requiresMCL_USE_GRUMPKIN=1andMCL_FP_BIT=256) - Grumpkin382 : y^2 = x^3 + 2 over the Fr of BN382 (the 2-cycle with BN382, class
GG, the group operations only, requiresMCL_USE_GRUMPKIN=2andMCL_FP_BIT=MCL_FR_BIT=384)
BLS signature
See bls if you want mcl for BLS-signature.
C-API
See api.md and FAQ for serialization and hash-to-curve.
How to build on Linux and macOS
x86-64/ARM/ARM64 Linux, macOS and mingw64 are supported.
GMP is necessary only to build test programs.
sudo apt install libgmp-devon Ubuntubrew install gmpon macOS
mulVecMT and millerLoopVecMT use pthread on Linux / macOS (-pthread may be required to link statically with glibc < 2.34) and the Windows threads on Windows. No other library is required.
How to build with Makefile
For x86-64 Linux and macOS,
git clone https://github.com/herumi/mcl
cd mcl
make -j4
clang++ is required except for x86-64 on Linux and Windows (MinGW).
make -j4 CXX=clang++
lib/libmcl.a: static librarylib/libmcl.so: shared library
How to build with CMake
For x86-64 Linux and macOS.
mkdir build
cd build
cmake ..
make
For other platforms (including MinGW), clang++ is required.
mkdir build
cd build
cmake .. -DCMAKE_CXX_COMPILER=clang++
make
Use clang++ instead of gcc on mingw.
For Visual Studio, (REMARK : It is not maintained; use the vcxproj file.)
mkdir build
cd build
cmake .. -A x64
msbuild mcl.sln /p:Configuration=Release /m
clang++ (e.g. the Visual Studio C++ Clang tools) is used for src/base64.ll if it is found, as mklib.bat does.
Without it, the library is slower on CPUs without BMI2/ADX.
lib/Release/mcl.lib: static librarybin/Release/{mcl.lib,mcl.dll}: dynamic librarycmake --installputs them inlib/andbin/of the prefix in the same way. They use the static CRT (/MT) asmklib.batdoes;-DMCL_MSVC_RUNTIME_DLL=ONselects/MD.
How to build a static library with Visual Studio
Open mcl.sln and build it.
src/proj/mcllib/mcllib.vcxproj is to build a static library lib/mcl.lib, which is built with MCL_FP_BIT=384.
options
see cmake .. -LA.
tests
Build test binaries in ./bin.
cmake .. -DMCL_BUILD_TESTING=ON
make -j4
How to make from src/{base,bint}{32,64}.ll
clang (clang-cl on Windows) is necessary to build files with the suffix .ll.
- BIT = 64 (if 64-bit CPU) else 32
src/base${BIT}.llis necessary ifMCL_USE_LLVMis defined.- This code is used if xbyak is not used.
src/bint${BIT}.llis necessary ifMCL_BINT_ASM=1.src/asm/bint-x64-amd64.S(src/asm/bint-x64-mingw.Son MinGW,src/asm/bint-x64-win.asmon Windows) is used instead ifMCL_BINT_ASM_X64=1.- It is faster than
src/bint64.llbecause it uses mulx/adox/adcx if the CPU supports them (selected at runtime bymcl::bint::getCpuType()).
These files may be unified in the future.
How to test the BLS12-381 pairing
# C
make bin/bn_c384_256_test.exe && bin/bn_c384_256_test.exe
# C++
make bin/bls12_test.exe && bin/bls12_test.exe
How to make a library for BLS12-381 without Xbyak
On x64 environments, mcl uses JIT code, but if you want to avoid it,
make lib/libmcl.a MCL_STATIC_CODE=1 -j
# test of pairing
make test_static
The generated library supports only BLS12_381 and requires compiler options -DMCL_FP_BIT=384 -DMCL_STATIC_CODE.
How to profile on Linux
Use perf
make MCL_USE_PROF=1 bin/bls12_test.exe
env MCL_PROF=1 bin/bls12_test.exe
Use Intel VTune profiler
Suppose VTune is installed in /opt/intel/vtune_amplifier/.
make MCL_USE_PROF=2 bin/bls12_test.exe
env MCL_PROF=2 bin/bls12_test.exe
How to build on 32-bit x86 Linux
Build GMP for 32-bit mode.
sudo apt install g++-multilib
sudo apt install clang-21
cd <GMP dir>
env ABI=32 ./configure --enable-cxx --prefix=<install dir>
make -j install
cd <mcl dir>
make ARCH=x86 LLVM_VER=-21 GMP_DIR=<install dir>
How to build a library for arm with clang++ on Linux
make -f Makefile.cross BIT=32 TARGET=armv7l
sudo apt install g++-arm-linux-gnueabi
arm-linux-gnueabi-g++ sample/pairing.cpp -O3 -DNDEBUG -I ./include/ lib/libmcl.a -DMCL_FP_BIT=384
env QEMU_LD_PREFIX=/usr/arm-linux-gnueabi/ qemu-arm ./a.out
The static library libbls384_256.a built by bls/Makefile.onelib in bls contains all mcl functions. So please see the comment of Makefile.onelib if you want to build this library on the other platform such as Mingw64 on Linux.
How to build a library for RISC-V with clang++ on Linux
Install clang++ and
sudo apt install g++-14-riscv64-linux-gnu binutils-riscv64-unknown-elf
make -f Makefile.cross test TARGET=riscv64-linux-gnu LD_PREFIX=riscv64-linux-gnu
How to build on 64-bit Windows with Visual Studio
mklib.bat builds the library and mk.bat builds a test or sample with it.
mklib [dll] [clang=(0|1|2)] [-256] [-grumpkin] [-grumpkin382]
mk (-s|-d) <source file>
dll: buildbin\mcl.dll(andbin\mcl.libfor import) instead of the static librarylib\mcl.lib.clang=1(default): buildsrc\base64.llwith clang++ (MCL_USE_LLVM=1). clang++ (installed by the LLVM component of Visual Studio) is required.clang=0: build without clang++ (src\base64.llis not used).clang=2: also buildsrc\fp.cppandsrc\msm_avx.cppwith clang-cl instead of cl, with the same optimization options as Makefile on x64 (CLANG_CFLAGSinsetvar.bat). Some functions such asFr::mulUnitbecome faster.mklibleavesUSE_CLANG=2in the environment, and thenmkalso uses clang-cl. A program that links the library built withclang=2requiresclang_rt.builtins-x86_64.lib(CLANG_RT_LIBinsetvar.bat, found viaclang -print-resource-dir);mkadds it ifUSE_CLANG=2.-256: build withMCL_FP_BIT=256(BN254, secp256k1, ...) intoobj256\andlib\mcl256.lib(bin\mcl256.dllandbin\mcl256.libwithdll) instead ofobj\andlib\mcl.lib(MCL_FP_BIT=384), so that both libraries can coexist.-grumpkin: also defineMCL_USE_GRUMPKIN=1(the curveGG, Grumpkin). It requires-256.mklibleavesMCL_FP_BITandMCL_USE_GRUMPKINin the environment, and thenmkcompiles with the same flags and linkslib\mcl256.lib(bin\mcl256.lib).-grumpkin382: defineMCL_USE_GRUMPKIN=2(the curveGG, Grumpkin382) andMCL_FR_BIT=384, and build intoobj384\andlib\mcl384.lib. It can not be used with-256.mk -slinkslib\mcl.libandmk -dlinksbin\mcl.libwith/DMCL_DLL. The executable is put inbin\.
Open a console window, and
git clone https://github.com/herumi/mcl
cd mcl
# static library (support both C/C++ API)
mklib
mk -s test\bls12_test.cpp && bin\bls12_test.exe
# dynamic library (support both C/C++ API)
mklib dll
mk -d test\bn_c384_256_test.cpp && bin\bn_c384_256_test.exe
mk -d test\bls12_test.cpp && bin\bls12_test.exe
# static library of MCL_FP_BIT=256 with Grumpkin (lib\mcl256.lib)
mklib -256 -grumpkin
mk -s test\grumpkin_test.cpp && bin\grumpkin_test.exe
A C++ client of mcl.dll must be compiled with /DMCL_DLL (mk -d does it); the static data of Fp, Fr, G1, and G2 then refers to the instances in the DLL.
(not maintained)
Open mcl.sln and build or if you have msbuild.exe
msbuild /p:Configuration=Release
How to build ARM64 Windows binaries on x64 Windows
Install Clang for Visual Studio.
Use batch files
Open command prompt and run
cd mcl
setvar_arm64.bat
"%VS_PATH%"\vc\auxiliary\build\vcvarsamd64_arm64.bat
# static library
mklib_arm64
mk_arm64 -s test\bls12_test.cpp
# dynamic library
mklib_arm64 dll
mk_arm64 -d test\bn_c384_256_test.cpp
lib/mcl.lib: static librarybin/{mcl.lib,mcl.dll}: dynamic library
Use cmake
cd mcl
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=cmake/arm64-windows-toolchain.cmake
cmake --build build --config Release
build/lib/mcl.lib: static librarybuild/bin/{mcl.lib,mcl.dll}: dynamic library
How to build on ARM64 Windows
cd mcl
cmake -S . -B build -A arm64
cmake --build build --config Release
- The toolchain file is only needed for the x64-host cross build. On ARM64 Windows the build is detected automatically and uses the same code path.
C# test
cd mcl
mklib dll
cd ffi/cs
dotnet build mcl.sln
cd ../../bin
../ffi/cs/test/bin/Debug/net8.0/test.exe
How to build for wasm(WebAssembly)
mcl supports emcc (Emscripten) and test/bn_test.cpp runs on browsers such as Firefox, Chrome and Edge.
The timing of a pairing on BN254 is 2.8msec on 64-bit Firefox with Skylake 3.4GHz.
Standalone wasm build with Makefile.wasm
Build mcl.wasm without Emscripten using clang's wasm target directly.
Prerequisites
- clang/clang++, wasm-ld (LLVM 21 or later)
- wasm-opt (from binaryen)
- python3
Build
make -f Makefile.wasm LLVM_VER=-21
This generates lib/mcl.wasm and lib/mcl_c.js (wasm binary embedded as base64 in JS).
Set LLVM_VER to match your installed LLVM version suffix (e.g., -21 for clang-21).
Node.js
- mcl-wasm pairing library
- bls-wasm BLS signature library
- she-wasm 2 Level Homomorphic Encryption library
API for Two level homomorphic encryption
- Efficient Two-level Homomorphic Encryption in Prime-order Bilinear Groups and A Fast Implementation in WebAssembly, N. Attrapadung, G. Hanaoka, S. Mitsunari, Y. Sakai, K. Shimizu, and T. Teruya. ASIACCS 2018
- she-api
- she-api(Japanese)
Java API
See java.md
License
modified new BSD License http://opensource.org/licenses/BSD-3-Clause
This library contains parts of the following software, licensed under the BSD-3-Clause license.
References
- ate-pairing
- Faster Explicit Formulas for Computing Pairings over Ordinary Curves, D.F. Aranha, K. Karabina, P. Longa, C.H. Gebotys, J. Lopez, EUROCRYPT 2011, (preprint)
- High-Speed Software Implementation of the Optimal Ate Pairing over Barreto-Naehrig Curves, Jean-Luc Beuchat, Jorge Enrique González Díaz, Shigeo Mitsunari, Eiji Okamoto, Francisco Rodríguez-Henríquez, Tadanori Teruya, Pairing 2010, (preprint)
- Faster hashing to G2,Laura Fuentes-Castañeda, Edward Knapp, Francisco Rodríguez-Henríquez, SAC 2011, (PDF)
- Skew Frobenius Map and Efficient Scalar Multiplication for Pairing–Based Cryptography, Y. Sakemi, Y. Nogami, K. Okeya, Y. Morikawa, CANS 2008.
compatibility
- mclBnGT_inv returns a - b w, a conjugate of x for x = a + b w in Fp12 = Fp6[w]
- use mclBnGT_invGeneric if x is not in GT
- mclBn_setETHserialization(true) (de)serializes according to ETH2.0 serialization of BLS12-381 when BLS12-381 is used.
- (Break backward compatibility) libmcl_dy.a is renamed to libmcl.a
- The option SHARE_BASENAME_SUF is removed
- 2nd argument of
mclBn_initis changed frommaxUnitSizetocompiledTimeVar, which must beMCLBN_COMPILED_TIME_VAR. - break backward compatibility of mapToGi for BLS12. A map-to-function for BN is used.
(The old function, which used to be enabled by defining
MCL_USE_OLD_MAPTO_FOR_BLS12, has been removed.)
FAQ
How do I set the hash value to Fr?
The behavior of the setHashOf function may be a little different from what you want.
- https://github.com/herumi/mcl/blob/master/api.md#hash-to-curve-function
- https://github.com/herumi/mcl/blob/master/api.md#set-bufsize-bytes-from-buf-to-x-with-masking-according-to-the-following-method
Please use the following code:
template<class F>
void setHash(F& x, const void *msg, size_t msgSize)
{
uint8_t md[32];
mcl::fp::sha256(md, sizeof(md), msg, msgSize);
x.setBigEndianMod(md, sizeof(md));
// or x.setLittleEndianMod(md, sizeof(md));
}
History
- 2022/Apr/10 v1.60 improve {G1,G2}::mulVec
- 2022/Mar/25 v1.59 add set DST functions for hashMapToGi
- 2022/Mar/24 add F::invVec, G::normalizeVec
- 2022/Mar/08 v1.58 improve SECP256K1 for x64
- 2022/Feb/13 v1.57 add mulVecMT
- 2021/Aug/26 v1.52 improve {G1,G2}::isValidOrder() for BLS12-381
- 2021/May/04 v1.50 support s390x(systemz)
- 2021/Apr/21 v1.41 fix inner function of mapToGi for large dst (not affect hashAndMapToGi)
- 2021/May/24 v1.40 fix sigsegv in valgrind
- 2021/Jan/28 v1.31 fix : call setOrder in init for isValidOrder
- 2021/Jan/28 v1.30 a little optimization of Fp operations
- 2020/Nov/14 v1.28 support M1 mac
- 2020/Jun/07 v1.22 remove old hash-to-curve functions
- 2020/Jun/04 v1.21 mapToG1 and hashAndMapToG1 are compatible to irtf/eip-2537
- 2020/May/13 v1.09 support draft-irtf-cfrg-hash-to-curve-07
- 2020/Mar/26 v1.07 change DST for hash-to-curve-06
- 2020/Mar/15 v1.06 support hash-to-curve-06
- 2020/Jan/31 v1.05 mclBn_ethMsgToFp2 has changed to append zero byte at the end of msg
- 2020/Jan/25 v1.04 add new hash functions
- 2019/Dec/05 v1.03 disable to check the order in setStr
- 2019/Sep/30 v1.00 add some functions to bn.h ; api.md.
- 2019/Sep/22 v0.99 add mclBnG1_mulVec, etc.
- 2019/Sep/08 v0.98 bugfix Ec::add(P, Q, R) when P == R
- 2019/Aug/14 v0.97 add some C api functions
- 2019/Jul/26 v0.96 improved scalar multiplication
- 2019/Jun/03 v0.95 fix a parser of 0b10 with base = 16
- 2019/Apr/29 v0.94 mclBn_setETHserialization supports ETH2.0 serialization of BLS12-381
- 2019/Apr/24 v0.93 support ios
- 2019/Mar/22 v0.92 shortcut for Ec::mul(Px, P, x) if P = 0
- 2019/Mar/21 python binding of she256 for Linux/Mac/Windows
- 2019/Mar/14 v0.91 modp supports mcl-wasm
- 2019/Mar/12 v0.90 fix Vint::setArray(x) for x == this
- 2019/Mar/07 add mclBnFr_setLittleEndianMod, mclBnFp_setLittleEndianMod
- 2019/Feb/20 LagrangeInterpolation sets out = yVec[0] if k = 1
- 2019/Jan/31 add mclBnFp_mapToG1, mclBnFp2_mapToG2
- 2019/Jan/31 fix crash on x64-CPU without AVX (thanks to mortdeus)
Author
MITSUNARI Shigeo([email protected])
Sponsors welcome
Explore projetos relacionados
Carregando…