Browse Source

scrypt: Add unit tests for PBKDF2_SHA256_80_128 and salsa20_8

Luke Dashjr 11 years ago
parent
commit
ded4838dda
3 changed files with 53 additions and 0 deletions
  1. 3 0
      miner.c
  2. 49 0
      scrypt.c
  3. 1 0
      scrypt.h

+ 3 - 0
miner.c

@@ -12259,6 +12259,9 @@ int main(int argc, char *argv[])
 		test_intrange();
 		test_intrange();
 		test_decimal_width();
 		test_decimal_width();
 		test_domain_funcs();
 		test_domain_funcs();
+#ifdef USE_SCRYPT
+		test_scrypt();
+#endif
 		test_target();
 		test_target();
 		test_uri_get_param();
 		test_uri_get_param();
 		utf8_test();
 		utf8_test();

+ 49 - 0
scrypt.c

@@ -403,6 +403,55 @@ static void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint
 	PBKDF2_SHA256_80_128_32(input, X, ostate);
 	PBKDF2_SHA256_80_128_32(input, X, ostate);
 }
 }
 
 
+static
+void bin2hex32(char * const out_hex, const uint32_t * const data, const size_t n)
+{
+	uint32_t dataswap[n];
+	swap32tobe(dataswap, data, n);
+	bin2hex(out_hex, dataswap, n * 4);
+}
+
+void test_scrypt(void)
+{
+	static const uint32_t input[20] = {0};
+	uint32_t X[32];
+	char hex[257];
+	{
+		PBKDF2_SHA256_80_128(input, X);
+		static const uint32_t expect_X[] = {
+			0x0ea9ea2c, 0x458a4459, 0xac2e8931, 0x227bb8f5,
+			0xf2b1fe63, 0x65f4ca78, 0xc13ee80a, 0x9dd6a8b9,
+			0x37a70962, 0xce24556e, 0x169081af, 0x73a06c4c,
+			0x7feffbbe, 0x90188614, 0x499f4152, 0x174f00cf,
+			0x5a2f89a9, 0x9f98d171, 0x2ff50782, 0xc8c551b1,
+			0xcf4afba2, 0x089745f0, 0x37553b1f, 0xbca60eec,
+			0x193ed225, 0x0d4c2da1, 0x4a670674, 0x4420645c,
+			0x432ead7e, 0xa70b8496, 0x1d992334, 0x842b14de,
+		};
+		if (memcmp(expect_X, X, sizeof(expect_X)))
+		{
+			bin2hex32(hex, X, 32);
+			applog(LOG_ERR, "%s: %s failed (got %s)", __func__, "PBKDF2_SHA256_80_128", hex);
+		}
+	}
+	{
+		for (int i = 0; i < 0x10; ++i)
+			X[i] = i;
+		salsa20_8(X, input);
+		static const uint32_t expect_X[] = {
+			0x4fdd18f5, 0xe08388b9, 0xc05479a8, 0x7086ab5c,
+			0x0888bb83, 0x75102855, 0x58a08522, 0x166cf522,
+			0x0f2a4a9d, 0x232514d2, 0x0bc658d7, 0x681b4136,
+			0x0586532d, 0xd271b814, 0x2a045976, 0x5d47fa5a,
+		};
+		if (memcmp(expect_X, X, sizeof(expect_X)))
+		{
+			bin2hex32(hex, X, 16);
+			applog(LOG_ERR, "%s; %s failed (got %s)", __func__, "salsa20_8", hex);
+		}
+	}
+}
+
 /* 131583 rounded up to 4 byte alignment */
 /* 131583 rounded up to 4 byte alignment */
 #define SCRATCHBUF_SIZE	(131584)
 #define SCRATCHBUF_SIZE	(131584)
 
 

+ 1 - 0
scrypt.h

@@ -6,6 +6,7 @@
 #include "miner.h"
 #include "miner.h"
 
 
 #ifdef USE_SCRYPT
 #ifdef USE_SCRYPT
+extern void test_scrypt(void);
 extern void scrypt_hash_data(unsigned char *out_hash, const unsigned char *data);
 extern void scrypt_hash_data(unsigned char *out_hash, const unsigned char *data);
 extern void scrypt_regenhash(struct work *work);
 extern void scrypt_regenhash(struct work *work);