ut_ecc.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_ecc: elliptic curve unit test for the MMGen suite
  4. """
  5. from mmgen.color import gray,pink,blue
  6. from mmgen.proto.secp256k1.secp256k1 import pubkey_gen,pubkey_tweak_add,pubkey_check
  7. from ..include.common import cfg,qmsg,vmsg
  8. from ..include.ecc import pubkey_tweak_add_pyecdsa
  9. from mmgen.protocol import CoinProtocol
  10. secp256k1_group_order = CoinProtocol.Secp256k1.secp256k1_group_order
  11. class unit_tests:
  12. def pubkey_ops(self,name,ut):
  13. vmsg(f' Generating pubkey, adding scalar 123456789 to pubkey:')
  14. pk_addend_bytes = int.to_bytes(123456789,length=32,byteorder='big')
  15. for privkey in (
  16. 'beadcafe' * 8,
  17. f'{1:064x}',
  18. f'{secp256k1_group_order-1:x}',
  19. ):
  20. vmsg(f' privkey = 0x{privkey}')
  21. for compressed,length in ((False,65),(True,33)):
  22. vmsg(f' {compressed=}')
  23. pubkey_bytes = pubkey_gen(bytes.fromhex(privkey),int(compressed))
  24. pubkey_check(pubkey_bytes)
  25. vmsg(f' pubkey: {pubkey_bytes.hex()}')
  26. res1 = pubkey_tweak_add(pubkey_bytes, pk_addend_bytes)
  27. pubkey_check(res1)
  28. vmsg(f' tweaked: {res1.hex()}')
  29. res2 = pubkey_tweak_add_pyecdsa(pubkey_bytes,pk_addend_bytes)
  30. pubkey_check(res2)
  31. assert len(res1) == length
  32. assert res1 == res2
  33. return True
  34. def pubkey_errors(self,name,ut):
  35. def gen1(): pubkey_gen(bytes(32),1)
  36. def gen2(): pubkey_gen(secp256k1_group_order.to_bytes(length=32,byteorder='big'),1)
  37. def gen3(): pubkey_gen((secp256k1_group_order+1).to_bytes(length=32,byteorder='big'),1)
  38. def gen4(): pubkey_gen(bytes.fromhex('ff'*32),1)
  39. def gen5(): pubkey_gen(bytes.fromhex('ab'*31),1)
  40. def gen6(): pubkey_gen(bytes.fromhex('ab'*33),1)
  41. pubkey_bytes = pubkey_gen(bytes.fromhex('beadcafe'*8), 1)
  42. def tweak1(): pubkey_tweak_add(pubkey_bytes,bytes(32))
  43. def tweak2(): pubkey_tweak_add(bytes.fromhex('03'*64),int.to_bytes(1,length=32,byteorder='big'))
  44. def check1(): pubkey_check(bytes.fromhex('04'*33))
  45. def check2(): pubkey_check(bytes.fromhex('03'*65))
  46. def check3(): pubkey_check(bytes.fromhex('02'*65))
  47. def check4(): pubkey_check(bytes.fromhex('03'*64))
  48. def check5(): pubkey_check(b'')
  49. bad_data = (
  50. ('privkey == 0', 'ValueError', 'Private key not in allowable range', gen1),
  51. ('privkey == group order', 'ValueError', 'Private key not in allowable range', gen2),
  52. ('privkey == group order+1', 'ValueError', 'Private key not in allowable range', gen3),
  53. ('privkey == 2^256-1', 'ValueError', 'Private key not in allowable range', gen4),
  54. ('len(privkey) == 31', 'ValueError', 'Private key length not 32 bytes', gen5),
  55. ('len(privkey) == 33', 'ValueError', 'Private key length not 32 bytes', gen6),
  56. ('tweak == 0', 'ValueError', 'Tweak not in allowable range', tweak1),
  57. ('pubkey length == 64', 'ValueError', 'Serialized public key length not', tweak2),
  58. ('invalid pubkey (33 bytes)', 'ValueError', 'Invalid first byte', check1),
  59. ('invalid pubkey (65 bytes)', 'ValueError', 'Invalid first byte', check2),
  60. ('invalid pubkey (65 bytes)', 'ValueError', 'Invalid first byte', check3),
  61. ('pubkey length == 64', 'ValueError', 'Serialized public key length not', check4),
  62. ('pubkey length == 0', 'ValueError', 'Serialized public key length not', check5),
  63. )
  64. ut.process_bad_data(bad_data,pfx='')
  65. return True