|  | @@ -14,15 +14,19 @@ | 
														
													
														
															
																|  |  | #include <decaf/crypto.h> |  |  | #include <decaf/crypto.h> | 
														
													
														
															
																|  |  | #include <decaf/crypto.hxx> |  |  | #include <decaf/crypto.hxx> | 
														
													
														
															
																|  |  | #include <stdio.h> |  |  | #include <stdio.h> | 
														
													
														
															
																|  |  | #include <memcheck.h> |  |  |  | 
														
													
														
															
																|  |  |  |  |  | #include <valgrind/memcheck.h> | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  | using namespace decaf; |  |  | using namespace decaf; | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  | static const long NTESTS = 100; |  |  |  | 
														
													
														
															
																|  |  |  |  |  | static const long NTESTS = 1; | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  | const char *undef_str = "Valgrind thinks this string is undefined." |  |  |  | 
														
													
														
															
																|  |  |  |  |  | const char *undef_str = "Valgrind thinks this string is undefined."; | 
														
													
														
															
																|  |  | const Block undef_block(undef_str); |  |  | const Block undef_block(undef_str); | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  |  |  |  | static inline void ignore(decaf_error_t x) { | 
														
													
														
															
																|  |  |  |  |  | (void)x; | 
														
													
														
															
																|  |  |  |  |  | } | 
														
													
														
															
																|  |  |  |  |  | 
 | 
														
													
														
															
																|  |  | template<typename Group> struct Tests { |  |  | template<typename Group> struct Tests { | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  | typedef typename Group::Scalar Scalar; |  |  | typedef typename Group::Scalar Scalar; | 
														
													
												
													
														
															
																|  | @@ -31,27 +35,26 @@ typedef typename Group::Precomputed Precomputed; | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  | static void test_arithmetic() { |  |  | static void test_arithmetic() { | 
														
													
														
															
																|  |  | SpongeRng rng(Block("test_arithmetic")); |  |  | SpongeRng rng(Block("test_arithmetic")); | 
														
													
														
															
																|  |  | rng.stir(undef_str); |  |  |  | 
														
													
														
															
																|  |  |  |  |  | rng.stir(undef_block); | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | Test test("Arithmetic"); |  |  |  | 
														
													
														
															
																|  |  | Scalar x(rng),y(rng),z; |  |  | Scalar x(rng),y(rng),z; | 
														
													
														
															
																|  |  | FixedBlock<Group::Scalar::SER_BYTES> Ser; |  |  |  | 
														
													
														
															
																|  |  |  |  |  | uint8_t ser[Group::Scalar::SER_BYTES]; | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | for (int i=0; i<NTESTS; i++) { |  |  | for (int i=0; i<NTESTS; i++) { | 
														
													
														
															
																|  |  | (void)(x+y); |  |  | (void)(x+y); | 
														
													
														
															
																|  |  | (void)(x-y); |  |  | (void)(x-y); | 
														
													
														
															
																|  |  | (void)(x*y); |  |  | (void)(x*y); | 
														
													
														
															
																|  |  | (void)(x/y); // Probably fails? |  |  |  | 
														
													
														
															
																|  |  |  |  |  | //(void)(x/y); // TODO: Fails due to zero check, but needs to be tested anyway. | 
														
													
														
															
																|  |  | (void)(x==y); |  |  | (void)(x==y); | 
														
													
														
															
																|  |  | (void)(z=y); |  |  | (void)(z=y); | 
														
													
														
															
																|  |  | x.serialize(ser); |  |  |  | 
														
													
														
															
																|  |  |  |  |  | x.serializeInto(ser); | 
														
													
														
															
																|  |  | x = y; |  |  | x = y; | 
														
													
														
															
																|  |  | } |  |  | } | 
														
													
														
															
																|  |  | } |  |  | } | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  | static void test_elligator() { |  |  | static void test_elligator() { | 
														
													
														
															
																|  |  | SpongeRng rng(Block("test_elligator")); |  |  | SpongeRng rng(Block("test_elligator")); | 
														
													
														
															
																|  |  | rng.stir(undef_str); |  |  |  | 
														
													
														
															
																|  |  |  |  |  | rng.stir(undef_block); | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | for (int i=0; i<NTESTS; i++) { |  |  | for (int i=0; i<NTESTS; i++) { | 
														
													
														
															
																|  |  | Point x(rng); |  |  | Point x(rng); | 
														
													
												
													
														
															
																|  | @@ -62,12 +65,16 @@ static void test_elligator() { | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  | static void test_ec() { |  |  | static void test_ec() { | 
														
													
														
															
																|  |  | SpongeRng rng(Block("test_ec")); |  |  | SpongeRng rng(Block("test_ec")); | 
														
													
														
															
																|  |  | rng.stir(undef_str); |  |  |  | 
														
													
														
															
																|  |  |  |  |  | rng.stir(undef_block); | 
														
													
														
															
																|  |  |  |  |  | 
 | 
														
													
														
															
																|  |  |  |  |  | uint8_t ser[Group::Point::SER_BYTES]; | 
														
													
														
															
																|  |  | 
 |  |  | 
 | 
														
													
														
															
																|  |  | for (int i=0; i<NTESTS; i++) { |  |  | for (int i=0; i<NTESTS; i++) { | 
														
													
														
															
																|  |  | Scalar y(rng),z(rng); |  |  | Scalar y(rng),z(rng); | 
														
													
														
															
																|  |  | Point p(rng),q(rng),r; |  |  | Point p(rng),q(rng),r; | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  |  |  |  | 
 | 
														
													
														
															
																|  |  |  |  |  | p.serializeInto(ser); | 
														
													
														
															
																|  |  |  |  |  | ignore(Group::Point::decode(p,FixedBlock<Group::Point::SER_BYTES>(ser))); | 
														
													
														
															
																|  |  | (void)(p*y); |  |  | (void)(p*y); | 
														
													
														
															
																|  |  | (void)(p+q); |  |  | (void)(p+q); | 
														
													
														
															
																|  |  | (void)(p-q); |  |  | (void)(p-q); | 
														
													
												
													
														
															
																|  | @@ -75,10 +82,10 @@ static void test_ec() { | 
														
													
														
															
																|  |  | (void)(p.times_two()); |  |  | (void)(p.times_two()); | 
														
													
														
															
																|  |  | (void)(p==q); |  |  | (void)(p==q); | 
														
													
														
															
																|  |  | (void)(p.debugging_torque()); |  |  | (void)(p.debugging_torque()); | 
														
													
														
															
																|  |  | (void)(p.non_secret_combo_with_base(y,z)); // Should fail |  |  |  | 
														
													
														
															
																|  |  |  |  |  | //(void)(p.non_secret_combo_with_base(y,z)); // Should fail | 
														
													
														
															
																|  |  | (void)(Precomputed(p)*y); |  |  | (void)(Precomputed(p)*y); | 
														
													
														
															
																|  |  | p.dual_scalarmul(q,r,y,z); |  |  | p.dual_scalarmul(q,r,y,z); | 
														
													
														
															
																|  |  | p.double_scalarmul(q,r,y,z); |  |  |  | 
														
													
														
															
																|  |  |  |  |  | Group::Point::double_scalarmul(p,y,q,z); | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | } |  |  | } | 
														
													
														
															
																|  |  | } |  |  | } | 
														
													
												
													
														
															
																|  | @@ -107,7 +114,5 @@ int main(int argc, char **argv) { | 
														
													
														
															
																|  |  | Tests<Ed448Goldilocks>::test_ec(); |  |  | Tests<Ed448Goldilocks>::test_ec(); | 
														
													
														
															
																|  |  | Tests<Ed448Goldilocks>::test_crypto(); |  |  | Tests<Ed448Goldilocks>::test_crypto(); | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | if (passing) printf("Passed all tests.\n"); |  |  |  | 
														
													
														
															
																|  |  |  |  |  |  | 
														
													
														
															
																|  |  | return passing ? 0 : 1; |  |  |  | 
														
													
														
															
																|  |  |  |  |  | return 0; | 
														
													
														
															
																|  |  | } |  |  | } |