noir-lang / noir-examples

A repo of example Noir projects.
MIT License
40 stars 14 forks source link

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

Closed chung080702 closed 1 year ago

chung080702 commented 1 year ago

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);

    })
})
critesjosh commented 1 year ago

The pedersen hash implementation for noir (and bb.js) changed in a recent version. You can see the foundry-voting example was updated here: https://github.com/noir-lang/noir-examples/commit/8472f63ca0f057ebcb60440daf25b22292afb087

@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.

chung080702 commented 1 year ago

@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?

critesjosh commented 1 year ago

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.

signorecello commented 1 year ago

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