Skip to content

Commit

Permalink
Ran formatting tool
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineRondelet committed Oct 1, 2019
1 parent fb14bb5 commit cbc1976
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
12 changes: 8 additions & 4 deletions src/circuits/blake2s/blake2s_comp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ class BLAKE2s_256_comp : public libsnark::gadget<FieldT>
std::array<std::array<FieldT, BLAKE2s_word_size>, 2> t;

std::array<libsnark::pb_variable_array<FieldT>, BLAKE2s_word_number> block;
std::array<std::array<libsnark::pb_variable_array<FieldT>,
BLAKE2s_word_number>, rounds + 1> v;
std::array<std::array<libsnark::pb_variable_array<FieldT>,
BLAKE2s_word_number>, rounds> v_temp;
std::array<
std::array<libsnark::pb_variable_array<FieldT>, BLAKE2s_word_number>,
rounds + 1>
v;
std::array<
std::array<libsnark::pb_variable_array<FieldT>, BLAKE2s_word_number>,
rounds>
v_temp;
std::array<libsnark::pb_variable_array<FieldT>, 8> output_bytes;
libsnark::block_variable<FieldT> input_block;
libsnark::digest_variable<FieldT> output;
Expand Down
11 changes: 8 additions & 3 deletions src/circuits/blake2s/blake2s_comp.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ BLAKE2s_256_comp<FieldT>::BLAKE2s_256_comp(
// Allocate and format the 16 input block variable
for (size_t i = 0; i < BLAKE2s_word_number; i++) {
block[i].allocate(
pb, BLAKE2s_word_size, FMT(this->annotation_prefix, " block_%zu", i));
pb,
BLAKE2s_word_size,
FMT(this->annotation_prefix, " block_%zu", i));
}

// Setup constants, hash parameters and initialize the state
Expand Down Expand Up @@ -46,7 +48,9 @@ BLAKE2s_256_comp<FieldT>::BLAKE2s_256_comp(
// Allocate output bytes (before swapping endianness and appending)
for (size_t i = 0; i < 8; i++) {
output_bytes[i].allocate(
pb, BLAKE2s_word_size, FMT(this->annotation_prefix, " output_byte_%zu", i));
pb,
BLAKE2s_word_size,
FMT(this->annotation_prefix, " output_byte_%zu", i));
}

// Set up the g_primitive gadgets used in the compression function
Expand Down Expand Up @@ -99,7 +103,8 @@ template<typename FieldT> void BLAKE2s_256_comp<FieldT>::generate_r1cs_witness()
// Allocate and format the 16 input block variable
for (size_t i = 0; i < BLAKE2s_word_number; i++) {
std::vector<FieldT> temp_vector(
padded_input.begin() + BLAKE2s_word_size * i, padded_input.begin() + BLAKE2s_word_size * (i + 1));
padded_input.begin() + BLAKE2s_word_size * i,
padded_input.begin() + BLAKE2s_word_size * (i + 1));
std::vector<FieldT> swapped_vector = swap_byte_endianness(temp_vector);
block[i].fill_with_field_elements(this->pb, swapped_vector);
}
Expand Down
3 changes: 2 additions & 1 deletion src/circuits/blake2s/blake2s_comp_setup.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ void BLAKE2s_256_comp<FieldT>::setup_counter(size_t len_input_block)
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0 // 00
};
for (size_t i = 0; int(i) < std::min(int(BLAKE2s_word_size), int(bit_size)); i++) {
for (size_t i = 0; int(i) < std::min(int(BLAKE2s_word_size), int(bit_size));
i++) {
t[0][BLAKE2s_word_size - i - 1] = length_bits[bit_size - i - 1];
}

Expand Down
12 changes: 8 additions & 4 deletions src/circuits/blake2s/g_primitive.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ g_primitive<FieldT>::g_primitive(
new double_bit32_sum_eq_gadget<FieldT>(pb, a1_temp, x, a1));

// v[d] := (v[d] ^ v[a]) >>> R1
d_xor_gadget.reset(new xor_rot_gadget<FieldT>(pb, d, a1, rotation_constant_r1, d1));
d_xor_gadget.reset(
new xor_rot_gadget<FieldT>(pb, d, a1, rotation_constant_r1, d1));

// v[c] := (v[c] + v[d]) mod 2^32
c1_gadget.reset(new double_bit32_sum_eq_gadget<FieldT>(pb, c, d1, c1));

// v[b] := (v[b] ^ v[c]) >>> R2
b_xor_gadget.reset(new xor_rot_gadget<FieldT>(pb, b, c1, rotation_constant_r2, b1));
b_xor_gadget.reset(
new xor_rot_gadget<FieldT>(pb, b, c1, rotation_constant_r2, b1));

// v[a] := (v[a] + v[b] + y) mod 2^32
a2_1_gadget.reset(
Expand All @@ -54,13 +56,15 @@ g_primitive<FieldT>::g_primitive(
new double_bit32_sum_eq_gadget<FieldT>(pb, a2_temp, y, a2));

// v[d] := (v[d] ^ v[a]) >>> R3
d1_xor_gadget.reset(new xor_rot_gadget<FieldT>(pb, d1, a2, rotation_constant_r3, d2));
d1_xor_gadget.reset(
new xor_rot_gadget<FieldT>(pb, d1, a2, rotation_constant_r3, d2));

// v[c] := (v[c] + v[d]) mod 2^32
c2_gadget.reset(new double_bit32_sum_eq_gadget<FieldT>(pb, c1, d2, c2));

// v[b] := (v[b] ^ v[c]) >>> R4
b1_xor_gadget.reset(new xor_rot_gadget<FieldT>(pb, b1, c2, rotation_constant_r4, b2));
b1_xor_gadget.reset(
new xor_rot_gadget<FieldT>(pb, b1, c2, rotation_constant_r4, b2));
};

template<typename FieldT> void g_primitive<FieldT>::generate_r1cs_constraints()
Expand Down
14 changes: 5 additions & 9 deletions src/circuits/simple_gadgets.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ template<typename FieldT> void xor_gadget<FieldT>::generate_r1cs_constraints()
// res = a XOR b <=> (2.a) * b = a + b - res
this->pb.add_r1cs_constraint(
libsnark::r1cs_constraint<FieldT>(
2 * a[i],
b[i],
a[i] + b[i] - res[i]),
2 * a[i], b[i], a[i] + b[i] - res[i]),
FMT(this->annotation_prefix, " xored_bits_%zu", i));
}
};
Expand Down Expand Up @@ -89,8 +87,8 @@ void xor_constant_gadget<FieldT>::generate_r1cs_constraints()
libsnark::r1cs_constraint<FieldT>(
-FieldT("2") * (FieldT("1") - FieldT("2") * c[i]) * a[i],
b[i],
res[i] - c[i] - a[i] * (FieldT("1") - FieldT("2") * c[i])
- b[i] * (FieldT("1") - FieldT("2") * c[i])),
res[i] - c[i] - a[i] * (FieldT("1") - FieldT("2") * c[i]) -
b[i] * (FieldT("1") - FieldT("2") * c[i])),
FMT(this->annotation_prefix, " rotated_xored_bits_%zu", i));
}
};
Expand Down Expand Up @@ -139,9 +137,7 @@ void xor_rot_gadget<FieldT>::generate_r1cs_constraints()
for (size_t i = 0; i < a.size(); i++) {
this->pb.add_r1cs_constraint(
libsnark::r1cs_constraint<FieldT>(
2 * a[i],
b[i],
a[i] + b[i] - res[(i + shift) % a.size()]),
2 * a[i], b[i], a[i] + b[i] - res[(i + shift) % a.size()]),
FMT(this->annotation_prefix, " rotated_xored_bits_%zu", i));
}
};
Expand Down Expand Up @@ -185,7 +181,7 @@ void double_bit32_sum_eq_gadget<FieldT>::generate_r1cs_constraints(
//
// Below, we propose an alternative way to constraint the result to
// be a boolean string and to be the valid sum of a and b.
//
//
// Let a and b be the input bit string of length 32 bits (uint32)
// Let res be the claimed result of a + b of length 33 bits (an additional
// bit account for the potential carry of the addition of a and b)
Expand Down

0 comments on commit cbc1976

Please sign in to comment.