Skip to content

Commit

Permalink
Add xonly_[pubkey|privkey]_tweak_add and xonly_tweak_verify
Browse files Browse the repository at this point in the history
  • Loading branch information
afk11 committed Oct 7, 2019
1 parent 76056f0 commit 5ac425e
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 2 deletions.
7 changes: 6 additions & 1 deletion secp256k1/php_secp256k1.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ PHP_FUNCTION(secp256k1_ec_pubkey_parse);
PHP_FUNCTION(secp256k1_ec_pubkey_serialize);

PHP_FUNCTION(secp256k1_ec_privkey_tweak_add);
PHP_FUNCTION(secp256k1_ec_privkey_tweak_mul);
PHP_FUNCTION(secp256k1_ec_pubkey_tweak_add);
PHP_FUNCTION(secp256k1_ec_privkey_tweak_mul);
PHP_FUNCTION(secp256k1_ec_pubkey_tweak_mul);

PHP_FUNCTION(secp256k1_ec_pubkey_combine);
Expand All @@ -80,6 +80,11 @@ PHP_FUNCTION(secp256k1_nonce_function_rfc6979);
PHP_FUNCTION(secp256k1_xonly_pubkey_create);
PHP_FUNCTION(secp256k1_xonly_pubkey_parse);
PHP_FUNCTION(secp256k1_xonly_pubkey_serialize);
PHP_FUNCTION(secp256k1_xonly_pubkey_from_pubkey);
PHP_FUNCTION(secp256k1_xonly_pubkey_to_pubkey);
PHP_FUNCTION(secp256k1_xonly_privkey_tweak_add);
PHP_FUNCTION(secp256k1_xonly_pubkey_tweak_add);
PHP_FUNCTION(secp256k1_xonly_pubkey_tweak_verify);

/* Recovery module */
#ifdef SECP256K1_MODULE_RECOVERY
Expand Down
238 changes: 237 additions & 1 deletion secp256k1/secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,60 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_serialize, I
ZEND_ARG_TYPE_INFO(0, pubkey, IS_RESOURCE, 0)
ZEND_END_ARG_INFO();

#if (PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70200)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_from_pubkey, IS_LONG, NULL, 0)
#else
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_from_pubkey, IS_LONG, 0)
#endif
ZEND_ARG_TYPE_INFO(0, context, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(1, xonly_pubkey, IS_RESOURCE, 1)
ZEND_ARG_TYPE_INFO(1, sign, IS_LONG, 1)
ZEND_ARG_TYPE_INFO(0, pubkey, IS_RESOURCE, 0)
ZEND_END_ARG_INFO();

#if (PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70200)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_to_pubkey, IS_LONG, NULL, 0)
#else
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_to_pubkey, IS_LONG, 0)
#endif
ZEND_ARG_TYPE_INFO(0, context, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(1, pubkey, IS_RESOURCE, 1)
ZEND_ARG_TYPE_INFO(0, xonly_pubkey, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(0, sign, IS_LONG, 0)
ZEND_END_ARG_INFO();

#if (PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70200)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_privkey_tweak_add, IS_LONG, NULL, 0)
#else
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_privkey_tweak_add, IS_LONG, 0)
#endif
ZEND_ARG_TYPE_INFO(0, context, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(1, seckey, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, tweak32, IS_STRING, 0)
ZEND_END_ARG_INFO();

#if (PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70200)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_tweak_add, IS_LONG, NULL, 0)
#else
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_tweak_add, IS_LONG, 0)
#endif
ZEND_ARG_TYPE_INFO(0, context, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(1, outputPubKey, IS_RESOURCE, 1)
ZEND_ARG_TYPE_INFO(0, internalPubKey, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(0, tweak32, IS_STRING, 0)
ZEND_END_ARG_INFO();

#if (PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70200)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_tweak_verify, IS_LONG, NULL, 0)
#else
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_xonly_pubkey_tweak_verify, IS_LONG, 0)
#endif
ZEND_ARG_TYPE_INFO(0, context, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(0, outputPubKey, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(0, internalPubKey, IS_RESOURCE, 0)
ZEND_ARG_TYPE_INFO(0, tweak32, IS_STRING, 0)
ZEND_END_ARG_INFO();

//recovery
#ifdef SECP256K1_MODULE_RECOVERY
#if (PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70200)
Expand Down Expand Up @@ -611,7 +665,11 @@ const zend_function_entry secp256k1_functions[] = {
PHP_FE(secp256k1_xonly_pubkey_create, arginfo_secp256k1_xonly_pubkey_create)
PHP_FE(secp256k1_xonly_pubkey_parse, arginfo_secp256k1_xonly_pubkey_parse)
PHP_FE(secp256k1_xonly_pubkey_serialize, arginfo_secp256k1_xonly_pubkey_serialize)

PHP_FE(secp256k1_xonly_pubkey_from_pubkey, arginfo_secp256k1_xonly_pubkey_from_pubkey)
PHP_FE(secp256k1_xonly_pubkey_to_pubkey, arginfo_secp256k1_xonly_pubkey_to_pubkey)
PHP_FE(secp256k1_xonly_privkey_tweak_add, arginfo_secp256k1_xonly_privkey_tweak_add)
PHP_FE(secp256k1_xonly_pubkey_tweak_add, arginfo_secp256k1_xonly_pubkey_tweak_add)
PHP_FE(secp256k1_xonly_pubkey_tweak_verify, arginfo_secp256k1_xonly_pubkey_tweak_verify)
// secp256k1_recovery.h
#ifdef SECP256K1_MODULE_RECOVERY
PHP_FE(secp256k1_ecdsa_recoverable_signature_parse_compact, arginfo_secp256k1_ecdsa_recoverable_signature_parse_compact)
Expand Down Expand Up @@ -1856,6 +1914,184 @@ PHP_FUNCTION(secp256k1_xonly_pubkey_serialize)
}
/* }}} */


/* {{{ proto int secp256k1_xonly_pubkey_from_pubkey(resource context, resource &xonly_pubkey, int &sign, resource pubkey)
* Convert a recoverable signature into a normal signature. */
PHP_FUNCTION(secp256k1_xonly_pubkey_from_pubkey)
{
zval *zCtx, *zXOnlyPubKey, *zSign, *zPubKey;
secp256k1_context *ctx;
secp256k1_xonly_pubkey *xonly_pubkey;
secp256k1_pubkey *pubkey;
int sign;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/z/r", &zCtx, &zXOnlyPubKey, &zSign, &zPubKey) == FAILURE) {
RETURN_LONG(0);
}

if ((ctx = php_get_secp256k1_context(zCtx)) == NULL) {
RETURN_LONG(0);
}

if ((pubkey = php_get_secp256k1_pubkey(zPubKey)) == NULL) {
RETURN_LONG(0);
}

xonly_pubkey = emalloc(sizeof(secp256k1_xonly_pubkey));
result = secp256k1_xonly_pubkey_from_pubkey(ctx, xonly_pubkey, &sign, pubkey);
if (result) {
zval_dtor(zXOnlyPubKey);
ZVAL_RES(zXOnlyPubKey, zend_register_resource(xonly_pubkey, le_secp256k1_xonly_pubkey));
zval_dtor(zSign);
ZVAL_LONG(zSign, sign);
}

RETURN_LONG(result);
}
/* }}} */

/* {{{ proto int secp256k1_xonly_pubkey_to_pubkey(resource context, resource &pubkey, resource pubkey, int sign)
* Convert a recoverable signature into a normal signature. */
PHP_FUNCTION(secp256k1_xonly_pubkey_to_pubkey)
{
zval *zCtx, *zXOnlyPubKey, *zPubKey;
secp256k1_context *ctx;
secp256k1_xonly_pubkey *xonly_pubkey;
secp256k1_pubkey *pubkey;
long sign;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/rl", &zCtx, &zPubKey, &zXOnlyPubKey, &sign) == FAILURE) {
RETURN_LONG(0);
}

if ((ctx = php_get_secp256k1_context(zCtx)) == NULL) {
RETURN_LONG(0);
} else if ((xonly_pubkey = php_get_secp256k1_xonly_pubkey(zXOnlyPubKey)) == NULL) {
RETURN_LONG(0);
}

pubkey = emalloc(sizeof(secp256k1_pubkey));
result = secp256k1_xonly_pubkey_to_pubkey(ctx, pubkey, xonly_pubkey, sign);
if (result) {
zval_dtor(zPubKey);
ZVAL_RES(zPubKey, zend_register_resource(pubkey, le_secp256k1_pubkey));
}

RETURN_LONG(result);
}
/* }}} */

/* {{{ proto int secp256k1_xonly_privkey_tweak_add(resource context, string &key32, string tweak32)
* Tweak a private key by adding tweak to it. */
PHP_FUNCTION(secp256k1_xonly_privkey_tweak_add)
{
zval *zCtx, *zSecKey;
secp256k1_context *ctx;
zend_string *zTweak;
unsigned char *tweak;
unsigned char newseckey[SECRETKEY_LENGTH];
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/S", &zCtx, &zSecKey, &zTweak) == FAILURE) {
RETURN_LONG(0);
}

if ((ctx = php_get_secp256k1_context(zCtx)) == NULL) {
RETURN_LONG(0);
}

if (Z_STRLEN_P(zSecKey) != SECRETKEY_LENGTH) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "secp256k1_xonly_privkey_tweak_add(): Parameter 2 should be 32 bytes");
return;
} else if (zTweak->len != SECRETKEY_LENGTH) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "secp256k1_xonly_privkey_tweak_add(): Parameter 3 should be 32 bytes");
return;
}

memcpy(newseckey, Z_STRVAL_P(zSecKey), SECRETKEY_LENGTH);
result = secp256k1_xonly_privkey_tweak_add(ctx, newseckey, (unsigned char *) zTweak->val);
if (result) {
zval_dtor(zSecKey);
ZVAL_STRINGL(zSecKey, (const char *) newseckey, SECRETKEY_LENGTH);
}
RETURN_LONG(result);
}
/* }}} */

/* {{{ proto int secp256k1_xonly_pubkey_tweak_add(resource context, resource &output_pubkey, resource internal_pubkey, string tweak32)
* Tweak a public key by adding tweak times the generator to it. */
PHP_FUNCTION(secp256k1_xonly_pubkey_tweak_add)
{
zval *zCtx, *zOutputPubKey, *zInternalPubKey;
secp256k1_context *ctx;
secp256k1_pubkey *output_pubkey;
secp256k1_xonly_pubkey *internal_pubkey;
zend_string *zTweak;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrS", &zCtx, &zOutputPubKey, &zInternalPubKey, &zTweak) == FAILURE) {
RETURN_LONG(0);
}

if ((ctx = php_get_secp256k1_context(zCtx)) == NULL) {
RETURN_LONG(0);
} else if ((internal_pubkey = php_get_secp256k1_xonly_pubkey(zInternalPubKey)) == NULL) {
RETURN_LONG(0);
}

if (zTweak->len != SECRETKEY_LENGTH) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "secp256k1_xonly_pubkey_tweak_add(): Parameter 4 should be 32 bytes");
return;
}

output_pubkey = emalloc(sizeof(secp256k1_pubkey));
result = secp256k1_xonly_pubkey_tweak_add(ctx, output_pubkey, internal_pubkey, (unsigned char *)zTweak->val);
if (result) {
zval_dtor(zOutputPubKey);
ZVAL_RES(zOutputPubKey, zend_register_resource(output_pubkey, le_secp256k1_pubkey));
} else {
efree(output_pubkey);
}
RETURN_LONG(result);
}
/* }}} */

/* {{{ proto int secp256k1_xonly_pubkey_tweak_verify(resource context, resource output_pubkey, resource internal_pubkey, string tweak32)
* Tweak a public key by adding tweak times the generator to it. */
PHP_FUNCTION(secp256k1_xonly_pubkey_tweak_verify)
{
zval *zCtx, *zOutputPubKey, *zInternalPubKey;
secp256k1_context *ctx;
secp256k1_pubkey *output_pubkey;
secp256k1_xonly_pubkey *internal_pubkey;
zend_string *zTweak32;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrS", &zCtx, &zOutputPubKey, &zInternalPubKey, &zTweak32) == FAILURE) {
RETURN_LONG(0);
}

if ((ctx = php_get_secp256k1_context(zCtx)) == NULL) {
RETURN_LONG(0);
} else if ((output_pubkey = php_get_secp256k1_pubkey(zOutputPubKey)) == NULL) {
RETURN_LONG(0);
} else if ((internal_pubkey = php_get_secp256k1_xonly_pubkey(zInternalPubKey)) == NULL) {
RETURN_LONG(0);
}

if (zTweak32->len != SECRETKEY_LENGTH) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "secp256k1_xonly_pubkey_tweak_verify(): Parameter 4 should be 32 bytes");
return;
}

result = secp256k1_xonly_pubkey_tweak_verify(ctx, output_pubkey, internal_pubkey, (unsigned char *)zTweak32->val);

RETURN_LONG(result);
}
/* }}} */

/* Begin recovery module functions */
#ifdef SECP256K1_MODULE_RECOVERY

Expand Down

0 comments on commit 5ac425e

Please sign in to comment.