mktable.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include "bignum.h"
  4. #include "bip32.h"
  5. #include "ecdsa.h"
  6. #include "rand.h"
  7. /*
  8. * This program prints the contents of the ecdsa_curve.cp array.
  9. * The entry cp[i][j] contains the number (2*j+1)*16^i*G,
  10. * where G is the generator of the specified elliptic curve.
  11. */
  12. int main(int argc, char **argv) {
  13. int i, j, k;
  14. if (argc != 2) {
  15. printf("Usage: %s CURVE_NAME\n", argv[0]);
  16. return 1;
  17. }
  18. const char *name = argv[1];
  19. const curve_info *info = get_curve_by_name(name);
  20. if (info == 0) {
  21. printf("Unknown curve '%s'\n", name);
  22. return 1;
  23. }
  24. const ecdsa_curve *curve = info->params;
  25. if (curve == 0) {
  26. printf("Unknown curve params");
  27. return 1;
  28. }
  29. curve_point ng = curve->G;
  30. curve_point pow2ig = curve->G;
  31. for (i = 0; i < 64; i++) {
  32. // invariants:
  33. // pow2ig = 16^i * G
  34. // ng = pow2ig
  35. printf("\t{\n");
  36. for (j = 0; j < 8; j++) {
  37. // invariants:
  38. // pow2ig = 16^i * G
  39. // ng = (2*j+1) * 16^i * G
  40. #ifndef NDEBUG
  41. curve_point checkresult;
  42. bignum256 a;
  43. bn_zero(&a);
  44. a.val[(4 * i) / BN_BITS_PER_LIMB] = ((uint32_t)2 * j + 1)
  45. << ((4 * i) % BN_BITS_PER_LIMB);
  46. bn_normalize(&a);
  47. point_multiply(curve, &a, &curve->G, &checkresult);
  48. assert(point_is_equal(&checkresult, &ng));
  49. #endif
  50. printf("\t\t/* %2d*16^%d*G: */\n\t\t{{{", 2 * j + 1, i);
  51. // print x coordinate
  52. for (k = 0; k < 9; k++) {
  53. printf((k < 8 ? "0x%08x, " : "0x%04x"), ng.x.val[k]);
  54. }
  55. printf("}},\n\t\t {{");
  56. // print y coordinate
  57. for (k = 0; k < 9; k++) {
  58. printf((k < 8 ? "0x%08x, " : "0x%04x"), ng.y.val[k]);
  59. }
  60. if (j == 7) {
  61. printf("}}}\n\t},\n");
  62. } else {
  63. printf("}}},\n");
  64. point_add(curve, &pow2ig, &ng);
  65. }
  66. point_add(curve, &pow2ig, &ng);
  67. }
  68. pow2ig = ng;
  69. }
  70. return 0;
  71. }