Skip to content

Commit

Permalink
Merge pull request #795 from AleoHQ/zkml-rebase
Browse files Browse the repository at this point in the history
[Fix] Create proper serialization for ZkML proving & verification from worker
  • Loading branch information
kpandl authored Oct 27, 2023
2 parents 215eb9a + d445132 commit 4c24796
Show file tree
Hide file tree
Showing 29 changed files with 486 additions and 320 deletions.
24 changes: 11 additions & 13 deletions create-aleo-app/template-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function localProgramExecution(program, aleoFunction, inputs) {

// Create a key provider in order to re-use the same key for each execution
const keyProvider = new AleoKeyProvider();
keyProvider.useCache(true);
programManager.setKeyProvider(keyProvider);

// Pre-synthesize the program keys and then cache them in memory using key provider
Expand All @@ -36,24 +37,21 @@ async function localProgramExecution(program, aleoFunction, inputs) {
hello_hello_program,
"hello",
["5u32", "5u32"],
false,
true,
undefined,
keyProviderParams,
);
console.log(executionResponse.getOutputs())

executionResponse = await programManager.executeOffline(
hello_hello_program,
"hello",
["5u32", "5u32"],
false,
keyProviderParams,
);
return executionResponse.getOutputs();
console.log("hello_hello/hello executed - result:", executionResponse.getOutputs());

// Verify the execution using the verifying key that was generated earlier.
if (programManager.verifyExecution(executionResponse)) {
console.log("hello_hello/hello execution verified!");
} else {
throw("Execution failed verification!");
}
}

const start = Date.now();
console.log("Starting execute!");
const result = await localProgramExecution();
console.log(result);
await localProgramExecution();
console.log("Execute finished!", Date.now() - start);
2 changes: 1 addition & 1 deletion create-aleo-app/template-react-leo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"install-leo": "./install.sh"
},
"dependencies": {
"@aleohq/sdk": "^0.6.0",
"@aleohq/sdk": "^0.6.5",
"comlink": "^4.4.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
4 changes: 2 additions & 2 deletions create-aleo-app/template-react-leo/src/workers/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function deployProgram(program) {
keyProvider.useCache(true);

// Create a record provider that will be used to find records and transaction data for Aleo programs
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");

// Use existing account with funds
const account = new Account({
Expand All @@ -48,7 +48,7 @@ async function deployProgram(program) {

// Initialize a program manager to talk to the Aleo network with the configured key and record providers
const programManager = new ProgramManager(
"https://vm.aleo.org/api",
"https://api.explorer.aleo.org/v1",
keyProvider,
recordProvider,
);
Expand Down
15 changes: 5 additions & 10 deletions create-aleo-app/template-react-zkml/src/Main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,21 @@ const Main = () => {

console.log("starting to measure proving time, before execution")

var result = await aleoWorker.localProgramExecution(
var [result, executionResponse] = await aleoWorker.localProgramExecution(
model,
"main",
input_array,
true
);

var executionResponse = result[1];
// convert JSON from string to object
executionResponse = JSON.parse(executionResponse);
proofText = executionResponse["proof"];
proofText = executionResponse;
console.log("executionResponse", executionResponse)
result = result[0]

proving_end_time = performance.now();
console.log("proving time in seconds", (proving_end_time - proving_start_time) / 1000);

proving_finished = true;

//const execution = result.getExecution();

console.log("result", result);
//console.log("execution", execution);

Expand All @@ -117,7 +111,9 @@ const Main = () => {
//console.log("i", i)
//console.log("result[i]", result[i])
//console.log("typeof(result[i])", typeof(result[i]))
var output = String(result[i]).replace("i64", "");
// convert JSON result string to JSON object
const result_JSON = JSON.parse(result[i]);
var output = String(result_JSON["value"]).replace("i64", "");
//console.log("output", output)
output = Number(output);
output = output / output_fixed_point_scaling_factor;
Expand Down Expand Up @@ -166,7 +162,6 @@ const Main = () => {

//alert(JSON.stringify(converted_features));


}

const getTopLeftPixelData = async () => {
Expand Down
4 changes: 2 additions & 2 deletions create-aleo-app/template-react-zkml/src/variables.js

Large diffs are not rendered by default.

16 changes: 6 additions & 10 deletions create-aleo-app/template-react-zkml/src/workers/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
AleoNetworkClient,
Program,
NetworkRecordProvider,
ExecutionResponse,
AleoKeyProviderParams,
verifyFunctionExecution,
} from "@aleohq/sdk";
import { expose, proxy } from "comlink";
import {sample_inputs} from "../variables.js";
Expand All @@ -19,7 +19,7 @@ await initThreadPool();
// Initialize a program manager with a keyprovider that will cache our keys
const keyProvider = new AleoKeyProvider();
keyProvider.useCache(true);
const programManager = new ProgramManager({ host: "https://api.explorer.aleo.org/v1", keyProvider: keyProvider});
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, undefined);
const account = new Account();
programManager.setAccount(account);

Expand Down Expand Up @@ -65,9 +65,7 @@ async function localProgramExecution(program_source, aleoFunction, inputs) {

console.log("Getting outputs");
const outputs = executionResponse.getOutputs(); // proof: executionResponse.
const execution = executionResponse.getExecution().toString();
console.log("outputs", outputs);
console.log("execution", execution);
const execution = executionResponse.toString()
return [outputs, execution];
}

Expand All @@ -76,11 +74,9 @@ async function getPrivateKey() {
return proxy(key);
}

async function verifyExecution(execution_string, program, aleoFunction) {
const keySearchParams = new AleoKeyProviderParams({cacheKey: `${program.id()}/${aleoFunction}`});

const [provingKey, verifyingKey] = programManager.keyProvider.functionKeys(keySearchParams);
return verifyFunctionExecution(execution_string, verifyingKey, program, aleoFunction);
async function verifyExecution(execution) {
const executionResponse = ExecutionResponse.fromString(execution);
return programManager.verifyExecution(executionResponse);
}

async function deployProgram(program) {
Expand Down
26 changes: 13 additions & 13 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ const keyProvider = new AleoKeyProvider();
keyProvider.useCache = true;
// Create a record provider that will be used to find records and transaction data for Aleo programs
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const recordProvider = new NetworkRecordProvider(account, networkClient);
// Initialize a program manager to talk to the Aleo network with the configured key and record providers
const programName = "hello_hello.aleo";
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
// Provide a key search parameter to find the correct key for the program if they are stored in a memory cache
const keySearchParams = { "cacheKey": "hello_hello:hello" };
Expand Down Expand Up @@ -325,7 +325,7 @@ const keyProvider = new AleoKeyProvider();
keyProvider.useCache(true);

// Create a record provider that will be used to find records and transaction data for Aleo programs
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");

// Use existing account with funds
const account = new Account({
Expand All @@ -335,7 +335,7 @@ const account = new Account({
const recordProvider = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager to talk to the Aleo network with the configured key and record providers
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account)

// Define an Aleo program to deploy
Expand Down Expand Up @@ -388,9 +388,9 @@ import * as aleo from "@aleohq/sdk";
await aleo.initThreadPool();

/// The program manager is initialized with a key provider and a record provider
const defaultHost = "https://vm.aleo.org/api";
const defaultHost = "https://api.explorer.aleo.org/v1";
const keyProvider = new aleo.AleoKeyProvider();
const recordProvider = new aleo.NetworkRecordProvider(new Account(), "https://vm.aleo.org/api");
const recordProvider = new aleo.NetworkRecordProvider(new Account(), "https://api.explorer.aleo.org/v1");
const programManager = new aleo.ProgramManager(
defaultHost,
keyProvider,
Expand Down Expand Up @@ -767,13 +767,13 @@ import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNe

// Create a new NetworkClient, KeyProvider, and RecordProvider
const account = Account.from_string({privateKey: "user1PrivateKey"});
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const keyProvider = new AleoKeyProvider();
const recordProvider = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager with the key provider to automatically fetch keys for executions
const USER_1_ADDRESS = "user1Address";
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account);

// Send a private transfer to yourself
Expand Down Expand Up @@ -808,7 +808,7 @@ assert(public_balance === 0);
As shown above, a public balance of any address can be checked with `getMappingValue` function of the `NetworkClient`.
```typescript
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const USER_1_ADDRESS = "user1Address";
const public_balance = networkClient.getMappingValue("credits.aleo", USER_1_ADDRESS);
```
Expand Down Expand Up @@ -897,13 +897,13 @@ import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNe

// Create a new NetworkClient, KeyProvider, and RecordProvider
const account = Account.from_string({privateKey: "user1PrivateKey"});
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const keyProvider = new AleoKeyProvider();
const recordProvider = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager with the key provider to automatically fetch keys for executions
const USER_2_ADDRESS = "user2Address";
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account);

/// Send private transfer to user 2
Expand All @@ -918,12 +918,12 @@ import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNe

// Create a new NetworkClient, KeyProvider, and RecordProvider
const account = Account.from_string({privateKey: "user2PrivateKey"});
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const keyProvider = new AleoKeyProvider();
const recordProvider_User2 = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager with the key provider to automatically fetch keys for executions
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account);

// Fetch the transaction from the network that user 1 sent
Expand Down
2 changes: 1 addition & 1 deletion sdk/docs/AleoNetworkClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let local_connection = new AleoNetworkClient("http://localhost:3030");

// Connection to a public beacon node
let public_connection = new AleoNetworkClient("https://vm.aleo.org/api");</code></pre></div></div></div><h2 id="classes" class="subsection-title has-anchor">Classes</h2><dl><dt><a href="AleoNetworkClient.html">AleoNetworkClient</a></dt><dd></dd></dl><h2 id="methods" class="subsection-title has-anchor">Methods</h2><h3 class="name has-anchor" id="findUnspentRecords"><span class="type-signature">(async) </span>findUnspentRecords<span class="signature">()</span></h3><div class="description">Attempts to find unspent records in the Aleo blockchain for a specified private key</div><dl class="details"><div class="details-item-container"><dt class="tag-source bold">Source</dt><dd class="tag-source"><ul><li><a href="aleo_network_client.ts.html">network-client.ts</a>, <a href="aleo_network_client.ts.html#line230">line 230</a></li></ul></dd></div></dl><div class="method-member-container flex flex-col w-100 overflow-auto mt-20"><strong>Example</strong><div class="rel"><pre class="prettyprint"><code>// Find all unspent records
let public_connection = new AleoNetworkClient("https://api.explorer.aleo.org/v1");</code></pre></div></div></div><h2 id="classes" class="subsection-title has-anchor">Classes</h2><dl><dt><a href="AleoNetworkClient.html">AleoNetworkClient</a></dt><dd></dd></dl><h2 id="methods" class="subsection-title has-anchor">Methods</h2><h3 class="name has-anchor" id="findUnspentRecords"><span class="type-signature">(async) </span>findUnspentRecords<span class="signature">()</span></h3><div class="description">Attempts to find unspent records in the Aleo blockchain for a specified private key</div><dl class="details"><div class="details-item-container"><dt class="tag-source bold">Source</dt><dd class="tag-source"><ul><li><a href="aleo_network_client.ts.html">network-client.ts</a>, <a href="aleo_network_client.ts.html#line230">line 230</a></li></ul></dd></div></dl><div class="method-member-container flex flex-col w-100 overflow-auto mt-20"><strong>Example</strong><div class="rel"><pre class="prettyprint"><code>// Find all unspent records
const privateKey = "[PRIVATE_KEY]";
let records = connection.findUnspentRecords(0, undefined, privateKey);

Expand Down
2 changes: 1 addition & 1 deletion sdk/docs/aleo_network_client.ts.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* let local_connection = new AleoNetworkClient("http://localhost:3030");
*
* // Connection to a public beacon node
* let public_connection = new AleoNetworkClient("https://vm.aleo.org/api");
* let public_connection = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
*/
export class AleoNetworkClient {
host: string;
Expand Down
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aleohq/sdk",
"version": "0.6.2",
"version": "0.6.5",
"description": "A Software Development Kit (SDK) for Zero-Knowledge Transactions",
"collaborators": [
"The Aleo Team <[email protected]>"
Expand Down
14 changes: 7 additions & 7 deletions sdk/src/function-key-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ interface FunctionKeyProvider {
*
*
* const keyProvider = new AleoKeyProvider();
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
* // Initialize a program manager with the key provider to automatically fetch keys for value transfers
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
*
* // Keys can also be fetched manually
Expand All @@ -120,12 +120,12 @@ interface FunctionKeyProvider {
*
* @example
* // Create a new object which implements the KeyProvider interface
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const keyProvider = new AleoKeyProvider();
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
* // Initialize a program manager with the key provider to automatically fetch keys for value transfers
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
*
* // Keys can also be fetched manually
Expand Down Expand Up @@ -271,13 +271,13 @@ class AleoKeyProvider implements FunctionKeyProvider {
*
* @example
* // Create a new object which implements the KeyProvider interface
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const keyProvider = new AleoKeyProvider();
* const recordProvider = new NetworkRecordProvider(account, networkClient);
* const AleoProviderParams = new AleoProviderParams("https://testnet3.parameters.aleo.org/transfer_private.");
*
* // Initialize a program manager with the key provider to automatically fetch keys for value transfers
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
*
* // Keys can also be fetched manually using the key provider
Expand Down Expand Up @@ -323,7 +323,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
*
* @example
* // Create a new AleoKeyProvider object
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const keyProvider = new AleoKeyProvider();
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
Expand Down
Loading

0 comments on commit 4c24796

Please sign in to comment.