PIVX Core  5.6.99
P2P Digital Currency
sapling_util.cpp
Go to the documentation of this file.
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 std::vector<unsigned char> convertIntToVectorLE(const uint64_t val_int) {
15  std::vector<unsigned char> bytes;
16 
17  for(size_t i = 0; i < 8; i++) {
18  bytes.push_back(val_int >> (i * 8));
19  }
20 
21  return bytes;
22 }
23 
24 // Convert bytes into boolean vector. (MSB to LSB)
25 std::vector<bool> convertBytesVectorToVector(const std::vector<unsigned char>& bytes) {
26  std::vector<bool> ret;
27  ret.resize(bytes.size() * 8);
28 
29  unsigned char c;
30  for (size_t i = 0; i < bytes.size(); i++) {
31  c = bytes.at(i);
32  for (size_t j = 0; j < 8; j++) {
33  ret.at((i*8)+j) = (c >> (7-j)) & 1;
34  }
35  }
36 
37  return ret;
38 }
39 
40 // Convert boolean vector (big endian) to integer
41 uint64_t convertVectorToInt(const std::vector<bool>& v) {
42  if (v.size() > 64) {
43  throw std::length_error ("boolean vector can't be larger than 64 bits");
44  }
45 
46  uint64_t result = 0;
47  for (size_t i=0; i<v.size();i++) {
48  if (v.at(i)) {
49  result |= (uint64_t)1 << ((v.size() - 1) - i);
50  }
51  }
52 
53  return result;
54 }
55 
57  uint256 ret;
58  randombytes_buf(ret.begin(), 32);
59 
60  return ret;
61 }
unsigned char * begin()
Definition: uint256.h:63
256-bit opaque blob.
Definition: uint256.h:138
uint256 random_uint256()
uint64_t convertVectorToInt(const std::vector< bool > &v)
std::vector< unsigned char > convertIntToVectorLE(const uint64_t val_int)
std::vector< bool > convertBytesVectorToVector(const std::vector< unsigned char > &bytes)