Input string of variable length in circom?

497 views Asked by At

I would like to show that a user knows a preimage to a sha256 hash in circom. The preimage can be of any length, but realistically between 100-700 bytes. I tried code:

template ArbitraryLengthSha256 () {  
   signal input nBits;
   signal input preimage[nBits];  
   signal output result[256];  
   var i;
   component hashed = Sha256(nBits);
   for (i=0; i<nBits; i++) {
      hashed.in[i] <== preimage[i];
   }
   for (i=0; i<256; i++) {
      result[i] <== hashed.out[i];
   }
}

which results in the error:

error[T20460]: Typing error found
  ┌─ "ppp.circom":8:26
  │
8 │    signal input preimage[nBits];  
  │                          ^^^^^ The length of every array must known during the constraint generation phase

error[T20461]: Typing error found
   ┌─ "ppp.circom":14:23
   │
14 │    component hashed = Sha256(nBits);
   │                       ^^^^^^^^^^^^^ Every component instantiation must be resolved during the constraint generation phase

error[T2005]: Typing error found
   ┌─ "ppp.circom":15:14
   │
15 │    for (i=0; i<nBits; i++) {
   │              ^^^^^^^ There are constraints depending on the value of the condition and it can be unknown during the constraint generation phase

Is there any way to do a sha256 hash of an arbitrary-length string in circom?

0

There are 0 answers