From 7a82095179a8d3d99949823346ef083673f82eff Mon Sep 17 00:00:00 2001 From: Michael Turner Date: Wed, 25 Oct 2023 20:42:24 +0200 Subject: [PATCH] Create verification function --- .../template-react-zkml/src/workers/worker.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/create-aleo-app/template-react-zkml/src/workers/worker.js b/create-aleo-app/template-react-zkml/src/workers/worker.js index 41a5c2601..55797d8e3 100644 --- a/create-aleo-app/template-react-zkml/src/workers/worker.js +++ b/create-aleo-app/template-react-zkml/src/workers/worker.js @@ -1,12 +1,13 @@ import { Account, + Execution, ProgramManager, PrivateKey, initThreadPool, AleoKeyProvider, AleoNetworkClient, Program, - NetworkRecordProvider, AleoKeyProviderParams, + NetworkRecordProvider, AleoKeyProviderParams, verifyFunctionExecution } from "@aleohq/sdk"; import { expose, proxy } from "comlink"; import {sample_inputs} from "../variables.js"; @@ -55,14 +56,16 @@ async function localProgramExecution(program_source, aleoFunction, inputs) { if (cacheFunctionKeys) { console.log("Caching keys"); const keys = executionResponse.getKeys(program.id(), aleoFunction); - programManager.keyProvider.cacheKeys(keySearchParams.cacheKey, [keys.provingKey(), keys.verifyingKey()]); + const verifyingKey = keys.verifyingKey(); + + programManager.keyProvider.cacheKeys(keySearchParams.cacheKey, [keys.provingKey(), verifyingKey]); console.log(`Cached keys for ${keySearchParams.cacheKey}`); } console.log("Getting outputs"); const outputs = executionResponse.getOutputs(); // proof: executionResponse. console.log("outputs", outputs); - return outputs; + return [outputs, executionResponse.getExecution().toString()]; } async function getPrivateKey() { @@ -70,6 +73,14 @@ async function getPrivateKey() { return proxy(key); } +async function verifyExecution(execution_string, program, aleoFunction) { + const execution = Execution.fromString(execution_string); + const keySearchParams = new AleoKeyProviderParams({cacheKey: `${program.id()}/${aleoFunction}`}); + + const [provingKey, verifyingKey] = programManager.keyProvider.functionKeys(keySearchParams); + return verifyFunctionExecution(execution, verifyingKey, program, aleoFunction); +} + async function deployProgram(program) { const keyProvider = new AleoKeyProvider(); keyProvider.useCache(true); @@ -106,5 +117,5 @@ async function deployProgram(program) { return tx_id; } -const workerMethods = { deployProgram, getPrivateKey, localProgramExecution, synthesizeKeys }; +const workerMethods = { deployProgram, getPrivateKey, localProgramExecution, synthesizeKeys, verifyExecution}; expose(workerMethods);