Dash Core  0.12.2.1
P2P Digital Currency
main_impl.h
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2014-2015 Pieter Wuille *
3  * Distributed under the MIT software license, see the accompanying *
4  * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6 
7 #ifndef SECP256K1_MODULE_SCHNORR_MAIN
8 #define SECP256K1_MODULE_SCHNORR_MAIN
9 
12 
13 static void secp256k1_schnorr_msghash_sha256(unsigned char *h32, const unsigned char *r32, const unsigned char *msg32) {
16  secp256k1_sha256_write(&sha, r32, 32);
17  secp256k1_sha256_write(&sha, msg32, 32);
18  secp256k1_sha256_finalize(&sha, h32);
19 }
20 
21 static const unsigned char secp256k1_schnorr_algo16[17] = "Schnorr+SHA256 ";
22 
23 int secp256k1_schnorr_sign(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
24  secp256k1_scalar sec, non;
25  int ret = 0;
26  int overflow = 0;
27  unsigned int count = 0;
28  VERIFY_CHECK(ctx != NULL);
30  ARG_CHECK(msg32 != NULL);
31  ARG_CHECK(sig64 != NULL);
32  ARG_CHECK(seckey != NULL);
33  if (noncefp == NULL) {
35  }
36 
37  secp256k1_scalar_set_b32(&sec, seckey, NULL);
38  while (1) {
39  unsigned char nonce32[32];
40  ret = noncefp(nonce32, msg32, seckey, secp256k1_schnorr_algo16, (void*)noncedata, count);
41  if (!ret) {
42  break;
43  }
44  secp256k1_scalar_set_b32(&non, nonce32, &overflow);
45  memset(nonce32, 0, 32);
46  if (!secp256k1_scalar_is_zero(&non) && !overflow) {
47  if (secp256k1_schnorr_sig_sign(&ctx->ecmult_gen_ctx, sig64, &sec, &non, NULL, secp256k1_schnorr_msghash_sha256, msg32)) {
48  break;
49  }
50  }
51  count++;
52  }
53  if (!ret) {
54  memset(sig64, 0, 64);
55  }
58  return ret;
59 }
60 
61 int secp256k1_schnorr_verify(const secp256k1_context* ctx, const unsigned char *sig64, const unsigned char *msg32, const secp256k1_pubkey *pubkey) {
62  secp256k1_ge q;
63  VERIFY_CHECK(ctx != NULL);
65  ARG_CHECK(msg32 != NULL);
66  ARG_CHECK(sig64 != NULL);
67  ARG_CHECK(pubkey != NULL);
68 
69  secp256k1_pubkey_load(ctx, &q, pubkey);
71 }
72 
73 int secp256k1_schnorr_recover(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *sig64, const unsigned char *msg32) {
74  secp256k1_ge q;
75 
76  VERIFY_CHECK(ctx != NULL);
78  ARG_CHECK(msg32 != NULL);
79  ARG_CHECK(sig64 != NULL);
80  ARG_CHECK(pubkey != NULL);
81 
83  secp256k1_pubkey_save(pubkey, &q);
84  return 1;
85  } else {
86  memset(pubkey, 0, sizeof(*pubkey));
87  return 0;
88  }
89 }
90 
91 int secp256k1_schnorr_generate_nonce_pair(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, unsigned char *privnonce32, const unsigned char *sec32, const unsigned char *msg32, secp256k1_nonce_function noncefp, const void* noncedata) {
92  int count = 0;
93  int ret = 1;
94  secp256k1_gej Qj;
95  secp256k1_ge Q;
96  secp256k1_scalar sec;
97 
98  VERIFY_CHECK(ctx != NULL);
100  ARG_CHECK(msg32 != NULL);
101  ARG_CHECK(sec32 != NULL);
102  ARG_CHECK(pubnonce != NULL);
103  ARG_CHECK(privnonce32 != NULL);
104 
105  if (noncefp == NULL) {
107  }
108 
109  do {
110  int overflow;
111  ret = noncefp(privnonce32, sec32, msg32, secp256k1_schnorr_algo16, (void*)noncedata, count++);
112  if (!ret) {
113  break;
114  }
115  secp256k1_scalar_set_b32(&sec, privnonce32, &overflow);
116  if (overflow || secp256k1_scalar_is_zero(&sec)) {
117  continue;
118  }
120  secp256k1_ge_set_gej(&Q, &Qj);
121 
122  secp256k1_pubkey_save(pubnonce, &Q);
123  break;
124  } while(1);
125 
127  if (!ret) {
128  memset(pubnonce, 0, sizeof(*pubnonce));
129  }
130  return ret;
131 }
132 
133 int secp256k1_schnorr_partial_sign(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg32, const unsigned char *sec32, const secp256k1_pubkey *pubnonce_others, const unsigned char *secnonce32) {
134  int overflow = 0;
135  secp256k1_scalar sec, non;
136  secp256k1_ge pubnon;
137  VERIFY_CHECK(ctx != NULL);
139  ARG_CHECK(msg32 != NULL);
140  ARG_CHECK(sig64 != NULL);
141  ARG_CHECK(sec32 != NULL);
142  ARG_CHECK(secnonce32 != NULL);
143  ARG_CHECK(pubnonce_others != NULL);
144 
145  secp256k1_scalar_set_b32(&sec, sec32, &overflow);
146  if (overflow || secp256k1_scalar_is_zero(&sec)) {
147  return -1;
148  }
149  secp256k1_scalar_set_b32(&non, secnonce32, &overflow);
150  if (overflow || secp256k1_scalar_is_zero(&non)) {
151  return -1;
152  }
153  secp256k1_pubkey_load(ctx, &pubnon, pubnonce_others);
154  return secp256k1_schnorr_sig_sign(&ctx->ecmult_gen_ctx, sig64, &sec, &non, &pubnon, secp256k1_schnorr_msghash_sha256, msg32);
155 }
156 
157 int secp256k1_schnorr_partial_combine(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char * const *sig64sin, size_t n) {
158  ARG_CHECK(sig64 != NULL);
159  ARG_CHECK(n >= 1);
160  ARG_CHECK(sig64sin != NULL);
161  return secp256k1_schnorr_sig_combine(sig64, n, sig64sin);
162 }
163 
164 #endif
static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx)
#define VERIFY_CHECK(cond)
Definition: util.h:64
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *a)
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_default
Definition: secp256k1.c:347
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:138
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
static void secp256k1_schnorr_msghash_sha256(unsigned char *h32, const unsigned char *r32, const unsigned char *msg32)
Definition: main_impl.h:13
int secp256k1_schnorr_recover(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *sig64, const unsigned char *msg32)
Definition: main_impl.h:73
int secp256k1_schnorr_generate_nonce_pair(const secp256k1_context *ctx, secp256k1_pubkey *pubnonce, unsigned char *privnonce32, const unsigned char *sec32, const unsigned char *msg32, secp256k1_nonce_function noncefp, const void *noncedata)
Definition: main_impl.h:91
static void secp256k1_sha256_finalize(secp256k1_sha256_t *hash, unsigned char *out32)
static void secp256k1_sha256_write(secp256k1_sha256_t *hash, const unsigned char *data, size_t size)
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:55
#define ARG_CHECK(cond)
Definition: secp256k1.c:23
static secp256k1_context * ctx
Definition: tests.c:42
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
int secp256k1_schnorr_partial_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const unsigned char *sec32, const secp256k1_pubkey *pubnonce_others, const unsigned char *secnonce32)
Definition: main_impl.h:133
static void secp256k1_sha256_initialize(secp256k1_sha256_t *hash)
secp256k1_ecmult_context ecmult_ctx
Definition: secp256k1.c:54
static void secp256k1_scalar_clear(secp256k1_scalar *r)
static int secp256k1_schnorr_sig_combine(unsigned char *sig64, size_t n, const unsigned char *const *sig64ins)
int secp256k1_schnorr_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg32, const secp256k1_pubkey *pubkey)
Definition: main_impl.h:61
static int secp256k1_schnorr_sig_sign(const secp256k1_ecmult_gen_context *ctx, unsigned char *sig64, const secp256k1_scalar *key, const secp256k1_scalar *nonce, const secp256k1_ge *pubnonce, secp256k1_schnorr_msghash hash, const unsigned char *msg32)
int secp256k1_schnorr_partial_combine(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *const *sig64sin, size_t n)
Definition: main_impl.h:157
int secp256k1_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *noncedata)
Definition: main_impl.h:23
int(* secp256k1_nonce_function)(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int attempt)
Definition: secp256k1.h:92
static int count
Definition: tests.c:41
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition: secp256k1.c:119
static int secp256k1_schnorr_sig_verify(const secp256k1_ecmult_context *ctx, const unsigned char *sig64, const secp256k1_ge *pubkey, secp256k1_schnorr_msghash hash, const unsigned char *msg32)
static int secp256k1_schnorr_sig_recover(const secp256k1_ecmult_context *ctx, const unsigned char *sig64, secp256k1_ge *pubkey, secp256k1_schnorr_msghash hash, const unsigned char *msg32)
static const unsigned char secp256k1_schnorr_algo16[17]
Definition: main_impl.h:21
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ctx)