Skip to content

Commit

Permalink
batch test for contracts
Browse files Browse the repository at this point in the history
Signed-off-by: Chengxuan Xing <[email protected]>
  • Loading branch information
Chengxuan committed Sep 26, 2024
1 parent 2a989b6 commit 7602dae
Show file tree
Hide file tree
Showing 9 changed files with 1,016 additions and 282 deletions.
28 changes: 12 additions & 16 deletions solidity/test/gas_cost/zeto_anon_enc_nullifier_kyc_cost_analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ describe.skip('(Gas cost analysis) Zeto based fungible token with anonymity usin
owners: User[],
gasHistories: number[]
) {
let nullifiers: [BigNumberish, BigNumberish];
let outputCommitments: [BigNumberish, BigNumberish];
let nullifiers: BigNumberish[];
let outputCommitments: BigNumberish[];
let encryptedValues: BigNumberish[];
let encryptionNonce: BigNumberish;
let encodedProof: any;
Expand Down Expand Up @@ -470,22 +470,18 @@ describe.skip('(Gas cost analysis) Zeto based fungible token with anonymity usin
BigNumberish,
BigNumberish
];
const inputCommitments: [BigNumberish, BigNumberish] = inputs.map(
const inputCommitments: BigNumberish[] = inputs.map(
(input) => input.hash
) as [BigNumberish, BigNumberish];
) as BigNumberish[];
const inputValues = inputs.map((input) => BigInt(input.value || 0n));
const inputSalts = inputs.map((input) => input.salt || 0n);
const outputCommitments: [BigNumberish, BigNumberish] = outputs.map(
const outputCommitments: BigNumberish[] = outputs.map(
(output) => output.hash
) as [BigNumberish, BigNumberish];
) as BigNumberish[];
const outputValues = outputs.map((output) => BigInt(output.value || 0n));
const outputOwnerPublicKeys: [
[BigNumberish, BigNumberish],
[BigNumberish, BigNumberish]
] = owners.map((owner) => owner.babyJubPublicKey) as [
[BigNumberish, BigNumberish],
[BigNumberish, BigNumberish]
];
const outputOwnerPublicKeys: BigNumberish[][] = owners.map(
(owner) => owner.babyJubPublicKey
) as BigNumberish[][];
const encryptionNonce: BigNumberish = newEncryptionNonce() as BigNumberish;
const encryptInputs = stringifyBigInts({
encryptionNonce,
Expand All @@ -499,7 +495,7 @@ describe.skip('(Gas cost analysis) Zeto based fungible token with anonymity usin
inputSalts,
inputOwnerPrivateKey: signer.formattedPrivateKey,
utxosRoot,
enabled: [nullifiers[0] !== 0n ? 1 : 0, nullifiers[1] !== 0n ? 1 : 0],
enabled: nullifiers.map((n) => (n !== 0n ? 1 : 0)),
utxosMerkleProof,
identitiesRoot,
identitiesMerkleProof,
Expand Down Expand Up @@ -539,8 +535,8 @@ describe.skip('(Gas cost analysis) Zeto based fungible token with anonymity usin

async function sendTx(
signer: User,
nullifiers: [BigNumberish, BigNumberish],
outputCommitments: [BigNumberish, BigNumberish],
nullifiers: BigNumberish[],
outputCommitments: BigNumberish[],
root: BigNumberish,
encryptedValues: BigNumberish[],
encryptionNonce: BigNumberish,
Expand Down
104 changes: 67 additions & 37 deletions solidity/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { readFileSync } from "fs";
import * as path from "path";
import { readFileSync } from 'fs';
import * as path from 'path';
import { BigNumberish } from 'ethers';
import { groth16 } from 'snarkjs';
import { loadCircuit, encodeProof } from "zeto-js";
import { User, UTXO } from "./lib/utils";
import { loadCircuit, encodeProof } from 'zeto-js';
import { User, UTXO } from './lib/utils';

function provingKeysRoot() {
const PROVING_KEYS_ROOT = process.env.PROVING_KEYS_ROOT;
if (!PROVING_KEYS_ROOT) {
throw new Error("PROVING_KEYS_ROOT env var is not set");
throw new Error('PROVING_KEYS_ROOT env var is not set');
}
return PROVING_KEYS_ROOT;
}
Expand All @@ -45,46 +45,64 @@ export function loadProvingKeys(type: string) {
export async function prepareDepositProof(signer: User, output: UTXO) {
const outputCommitments: [BigNumberish] = [output.hash] as [BigNumberish];
const outputValues = [BigInt(output.value || 0n)];
const outputOwnerPublicKeys: [[BigNumberish, BigNumberish]] = [signer.babyJubPublicKey] as [[BigNumberish, BigNumberish]];
const outputOwnerPublicKeys: [[BigNumberish, BigNumberish]] = [
signer.babyJubPublicKey,
] as [[BigNumberish, BigNumberish]];

const inputObj = {
outputCommitments,
outputValues,
outputSalts: [output.salt],
outputOwnerPublicKeys
outputOwnerPublicKeys,
};

const circuit = await loadCircuit('check_hashes_value');
const { provingKeyFile } = loadProvingKeys('check_hashes_value');

const startWitnessCalculation = Date.now();
const witness = await circuit.calculateWTNSBin(
inputObj,
true
);
const witness = await circuit.calculateWTNSBin(inputObj, true);
const timeWithnessCalculation = Date.now() - startWitnessCalculation;

const startProofGeneration = Date.now();
const { proof, publicSignals } = await groth16.prove(provingKeyFile, witness) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
const { proof, publicSignals } = (await groth16.prove(
provingKeyFile,
witness
)) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
const timeProofGeneration = Date.now() - startProofGeneration;

console.log(`Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`);
console.log(
`Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`
);

const encodedProof = encodeProof(proof);
return {
outputCommitments,
encodedProof
encodedProof,
};
}

export async function prepareNullifierWithdrawProof(signer: User, inputs: UTXO[], _nullifiers: UTXO[], output: UTXO, root: BigInt, merkleProof: BigInt[][]) {
const nullifiers = _nullifiers.map((nullifier) => nullifier.hash) as [BigNumberish, BigNumberish];
const inputCommitments: [BigNumberish, BigNumberish] = inputs.map((input) => input.hash) as [BigNumberish, BigNumberish];
export async function prepareNullifierWithdrawProof(
signer: User,
inputs: UTXO[],
_nullifiers: UTXO[],
output: UTXO,
root: BigInt,
merkleProof: BigInt[][]
) {
const nullifiers = _nullifiers.map((nullifier) => nullifier.hash) as [
BigNumberish,
BigNumberish
];
const inputCommitments: BigNumberish[] = inputs.map(
(input) => input.hash
) as BigNumberish[];
const inputValues = inputs.map((input) => BigInt(input.value || 0n));
const inputSalts = inputs.map((input) => input.salt || 0n);
const outputCommitments: [BigNumberish] = [output.hash] as [BigNumberish];
const outputValues = [BigInt(output.value || 0n)];
const outputOwnerPublicKeys: [[BigNumberish, BigNumberish]] = [signer.babyJubPublicKey] as [[BigNumberish, BigNumberish]];
const outputOwnerPublicKeys: [[BigNumberish, BigNumberish]] = [
signer.babyJubPublicKey,
] as [[BigNumberish, BigNumberish]];

const inputObj = {
nullifiers,
Expand All @@ -98,39 +116,49 @@ export async function prepareNullifierWithdrawProof(signer: User, inputs: UTXO[]
outputCommitments,
outputValues,
outputSalts: [output.salt],
outputOwnerPublicKeys
outputOwnerPublicKeys,
};
const circuit = await loadCircuit('check_nullifier_value');
const { provingKeyFile } = loadProvingKeys('check_nullifier_value');

const startWitnessCalculation = Date.now();
const witness = await circuit.calculateWTNSBin(
inputObj,
true
);
const witness = await circuit.calculateWTNSBin(inputObj, true);
const timeWithnessCalculation = Date.now() - startWitnessCalculation;

const startProofGeneration = Date.now();
const { proof, publicSignals } = await groth16.prove(provingKeyFile, witness) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
const { proof, publicSignals } = (await groth16.prove(
provingKeyFile,
witness
)) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
const timeProofGeneration = Date.now() - startProofGeneration;

console.log(`Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`);
console.log(
`Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`
);

const encodedProof = encodeProof(proof);
return {
nullifiers,
outputCommitments,
encodedProof
encodedProof,
};
}

export async function prepareWithdrawProof(signer: User, inputs: UTXO[], output: UTXO) {
const inputCommitments: [BigNumberish, BigNumberish] = inputs.map((input) => input.hash) as [BigNumberish, BigNumberish];
export async function prepareWithdrawProof(
signer: User,
inputs: UTXO[],
output: UTXO
) {
const inputCommitments: BigNumberish[] = inputs.map(
(input) => input.hash
) as BigNumberish[];
const inputValues = inputs.map((input) => BigInt(input.value || 0n));
const inputSalts = inputs.map((input) => input.salt || 0n);
const outputCommitments: [BigNumberish] = [output.hash] as [BigNumberish];
const outputValues = [BigInt(output.value || 0n)];
const outputOwnerPublicKeys: [[BigNumberish, BigNumberish]] = [signer.babyJubPublicKey] as [[BigNumberish, BigNumberish]];
const outputOwnerPublicKeys: [[BigNumberish, BigNumberish]] = [
signer.babyJubPublicKey,
] as [[BigNumberish, BigNumberish]];

const inputObj = {
inputCommitments,
Expand All @@ -140,28 +168,30 @@ export async function prepareWithdrawProof(signer: User, inputs: UTXO[], output:
outputCommitments,
outputValues,
outputSalts: [output.salt],
outputOwnerPublicKeys
outputOwnerPublicKeys,
};
const circuit = await loadCircuit('check_inputs_outputs_value');
const { provingKeyFile } = loadProvingKeys('check_inputs_outputs_value');

const startWitnessCalculation = Date.now();
const witness = await circuit.calculateWTNSBin(
inputObj,
true
);
const witness = await circuit.calculateWTNSBin(inputObj, true);
const timeWithnessCalculation = Date.now() - startWitnessCalculation;

const startProofGeneration = Date.now();
const { proof, publicSignals } = await groth16.prove(provingKeyFile, witness) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
const { proof, publicSignals } = (await groth16.prove(
provingKeyFile,
witness
)) as { proof: BigNumberish[]; publicSignals: BigNumberish[] };
const timeProofGeneration = Date.now() - startProofGeneration;

console.log(`Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`);
console.log(
`Witness calculation time: ${timeWithnessCalculation}ms. Proof generation time: ${timeProofGeneration}ms.`
);

const encodedProof = encodeProof(proof);
return {
inputCommitments,
outputCommitments,
encodedProof
encodedProof,
};
}
Loading

0 comments on commit 7602dae

Please sign in to comment.