Skip to content
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

Install extension from a local secp256k1 version #125

Open
wants to merge 7 commits into
base: master
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
37 changes: 36 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ modules
missing
*.lo
*.la
config.*
.deps
.gdbinit
.gdbinit.default
Expand All @@ -30,3 +29,39 @@ mkinstalldirs
run-tests.php
tags
vendor
.deps
CMakeLists.txt
*.lo
*.la
.libs
acinclude.m4
aclocal.m4
autom4te.cache
build
config.guess
config.h
config.h.in
config.log
config.nice
config.status
config.sub
configure
configure.in
include
install-sh
libtool
ltmain.sh
Makefile
Makefile.fragments
Makefile.global
Makefile.objects
missing
mkinstalldirs
modules
run-tests.php
tests/*/*.diff
tests/*/*.out
tests/*/*.php
tests/*/*.exp
tests/*/*.log
tests/*/*.sh
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ install:
&& ./configure --enable-tests=no --enable-benchmark=no \
--enable-experimental --enable-module-{ecdh,recovery} \
&& make -j$(nproc) && sudo make install && cd ..
- |
cd secp256k1 && phpize \
&& ./configure && make -j$(nproc) && sudo make install \
&& cd ..
- phpize && ./configure && make
- ls /home/travis/build/Bit-Wasp/secp256k1-php/.libs/secp256k1.so
- ls /home/travis/build/Bit-Wasp/secp256k1-php/modules/secp256k1.so
- composer update

before_script:
Expand All @@ -52,8 +51,9 @@ before_script:

script:
- travis/verify_stubs.sh
- cd secp256k1/ && REPORT_EXIT_STATUS=1 make test || (find tests/*.log -type f -exec cat {} + ; exit 1) && cd ..
- travis/run_coverage_test.sh || exit 1
- travis/validate_examples.sh || exit 1
- REPORT_EXIT_STATUS=1 make test || (find tests/*.log -type f -exec cat {} + ; exit 1)
- travis/run_coverage_test.sh || exit 1

after_script:
- bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash)
54 changes: 54 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
dnl $Id$
dnl config.m4 for extension secp256k1

PHP_ARG_WITH(secp256k1, for secp256k1 support,
dnl Make sure that the comment is aligned:
[ --with-secp256k1 Include secp256k1 support])

if test "$PHP_SECP256K1" != "no"; then
dnl Write more examples of tests her

AC_PATH_PROG(PKG_CONFIG, pkg-config, no)

dnl system libsecp256k1, depends on libsecp256k1
AC_MSG_CHECKING(for libsecp256k1)

if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists libsecp256k1; then
if $PKG_CONFIG libsecp256k1 --atleast-version 0.1; then
LIBSECP256K1_CFLAGS=`$PKG_CONFIG libsecp256k1 --cflags`
LIBSECP256K1_LIBDIR=`$PKG_CONFIG libsecp256k1 --variable=libdir`
LIBSECP256K1_VERSION=`$PKG_CONFIG libsecp256k1 --modversion`
AC_MSG_RESULT(from pkgconfig: version $LIBSECP256K1_VERSION found in $LIBSECP256K1_LIBDIR)

else
AC_MSG_ERROR(system libsecp2561 must be upgraded to version >= 0.11)
fi

PHP_CHECK_LIBRARY(secp256k1, secp256k1_context_create,
[
PHP_ADD_LIBRARY_WITH_PATH(secp256k1, $LIBSECP256K1_LIBDIR, SECP256K1_SHARED_LIBADD)
AC_DEFINE(HAVE_LIBSECP256K1,1,[ ])
AC_DEFINE(HAVE_SECP256K1,1,[ ])
], [
AC_MSG_ERROR(could not find usable libsecp256k1)
], [
-L$LIBSECP256K1_LIBDIR
])

PHP_SUBST(SECP256K1_SHARED_LIBADD)
PHP_NEW_EXTENSION(secp256k1, secp256k1.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

else

PHP_SECP256K1_SOURCES="$PHP_SECP256K1_SOURCES secp256k1/src/secp256k1.c "

AC_DEFINE(HAVE_SECP256K1,1,[ ])
PHP_NEW_EXTENSION(secp256k1, secp256k1.c $PHP_SECP256K1_SOURCES, $ext_shared)
PHP_ADD_INCLUDE([$ext_srcdir/secp256k1])
PHP_ADD_INCLUDE([$ext_srcdir/secp256k1/include])
PHP_ADD_BUILD_DIR($ext_builddir/secp256k1/src, 1)
PHP_SUBST(SECP256K1_SHARED_LIBADD)

fi

fi
File renamed without changes.
154 changes: 154 additions & 0 deletions lax_der.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include <stdlib.h>
#include <stdint.h>

/* Parse a DER signature with arbitrary
/** This function is taken from the libsecp256k1 distribution and implements
* DER parsing for ECDSA signatures, while supporting an arbitrary subset of
* format violations.
*
* Supported violations include negative integers, excessive padding, garbage
* at the end, and overly long length descriptors. This is safe to use in
* Bitcoin because since the activation of BIP66, signatures are verified to be
* strict DER before being passed to this module, and we know it supports all
* violations present in the blockchain before that point.
*/
int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
size_t rpos, rlen, spos, slen;
size_t pos = 0;
size_t lenbyte;
unsigned char tmpsig[64] = {0};
int overflow = 0;

/* Hack to initialize sig with a correctly-parsed but invalid signature. */
secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);

/* Sequence tag byte */
if (pos == inputlen || input[pos] != 0x30) {
return 0;
}
pos++;

/* Sequence length bytes */
if (pos == inputlen) {
return 0;
}
lenbyte = input[pos++];
if (lenbyte & 0x80) {
lenbyte -= 0x80;
if (pos + lenbyte > inputlen) {
return 0;
}
pos += lenbyte;
}

/* Integer tag byte for R */
if (pos == inputlen || input[pos] != 0x02) {
return 0;
}
pos++;

/* Integer length for R */
if (pos == inputlen) {
return 0;
}
lenbyte = input[pos++];
if (lenbyte & 0x80) {
lenbyte -= 0x80;
if (pos + lenbyte > inputlen) {
return 0;
}
while (lenbyte > 0 && input[pos] == 0) {
pos++;
lenbyte--;
}
if (lenbyte >= sizeof(size_t)) {
return 0;
}
rlen = 0;
while (lenbyte > 0) {
rlen = (rlen << 8) + input[pos];
pos++;
lenbyte--;
}
} else {
rlen = lenbyte;
}
if (rlen > inputlen - pos) {
return 0;
}
rpos = pos;
pos += rlen;

/* Integer tag byte for S */
if (pos == inputlen || input[pos] != 0x02) {
return 0;
}
pos++;

/* Integer length for S */
if (pos == inputlen) {
return 0;
}
lenbyte = input[pos++];
if (lenbyte & 0x80) {
lenbyte -= 0x80;
if (pos + lenbyte > inputlen) {
return 0;
}
while (lenbyte > 0 && input[pos] == 0) {
pos++;
lenbyte--;
}
if (lenbyte >= sizeof(size_t)) {
return 0;
}
slen = 0;
while (lenbyte > 0) {
slen = (slen << 8) + input[pos];
pos++;
lenbyte--;
}
} else {
slen = lenbyte;
}
if (slen > inputlen - pos) {
return 0;
}
spos = pos;
pos += slen;

/* Ignore leading zeroes in R */
while (rlen > 0 && input[rpos] == 0) {
rlen--;
rpos++;
}
/* Copy R value */
if (rlen > 32) {
overflow = 1;
} else {
memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
}

/* Ignore leading zeroes in S */
while (slen > 0 && input[spos] == 0) {
slen--;
spos++;
}
/* Copy S value */
if (slen > 32) {
overflow = 1;
} else {
memcpy(tmpsig + 64 - slen, input + spos, slen);
}

if (!overflow) {
overflow = !secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
}
if (overflow) {
/* Overwrite the result again with a correctly-parsed but invalid
signature if parsing failed. */
memset(tmpsig, 0, 64);
secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig);
}
return 1;
}
7 changes: 7 additions & 0 deletions secp256k1/php_secp256k1.h → php_secp256k1.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/* $Id$ */


#if defined(HAVE_LIBSECP256K1)
#include <secp256k1.h>
#include <secp256k1_ecdh.h>
#include <secp256k1_recovery.h>
#else
#include "secp256k1/include/secp256k1.h"
#include "secp256k1/include/secp256k1_recovery.h"
#endif

#ifndef PHP_SECP256K1_H
#define PHP_SECP256K1_H
Expand Down
5 changes: 5 additions & 0 deletions secp256k1/secp256k1.c → secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_secp256k1.h"
#if defined(HAVE_LIBSECP256K1)
#include "lax_der.h"
#else
#include "secp256k1/contrib/lax_der.h"
#endif
#include "zend_exceptions.h"

static zend_class_entry *spl_ce_InvalidArgumentException;

Expand Down
36 changes: 0 additions & 36 deletions secp256k1/.gitignore

This file was deleted.

Loading