Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
sha256_ethereum.hpp
Go to the documentation of this file.
1 #ifndef __ZETH_CIRCUITS_SHA256_ETHEREUM_HPP__
2 #define __ZETH_CIRCUITS_SHA256_ETHEREUM_HPP__
3 
4 // DISCLAIMER:
5 // Content taken and adapted from:
6 // https://gist.github.com/kobigurk/24c25e68219df87c348f1a78db51bb52
7 
8 // This gadget implements the interface of the HashT template
9 
10 #include <iostream>
11 #include <libsnark/gadgetlib1/gadget.hpp>
12 #include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>
13 #include <libsnark/gadgetlib1/gadgets/hashes/hash_io.hpp>
14 #include <libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_components.hpp>
15 #include <libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp>
16 #include <libsnark/gadgetlib1/protoboard.hpp>
17 #include <libsnark/reductions/r1cs_to_qap/r1cs_to_qap.hpp>
18 
19 namespace libzeth
20 {
21 
22 const size_t SHA256_ETH_digest_size = 256;
23 const size_t SHA256_ETH_block_size = 512;
24 
25 template<typename FieldT>
26 class sha256_ethereum : public libsnark::gadget<FieldT>
27 {
28 private:
29  std::shared_ptr<libsnark::block_variable<FieldT>> block1;
30  std::shared_ptr<libsnark::block_variable<FieldT>> block2;
31  std::shared_ptr<libsnark::sha256_compression_function_gadget<FieldT>>
32  hasher1;
33  std::shared_ptr<libsnark::digest_variable<FieldT>> intermediate_hash;
34  std::shared_ptr<libsnark::sha256_compression_function_gadget<FieldT>>
35  hasher2;
36 
37 public:
38  // Important to define the hash_value_type as it is used in the merkle tree
39  typedef libff::bit_vector hash_value_type;
40  // Same as above, this is used in the merkle tree
41  typedef libsnark::merkle_authentication_path
43 
45  libsnark::protoboard<FieldT> &pb,
46  const libsnark::block_variable<FieldT> &input_block,
47  const libsnark::digest_variable<FieldT> &output,
48  const std::string &annotation_prefix = "sha256_ethereum");
49 
50  void generate_r1cs_constraints(const bool ensure_output_bitness = true);
51  void generate_r1cs_witness();
52 
53  static size_t get_block_len();
54  static size_t get_digest_len();
55  static libff::bit_vector get_hash(const libff::bit_vector &input);
56 
57  static size_t expected_constraints(const bool ensure_output_bitness);
58 };
59 
60 } // namespace libzeth
61 
63 
64 #endif // __ZETH_CIRCUITS_SHA256_ETHEREUM_HPP__
libzeth
Definition: binary_operation.hpp:15
libzeth::sha256_ethereum::get_digest_len
static size_t get_digest_len()
libzeth::sha256_ethereum::sha256_ethereum
sha256_ethereum(libsnark::protoboard< FieldT > &pb, const libsnark::block_variable< FieldT > &input_block, const libsnark::digest_variable< FieldT > &output, const std::string &annotation_prefix="sha256_ethereum")
libzeth::SHA256_ETH_block_size
const size_t SHA256_ETH_block_size
Definition: sha256_ethereum.hpp:23
sha256_ethereum.tcc
libzeth::SHA256_ETH_digest_size
const size_t SHA256_ETH_digest_size
Definition: sha256_ethereum.hpp:22
libzeth::sha256_ethereum::expected_constraints
static size_t expected_constraints(const bool ensure_output_bitness)
libzeth::sha256_ethereum::get_block_len
static size_t get_block_len()
libzeth::sha256_ethereum::hash_value_type
libff::bit_vector hash_value_type
Definition: sha256_ethereum.hpp:39
libzeth::sha256_ethereum::get_hash
static libff::bit_vector get_hash(const libff::bit_vector &input)
libzeth::sha256_ethereum::merkle_authentication_path_type
libsnark::merkle_authentication_path merkle_authentication_path_type
Definition: sha256_ethereum.hpp:42
libzeth::sha256_ethereum
Definition: sha256_ethereum.hpp:26
libzeth::sha256_ethereum::generate_r1cs_witness
void generate_r1cs_witness()
libzeth::sha256_ethereum::generate_r1cs_constraints
void generate_r1cs_constraints(const bool ensure_output_bitness=true)