2016-05-12 12:19:13 -07:00
|
|
|
#include "zcash/util.h"
|
|
|
|
#include <algorithm>
|
2016-06-28 09:08:50 -07:00
|
|
|
#include <stdexcept>
|
2016-05-12 12:19:13 -07:00
|
|
|
|
|
|
|
std::vector<unsigned char> convertIntToVectorLE(const uint64_t val_int) {
|
|
|
|
std::vector<unsigned char> bytes;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < 8; i++) {
|
2020-08-27 12:50:37 -07:00
|
|
|
bytes.push_back((unsigned char) (val_int >> (i * 8)));
|
2016-05-12 12:19:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
2016-06-28 09:08:50 -07:00
|
|
|
|
|
|
|
// Convert bytes into boolean vector. (MSB to LSB)
|
|
|
|
std::vector<bool> convertBytesVectorToVector(const std::vector<unsigned char>& bytes) {
|
|
|
|
std::vector<bool> ret;
|
|
|
|
ret.resize(bytes.size() * 8);
|
|
|
|
|
|
|
|
unsigned char c;
|
|
|
|
for (size_t i = 0; i < bytes.size(); i++) {
|
|
|
|
c = bytes.at(i);
|
|
|
|
for (size_t j = 0; j < 8; j++) {
|
|
|
|
ret.at((i*8)+j) = (c >> (7-j)) & 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert boolean vector (big endian) to integer
|
|
|
|
uint64_t convertVectorToInt(const std::vector<bool>& v) {
|
|
|
|
if (v.size() > 64) {
|
|
|
|
throw std::length_error ("boolean vector can't be larger than 64 bits");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t result = 0;
|
|
|
|
for (size_t i=0; i<v.size();i++) {
|
|
|
|
if (v.at(i)) {
|
|
|
|
result |= (uint64_t)1 << ((v.size() - 1) - i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|