HarryR / ethsnarks

A toolkit for viable zk-SNARKS on Ethereum, Web, Mobile and Desktop
GNU Lesser General Public License v3.0
240 stars 57 forks source link

Using SHA256 gadget with arbitrary sized input #163

Open amadilsons opened 4 years ago

amadilsons commented 4 years ago

I'm new to the zk-snarks world, still trying to figure out how everything works.

I would like to generate a proof of pre-image using SHA256 and an arbitrary sized input.

I was wondering if you could make available some sort of tutorial on how to achieve this, or at least provide some general guidelines on how to use the SHA256 gadget.

Thank you in advance!

HarryR commented 4 years ago

Hi,

https://github.com/Ethsnarks/ethsnarks-hashpreimage is an example of how to prove the preimage to hash.

The problem with an arbitrary input size is that the zk-snark circuit is fixed sized, you can hash an arbitrary number of bits (in a way which is compatible with the SHA256 function on Ethereum and everywhere else), as long as you only ever have to prove that you've hashed that specific number of bits.

There is a 'gadget' at https://github.com/HarryR/ethsnarks/blob/master/src/gadgets/sha256_many.cpp which will take an arbitrary number of input bytes and hash them in a way which is compatible with a normal SHA256 function (as-long as you pad it to full bytes, rather than e.g. 12 bits)