/* CryptoNight Step 4: Sequentially pass through the mixing buffer * and use 10 rounds of AES encryption to mix the random data back * into the 'text' buffer. */ private static void EncryptScratchpadToText(CNState cnState, ICryptoNight cnParams, byte[] scratchpad) { /* Reinitialize text from state */ byte[] text = cnState.GetText(); /* Expand our initial key into many for each round of pseudo aes */ byte[] expandedKeys = AES.ExpandKey(cnState.GetAESKey2(), false); for (int i = 0; i < cnParams.InitRounds; i++) { for (int j = 0; j < Constants.INIT_SIZE_BLOCK; j++) { int offsetA = j * AES.BLOCK_SIZE; int offsetB = (i * Constants.INIT_SIZE_BYTE) + offsetA; XORBlocks(text, scratchpad, offsetA, offsetB); /* Need to pass the array with an offset because we manip * it in place */ AES.AESBPseudoRound(expandedKeys, text, offsetA); } } cnState.SetText(text); }
/* CryptoNight Step 4: Sequentially pass through the mixing buffer * and use 10 rounds of AES encryption to mix the random data back * into the 'text' buffer. */ private static void EncryptScratchpadToText(CNState cnState, ICryptoNight cnParams, byte[] scratchpad) { /* Reinitialize text from state */ byte[] text = cnState.GetText(); /* Expand our initial key into many for each round of pseudo aes */ byte[] expandedKeys = AES.ExpandKey(cnState.GetAESKey2(), true); for (int i = 0; i < cnParams.InitRounds; i++) { AES.AESPseudoRoundXOR( expandedKeys, text, scratchpad, i * Constants.INIT_SIZE_BYTE ); } cnState.SetText(text); }
/* CryptoNight Step 2: Iteratively encrypt the results from Keccak * to fill the large scratchpad */ public static byte[] FillScratchpad(CNState cnState, ICryptoNight cnParams) { /* Expand our initial key into many for each round of pseudo aes */ byte[] expandedKeys = AES.ExpandKey(cnState.GetAESKey(), cnParams.Intrinsics); /* Our large scratchpad, 2MB in default CN */ byte[] scratchpad = new byte[cnParams.Memory]; byte[] text = cnState.GetText(); if (cnParams.Intrinsics && Aes.IsSupported && Sse2.IsSupported) { /* Fill the scratchpad with AES encryption of text */ for (int i = 0; i < cnParams.InitRounds; i++) { AES.AESPseudoRoundNative(expandedKeys, text); /* Write text to the scratchpad, at the offset * i * InitSizeByte */ Buffer.BlockCopy(text, 0, scratchpad, i * Constants.INIT_SIZE_BYTE, text.Length); } } else { /* Fill the scratchpad with AES encryption of text */ for (int i = 0; i < cnParams.InitRounds; i++) { AES.AESPseudoRound(expandedKeys, text, cnParams.Intrinsics); /* Write text to the scratchpad, at the offset * i * InitSizeByte */ Buffer.BlockCopy(text, 0, scratchpad, i * Constants.INIT_SIZE_BYTE, text.Length); } } return(scratchpad); }