noir-lang/noir-examples

Perdersen hash result on the circuit is different from that on the @aztec/bb.js library

Closed this issue · 4 comments

Perdersen hash result on the circuit is different from that on the @aztec/bb.js library. I ran your test at stealdrop and got the error that new_root is different from merkle_root. Then I write a simple test to check the perdersen hash of 1 and 2. The result in the circuit is 0x092d73d58bf37ff31acb813bb9b3b8aef71ef86db847bc92e9513bcbd98f7ac7, and the result in the library is 0x1ecc3e451bab2412ce126da45504efbfec396a455045bfec.

Code in circuit:

fn main(
  x: pub Field,
  y: pub Field,
) {
  let d = std::hash::pedersen([x,y]);
  std::println(d);
}

Code in ts:

import { Fr } from '@aztec/bb.js/dest/types';
describe("test", () => {
    test("pedersen hash", async () => {
        var api = await newBarretenbergApiSync();
        api.pedersenHashInit();
        var z = BigInt(api.pedersenHashPair(new Fr(1n), new Fr(2n)).toString()).toString(16);
        console.log(z);

    })
})

The pedersen hash implementation for noir (and bb.js) changed in a recent version. You can see the foundry-voting example was updated here: 8472f63

@signorecello will update the stealthdrop soon--he's out for a bit. If you'd like to take a shot at updating the example, feel free to open a PR.

@critesjosh The foundary-voting example only has the pedersen hash in noir. I need js library that support the pedersen hash like that. Is there any other hash function in bb.js with the same implementation as in noir?

You can do pedersen hashes with bb.js. You can see how it is done in the tests here.

Call api.pedersenHashInit(); then maybe api.pedersenHashMultiple

I am working on getting the exact function that will do you what you need, but try it out.

hey @chung080702 you should have a correct implementation on the main branch now, thanks for reporting!