Skip to content

Commit

Permalink
hash module
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Mar 15, 2024
1 parent ff86514 commit 5b66689
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
"typescript": "^5.4.2"
},
"dependencies": {
"@noir-lang/backend_barretenberg": "0.25.0-00d6494.nightly",
"@noir-lang/noir_js": "0.25.0-00d6494.nightly",
"ethers": "^6.11.1"
}
}
66 changes: 63 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class Field {
return new Field(0n);
}

hex(): string {
return zeroPadValue(toBeHex(this.value), 32);
}

add(other: Field): Field {
return new Field((this.value + other.value) % PRIME);
}
Expand Down
46 changes: 46 additions & 0 deletions src/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ProofData } from '@noir-lang/noir_js';

import hash_1 from '@ultralane/circuits/bin/hash_1/target/hash_1.json';
import hash_2 from '@ultralane/circuits/bin/hash_2/target/hash_2.json';
import hash_3 from '@ultralane/circuits/bin/hash_3/target/hash_3.json';

import { toBigInt } from 'ethers';

import { circuit } from './utils';
import { Field } from './field';

export async function hash(preimage: Field[]): Promise<Field> {
const json = hash_json(preimage.length);
const noir = await circuit(json);
const result = await noir.execute({
input: preimage.map((x) => x.hex()),
});
return new Field(toBigInt(result.returnValue as string));
}

export async function hash_prove(preimage: Field[]): Promise<ProofData> {
const json = hash_json(preimage.length);
const noir = await circuit(json);
const proof = await noir.generateProof({
input: preimage.map((x) => x.hex()),
});
return proof;
}

export function hash_json(length: number) {
let json;
switch (length) {
case 1:
json = hash_1;
break;
case 2:
json = hash_2;
break;
case 3:
json = hash_3;
break;
default:
throw new Error('preimage length not supported: ' + length);
}
return json;
}
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BarretenbergBackend } from '@noir-lang/backend_barretenberg';
import { Noir } from '@noir-lang/noir_js';

export async function circuit(json: any) {
const backend = new BarretenbergBackend(json);
const noir = new Noir(json, backend);
await noir.init();
return noir;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "esnext",
"moduleResolution": "node",
"target": "esnext",
"allowImportingTsExtensions": false
"allowImportingTsExtensions": false,
"resolveJsonModule": true
}
}

0 comments on commit 5b66689

Please sign in to comment.