Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean-up c++ functions #151

Merged
merged 3 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 1 addition & 31 deletions libzeth/circuits/circuits_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,6 @@
namespace libzeth
{

void insert_bits256(std::vector<bool> &into, bits256 from)
{
std::vector<bool> blob = get_vector_from_bits256(from);
into.insert(into.end(), blob.begin(), blob.end());
};

void insert_bits64(std::vector<bool> &into, bits64 from)
{
std::vector<bool> num = get_vector_from_bits64(from);
into.insert(into.end(), num.begin(), num.end());
};

std::vector<unsigned long> bit_list_to_ints(
std::vector<bool> bit_list, const size_t wordsize)
{
std::vector<unsigned long> res;
size_t iterations = bit_list.size() / wordsize + 1;
for (size_t i = 0; i < iterations; ++i) {
unsigned long current = 0;
for (size_t j = 0; j < wordsize; ++j) {
if (bit_list.size() == (i * wordsize + j))
break;
current +=
(bit_list[i * wordsize + j] * (1ul << (wordsize - 1 - j)));
}
res.push_back(current);
}
return res;
}

std::vector<bool> convert_to_binary(size_t n)
{
std::vector<bool> res;
Expand All @@ -55,4 +25,4 @@ std::vector<bool> convert_to_binary(size_t n)
return res;
}

} // namespace libzeth
} // namespace libzeth
6 changes: 0 additions & 6 deletions libzeth/circuits/circuits_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@
namespace libzeth
{

template<typename T> std::vector<bool> convert_to_binary_LE(T x, int bitlen);
std::vector<bool> convert_to_binary(size_t n);

template<typename T> T swap_endianness_u64(T v);
template<typename FieldT>
libsnark::linear_combination<FieldT> packed_addition(
libsnark::pb_variable_array<FieldT> input);
template<typename FieldT>
libsnark::pb_variable_array<FieldT> from_bits(
std::vector<bool> bits, const libsnark::pb_variable<FieldT> &ZERO);
void insert_bits256(std::vector<bool> &into, bits256 from);
void insert_bits64(std::vector<bool> &into, bits64 from);
std::vector<unsigned long> bit_list_to_ints(
std::vector<bool> bit_list, const size_t wordsize);

} // namespace libzeth
#include "libzeth/circuits/circuits_utils.tcc"
Expand Down
46 changes: 1 addition & 45 deletions libzeth/circuits/circuits_utils.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,7 @@ namespace libzeth
// multiplicative identity of the field FieldT) Thus we are safe here. The ONE
// is well equal to the value FieldT::one()

// Converts a given number encoded on bitlen bits into a
// binary string of lentgh bitlen.
// The encoding is Little Endian.
template<typename T> std::vector<bool> convert_to_binary_LE(T x, int bitlen)
{
std::vector<bool> ret;
for (int i = 0; i < bitlen; i++) {
if (x & 1)
ret.push_back(1);
else
ret.push_back(0);
x >>= 1;
}
return ret;
};

// This function reverses the byte endianness
//
// Example input/output:
//
// Before swap (in): After Swap (out):
// 0011 0111 0000 0000
// 1000 0010 0000 0000
// 1101 1010 1001 0000
// 1100 1110 1001 1101
// 1001 1101 1100 1110
// 1001 0000 1101 1010
// 0000 0000 1000 0010
// 0000 0000 0011 0111
template<typename T> T swap_endianness_u64(T v)
{
if (v.size() != 64) {
throw std::length_error(
"invalid bit length for 64-bit unsigned integer");
}

for (size_t i = 0; i < 4; i++) {
for (size_t j = 0; j < 8; j++) {
std::swap(v[i * 8 + j], v[((7 - i) * 8) + j]);
}
}

return v;
};

// Addition of binary strings in F_r in big endian
AntoineRondelet marked this conversation as resolved.
Show resolved Hide resolved
rrtoledo marked this conversation as resolved.
Show resolved Hide resolved
template<typename FieldT>
libsnark::linear_combination<FieldT> packed_addition(
libsnark::pb_variable_array<FieldT> inputs)
Expand Down
22 changes: 9 additions & 13 deletions libzeth/test/commitments_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,16 @@ TEST(TestCOMMs, TestCOMMGadget)
ZERO.allocate(pb, "zero");
pb.val(ZERO) = FieldT::zero();

bits384 trap_r_bits384 = get_bits384_from_vector(hex_to_binary_vector(
bits384 trap_r_bits384 = hex_value_to_bits384(
"0F000000000000FF00000000000000FF00000000000000FF00000000000000FF00"
"000000000000FF00000000000000FF"));
bits64 value_bits64 =
get_bits64_from_vector(hex_to_binary_vector("2F0000000000000F"));
bits256 rho_bits256 = get_bits256_from_vector(
hex_digest_to_binary_vector("FFFF000000000000000000000000000000"
"000000000000000000000000009009"));
bits256 a_pk_bits256 = get_bits256_from_vector(
hex_digest_to_binary_vector("5c36fea42b82800d74304aa4f875142b42"
"1b4f2847e7c41c1077fbbcfd63f886"));
bits256 cm_bits256 = get_bits256_from_vector(
hex_digest_to_binary_vector("f1378e66b0037337743d1ca5c83ed28c4e"
"873c3fb242dcef3ff0db4ebe82c15f"));
"000000000000FF00000000000000FF");
bits64 value_bits64 = hex_value_to_bits64("2F0000000000000F");
bits256 rho_bits256 = hex_digest_to_bits256(
"FFFF000000000000000000000000000000000000000000000000000000009009");
bits256 a_pk_bits256 = hex_digest_to_bits256(
"5c36fea42b82800d74304aa4f875142b421b4f2847e7c41c1077fbbcfd63f886");
bits256 cm_bits256 = hex_digest_to_bits256(
"f1378e66b0037337743d1ca5c83ed28c4e873c3fb242dcef3ff0db4ebe82c15f");

// hex: 0xAF000000000000FF00000000000000FF00000000000000FF00000000000000FF
libsnark::pb_variable_array<FieldT> a_pk;
Expand Down
43 changes: 20 additions & 23 deletions libzeth/test/note_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ TEST(TestNoteCircuits, TestInputNoteGadget)

libff::enter_block(
"Initialize the coins' data (nullifier, a_sk and a_pk, cm, rho)", true);
bits384 trap_r_bits384 = get_bits384_from_vector(hex_to_binary_vector(
bits384 trap_r_bits384 = hex_value_to_bits384(
"0F000000000000FF00000000000000FF00000000000000FF00000000000000FF00"
"000000000000FF00000000000000FF"));
bits64 value_bits64 =
get_bits64_from_vector(hex_to_binary_vector("2F0000000000000F"));
bits256 a_sk_bits256 = get_bits256_from_vector(hex_digest_to_binary_vector(
"FF0000000000000000000000000000000000000000000000000000000000000F"));
bits256 rho_bits256 = get_bits256_from_vector(hex_digest_to_binary_vector(
"FFFF000000000000000000000000000000000000000000000000000000009009"));
"000000000000FF00000000000000FF");
bits64 value_bits64 = hex_value_to_bits64("2F0000000000000F");
bits256 a_sk_bits256 = hex_digest_to_bits256(
"FF0000000000000000000000000000000000000000000000000000000000000F");
bits256 rho_bits256 = hex_digest_to_bits256(
"FFFF000000000000000000000000000000000000000000000000000000009009");

// Get a_pk from a_sk (PRF)
//
Expand All @@ -69,9 +68,8 @@ TEST(TestNoteCircuits, TestInputNoteGadget)
// 0x0000000000000000000000000000000000000000000000000000000000000000
// a_pk = blake2sCompress( 1100 || [a_sk]_252 || 0^256)
// Generated directly from a_sk and hashlib blake2s
bits256 a_pk_bits256 = get_bits256_from_vector(
hex_digest_to_binary_vector("f172d7299ac8ac974ea59413e4a8769182"
"6df038ba24a2b52d5c5d15c2cc8c49"));
bits256 a_pk_bits256 = hex_digest_to_bits256(
"f172d7299ac8ac974ea59413e4a87691826df038ba24a2b52d5c5d15c2cc8c49");

// Get nf from a_sk and rho (PRF)
//
Expand All @@ -80,8 +78,8 @@ TEST(TestNoteCircuits, TestInputNoteGadget)
// 0xEFF0000000000000000000000000000000000000000000000000000000000000
// rho = FFFF000000000000000000000000000000000000000000000000000000009009
// The test vector generated directly from a_sk and hashlib blake2s, gives:
bits256 nf_bits256 = get_bits256_from_vector(hex_digest_to_binary_vector(
"ff2f41920346251f6e7c67062149f98bc90c915d3d3020927ca01deab5da0fd7"));
bits256 nf_bits256 = hex_digest_to_bits256(
"ff2f41920346251f6e7c67062149f98bc90c915d3d3020927ca01deab5da0fd7");

// Get the coin's commitment (COMM)
//
Expand Down Expand Up @@ -173,23 +171,22 @@ TEST(TestNoteCircuits, TestOutputNoteGadget)

libff::enter_block(
"Initialize the output coins' data (a_pk, cm, rho)", true);
bits384 trap_r_bits384 = get_bits384_from_vector(hex_to_binary_vector(
bits384 trap_r_bits384 = hex_value_to_bits384(
"0F000000000000FF00000000000000FF00000000000000FF00000000000000FF00"
"000000000000FF00000000000000FF"));
bits64 value_bits64 =
get_bits64_from_vector(hex_to_binary_vector("2F0000000000000F"));
bits256 rho_bits256 = get_bits256_from_vector(hex_digest_to_binary_vector(
"FFFF000000000000000000000000000000000000000000000000000000009009"));
bits256 a_pk_bits256 = get_bits256_from_vector(hex_digest_to_binary_vector(
"6461f753bfe21ba2219ced74875b8dbd8c114c3c79d7e41306dd82118de1895b"));
"000000000000FF00000000000000FF");
bits64 value_bits64 = hex_value_to_bits64("2F0000000000000F");
bits256 rho_bits256 = hex_digest_to_bits256(
"FFFF000000000000000000000000000000000000000000000000000000009009");
bits256 a_pk_bits256 = hex_digest_to_bits256(
"6461f753bfe21ba2219ced74875b8dbd8c114c3c79d7e41306dd82118de1895b");

// Get the coin's commitment (COMM)
//
// inner_k = blake2sCompress(a_pk || rho)
// outer_k = blake2sCompress(r || [inner_commitment]_128)
// cm = blake2sCompress(outer_k || 0^192 || value_v)
bits256 cm_bits256 = get_bits256_from_vector(hex_digest_to_binary_vector(
"626876b3e2747325f469df067b1f86c8474ffe85e97f56f273c5798dcfccd925"));
bits256 cm_bits256 = hex_digest_to_bits256(
"626876b3e2747325f469df067b1f86c8474ffe85e97f56f273c5798dcfccd925");
libff::leave_block(
"Initialize the output coins' data (a_pk, cm, rho)", true);

Expand Down
Loading