notes.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package secp256k1
  17. /*
  18. <HaltingState> sipa, int secp256k1_ecdsa_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed);
  19. <HaltingState> is that how i generate private/public keys?
  20. <sipa> HaltingState: you pass in a random 32-byte string as seckey
  21. <sipa> HaltingState: if it is valid, the corresponding pubkey is put in pubkey
  22. <sipa> and true is returned
  23. <sipa> otherwise, false is returned
  24. <sipa> around 1 in 2^128 32-byte strings are invalid, so the odds of even ever seeing one is extremely rare
  25. <sipa> private keys are mathematically numbers
  26. <sipa> each has a corresponding point on the curve as public key
  27. <sipa> a private key is just a number
  28. <sipa> a public key is a point with x/y coordinates
  29. <sipa> almost every 256-bit number is a valid private key (one with a point on the curve corresponding to it)
  30. <sipa> HaltingState: ok?
  31. <sipa> more than half of random points are not on the curve
  32. <sipa> and actually, it is less than the square root, not less than half, sorry :)
  33. !!!
  34. <sipa> a private key is a NUMBER
  35. <sipa> a public key is a POINT
  36. <gmaxwell> half the x,y values in the field are not on the curve, a private key is an integer.
  37. <sipa> HaltingState: yes, n,q = private keys; N,Q = corresponding public keys (N=n*G, Q=q*G); then it follows that n*Q = n*q*G = q*n*G = q*N
  38. <sipa> that's the reason ECDH works
  39. <sipa> multiplication is associative and commutativ
  40. */
  41. /*
  42. <HaltingState> sipa, ok; i am doing compact signatures and I want to know; can someone change the signature to get another valid signature for same message without the private key
  43. <HaltingState> because i know they can do that for the normal 72 byte signatures that openssl was putting out
  44. <sipa> HaltingState: if you don't enforce non-malleability, yes
  45. <sipa> HaltingState: if you force the highest bit of t
  46. <sipa> it _creates_ signatures that already satisfy that condition
  47. <sipa> but it will accept ones that don't
  48. <sipa> maybe i should change that, and be strict
  49. <HaltingState> yes; i want some way to know signature is valid but fails malleability
  50. <sipa> well if the highest bit of S is 1, you can take its complement
  51. <sipa> and end up with a valid signature
  52. <sipa> that is canonical
  53. */
  54. /*
  55. <HaltingState> sipa, I am signing messages and highest bit of the compact signature is 1!!!
  56. <HaltingState> if (b & 0x80) == 0x80 {
  57. <HaltingState> log.Panic("b= %v b2= %v \n", b, b&0x80)
  58. <HaltingState> }
  59. <sipa> what bit?
  60. * Pengoo has quit (Ping timeout: 272 seconds)
  61. <HaltingState> the highest bit of the first byte of signature
  62. <sipa> it's the highest bit of S
  63. <sipa> so the 32nd byte
  64. <HaltingState> wtf
  65. */
  66. /*
  67. For instance, nonces are used in HTTP digest access authentication to calculate an MD5 digest
  68. of the password. The nonces are different each time the 401 authentication challenge
  69. response code is presented, thus making replay attacks virtually impossible.
  70. can verify client/server match without sending password over network
  71. */
  72. /*
  73. <hanihani> one thing I dont get about armory for instance,
  74. is how the hot-wallet can generate new addresses without
  75. knowing the master key
  76. */
  77. /*
  78. <HaltingState> i am yelling at the telehash people for using secp256r1
  79. instead of secp256k1; they thing r1 is "more secure" despite fact that
  80. there is no implementation that works and wrapping it is now taking
  81. up massive time, lol
  82. <gmaxwell> ...
  83. <gmaxwell> You know that the *r curves are selected via an undisclosed
  84. secret process, right?
  85. <gmaxwell> HaltingState: telehash is offtopic for this channel.
  86. */
  87. /*
  88. For instance, nonces are used in HTTP digest access authentication to calculate an MD5 digest
  89. of the password. The nonces are different each time the 401 authentication challenge
  90. response code is presented, thus making replay attacks virtually impossible.
  91. can verify client/server match without sending password over network
  92. */
  93. /*
  94. void secp256k1_start(void);
  95. void secp256k1_stop(void);
  96. * Verify an ECDSA signature.
  97. * Returns: 1: correct signature
  98. * 0: incorrect signature
  99. * -1: invalid public key
  100. * -2: invalid signature
  101. *
  102. int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen,
  103. const unsigned char *sig, int siglen,
  104. const unsigned char *pubkey, int pubkeylen);
  105. http://www.nilsschneider.net/2013/01/28/recovering-bitcoin-private-keys.html
  106. Why did this work? ECDSA requires a random number for each signature. If this random
  107. number is ever used twice with the same private key it can be recovered.
  108. This transaction was generated by a hardware bitcoin wallet using a pseudo-random number
  109. generator that was returning the same “random” number every time.
  110. Nonce is 32 bytes?
  111. * Create an ECDSA signature.
  112. * Returns: 1: signature created
  113. * 0: nonce invalid, try another one
  114. * In: msg: the message being signed
  115. * msglen: the length of the message being signed
  116. * seckey: pointer to a 32-byte secret key (assumed to be valid)
  117. * nonce: pointer to a 32-byte nonce (generated with a cryptographic PRNG)
  118. * Out: sig: pointer to a 72-byte array where the signature will be placed.
  119. * siglen: pointer to an int, which will be updated to the signature length (<=72).
  120. *
  121. int secp256k1_ecdsa_sign(const unsigned char *msg, int msglen,
  122. unsigned char *sig, int *siglen,
  123. const unsigned char *seckey,
  124. const unsigned char *nonce);
  125. * Create a compact ECDSA signature (64 byte + recovery id).
  126. * Returns: 1: signature created
  127. * 0: nonce invalid, try another one
  128. * In: msg: the message being signed
  129. * msglen: the length of the message being signed
  130. * seckey: pointer to a 32-byte secret key (assumed to be valid)
  131. * nonce: pointer to a 32-byte nonce (generated with a cryptographic PRNG)
  132. * Out: sig: pointer to a 64-byte array where the signature will be placed.
  133. * recid: pointer to an int, which will be updated to contain the recovery id.
  134. *
  135. int secp256k1_ecdsa_sign_compact(const unsigned char *msg, int msglen,
  136. unsigned char *sig64,
  137. const unsigned char *seckey,
  138. const unsigned char *nonce,
  139. int *recid);
  140. * Recover an ECDSA public key from a compact signature.
  141. * Returns: 1: public key succesfully recovered (which guarantees a correct signature).
  142. * 0: otherwise.
  143. * In: msg: the message assumed to be signed
  144. * msglen: the length of the message
  145. * compressed: whether to recover a compressed or uncompressed pubkey
  146. * recid: the recovery id (as returned by ecdsa_sign_compact)
  147. * Out: pubkey: pointer to a 33 or 65 byte array to put the pubkey.
  148. * pubkeylen: pointer to an int that will contain the pubkey length.
  149. *
  150. recovery id is between 0 and 3
  151. int secp256k1_ecdsa_recover_compact(const unsigned char *msg, int msglen,
  152. const unsigned char *sig64,
  153. unsigned char *pubkey, int *pubkeylen,
  154. int compressed, int recid);
  155. * Verify an ECDSA secret key.
  156. * Returns: 1: secret key is valid
  157. * 0: secret key is invalid
  158. * In: seckey: pointer to a 32-byte secret key
  159. *
  160. int secp256k1_ecdsa_seckey_verify(const unsigned char *seckey);
  161. ** Just validate a public key.
  162. * Returns: 1: valid public key
  163. * 0: invalid public key
  164. *
  165. int secp256k1_ecdsa_pubkey_verify(const unsigned char *pubkey, int pubkeylen);
  166. ** Compute the public key for a secret key.
  167. * In: compressed: whether the computed public key should be compressed
  168. * seckey: pointer to a 32-byte private key.
  169. * Out: pubkey: pointer to a 33-byte (if compressed) or 65-byte (if uncompressed)
  170. * area to store the public key.
  171. * pubkeylen: pointer to int that will be updated to contains the pubkey's
  172. * length.
  173. * Returns: 1: secret was valid, public key stores
  174. * 0: secret was invalid, try again.
  175. *
  176. int secp256k1_ecdsa_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed);
  177. */