test-internals.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Tor: Removed, file is inclued in ed25519.c instead. */
  2. /* #include <stdio.h> */
  3. /* #include "ed25519-donna.h" */
  4. static int
  5. test_adds(void) {
  6. #if defined(HAVE_UINT128) && !defined(ED25519_SSE2)
  7. /* largest result for each limb from a mult or square: all elements except r1 reduced, r1 overflowed as far as possible */
  8. static const bignum25519 max_bignum = {
  9. 0x7ffffffffffff,0x8000000001230,0x7ffffffffffff,0x7ffffffffffff,0x7ffffffffffff
  10. };
  11. #if 0
  12. /* what max_bignum should fully reduce to */
  13. static const unsigned char max_bignum_raw[32] = {
  14. 0x12,0x00,0x00,0x00,0x00,0x00,0x88,0x91,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  16. };
  17. #endif
  18. /* (max_bignum + max_bignum)^2 */
  19. static const unsigned char max_bignum2_squared_raw[32] = {
  20. 0x10,0x05,0x00,0x00,0x00,0x00,0x80,0xdc,0x51,0x00,0x00,0x00,0x00,0x61,0xed,0x4a,
  21. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  22. };
  23. /* ((max_bignum + max_bignum) + max_bignum)^2 */
  24. static const unsigned char max_bignum3_squared_raw[32] = {
  25. 0x64,0x0b,0x00,0x00,0x00,0x00,0x20,0x30,0xb8,0x00,0x00,0x00,0x40,0x1a,0x96,0xe8,
  26. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27. };
  28. #else
  29. /* largest result for each limb from a mult or square: all elements except r1 reduced, r1 overflowed as far as possible */
  30. static const bignum25519 ALIGN(16) max_bignum = {
  31. 0x3ffffff,0x2000300,0x3ffffff,0x1ffffff,0x3ffffff,
  32. 0x1ffffff,0x3ffffff,0x1ffffff,0x3ffffff,0x1ffffff
  33. };
  34. /* what max_bignum should fully reduce to */
  35. static const unsigned char max_bignum2_squared_raw[32] = {
  36. 0x10,0x05,0x00,0x40,0xc2,0x06,0x40,0x80,0x41,0x02,0x00,0x00,0x00,0x00,0x00,0x00,
  37. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38. };
  39. /* (max_bignum * max_bignum) */
  40. static const unsigned char max_bignum3_squared_raw[32] = {
  41. 0x64,0x0b,0x00,0x10,0x35,0x0f,0x90,0x60,0x13,0x05,0x00,0x00,0x00,0x00,0x00,0x00,
  42. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43. };
  44. #endif
  45. unsigned char result[32];
  46. /* static const bignum25519 ALIGN(16) zero = {0}; */
  47. bignum25519 ALIGN(16) a, b /* , c */;
  48. /* size_t i; */
  49. /* a = (max_bignum + max_bignum) */
  50. curve25519_add(a, max_bignum, max_bignum);
  51. /* b = ((max_bignum + max_bignum) * (max_bignum + max_bignum)) */
  52. curve25519_mul(b, a, a);
  53. curve25519_contract(result, b);
  54. if (memcmp(result, max_bignum2_squared_raw, 32) != 0)
  55. return -1;
  56. curve25519_square(b, a);
  57. curve25519_contract(result, b);
  58. if (memcmp(result, max_bignum2_squared_raw, 32) != 0)
  59. return -1;
  60. /* b = (max_bignum + max_bignum + max_bignum) */
  61. curve25519_add_after_basic(b, a, max_bignum);
  62. /* a = ((max_bignum + max_bignum + max_bignum) * (max_bignum + max_bignum + max_bignum)) */
  63. curve25519_mul(a, b, b);
  64. curve25519_contract(result, a);
  65. if (memcmp(result, max_bignum3_squared_raw, 32) != 0)
  66. return -1;
  67. curve25519_square(a, b);
  68. curve25519_contract(result, a);
  69. if (memcmp(result, max_bignum3_squared_raw, 32) != 0)
  70. return -1;
  71. return 0;
  72. }
  73. static int
  74. test_subs(void) {
  75. #if defined(HAVE_UINT128) && !defined(ED25519_SSE2)
  76. /* largest result for each limb from a mult or square: all elements except r1 reduced, r1 overflowed as far as possible */
  77. static const bignum25519 max_bignum = {
  78. 0x7ffffffffffff,0x8000000001230,0x7ffffffffffff,0x7ffffffffffff,0x7ffffffffffff
  79. };
  80. /* what max_bignum should fully reduce to */
  81. static const unsigned char max_bignum_raw[32] = {
  82. 0x12,0x00,0x00,0x00,0x00,0x00,0x88,0x91,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  83. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  84. };
  85. /* (max_bignum * max_bignum) */
  86. static const unsigned char max_bignum_squared_raw[32] = {
  87. 0x44,0x01,0x00,0x00,0x00,0x00,0x20,0x77,0x14,0x00,0x00,0x00,0x40,0x58,0xbb,0x52,
  88. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  89. };
  90. #else
  91. /* largest result for each limb from a mult or square: all elements except r1 reduced, r1 overflowed as far as possible */
  92. static const bignum25519 ALIGN(16) max_bignum = {
  93. 0x3ffffff,0x2000300,0x3ffffff,0x1ffffff,0x3ffffff,
  94. 0x1ffffff,0x3ffffff,0x1ffffff,0x3ffffff,0x1ffffff
  95. };
  96. /* what max_bignum should fully reduce to */
  97. static const unsigned char max_bignum_raw[32] = {
  98. 0x12,0x00,0x00,0x04,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  99. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  100. };
  101. /* (max_bignum * max_bignum) */
  102. static const unsigned char max_bignum_squared_raw[32] = {
  103. 0x44,0x01,0x00,0x90,0xb0,0x01,0x10,0x60,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  104. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  105. };
  106. #endif
  107. unsigned char result[32];
  108. static const bignum25519 ALIGN(16) zero = {0};
  109. bignum25519 ALIGN(16) a, b /* , c */;
  110. /* size_t i; */
  111. /* a = max_bignum - 0, which expands to 2p + max_bignum - 0 */
  112. curve25519_sub(a, max_bignum, zero);
  113. curve25519_contract(result, a);
  114. if (memcmp(result, max_bignum_raw, 32) != 0)
  115. return -1;
  116. /* b = (max_bignum * max_bignum) */
  117. curve25519_mul(b, a, a);
  118. curve25519_contract(result, b);
  119. if (memcmp(result, max_bignum_squared_raw, 32) != 0)
  120. return -1;
  121. curve25519_square(b, a);
  122. curve25519_contract(result, b);
  123. if (memcmp(result, max_bignum_squared_raw, 32) != 0)
  124. return -1;
  125. /* b = ((a - 0) - 0) */
  126. curve25519_sub_after_basic(b, a, zero);
  127. curve25519_contract(result, b);
  128. if (memcmp(result, max_bignum_raw, 32) != 0)
  129. return -1;
  130. /* a = (max_bignum * max_bignum) */
  131. curve25519_mul(a, b, b);
  132. curve25519_contract(result, a);
  133. if (memcmp(result, max_bignum_squared_raw, 32) != 0)
  134. return -1;
  135. curve25519_square(a, b);
  136. curve25519_contract(result, a);
  137. if (memcmp(result, max_bignum_squared_raw, 32) != 0)
  138. return -1;
  139. return 0;
  140. }
  141. /* Tor: Removed, tests are invoked as a function instead. */
  142. #if 0
  143. int
  144. main() {
  145. int ret = 0;
  146. int single;
  147. single = test_adds();
  148. if (single) printf("test_adds: FAILED\n");
  149. ret |= single;
  150. single = test_subs();
  151. if (single) printf("test_subs: FAILED\n");
  152. ret |= single;
  153. if (!ret) printf("success\n");
  154. return ret;
  155. }
  156. #endif
  157. /* Tor: Added for initialization self-testing. */
  158. int
  159. ed25519_donna_selftest(void)
  160. {
  161. int ret = 0;
  162. ret |= test_adds();
  163. ret |= test_subs();
  164. return (ret == 0) ? 0 : -1;
  165. }