Commit 7ac0c929 authored by George Hotz's avatar George Hotz

use a bit less gas for keccak

parent 19853222
...@@ -9,22 +9,20 @@ library Lib_Keccak256 { ...@@ -9,22 +9,20 @@ library Lib_Keccak256 {
uint64[25] A; uint64[25] A;
} }
bytes public constant round_constant = hex"011a5e701f2179550e0c35263f4f5d535248166679582174";
function ROTL64(uint64 qword, uint64 n) internal pure returns (uint64) { function ROTL64(uint64 qword, uint64 n) internal pure returns (uint64) {
return ((qword) << (n) ^ ((qword) >> (64 - (n)))); return ((qword) << (n) ^ ((qword) >> (64 - (n))));
} }
function get_round_constant(uint round) internal pure returns (uint64) { function get_round_constant(uint round) internal pure returns (uint64) {
uint64 result = 0; uint64 result = 0;
uint8 roundInfo = uint8(round_constant[round]); uint8 roundInfo = uint8(0x7421587966164852535d4f3f26350c0e5579211f705e1a01 >> (round*8));
if (roundInfo & (1 << 6) != 0) { result |= (1 << 63); } result |= (uint64(roundInfo) << (63-6)) & (1 << 63);
if (roundInfo & (1 << 5) != 0) { result |= (1 << 31); } result |= (uint64(roundInfo) << (31-5)) & (1 << 31);
if (roundInfo & (1 << 4) != 0) { result |= (1 << 15); } result |= (uint64(roundInfo) << (15-4)) & (1 << 15);
if (roundInfo & (1 << 3) != 0) { result |= (1 << 7); } result |= (uint64(roundInfo) << (7-3)) & (1 << 7);
if (roundInfo & (1 << 2) != 0) { result |= (1 << 3); } result |= (uint64(roundInfo) << (3-2)) & (1 << 3);
if (roundInfo & (1 << 1) != 0) { result |= (1 << 1); } result |= (uint64(roundInfo) << (1-1)) & (1 << 1);
if (roundInfo & (1 << 0) != 0) { result |= (1 << 0); } result |= (uint64(roundInfo) << (0-0)) & (1 << 0);
return result; return result;
} }
...@@ -35,13 +33,13 @@ library Lib_Keccak256 { ...@@ -35,13 +33,13 @@ library Lib_Keccak256 {
uint j; uint j;
for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
C[i] = c.A[i]; C[i] = c.A[i];
for (j = 5; j < 25; j += 5) { C[i] ^= c.A[i + j]; } for (j = i+5; j < 25; j += 5) { C[i] ^= c.A[j]; }
} }
for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
D[i] = ROTL64(C[(i + 1) % 5], 1) ^ C[(i + 4) % 5]; D[i] = ROTL64(C[(i + 1) % 5], 1) ^ C[(i + 4) % 5];
} }
for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
for (j = 0; j < 25; j += 5) { c.A[i + j] ^= D[i]; } for (j = i; j < 25; j += 5) { c.A[j] ^= D[i]; }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment