scipr-lab / libsnark

C++ library for zkSNARKs
Other
1.81k stars 572 forks source link

Potential MacOS generate_r1cs_witness bug #130

Open drewstone opened 5 years ago

drewstone commented 5 years ago

I've been working on understanding an odd bug related to a SHA256 gadget apart of the ethsnarks repo. The issue can be found here: https://github.com/HarryR/ethsnarks/issues/27.

In particular, on the following code, the leftmost byte is zero'd out for reasons I'm having a hard time understanding.

    protoboard<FieldT> pb;

    // split the input buffer into the right & left components
    digest_variable<FieldT> left(pb, SHA256_digest_size, "left");
    digest_variable<FieldT> right(pb, SHA256_digest_size, "right");

    const libff::bit_vector left_bv = bytes_to_bv(input_buffer, SHA256_digest_size_bytes);
    const libff::bit_vector right_bv = bytes_to_bv(&input_buffer[SHA256_digest_size_bytes], SHA256_digest_size_bytes);

    left.generate_r1cs_witness(left_bv);
    right.generate_r1cs_witness(right_bv);

    auto left_bits = left.get_digest();
    uint8_t left_bytes[SHA256_digest_size_bytes];
    bv_to_bytes(left_bits, left_bytes);

    auto right_bits = right.get_digest();
    uint8_t right_bytes[SHA256_digest_size_bytes];
    bv_to_bytes(right_bits, right_bytes);

    print_bytes("LEFT", SHA256_digest_size_bytes, left_bytes);
    print_bytes("RIGHT", SHA256_digest_size_bytes, right_bytes);

Here's the output from my mac

LEFT: 0086D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08
RIGHT: 9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08

Here's the output from my linux (running Ubuntu 18.04 on a Microsoft Surface 4)

LEFT: 9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08
RIGHT: 9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08

Now, I'm unsure where exactly the generate_r1cs_witness is being directed to due to my beginners knowledge of c++. My guess is it is related to libsnark/libsnark/gadgetlib1/gadgets/hashes/hash_io.tcc. Does this sound on track? Any guidance would be appreciated.