RandomWOW/src/blake2_generator.cpp

53 lines
1.5 KiB
C++
Raw Normal View History

2019-04-12 17:36:08 +00:00
/*
Copyright (c) 2019 tevador
This file is part of RandomX.
RandomX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RandomX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RandomX. If not, see<http://www.gnu.org/licenses/>.
*/
2019-04-13 10:02:08 +00:00
#include <stddef.h>
2019-04-12 17:36:08 +00:00
#include "blake2/blake2.h"
#include "blake2/endian.h"
2019-04-20 14:53:06 +00:00
#include "blake2_generator.hpp"
2019-04-12 17:36:08 +00:00
2019-04-20 09:08:01 +00:00
namespace randomx {
2019-04-12 17:36:08 +00:00
2019-04-20 14:53:06 +00:00
constexpr int maxSeedSize = 60;
Blake2Generator::Blake2Generator(const void* seed, size_t seedSize, int nonce) : dataIndex(sizeof(data)) {
2019-04-12 17:36:08 +00:00
memset(data, 0, sizeof(data));
2019-04-20 14:53:06 +00:00
memcpy(data, seed, seedSize > maxSeedSize ? maxSeedSize : seedSize);
store32(&data[maxSeedSize], nonce);
2019-04-12 17:36:08 +00:00
}
uint8_t Blake2Generator::getByte() {
checkData(1);
return data[dataIndex++];
}
uint32_t Blake2Generator::getInt32() {
checkData(4);
auto ret = load32(&data[dataIndex]);
dataIndex += 4;
return ret;
}
void Blake2Generator::checkData(const size_t bytesNeeded) {
if (dataIndex + bytesNeeded > sizeof(data)) {
blake2b(data, sizeof(data), data, sizeof(data), nullptr, 0);
dataIndex = 0;
}
}
}