Skip to content

Commit

Permalink
Merge pull request #116 from chfast/progpow
Browse files Browse the repository at this point in the history
ProgPoW 0.9.2
  • Loading branch information
chfast authored Dec 10, 2018
2 parents a0b15d7 + 77396cb commit 3f4b4a1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
20 changes: 12 additions & 8 deletions lib/ethash/progpow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ inline uint32_t random_math(uint32_t a, uint32_t b, uint32_t selector) noexcept
NO_SANITIZE("unsigned-integer-overflow")
inline void random_merge(uint32_t& a, uint32_t b, uint32_t selector) noexcept
{
const auto x = (selector >> 16) % 31 + 1; // Additional non-zero selector from higher bits.
switch (selector % 4)
{
case 0:
Expand All @@ -156,10 +157,10 @@ inline void random_merge(uint32_t& a, uint32_t b, uint32_t selector) noexcept
a = (a ^ b) * 33;
break;
case 2:
a = rotl32(a, ((selector >> 16) % 32)) ^ b;
a = rotl32(a, x) ^ b;
break;
case 3:
a = rotr32(a, ((selector >> 16) % 32)) ^ b;
a = rotr32(a, x) ^ b;
break;
}
}
Expand All @@ -182,9 +183,8 @@ void round(
// Process lanes.
for (int i = 0; i < max_operations; ++i)
{
if (i < num_cache_accesses)
if (i < num_cache_accesses) // Random access to cached memory.
{
// Random access to cached memory.
const auto src = state.next_src();
const auto dst = state.next_dst();
const auto sel = state.rng();
Expand All @@ -195,11 +195,15 @@ void round(
random_merge(mix[l][dst], le::uint32(context.l1_cache[offset]), sel);
}
}
if (i < num_math_operations)
if (i < num_math_operations) // Random math.
{
// Random math.
const auto src1 = state.rng() % num_regs;
const auto src2 = state.rng() % num_regs;
// Generate 2 unique source indexes.
const auto src_rnd = state.rng() % (num_regs * (num_regs - 1));
const auto src1 = src_rnd % num_regs; // O <= src1 < num_regs
auto src2 = src_rnd / num_regs; // 0 <= src2 < num_regs - 1
if (src2 >= src1)
++src2;

const auto sel1 = state.rng();
const auto dst = state.next_dst();
const auto sel2 = state.rng();
Expand Down
71 changes: 36 additions & 35 deletions test/unittests/progpow_test_vectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,41 @@ struct progpow_hash_test_case

progpow_hash_test_case progpow_hash_test_cases[] = {
{0, "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000",
"a09ffaa0f2b5d47a98c2d4fbc0e90936710dd2b2a220fce04e8d55a6c6a093d6",
"7ea12cfc33f64616ab7dbbddf3362ee7dd3e1e20d60d860a85c51d6559c912c4"},
{49, "7ea12cfc33f64616ab7dbbddf3362ee7dd3e1e20d60d860a85c51d6559c912c4", "0000000006ff2c47",
"4e453d59426905122ef3d176a6fe660f29b53fdf2f82b5af2753dbaaebebf609",
"f0167e445f8510504ce024856ec614a1a4461610bf58caa32df731ee4c315641"},
{50, "f0167e445f8510504ce024856ec614a1a4461610bf58caa32df731ee4c315641", "00000000076e482e",
"4e5291ae6132f64bff00dd05861721b0da701f789e7e65d096b9affa24bffd7e",
"fdc3bce3e0d0b1a5af43f84acc7d5421d423ec5d3b7e41698178b24c459a6cbe"},
{99, "fdc3bce3e0d0b1a5af43f84acc7d5421d423ec5d3b7e41698178b24c459a6cbe", "000000003917afab",
"d35c7e4012204d1db243dc7cf0bf2075f897e362e6ad2b36c02e325cfc6f8dbb",
"5b014c2c706476b56cf3b9c37ed999d30b20c0fb038d27cc94c991dacef62033"},
{29950, "5b014c2c706476b56cf3b9c37ed999d30b20c0fb038d27cc94c991dacef62033", "005d409dbc23a62a",
"0c64704dedb0677149b47fabc6726e9ff0585233692c8562e485a330ce90c0e9",
"a01b432e82cacaae095ef402b575f1764c45247ba9cf17e99d5432cf00829ee2"},
{29999, "a01b432e82cacaae095ef402b575f1764c45247ba9cf17e99d5432cf00829ee2", "005db5fa4c2a3d03",
"3d95cad9cf4513bb31a4766d3a2f488bbff1baa57da8b2252e246ac91594c769",
"0fc3e6e1392033619f614ec3236d8fbfcefe94d9fdc341a4d7daeffa0b8ad35d"},
{30000, "0fc3e6e1392033619f614ec3236d8fbfcefe94d9fdc341a4d7daeffa0b8ad35d", "005db8607994ff30",
"7ee9d0c571ed35073404454eebe9a73a6d677a32446cf6c427ee63a63bd512da",
"b94de4495555dc2ab4ad8725cabd395178813c8c434134b2f25062b5f72dafb9"},
{30049, "b94de4495555dc2ab4ad8725cabd395178813c8c434134b2f25062b5f72dafb9", "005e2e215a8ca2e7",
"7a16d37208288152237afdc13724d26fe7aadf3cd354a42c587a4192761ef18e",
"e152d3770855cea35a94ee53ab321f93ee3a426513c6ab1ec5e8d81ea9a661d7"},
{30050, "e152d3770855cea35a94ee53ab321f93ee3a426513c6ab1ec5e8d81ea9a661d7", "005e30899481055e",
"005df2434f2a5265c2ed0d13dd12308795620202d2784a40967461c383f859a3",
"55d013e85571e46e914a7529909fbfc686965a92c7baaef2e89e5b5f533a6dc9"},
{30099, "55d013e85571e46e914a7529909fbfc686965a92c7baaef2e89e5b5f533a6dc9", "005ea6aef136f88b",
"d8b1046cc2c8273a06e6f7ce19b7b4aefb7fb43b141721663252e2872b654548",
"8ba5629b6affa0514c2f4951c3a63761465ef0e5be7cbb8f9ce230a5564faccb"},
{59950, "8ba5629b6affa0514c2f4951c3a63761465ef0e5be7cbb8f9ce230a5564faccb", "02ebe0503bd7b1da",
"b3131de1a747449e5328f50742447d5c6da637a5d141a117caf9a986bd524de9",
"10af438404304f4a7de0b07e7d08bfc80b521860237e3e2d47f77630eef5f742"},
{59999, "10af438404304f4a7de0b07e7d08bfc80b521860237e3e2d47f77630eef5f742", "02edb6275bd221e3",
"87f7d6c73fb86a5ed00d2ad7fff7b2a8a9796c3138b31f2473b89065946cb0ed",
"3863e5c767a6b0d28f5cf1d261e35c52fe03f7fd690d50c10596ec73d7595887"},
"faeb1be51075b03a4ff44b335067951ead07a3b078539ace76fd56fc410557a3",
"63155f732f2bf556967f906155b510c917e48e99685ead76ea83f4eca03ab12b"},
{49, "63155f732f2bf556967f906155b510c917e48e99685ead76ea83f4eca03ab12b", "0000000006ff2c47",
"c789c1180f890ec555ff42042913465481e8e6bc512cb981e1c1108dc3f2227d",
"9e7248f20914913a73d80a70174c331b1d34f260535ac3631d770e656b5dd922"},
{50, "9e7248f20914913a73d80a70174c331b1d34f260535ac3631d770e656b5dd922", "00000000076e482e",
"c7340542c2a06b3a7dc7222635f7cd402abf8b528ae971ddac6bbe2b0c7cb518",
"de37e1824c86d35d154cf65a88de6d9286aec4f7f10c3fc9f0fa1bcc2687188d"},
{99, "de37e1824c86d35d154cf65a88de6d9286aec4f7f10c3fc9f0fa1bcc2687188d", "000000003917afab",
"f5e60b2c5bfddd136167a30cbc3c8dbdbd15a512257dee7964e0bc6daa9f8ba7",
"ac7b55e801511b77e11d52e9599206101550144525b5679f2dab19386f23dcce"},
{29950, "ac7b55e801511b77e11d52e9599206101550144525b5679f2dab19386f23dcce", "005d409dbc23a62a",
"07393d15805eb08ee6fc6cb3ad4ad1010533bd0ff92d6006850246829f18fd6e",
"e43d7e0bdc8a4a3f6e291a5ed790b9fa1a0948a2b9e33c844888690847de19f5"},
{29999, "e43d7e0bdc8a4a3f6e291a5ed790b9fa1a0948a2b9e33c844888690847de19f5", "005db5fa4c2a3d03",
"7551bddf977491da2f6cfc1679299544b23483e8f8ee0931c4c16a796558a0b8",
"d34519f72c97cae8892c277776259db3320820cb5279a299d0ef1e155e5c6454"},
{30000, "d34519f72c97cae8892c277776259db3320820cb5279a299d0ef1e155e5c6454", "005db8607994ff30",
"f1c2c7c32266af9635462e6ce1c98ebe4e7e3ecab7a38aaabfbf2e731e0fbff4",
"8b6ce5da0b06d18db7bd8492d9e5717f8b53e7e098d9fef7886d58a6e913ef64"},
{30049, "8b6ce5da0b06d18db7bd8492d9e5717f8b53e7e098d9fef7886d58a6e913ef64", "005e2e215a8ca2e7",
"57fe6a9fbf920b4e91deeb66cb0efa971e08229d1a160330e08da54af0689add",
"c2c46173481b9ced61123d2e293b42ede5a1b323210eb2a684df0874ffe09047"},
{30050, "c2c46173481b9ced61123d2e293b42ede5a1b323210eb2a684df0874ffe09047", "005e30899481055e",
"ba30c61cc5a2c74a5ecaf505965140a08f24a296d687e78720f0b48baf712f2d",
"ea42197eb2ba79c63cb5e655b8b1f612c5f08aae1a49ff236795a3516d87bc71"},
{30099, "ea42197eb2ba79c63cb5e655b8b1f612c5f08aae1a49ff236795a3516d87bc71", "005ea6aef136f88b",
"cfd5e46048cd133d40f261fe8704e51d3f497fc14203ac6a9ef6a0841780b1cd",
"49e15ba4bf501ce8fe8876101c808e24c69a859be15de554bf85dbc095491bd6"},
{59950, "49e15ba4bf501ce8fe8876101c808e24c69a859be15de554bf85dbc095491bd6", "02ebe0503bd7b1da",
"21511fbaa31fb9f5fc4998a754e97b3083a866f4de86fa7500a633346f56d773",
"f5c50ba5c0d6210ddb16250ec3efda178de857b2b1703d8d5403bd0f848e19cf"},
{59999, "f5c50ba5c0d6210ddb16250ec3efda178de857b2b1703d8d5403bd0f848e19cf", "02edb6275bd221e3",
"653eda37d337e39d311d22be9bbd3458d3abee4e643bee4a7280a6d08106ef98",
"341562d10d4afb706ec2c8d5537cb0c810de02b4ebb0a0eea5ae335af6fb2e88"},

};
} // namespace
12 changes: 6 additions & 6 deletions test/unittests/test_progpow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ TEST(progpow, hash_empty)
auto& context = get_ethash_epoch_context_0();

const auto result = progpow::hash(context, 0, {}, 0);
const auto mix_hex = "a09ffaa0f2b5d47a98c2d4fbc0e90936710dd2b2a220fce04e8d55a6c6a093d6";
const auto final_hex = "7ea12cfc33f64616ab7dbbddf3362ee7dd3e1e20d60d860a85c51d6559c912c4";
const auto mix_hex = "faeb1be51075b03a4ff44b335067951ead07a3b078539ace76fd56fc410557a3";
const auto final_hex = "63155f732f2bf556967f906155b510c917e48e99685ead76ea83f4eca03ab12b";
EXPECT_EQ(to_hex(result.mix_hash), mix_hex);
EXPECT_EQ(to_hex(result.final_hash), final_hex);
}
Expand All @@ -49,8 +49,8 @@ TEST(progpow, hash_30000)
auto context = ethash::create_epoch_context(ethash::get_epoch_number(block_number));

const auto result = progpow::hash(*context, block_number, header, nonce);
const auto mix_hex = "44fa88669c864aa30ba7da46e557593289c4d1fb143a1c43813d512b14fb4636";
const auto final_hex = "b946ea7d74e3c619733ad73ac64a3c7671459b5d5d84d4f5c5cc09feb06ba2c3";
const auto mix_hex = "11f19805c58ab46610ff9c719dcf0a5f18fa2f1605798eef770c47219274767d";
const auto final_hex = "5b7ccd472dbefdd95b895cac8ece67ff0deb5a6bd2ecc6e162383d00c3728ece";
EXPECT_EQ(to_hex(result.mix_hash), mix_hex);
EXPECT_EQ(to_hex(result.final_hash), final_hex);
}
Expand Down Expand Up @@ -111,12 +111,12 @@ TEST(progpow, search)

EXPECT_NE(sr.mix_hash, ethash::hash256{});
EXPECT_NE(sr.final_hash, ethash::hash256{});
EXPECT_EQ(sr.nonce, 103);
EXPECT_EQ(sr.nonce, 185);
EXPECT_EQ(sr.mix_hash, srl.mix_hash);
EXPECT_EQ(sr.final_hash, srl.final_hash);
EXPECT_EQ(sr.nonce, srl.nonce);

auto r = progpow::hash(ctx, 0, {}, 103);
auto r = progpow::hash(ctx, 0, {}, 185);
EXPECT_EQ(sr.final_hash, r.final_hash);
EXPECT_EQ(sr.mix_hash, r.mix_hash);
}
Expand Down

0 comments on commit 3f4b4a1

Please sign in to comment.