Last active
October 19, 2017 16:26
-
-
Save christomoore/80534a332cc17f61b3284e74ecddcb04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
template<typename FieldT> | |
std::vector<FieldT> pack_bit_vector_into_field_element_vector(const bit_vector &v, const size_t chunk_bits) | |
{ | |
assert(chunk_bits <= FieldT::capacity()); | |
const size_t repacked_size = div_ceil(v.size(), chunk_bits); | |
std::vector<FieldT> result(repacked_size); | |
for (size_t i = 0; i < repacked_size; ++i) | |
{ | |
bigint<FieldT::num_limbs> b; | |
for (size_t j = 0; j < chunk_bits; ++j) | |
{ | |
b.data[j / GMP_NUMB_BITS] |= ((i * chunk_bits + j) < v.size() && v[i * chunk_bits + j] ? 1ll : 0ll) << (j % GMP_NUMB_BITS); | |
std::cout << v[i * chunk_bits + j] ; | |
//std::cout << " j: " << j << v[i * chunk_bits + j] << "\n"; | |
} | |
//std::cout << v[i * chunk_bits + j] ; | |
std::cout << " bytes : " << b << "\n"; | |
result[i] = FieldT(b); | |
std::cout << "GMP_NUMB_BITS : " << GMP_NUMB_BITS << " converted point : " << result[i] << "\n"; | |
} | |
return result; | |
} | |
Solidity equivalent ? | |
bit_vector &v = 0x108ff550871ad300000000000000000000000000000000000000000000000000; | |
chunk_bits = 253; | |
uint GMP_NUMB_BITS = 64; | |
*/ | |
contract C { | |
event done(uint256[] d); | |
function pack_bit_vector_into_bigint() { | |
bytes32 v = 0x108ff550871ad300000000000000000000000000000000000000000000000000; | |
uint chunk_bits = 256; | |
uint GMP_NUMB_BITS = 64 ; | |
uint repacked_size = 1; | |
uint[] memory data = new uint[](256); | |
for (uint i = 0; i < repacked_size; ++i){ | |
for (uint j = 0; j < chunk_bits; ++j){ | |
data[j / GMP_NUMB_BITS] = data[j / GMP_NUMB_BITS] | ((i * chunk_bits + j) < 256 && v[i * chunk_bits + j] != 0 ? 1 : 0) << (j % GMP_NUMB_BITS); | |
} | |
} | |
done(data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment