keygen.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. proto.xmr.keygen: Monero public key generation backends for the MMGen suite
  12. """
  13. from ...key import PubKey
  14. from ...keygen import keygen_base
  15. class backend:
  16. class base(keygen_base):
  17. def __init__(self,cfg):
  18. super().__init__(cfg)
  19. from ...proto.xmr.params import mainnet
  20. self.proto_cls = mainnet
  21. from ...util2 import get_keccak
  22. self.keccak_256 = get_keccak(cfg)
  23. def to_viewkey(self,privkey):
  24. return self.proto_cls.preprocess_key(
  25. self.proto_cls,
  26. self.keccak_256(privkey).digest(),
  27. None )
  28. class nacl(base):
  29. production_safe = True
  30. def __init__(self,cfg):
  31. super().__init__(cfg)
  32. from nacl.bindings import crypto_scalarmult_ed25519_base_noclamp
  33. self.scalarmultbase = crypto_scalarmult_ed25519_base_noclamp
  34. def to_pubkey(self,privkey):
  35. return PubKey(
  36. self.scalarmultbase( privkey ) +
  37. self.scalarmultbase( self.to_viewkey(privkey) ),
  38. compressed = privkey.compressed
  39. )
  40. class ed25519(base):
  41. production_safe = False
  42. def __init__(self,cfg):
  43. super().__init__(cfg)
  44. from ...contrib.ed25519 import edwards,encodepoint,B,scalarmult
  45. self.edwards = edwards
  46. self.encodepoint = encodepoint
  47. self.B = B
  48. self.scalarmult = scalarmult
  49. def scalarmultbase(self,privnum):
  50. """
  51. Source and license for scalarmultbase function:
  52. https://github.com/bigreddmachine/MoneroPy/blob/master/moneropy/crypto/ed25519.py
  53. Copyright (c) 2014-2016, The Monero Project
  54. All rights reserved.
  55. """
  56. if privnum == 0:
  57. return [0, 1]
  58. Q = self.scalarmult(self.B, privnum//2)
  59. Q = self.edwards(Q, Q)
  60. if privnum & 1:
  61. Q = self.edwards(Q, self.B)
  62. return Q
  63. @staticmethod
  64. def rev_bytes2int(in_bytes):
  65. return int.from_bytes( in_bytes[::-1], 'big' )
  66. def to_pubkey(self,privkey):
  67. return PubKey(
  68. self.encodepoint( self.scalarmultbase( self.rev_bytes2int(privkey) )) +
  69. self.encodepoint( self.scalarmultbase( self.rev_bytes2int(self.to_viewkey(privkey)) )),
  70. compressed = privkey.compressed
  71. )
  72. class ed25519ll_djbec(ed25519):
  73. def __init__(self,cfg):
  74. super().__init__(cfg)
  75. from ...contrib.ed25519ll_djbec import scalarmult
  76. self.scalarmult = scalarmult