PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vanitygen.py

https://github.com/BrotherPhil/Bitcoin
Python | 130 lines | 85 code | 18 blank | 27 comment | 8 complexity | 429b036962b62d751e10dd7d650c6992 MD5 | raw file
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2011 Erik Henriksson
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation.
  8. #
  9. # This program 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 General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; see the file COPYING. If not, write to
  16. # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. #
  18. from pywallet import *
  19. from operator import mod
  20. import threading
  21. import time
  22. # Config vars
  23. THREADS = 2
  24. SEARCH_STRING = "1abcd"
  25. # secp256k1
  26. _p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2FL
  27. _r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141L
  28. _b = 0x0000000000000000000000000000000000000000000000000000000000000007L
  29. _a = 0x0000000000000000000000000000000000000000000000000000000000000000L
  30. _Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798L
  31. _Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L
  32. def point_to_public_key(point):
  33. # public keys are 65 bytes long (520 bits)
  34. # 0x04 + 32-byte X-coordinate + 32-byte Y-coordinate
  35. hex_i2o_key = '04' + \
  36. '%064x' % point.x() + \
  37. '%064x' % point.y()
  38. return hex_i2o_key.decode('hex')
  39. def point_to_private_key(point, secret):
  40. # private keys are 279 bytes long (see crypto/ec/cec_asn1.c)
  41. # ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
  42. # ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
  43. # ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
  44. # ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
  45. hex_i2d_key = '308201130201010420' + \
  46. '%064x' % secret + \
  47. 'a081a53081a2020101302c06072a8648ce3d0101022100' + \
  48. '%064x' % _p + \
  49. '3006040100040107044104' + \
  50. '%064x' % _Gx + \
  51. '%064x' % _Gy + \
  52. '022100' + \
  53. '%064x' % _r + \
  54. '020101a14403420004' + \
  55. '%064x' % point.x() + \
  56. '%064x' % point.y()
  57. return hex_i2d_key.decode('hex')
  58. def private_key_to_bc_format(private_key):
  59. h = Hash(private_key)
  60. return b58encode(private_key + h[0:4])
  61. class vanitygen(threading.Thread):
  62. def __init__ (self, generator, secret, search_string):
  63. threading.Thread.__init__(self)
  64. self.generator = generator
  65. self.secret = secret
  66. self.search_string = search_string
  67. self.point = generator * secret
  68. self.count = 0
  69. def run(self):
  70. while (1):
  71. self.point = self.point + self.generator
  72. self.secret += 1
  73. self.count += 1
  74. pubkey = public_key_to_bc_address(point_to_public_key(self.point))
  75. if pubkey.find(self.search_string) == 0:
  76. self.print_keys()
  77. return
  78. def print_keys(self):
  79. print public_key_to_bc_address(point_to_public_key(self.point))
  80. print private_key_to_bc_format(point_to_private_key(self.point, self.secret))
  81. def getCount(self):
  82. return self.count
  83. def main():
  84. curve = CurveFp( _p, _a, _b )
  85. print "Search string: ", SEARCH_STRING
  86. #Init worker threads
  87. workers = []
  88. global count
  89. count = 0
  90. for i in range(0,THREADS):
  91. secret = random.randint(0xFFFFFFFFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
  92. generator = Point( curve, _Gx, _Gy, _r )
  93. worker = vanitygen(generator, secret, SEARCH_STRING)
  94. worker.daemon = True
  95. workers.append(worker)
  96. worker.start()
  97. print "Running vanitygen with %i threads" % THREADS
  98. def getCount():
  99. count = 0
  100. for worker in workers:
  101. count += worker.getCount()
  102. return count
  103. try:
  104. this_count = 0
  105. while threading.active_count():
  106. this_count = getCount()
  107. time.sleep(1)
  108. print "%i Keys/s" % (getCount() - this_count)
  109. except (KeyboardInterrupt, SystemExit):
  110. print '\n! Received keyboard interrupt, quitting threads.\n'
  111. if __name__ == '__main__':
  112. main()