Skip to content

Commit

Permalink
add root in the input class
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Mar 17, 2024
1 parent ca969e9 commit ec66f3f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InputMap } from '@noir-lang/noir_js';

import input_json from '@ultralane/circuits/bin/input/target/input.json';
import input_16_json from '@ultralane/circuits/bin/input_16/target/input_16.json';

import { Field, FieldRaw } from './field';
import { Note, NoteRaw } from './note';
Expand All @@ -17,7 +17,8 @@ export class Input {
public note: Note,
public pathIndex: Field,
public pathElements: Field[],
public MERKLE_DEPTH = 32
public root: Field,
public MERKLE_DEPTH = 16
) {
if (pathElements.length !== MERKLE_DEPTH) {
throw new Error(
Expand All @@ -27,21 +28,23 @@ export class Input {
}

static async circuit() {
return circuit(input_json);
return circuit(input_16_json);
}

static async random(
note?: Note,
pathIndex?: Field,
pathElements?: Field[],
MERKLE_DEPTH = 32
root?: Field,
MERKLE_DEPTH = 16
) {
pathElements =
pathElements ?? new Array(MERKLE_DEPTH).fill(0).map(() => Field.random());
return new Input(
note ?? (await Note.random()),
pathIndex ?? Field.random(),
pathElements
pathElements,
root ?? Field.random()
);
}

Expand All @@ -52,4 +55,14 @@ export class Input {
path_elements: this.pathElements.map((e) => e.raw()),
};
}

async prove(extAddress: string) {
const noir = await circuit(input_16_json);
const proof = await noir.generateProof({
root: this.root.raw(),
input: this.raw(),
_ext_address: Field.from(extAddress).raw(),
});
return proof;
}
}
1 change: 1 addition & 0 deletions src/note-merkle-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class NoteMerkleTree extends MerkleTree {
note,
new Field(index),
await this.merkleProof(index),
await this.calculateRoot(),
this.depth
);
}
Expand Down
15 changes: 12 additions & 3 deletions test/input.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Input } from '../src';
import { Field, Input } from '../src';
import 'jest';

describe('Input', () => {
it('raw', async () => {
// skipped because Input.random does not satisfy the constraint `assert_root`
it.skip('raw', async () => {
// this does not work anymore because input circuit was changed
const input = await Input.random();
const noir = await Input.circuit();
const _ext_address = Field.random();
const res = await noir.execute({
root: input.root.raw(),
input: input.raw(),
_ext_address: _ext_address.raw(),
});
expect(res.returnValue).toEqual(input.raw());
const val = res.returnValue as string[];
expect(val[0]).toEqual(input.note.amount.raw());
expect(val[1]).toEqual(await input.note.nullifierHex(input.pathIndex));
expect(val.length).toEqual(2);
});
});

0 comments on commit ec66f3f

Please sign in to comment.