Line data Source code
1 : // Copyright (c) 2016-2020 The ZCash developers 2 : // Copyright (c) 2020 The PIVX Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #include "sapling/sapling_util.h" 7 : #include "sync.h" 8 : 9 : #include <algorithm> 10 : #include <librustzcash.h> 11 : #include <stdexcept> 12 : #include <iostream> 13 : 14 0 : std::vector<unsigned char> convertIntToVectorLE(const uint64_t val_int) { 15 0 : std::vector<unsigned char> bytes; 16 : 17 0 : for(size_t i = 0; i < 8; i++) { 18 0 : bytes.push_back(val_int >> (i * 8)); 19 : } 20 : 21 0 : return bytes; 22 : } 23 : 24 : // Convert bytes into boolean vector. (MSB to LSB) 25 3297 : std::vector<bool> convertBytesVectorToVector(const std::vector<unsigned char>& bytes) { 26 3297 : std::vector<bool> ret; 27 3297 : ret.resize(bytes.size() * 8); 28 : 29 : unsigned char c; 30 108774 : for (size_t i = 0; i < bytes.size(); i++) { 31 105477 : c = bytes.at(i); 32 949293 : for (size_t j = 0; j < 8; j++) { 33 1687632 : ret.at((i*8)+j) = (c >> (7-j)) & 1; 34 : } 35 : } 36 : 37 3297 : return ret; 38 : } 39 : 40 : // Convert boolean vector (big endian) to integer 41 444 : uint64_t convertVectorToInt(const std::vector<bool>& v) { 42 444 : if (v.size() > 64) { 43 1 : throw std::length_error ("boolean vector can't be larger than 64 bits"); 44 : } 45 : 46 : uint64_t result = 0; 47 4363 : for (size_t i=0; i<v.size();i++) { 48 3920 : if (v.at(i)) { 49 766 : result |= (uint64_t)1 << ((v.size() - 1) - i); 50 : } 51 : } 52 : 53 443 : return result; 54 : } 55 : 56 1021 : uint256 random_uint256() { 57 1021 : uint256 ret; 58 1021 : randombytes_buf(ret.begin(), 32); 59 : 60 1021 : return ret; 61 : }