Skip to content

Commit

Permalink
ZK-559: (part 2) unit tests for shielder-sdk (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
kroist authored Jan 16, 2025
1 parent 142ae47 commit 70c5b53
Show file tree
Hide file tree
Showing 9 changed files with 1,017 additions and 64 deletions.
3 changes: 2 additions & 1 deletion ts/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default tseslint.config(
"eslint.config.mjs",
"**/update-imports.mjs",
"**/jest.config.ts",
"**/__tests/"
"**/__tests/",
"**/coverage/"
]
},
{
Expand Down
38 changes: 11 additions & 27 deletions ts/shielder-sdk/__tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
Converter,
CryptoClient,
DepositAdvice,
DepositCircuit,
DepositPubInputs,
Hasher,
NewAccountAdvice,
NewAccountCircuit,
Expand All @@ -19,7 +21,7 @@ import {
import { hexToBigInt } from "viem";

const SCALAR_MODULO = r;
export const ARITY = 7;
export const HASH_RATE = 7;
export const NOTE_VERSION = 0n;

const mockedHash = async (inputs: Scalar[]): Promise<Scalar> => {
Expand All @@ -36,7 +38,7 @@ const mockedHash = async (inputs: Scalar[]): Promise<Scalar> => {
};

const fullArityHash = async (inputs: Scalar[]): Promise<Scalar> => {
const scalarArray: Scalar[] = new Array<Scalar>(ARITY).fill(
const scalarArray: Scalar[] = new Array<Scalar>(HASH_RATE).fill(
Scalar.fromBigint(0n)
);
inputs.forEach((input, index) => {
Expand Down Expand Up @@ -86,7 +88,7 @@ class MockedHasher implements Hasher {
}

poseidonRate(): Promise<number> {
return Promise.resolve(7);
return Promise.resolve(HASH_RATE);
}
}

Expand All @@ -107,48 +109,30 @@ class MockedConverter implements Converter {

class MockedNoteTreeConfig implements NoteTreeConfig {
treeHeight(): Promise<number> {
return Promise.resolve(13);
return Promise.resolve(1);
}

arity(): Promise<number> {
return Promise.resolve(7);
return Promise.resolve(2);
}
}

class MockedNewAccountCircuit implements NewAccountCircuit {
prove(values: NewAccountAdvice): Promise<Proof> {
const string = JSON.stringify(values);
const encoder = new TextEncoder();
const data = encoder.encode(string);
return Promise.resolve(data);
return Promise.resolve(new Uint8Array());
}

async verify(proof: Proof, pubInputs: NewAccountPubInputs): Promise<boolean> {
const proofString = new TextDecoder().decode(proof);
const advice: NewAccountAdvice = JSON.parse(proofString);

return (
scalarsEqual(pubInputs.hId, await mockedHash([advice.id])) &&
scalarsEqual(
pubInputs.hNote,
await hashedNote(
advice.id,
advice.nullifier,
advice.trapdoor,
advice.initialDeposit
)
) &&
scalarsEqual(pubInputs.initialDeposit, advice.initialDeposit)
);
return Promise.resolve(true);
}
}

class MockedDepositCircuit implements DepositCircuit {
prove(): Promise<Proof> {
prove(values: DepositAdvice): Promise<Proof> {
return Promise.resolve(new Uint8Array());
}

verify(): Promise<boolean> {
async verify(proof: Proof, pubInputs: DepositPubInputs): Promise<boolean> {
return Promise.resolve(true);
}
}
Expand Down
Loading

0 comments on commit 70c5b53

Please sign in to comment.