TIZ
TIZ

Reputation: 181

Pedersen circom/circomlibjs inconsistency?

As a unit test for a larger use case, I am checking that indeed the pedersen hash I am doing in the frontend aligns with the expected hash done through a circom circuit. I am using a simple assert in the circuit and generating a witness and am feeding both the hashed and unhashed values to the circuit, recreating the hash to make sure that it goes through.

I am running a Pedersen hash in my frontend using circomlibjs. As a unit test, I have. a circuit with a simple assert that check whether the results from my frontend line up with the pedersen hash in the circom circuit.

The circuit I am using:

include "../node_modules/circomlib/circuits/bitify.circom";
include "../node_modules/circomlib/circuits/pedersen.circom";

template check() {
    signal input unhashed;
    signal input hashed;
    signal output createdHash[2];

    component hasher = Pedersen(256);
    component unhashedBits = Num2Bits(256);

    unhashedBits.in <== unhashed;

    for (var i = 0; i < 256; i++){
        hasher.in[i] <== unhashedBits.out[i];
    }

    createdHash[0] <== hasher.out[0];
    createdHash[1] <== hasher.out[1];

    hashed === createdHash[1];
}

component main = check();

In the frontend, I am running the following,

import { buildPedersenHash } from 'circomlibjs';


export function buff2hex(buff) {
    function i2hex(i) {
      return ('0' + i.toString(16)).slice(-2);
    }
    return '0x' + Array.from(buff).map(i2hex).join('');
}


const secret = (new TextEncoder(32)).encode("Hello");

var pedersen = await buildPedersenHash();
var h = pedersen.hash(secret);

console.log(buff2hex(secret));
console.log(buff2hex(h));

The values that are printed are:

0x48656c6c6f
0x0e90d7d613ab8b5ea7f4f8bc537db6bb0fa2e5e97bbac1c1f609ef9e6a35fd8b

Which are consistent with the test done here.

So I then create an input.json file which looks as follows,

{
    "unhashed": "0x48656c6c6f",
    "hashed": "0x0e90d7d613ab8b5ea7f4f8bc537db6bb0fa2e5e97bbac1c1f609ef9e6a35fd8b" 
}

And lastly run the following script to create a witness, in the hopes that the assert will go through.

# Compile the circuit
circom ${CIRCUIT}.circom --r1cs --wasm --sym --c

# Generate the witness.wtns
node ${CIRCUIT}_js/generate_witness.js ${CIRCUIT}_js/${CIRCUIT}.wasm input.json ${CIRCUIT}_js/witness.wtns

However, I keep getting an assert error,

Error: Error: Assert Failed.
Error in template check_11 line: 26

Which describes the assert in the circuit, so I assume there is an inconsistency in the hash.

Upvotes: 5

Views: 791

Answers (1)

TIZ
TIZ

Reputation: 181

For anyone who stumbles across this, it happens that the cause of issue is endianess. The issue was fixed by converting the unhashed to little endian in the input, I am not sure as to where exactly the problem is, but seems like the hasher reads it as big endian on the frontend but the input is expected little endian (or vice verse).

As I have managed to patch up a fix for this at the moment, I will stop investigating, but implore anyone who understand this further to give a better explanation.

Upvotes: 4

Related Questions