test-internals.c 5.9 KB

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