-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Marko Bencun <mbencun+pgp@gmail.com> Co-authored-by: Jonas Nick <jonasd.nick@gmail.com>
- Loading branch information
1 parent
ff4714e
commit 826bd04
Showing
6 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2020 The libsecp256k1-zkp Developers * | ||
* Distributed under the MIT software license, see the accompanying * | ||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.* | ||
**********************************************************************/ | ||
|
||
#ifndef SECP256K1_ECCOMMIT_H | ||
#define SECP256K1_ECCOMMIT_H | ||
|
||
/** Helper function to add a 32-byte value to a scalar */ | ||
static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak); | ||
/** Helper function to add a 32-byte value, times G, to an EC point */ | ||
static int secp256k1_ec_pubkey_tweak_add_helper(const secp256k1_ecmult_context* ecmult_ctx, secp256k1_ge *p, const unsigned char *tweak); | ||
|
||
/** Serializes elem as a 33 byte array. This is non-constant time with respect to | ||
* whether pubp is the point at infinity. Thus, you may need to declassify | ||
* pubp->infinity before calling this function. */ | ||
static int secp256k1_ec_commit_pubkey_serialize_const(secp256k1_ge *pubp, unsigned char *buf33); | ||
/** Compute an ec commitment tweak as hash(pubkey, data). */ | ||
static int secp256k1_ec_commit_tweak(unsigned char *tweak32, secp256k1_ge* pubp, secp256k1_sha256* sha, const unsigned char *data, size_t data_size); | ||
/** Compute an ec commitment as pubkey + hash(pubkey, data)*G. */ | ||
static int secp256k1_ec_commit(const secp256k1_ecmult_context* ecmult_ctx, secp256k1_ge* commitp, const secp256k1_ge* pubp, secp256k1_sha256* sha, const unsigned char *data, size_t data_size); | ||
/** Compute a secret key commitment as seckey + hash(pubkey, data). */ | ||
static int secp256k1_ec_commit_seckey(const secp256k1_ecmult_gen_context* ecmult_gen_ctx, secp256k1_scalar* seckey, secp256k1_ge* pubp, secp256k1_sha256* sha, const unsigned char *data, size_t data_size); | ||
/** Verify an ec commitment as pubkey + hash(pubkey, data)*G ?= commitment. */ | ||
static int secp256k1_ec_commit_verify(const secp256k1_ecmult_context* ecmult_ctx, const secp256k1_ge* commitp, const secp256k1_ge* pubp, secp256k1_sha256* sha, const unsigned char *data, size_t data_size); | ||
|
||
#endif /* SECP256K1_ECCOMMIT_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2020 The libsecp256k1 Developers * | ||
* Distributed under the MIT software license, see the accompanying * | ||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.* | ||
**********************************************************************/ | ||
|
||
#include <stddef.h> | ||
|
||
#include "eckey.h" | ||
#include "hash.h" | ||
|
||
/* from secp256k1.c */ | ||
static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak); | ||
static int secp256k1_ec_pubkey_tweak_add_helper(const secp256k1_ecmult_context* ecmult_ctx, secp256k1_ge *pubp, const unsigned char *tweak); | ||
|
||
static int secp256k1_ec_commit_pubkey_serialize_const(secp256k1_ge *pubp, unsigned char *buf33) { | ||
if (secp256k1_ge_is_infinity(pubp)) { | ||
return 0; | ||
} | ||
secp256k1_fe_normalize(&pubp->x); | ||
secp256k1_fe_normalize(&pubp->y); | ||
secp256k1_fe_get_b32(&buf33[1], &pubp->x); | ||
buf33[0] = secp256k1_fe_is_odd(&pubp->y) ? SECP256K1_TAG_PUBKEY_ODD : SECP256K1_TAG_PUBKEY_EVEN; | ||
return 1; | ||
} | ||
|
||
/* Compute an ec commitment tweak as hash(pubp, data). */ | ||
static int secp256k1_ec_commit_tweak(unsigned char *tweak32, secp256k1_ge* pubp, secp256k1_sha256* sha, const unsigned char *data, size_t data_size) | ||
{ | ||
unsigned char rbuf[33]; | ||
|
||
if (!secp256k1_ec_commit_pubkey_serialize_const(pubp, rbuf)) { | ||
return 0; | ||
} | ||
secp256k1_sha256_write(sha, rbuf, sizeof(rbuf)); | ||
secp256k1_sha256_write(sha, data, data_size); | ||
secp256k1_sha256_finalize(sha, tweak32); | ||
return 1; | ||
} | ||
|
||
/* Compute an ec commitment as pubp + hash(pubp, data)*G. */ | ||
static int secp256k1_ec_commit(const secp256k1_ecmult_context* ecmult_ctx, secp256k1_ge* commitp, const secp256k1_ge* pubp, secp256k1_sha256* sha, const unsigned char *data, size_t data_size) { | ||
unsigned char tweak[32]; | ||
|
||
*commitp = *pubp; | ||
return secp256k1_ec_commit_tweak(tweak, commitp, sha, data, data_size) | ||
&& secp256k1_ec_pubkey_tweak_add_helper(ecmult_ctx, commitp, tweak); | ||
} | ||
|
||
/* Compute the seckey of an ec commitment from the original secret key of the pubkey as seckey + | ||
* hash(pubp, data). */ | ||
static int secp256k1_ec_commit_seckey(secp256k1_scalar* seckey, secp256k1_ge* pubp, secp256k1_sha256* sha, const unsigned char *data, size_t data_size) { | ||
unsigned char tweak[32]; | ||
return secp256k1_ec_commit_tweak(tweak, pubp, sha, data, data_size) | ||
&& secp256k1_ec_seckey_tweak_add_helper(seckey, tweak); | ||
} | ||
|
||
/* Verify an ec commitment as pubp + hash(pubp, data)*G ?= commitment. */ | ||
static int secp256k1_ec_commit_verify(const secp256k1_ecmult_context* ecmult_ctx, const secp256k1_ge* commitp, const secp256k1_ge* pubp, secp256k1_sha256* sha, const unsigned char *data, size_t data_size) { | ||
secp256k1_gej pj; | ||
secp256k1_ge p; | ||
|
||
if (!secp256k1_ec_commit(ecmult_ctx, &p, pubp, sha, data, data_size)) { | ||
return 0; | ||
} | ||
|
||
/* Return p == commitp */ | ||
secp256k1_ge_neg(&p, &p); | ||
secp256k1_gej_set_ge(&pj, &p); | ||
secp256k1_gej_add_ge_var(&pj, &pj, commitp, NULL); | ||
return secp256k1_gej_is_infinity(&pj); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters