You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

204 lines
5.8 KiB

  1. /**
  2. * @file test_decaf.cxx
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * @brief C++ tests, because that's easier.
  10. */
  11. #include "decaf.hxx"
  12. #include "shake.hxx"
  13. #include <stdio.h>
  14. typedef decaf::decaf<448>::Scalar Scalar;
  15. typedef decaf::decaf<448>::Point Point;
  16. typedef decaf::decaf<448>::Precomputed Precomputed;
  17. static const long NTESTS = 10000;
  18. static void print(const char *name, const Scalar &x) {
  19. unsigned char buffer[DECAF_448_SCALAR_BYTES];
  20. x.encode(buffer);
  21. printf(" %s = 0x", name);
  22. for (int i=sizeof(buffer)-1; i>=0; i--) {
  23. printf("%02x", buffer[i]);
  24. }
  25. printf("\n");
  26. }
  27. static void print(const char *name, const Point &x) {
  28. unsigned char buffer[DECAF_448_SER_BYTES];
  29. x.encode(buffer);
  30. printf(" %s = 0x", name);
  31. for (int i=sizeof(buffer)-1; i>=0; i--) {
  32. printf("%02x", buffer[i]);
  33. }
  34. printf("\n");
  35. }
  36. static bool passing = true;
  37. class Test {
  38. public:
  39. bool passing_now;
  40. Test(const char *test) {
  41. passing_now = true;
  42. printf("%s...", test);
  43. if (strlen(test) < 27) printf("%*s",int(27-strlen(test)),"");
  44. fflush(stdout);
  45. }
  46. ~Test() {
  47. if (std::uncaught_exception()) {
  48. fail();
  49. printf(" due to uncaught exception.\n");
  50. }
  51. if (passing_now) printf("[PASS]\n");
  52. }
  53. void fail() {
  54. if (!passing_now) return;
  55. passing_now = passing = false;
  56. printf("[FAIL]\n");
  57. }
  58. };
  59. static bool arith_check(
  60. Test &test,
  61. const Scalar &x,
  62. const Scalar &y,
  63. const Scalar &z,
  64. const Scalar &r,
  65. const Scalar &l,
  66. const char *name
  67. ) {
  68. if (l == r) return true;
  69. test.fail();
  70. printf(" %s", name);
  71. print("x", x);
  72. print("y", y);
  73. print("z", z);
  74. print("lhs", r);
  75. print("rhs", l);
  76. return false;
  77. }
  78. static bool point_check(
  79. Test &test,
  80. const Point &p,
  81. const Point &q,
  82. const Point &R,
  83. const Scalar &x,
  84. const Scalar &y,
  85. const Point &l,
  86. const Point &r,
  87. const char *name
  88. ) {
  89. bool good = l==r;
  90. if (!p.validate()) { good = false; printf(" p invalid\n"); }
  91. if (!q.validate()) { good = false; printf(" q invalid\n"); }
  92. if (!r.validate()) { good = false; printf(" r invalid\n"); }
  93. if (!l.validate()) { good = false; printf(" l invalid\n"); }
  94. if (good) return true;
  95. test.fail();
  96. printf(" %s", name);
  97. print("x", x);
  98. print("y", y);
  99. print("p", p);
  100. print("q", q);
  101. print("r", R);
  102. print("lhs", r);
  103. print("rhs", l);
  104. return false;
  105. }
  106. static void test_arithmetic() {
  107. decaf::SpongeRng rng(decaf::Block("test_arithmetic"));
  108. Test test("Arithmetic");
  109. Scalar x(0),y(0),z(0);
  110. arith_check(test,x,y,z,INT_MAX,(decaf_word_t)INT_MAX,"cast from max");
  111. arith_check(test,x,y,z,INT_MIN,-Scalar(1+(decaf_word_t)INT_MAX),"cast from min");
  112. for (int i=0; i<NTESTS*10 && test.passing_now; i++) {
  113. /* TODO: pathological cases */
  114. size_t sob = DECAF_448_SCALAR_BYTES + 8 - (i%16);
  115. Scalar x(rng.read(sob));
  116. Scalar y(rng.read(sob));
  117. Scalar z(rng.read(sob));
  118. arith_check(test,x,y,z,x+y,y+x,"commute add");
  119. arith_check(test,x,y,z,x,x+0,"ident add");
  120. arith_check(test,x,y,z,x,x-0,"ident sub");
  121. arith_check(test,x,y,z,x+(y+z),(x+y)+z,"assoc add");
  122. arith_check(test,x,y,z,x*(y+z),x*y + x*z,"distributive mul/add");
  123. arith_check(test,x,y,z,x*(y-z),x*y - x*z,"distributive mul/add");
  124. arith_check(test,x,y,z,x*(y*z),(x*y)*z,"assoc mul");
  125. arith_check(test,x,y,z,x*y,y*x,"commute mul");
  126. arith_check(test,x,y,z,x,x*1,"ident mul");
  127. arith_check(test,x,y,z,0,x*0,"mul by 0");
  128. arith_check(test,x,y,z,-x,x*-1,"mul by -1");
  129. arith_check(test,x,y,z,x+x,x*2,"mul by 2");
  130. if (i%20) continue;
  131. if (y!=0) arith_check(test,x,y,z,x*y/y,x,"invert");
  132. arith_check(test,x,y,z,x/0,0,"invert0");
  133. }
  134. }
  135. static void test_ec() {
  136. decaf::SpongeRng rng(decaf::Block("test_ec"));
  137. Test test("EC");
  138. Point id = Point::identity(), base = Point::base();
  139. point_check(test,id,id,id,0,0,Point::from_hash(""),id,"fh0");
  140. point_check(test,id,id,id,0,0,Point::from_hash("\x01"),id,"fh1");
  141. for (int i=0; i<NTESTS && test.passing_now; i++) {
  142. /* TODO: pathological cases */
  143. Scalar x(rng);
  144. Scalar y(rng);
  145. Point p(rng);
  146. Point q(rng);
  147. decaf::SecureBuffer buffer(2*Point::HASH_BYTES);
  148. rng.read(buffer);
  149. Point r = Point::from_hash(buffer);
  150. point_check(test,p,q,r,0,0,p,Point((decaf::SecureBuffer)p),"round-trip");
  151. point_check(test,p,q,r,0,0,p+q,q+p,"commute add");
  152. point_check(test,p,q,r,0,0,p+(q+r),(p+q)+r,"assoc add");
  153. if (i%10) continue;
  154. point_check(test,p,q,r,x,0,x*(p+q),x*p+x*q,"distr mul");
  155. point_check(test,p,q,r,x,y,(x*y)*p,x*(y*p),"assoc mul");
  156. point_check(test,p,q,r,x,y,x*p+y*q,Point::double_scalarmul(x,p,y,q),"ds mul");
  157. point_check(test,base,q,r,x,y,x*base+y*q,q.non_secret_combo_with_base(y,x),"ds vt mul");
  158. point_check(test,p,q,r,x,0,Precomputed(p)*x,p*x,"precomp mul");
  159. point_check(test,p,q,r,0,0,r,
  160. Point::from_hash(buffer.slice(0,Point::HASH_BYTES))
  161. + Point::from_hash(buffer.slice(Point::HASH_BYTES,Point::HASH_BYTES)),
  162. "unih = hash+add"
  163. );
  164. point_check(test,p,q,r,x,0,Point(x.direct_scalarmul(decaf::SecureBuffer(p))),x*p,"direct mul");
  165. }
  166. }
  167. int main(int argc, char **argv) {
  168. (void) argc; (void) argv;
  169. test_arithmetic();
  170. test_ec();
  171. if (passing) printf("Passed all tests.\n");
  172. return passing ? 0 : 1;
  173. }