cryptographyethereumpragmacircomzk-snark

Hashing function Sha256 in Circom


During the hackathon ETH Global Paris Was attempting to integrate a circom circuit with hashing a birthday date to prove that the user know the date following a well known medium tutorial. Here is its code

pragma circom 2.0.0;
include "./circomlib/circuits/sha256/sha256.circom";

template Birthday(){
  component SHA = Sha256(6);
  signal input date[6];
  SHA.in <== date;

  signal output date_out[256];
  date_out <== SHA.out;
}

component main { public [ date ] } = Birthday();

/* INPUT = {
    "date": [10, 3, 0, 3, 0, 1]
} */

Errors, with Error: Assert Failed.

Error in template BinSum_17 line: 100

Error in template SigmaPlus_18 line: 44

Error in template Sha256compression_97 line: 83

Error in template Sha256_98 line: 73

Error in template Birthday_99 line: 7


Solution

  • I have asked my friend Kai Jun Eer on whether what I was implementing made any sense. He dived into the code for SHA256 and confirmed my suspicion that its implementation is not the best, to day the least.

    Kai, co creator of zrclib, recommended me to use Poseidon which is far more efficient.

    So here is the circuit code that helped us win 2023 ETH Global Paris:

    pragma circom 2.0.0;
    
    include "./circomlib/circuits/poseidon.circom";
    
    template Location(){
        signal input in[2];
        signal output out;
    
        component poseidon = Poseidon(2);
    
        poseidon.inputs[0] <== in[0];
        poseidon.inputs[1] <== in[1];
        out <== poseidon.out;
    }
    
    component main { public [ in ] } = Location();
    
    /* INPUT = {
        "in": [100, 100]
    } */