You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
8.2 KiB

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