Skip to content

Commit

Permalink
add OP_CHECKZKPVERIFY test, optimize non-asm code through flto and in…
Browse files Browse the repository at this point in the history
…line
  • Loading branch information
zilong-dai committed Nov 4, 2024
1 parent 429cc78 commit 70243ea
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 65 deletions.
10 changes: 3 additions & 7 deletions src/bls12-381/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
ACLOCAL_AMFLAGS = -I build-aux/m4

AM_CXXFLAGS = -std=c++11 -fpic -O2
AM_CPPFLAGS = -std=c++11 -fpic -O2
AM_CXXFLAGS = $(CXXFLAGS)

AM_CFLAGS = --fpic -O2
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libbls.pc

lib_LTLIBRARIES = libbls.la
libbls_la_SOURCES = src/groth16.cpp src/arithmetic.cpp src/fp.cpp src/g.cpp src/pairing.cpp src/scalar.cpp src/utils.cpp

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libbls.pc
64 changes: 29 additions & 35 deletions src/bls12-381/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,48 @@ AC_INIT([libbls], m4_join([.], _PKG_VERSION_MAJOR, _PKG_VERSION_MINOR, _PKG_VERS
AC_SUBST(LIB_VERSION_CURRENT, _LIB_VERSION_CURRENT)
AC_SUBST(LIB_VERSION_REVISION, _LIB_VERSION_REVISION)
AC_SUBST(LIB_VERSION_AGE, _LIB_VERSION_AGE)
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIR([build-aux/m4])
AC_CANONICAL_HOST
AH_TOP([#ifndef LIBMCL_CONFIG_H])
AH_TOP([#define LIBMCL_CONFIG_H])
AH_BOTTOM([#endif /*LIBMCL_CONFIG_H*/])

AM_INIT_AUTOMAKE([1.11.2 foreign subdir-objects])

m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
AC_ARG_ENABLE([blsasm], [AS_HELP_STRING([--enable-blsasm=yes|no], [Enable blsasm option])], [], [enable_blsasm=no])

AC_PROG_CXX
AC_PROG_CC
AM_PROG_AR

LT_INIT([win32-dll])


CXXFLAGS="-O3 -I include"

AC_CANONICAL_HOST
case "$host_os" in
*mingw64*)
CXXFLAGS="$CXXFLAGS -D__USE_MINGW_ANSI_STDIO=1"
;;
*cygwin*)
;;
*darwin*)
AC_DEFINE([DARWIN], [1], [Define if on Darwin/Mac OS X])
;;
*openbsd*)
CXXFLAGS="$CXXFLAGS -I/usr/local/include"
LDFLAGS="$LDFLAGS -L/usr/local/lib"
;;
*freebsd*)
CXXFLAGS="$CXXFLAGS -I/usr/local/include"
LDFLAGS="$LDFLAGS -L/usr/local/lib"
;;
*linux*)
CXXFLAGS="$CXXFLAGS -I/usr/local/include"
LDFLAGS="$LDFLAGS -L/usr/local/lib"
;;
AC_CANONICAL_TARGET
case $target_cpu in
x86_64*)
AC_MSG_NOTICE(["The target platform is $target_cpu"])
if test "x$enable_blsasm" = "xyes"; then
AC_MSG_NOTICE(["Enable blsasm"])
CXXFLAGS="$CXXFLAGS -D__x86_64_asm__"
else
AC_MSG_NOTICE(["Disable blsasm $host_os"])
case $host_os in
darwin*)
CXXFLAGS="$CXXFLAGS"
;;
*)
CXXFLAGS="$CXXFLAGS -flto"
;;
esac
fi
;;
*)
AC_MSG_NOTICE([The target platform is not x86_64])
CXXFLAGS="$CXXFLAGS -flto"
;;
esac


CXXFLAGS_WARN="-Wall -Wextra -Wformat=2 -Wcast-qual -Wcast-align -Wwrite-strings -Wfloat-equal -Wpointer-arith -Wundef"
CXXFLAGS="$CXXFLAGS $CXXFLAGS_WARN -I include -I src"

AC_SUBST(CXXFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(CXX)
AC_SUBST(CC)
AC_SUBST(CXXFLAGS)

AC_CONFIG_FILES([Makefile libbls.pc])

Expand All @@ -76,4 +69,5 @@ echo " CPPFLAGS = $CPPFLAGS"
echo " CXX = $CXX"
echo " CXXFLAGS = $CXXFLAGS"
echo " LDFLAGS = $LDFLAGS"
echo " ENABLE_BLSASM = $enable_blsasm"
echo
24 changes: 12 additions & 12 deletions src/bls12-381/include/bls12-381/arithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void _lsubtract(fp* z, const fp* x, const fp* y);
// The "smaller than 4p" here means the montgomery form itself as number is less than 4p.
// Therefore, at most ONE _ladd/_lsubstract/_ldouble is allowed before passing the result to _multiply,
// unless the algorithm makes sure the number is small.
#if defined(__x86_64__)
#if defined(__x86_64_asm__)
extern void (*_multiply)(fp*, const fp*, const fp*);
#else
void _multiply(fp*, const fp*, const fp*);
Expand All @@ -41,7 +41,7 @@ void _multiply(fp*, const fp*, const fp*);
// The carryOut output is guaranteed to be 0 or 1.
//
// This function's execution time does not depend on the inputs.
std::tuple<uint64_t, uint64_t> Add64(
inline std::tuple<uint64_t, uint64_t> Add64(
const uint64_t& x,
const uint64_t& y,
const uint64_t& carry
Expand All @@ -63,12 +63,12 @@ std::tuple<uint64_t, uint64_t> Sub64(
// half returned in lo.
//
// This function's execution time does not depend on the inputs.
std::tuple<uint64_t, uint64_t> Mul64(
inline std::tuple<uint64_t, uint64_t> Mul64(
const uint64_t& x,
const uint64_t& y
);

std::tuple<uint64_t, uint64_t, uint64_t> madd(
inline std::tuple<uint64_t, uint64_t, uint64_t> madd(
const uint64_t& a,
const uint64_t& b,
const uint64_t& t,
Expand All @@ -77,57 +77,57 @@ std::tuple<uint64_t, uint64_t, uint64_t> madd(
);

// madd0 hi = a*b + c (discards lo bits)
uint64_t madd0(
inline uint64_t madd0(
const uint64_t& a,
const uint64_t& b,
const uint64_t& c
);

// madd1 hi, lo = a*b + c
std::tuple<uint64_t, uint64_t> madd1(
inline std::tuple<uint64_t, uint64_t> madd1(
const uint64_t& a,
const uint64_t& b,
const uint64_t& c
);

// madd2 hi, lo = a*b + c + d
std::tuple<uint64_t, uint64_t> madd2(
inline std::tuple<uint64_t, uint64_t> madd2(
const uint64_t& a,
const uint64_t& b,
const uint64_t& c,
const uint64_t& d
);

// madd2s superhi, hi, lo = 2*a*b + c + d + e
std::tuple<uint64_t, uint64_t, uint64_t> madd2s(
inline std::tuple<uint64_t, uint64_t, uint64_t> madd2s(
const uint64_t& a,
const uint64_t& b,
const uint64_t& c,
const uint64_t& d,
const uint64_t& e
);

std::tuple<uint64_t, uint64_t, uint64_t> madd1s(
inline std::tuple<uint64_t, uint64_t, uint64_t> madd1s(
const uint64_t& a,
const uint64_t& b,
const uint64_t& d,
const uint64_t& e
);

std::tuple<uint64_t, uint64_t, uint64_t> madd2sb(
inline std::tuple<uint64_t, uint64_t, uint64_t> madd2sb(
const uint64_t& a,
const uint64_t& b,
const uint64_t& c,
const uint64_t& e
);

std::tuple<uint64_t, uint64_t, uint64_t> madd1sb(
inline std::tuple<uint64_t, uint64_t, uint64_t> madd1sb(
const uint64_t& a,
const uint64_t& b,
const uint64_t& e
);

std::tuple<uint64_t, uint64_t> madd3(
inline std::tuple<uint64_t, uint64_t> madd3(
const uint64_t& a,
const uint64_t& b,
const uint64_t& c,
Expand Down
2 changes: 1 addition & 1 deletion src/bls12-381/libbls.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@

Name: libmcl
Name: libbls
Description: Pairing Library
URL: https://github.com/QEDProtocol/bls12-381
Version: @PACKAGE_VERSION@
Expand Down
20 changes: 10 additions & 10 deletions src/bls12-381/src/arithmetic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <bls12-381/bls12-381.hpp>
#ifdef __x86_64__
#ifdef __x86_64_asm__
#include <cpuid.h>
#endif

Expand All @@ -8,7 +8,7 @@ using namespace std;
namespace bls12_381
{

#ifdef __x86_64__
#ifdef __x86_64_asm__
void _add(fp* z, const fp* x, const fp* y)
{
// x86_64 calling convention (https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI):
Expand Down Expand Up @@ -96,7 +96,7 @@ void _add(fp* z, const fp* x, const fp* y)
}
#endif

#ifdef __x86_64__
#ifdef __x86_64_asm__
void _ladd(fp* z, const fp* x, const fp* y)
{
// x86_64 calling convention (https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI):
Expand Down Expand Up @@ -136,7 +136,7 @@ void _ladd(fp* z, const fp* x, const fp* y)
}
#endif

#ifdef __x86_64__
#ifdef __x86_64_asm__
void _double(fp* z, const fp* x)
{
// x86_64 calling convention (https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI):
Expand Down Expand Up @@ -223,7 +223,7 @@ void _double(fp* z, const fp* x)
}
#endif

#ifdef __x86_64__
#ifdef __x86_64_asm__
void _ldouble(fp* z, const fp* x)
{
// x86_64 calling convention (https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI):
Expand Down Expand Up @@ -263,7 +263,7 @@ void _ldouble(fp* z, const fp* x)
}
#endif

#ifdef __x86_64__
#ifdef __x86_64_asm__
void _subtract(fp* z, const fp* x, const fp* y)
{
// x86_64 calling convention (https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI):
Expand Down Expand Up @@ -342,7 +342,7 @@ void _subtract(fp* z, const fp* x, const fp* y)
}
#endif

#ifdef __x86_64__
#ifdef __x86_64_asm__
void _lsubtract(fp* z, const fp* x, const fp* y)
{
// x86_64 calling convention (https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI):
Expand Down Expand Up @@ -382,7 +382,7 @@ void _lsubtract(fp* z, const fp* x, const fp* y)
}
#endif

#ifdef __x86_64__
#ifdef __x86_64_asm__
void __negate(fp* z, const fp* x)
{
// x86_64 calling convention (https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI):
Expand Down Expand Up @@ -436,7 +436,7 @@ void _negate(fp* z, const fp* x)
}
#endif

#ifdef __x86_64__
#ifdef __x86_64_asm__
void __multiply(fp* z, const fp* x, const fp* y)
{
// x86_64 calling convention (https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI):
Expand Down Expand Up @@ -1624,7 +1624,7 @@ void _multiply(fp* z, const fp* x, const fp* y)
}
#endif

#ifdef __x86_64__
#ifdef __x86_64_asm__
void _square(fp* z, const fp* x)
{
#ifdef __clang__
Expand Down
21 changes: 21 additions & 0 deletions src/test/script_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,27 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
}

BOOST_AUTO_TEST_CASE(script_CHECKZKPVERIFY)
{
const int flags = SCRIPT_VERIFY_P2SH;

CMutableTransaction txCredit;
DecodeHexTx(txCredit, "0200000001d5258a4fd597edc59d3a9166557c078f05bb992c926d90a0f18c75302d93f99000000000fd0203307f4e6a794e3317a1a2f06a8163ebd70a4a714a5f712d4259a9387d6589615bf63e95bb9233b844f47f5e2a0a312cb300308ddb1235bf6f33189982611ce46edbf62762cf1bda4e245c28c9622452e579078384c4a7c83b00b9b5cb96eaca9abe1130a882ef4edc6a154f1f62d53bf8bf55daeabdf528bbdc9fc7935f37449bafd302aaf19156548ba8b28d29a278ea92f094302882cbef8b8e4f89687f9e8eedc70e8400b0293c05e41c29252683855ecab667100996bda1d61091ccec5c61db9508814c50b9d96fecdd99ff136355dc90772a2ef7788afe1f8a332b34e8a30402477f8e38e874eba3946f6f4ba8c794a09d266507dd66ecb824f911036f2b6bf63ee7a5feb52105bdf466dac06ca427711d5b52c24de90120caec89bedcab3ce707981a6fa86d27b87b2c0a732feee3717aaddd6728877c007c76a82081c202329a17f5756142a72734f6832f7784e22b1e17260901039f504f9098a3884c5090c10d96e70d136fac9a3634428f3792e9b8d02eb903783f71fc8e6937f7f8b1360c8bee9146041a79251614eee9689073af20af273a280e841b078837e65a862279849c1251e1761bb6ac35f2649ea34c50fe63d02ab4af4ab3448fe5e8d4fff135d510a38558c9daee4ae190303c18368ce2ba9fd21e8d1ab0f617a9d249621b56f224b69f6c3a3e008a40b3aea5a5ab77eed73711e93c896023475b27e3a4f6124c50189647ab2c8de2fc37ef189f803704cc55087bfe1c56dcabbb2f343dc145a0470d18317696e1023a7574e8745e0ea301cc6cd679a61133a1c560d5aa3d38d91a999a8f666109495402c553bd7c82056b4c50bf358e5097046487c370c1dd6781dc11d6518717e23b334d4b09892a9763f09059687a7c136f6189568edd6d6f357c1c199a39fa0f723d2218762766f67fa8171b10e8b7e5dd88155651d37ca6b59c754c50092d3dfea8804a69cab1f76133032b85ee7e850977dd1fe578f3d9663bb43a08502a8fd7cb8c7f79c39fbe49f9cee082bf68dfd65e70ccdbfb4c6f834d5dcb3e4619bc44de9ca8aca12b502e74b7b50451b36d6d6d6d6d6d51ffffffff0100113d550200000017a914fda635e6bc2ef7efa82521342c1e3ab932153c628700000000");
CMutableTransaction txSpend;
DecodeHexTx(txSpend, "0200000002952da86dd9c8e4f2587be7adfa9ebe531cc05a8e733f7bd6c02c7ac9f03503b300000000fd020330a1cf4fd86ec455bbe8b983c0dd64abe78ef065724960fecce845873a9df15e10e16c60e948fddd722bd68ed469cca20e30a9cb9215f93c6d926a22367fab2e95d54560a06412ca51304da82d2ee75521a6f58996298c14fe566795d2f654efda0130f23b6a58474bfc3e89499bce8bf7b280b2d84707ffa7f86bc9c94a8174b99f7df7be7e531e8ea759f60abdb4864ad7023016e9e0aa1e58d75831dd90aef95657a5a5e96e9196e6a02f6cb8c6371d311d5654dda6dfd522626f4c90aa0861b067024c50b9d96fecdd99ff136355dc90772a2ef7788afe1f8a332b34e8a30402477f8e38e874eba3946f6f4ba8c794a09d266507dd66ecb824f911036f2b6bf63ee7a5feb52105bdf466dac06ca427711d5b52c24de9012000f3a40258113d7544ec3a1c548047ab9a14e5488320414a311d8de59b7414007c76a82081c202329a17f5756142a72734f6832f7784e22b1e17260901039f504f9098a3884c5090c10d96e70d136fac9a3634428f3792e9b8d02eb903783f71fc8e6937f7f8b1360c8bee9146041a79251614eee9689073af20af273a280e841b078837e65a862279849c1251e1761bb6ac35f2649ea34c50fe63d02ab4af4ab3448fe5e8d4fff135d510a38558c9daee4ae190303c18368ce2ba9fd21e8d1ab0f617a9d249621b56f224b69f6c3a3e008a40b3aea5a5ab77eed73711e93c896023475b27e3a4f6124c50189647ab2c8de2fc37ef189f803704cc55087bfe1c56dcabbb2f343dc145a0470d18317696e1023a7574e8745e0ea301cc6cd679a61133a1c560d5aa3d38d91a999a8f666109495402c553bd7c82056b4c50bf358e5097046487c370c1dd6781dc11d6518717e23b334d4b09892a9763f09059687a7c136f6189568edd6d6f357c1c199a39fa0f723d2218762766f67fa8171b10e8b7e5dd88155651d37ca6b59c754c50092d3dfea8804a69cab1f76133032b85ee7e850977dd1fe578f3d9663bb43a08502a8fd7cb8c7f79c39fbe49f9cee082bf68dfd65e70ccdbfb4c6f834d5dcb3e4619bc44de9ca8aca12b502e74b7b50451b36d6d6d6d6d6d51ffffffffa75fe7d127ffba6a51a6aa750aec49d6861b7e3cc809f259cfcf3d95c71574b900000000fd02033091d0f9aff7bd3da433eb5706a7914bc49d19ea781a9b8eee183929cdfc7a79773a244992d5afd3e9ddc61d70f73b0194300739ce49ac859b19f03d3c0c2bbb7ffb24e64c32e4e632fe74e3a53f5e5aa10ee3551061b3818aa3e126d960cb593f163035c36460f49449043c7892acebd0518122551501a7abd5a00df0962ce1f6977399a5ef4762c0d80ceec2c9833891c617301120658e06a7ede8415aded94c242e8a6d661732a1b8826114814421aff13fec59f847961ae3181b7a533d40f4aa3a924c50b9d96fecdd99ff136355dc90772a2ef7788afe1f8a332b34e8a30402477f8e38e874eba3946f6f4ba8c794a09d266507dd66ecb824f911036f2b6bf63ee7a5feb52105bdf466dac06ca427711d5b52c24de9012000f3a40258113d7544ec3a1c548047ab9a14e5488320414a311d8de59b7414007c76a82081c202329a17f5756142a72734f6832f7784e22b1e17260901039f504f9098a3884c5090c10d96e70d136fac9a3634428f3792e9b8d02eb903783f71fc8e6937f7f8b1360c8bee9146041a79251614eee9689073af20af273a280e841b078837e65a862279849c1251e1761bb6ac35f2649ea34c50fe63d02ab4af4ab3448fe5e8d4fff135d510a38558c9daee4ae190303c18368ce2ba9fd21e8d1ab0f617a9d249621b56f224b69f6c3a3e008a40b3aea5a5ab77eed73711e93c896023475b27e3a4f6124c50189647ab2c8de2fc37ef189f803704cc55087bfe1c56dcabbb2f343dc145a0470d18317696e1023a7574e8745e0ea301cc6cd679a61133a1c560d5aa3d38d91a999a8f666109495402c553bd7c82056b4c50bf358e5097046487c370c1dd6781dc11d6518717e23b334d4b09892a9763f09059687a7c136f6189568edd6d6f357c1c199a39fa0f723d2218762766f67fa8171b10e8b7e5dd88155651d37ca6b59c754c50092d3dfea8804a69cab1f76133032b85ee7e850977dd1fe578f3d9663bb43a08502a8fd7cb8c7f79c39fbe49f9cee082bf68dfd65e70ccdbfb4c6f834d5dcb3e4619bc44de9ca8aca12b502e74b7b50451b36d6d6d6d6d6d51ffffffff01003e6e560200000017a9149faff0ec8c48761a48a8023ed8fd7b8af103c0888700000000");

ScriptError err;
bool success = VerifyScript(txSpend.vin[0].scriptSig,
txCredit.vout[0].scriptPubKey,
&txSpend.vin[0].scriptWitness,
flags,
MutableTransactionSignatureChecker(&txSpend, 0, txCredit.vout[0].nValue),
&err);

BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
BOOST_CHECK(success);
}

BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
{
ScriptError err;
Expand Down

0 comments on commit 70243ea

Please sign in to comment.