feat: align latest zkstats lib and print srs path

- add generate_data_commitment.ipynb for data providers to commit to data
- change template.ipynb
	- support commitment and selected columns
	- print srs path. need to copy the srs from the path under `assets/out`
		to make verifyProof work
- modify page.tsx to work with generated data
This commit is contained in:
mhchia
2024-04-11 00:19:32 +08:00
parent f2aa02d6b7
commit d32299bf5d
7 changed files with 316 additions and 99 deletions

14
package-lock.json generated
View File

@@ -8,7 +8,7 @@
"name": "demo-next",
"version": "0.1.0",
"dependencies": {
"@ezkljs/engine": "7.0.0",
"@ezkljs/engine": "9.1.0",
"next": "14.0.4",
"react": "^18",
"react-dom": "^18"
@@ -115,9 +115,9 @@
}
},
"node_modules/@ezkljs/engine": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/@ezkljs/engine/-/engine-7.0.0.tgz",
"integrity": "sha512-mn1TFsNCwkpJTfTEX/YjLMCMRBvhwtnN7CGha+zLBqvM4mDelw0HmZHXsUlypnISdxBF5V8WIdb75z0WC/s+1w==",
"version": "9.1.0",
"resolved": "https://registry.npmjs.org/@ezkljs/engine/-/engine-9.1.0.tgz",
"integrity": "sha512-Uh2SzH8ky+9x1254qMJLUULNJ1Zp/idFKbOmJCH+RZqqryKRaMwISOm2JrGpobk0F6jyArRfECZBuiJBQs2TQw==",
"dependencies": {
"@types/json-bigint": "^1.0.1",
"json-bigint": "^1.0.0"
@@ -4474,9 +4474,9 @@
"dev": true
},
"@ezkljs/engine": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/@ezkljs/engine/-/engine-7.0.0.tgz",
"integrity": "sha512-mn1TFsNCwkpJTfTEX/YjLMCMRBvhwtnN7CGha+zLBqvM4mDelw0HmZHXsUlypnISdxBF5V8WIdb75z0WC/s+1w==",
"version": "9.1.0",
"resolved": "https://registry.npmjs.org/@ezkljs/engine/-/engine-9.1.0.tgz",
"integrity": "sha512-Uh2SzH8ky+9x1254qMJLUULNJ1Zp/idFKbOmJCH+RZqqryKRaMwISOm2JrGpobk0F6jyArRfECZBuiJBQs2TQw==",
"requires": {
"@types/json-bigint": "^1.0.1",
"json-bigint": "^1.0.0"

View File

@@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@ezkljs/engine": "7.0.0",
"@ezkljs/engine": "9.1.0",
"next": "14.0.4",
"react": "^18",
"react-dom": "^18"

4
public/assets/data.json Normal file
View File

@@ -0,0 +1,4 @@
{
"x": [46.2, 40.4, 44.8, 48.1, 51.2, 91.9, 38.2],
"y": [3.5, 3.0, 3.3, 3.6, 3.8, 8.3, 3.1]
}

View File

@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: zkstats==0.1.5 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (0.1.5)\n",
"Requirement already satisfied: click<9.0.0,>=8.1.7 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (8.1.7)\n",
"Requirement already satisfied: ezkl==9.1.0 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (9.1.0)\n",
"Requirement already satisfied: matplotlib<4.0.0,>=3.8.2 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (3.8.3)\n",
"Requirement already satisfied: numpy<2.0.0,>=1.26.2 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (1.26.4)\n",
"Requirement already satisfied: onnx<2.0.0,>=1.15.0 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (1.15.0)\n",
"Requirement already satisfied: requests<3.0.0,>=2.31.0 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (2.31.0)\n",
"Requirement already satisfied: scipy<2.0.0,>=1.11.4 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (1.12.0)\n",
"Requirement already satisfied: statistics<2.0.0,>=1.0.3 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (1.0.3.5)\n",
"Requirement already satisfied: torch<3.0.0,>=2.1.1 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from zkstats==0.1.5) (2.2.2)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (1.2.0)\n",
"Requirement already satisfied: cycler>=0.10 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (0.12.1)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (4.49.0)\n",
"Requirement already satisfied: kiwisolver>=1.3.1 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (1.4.5)\n",
"Requirement already satisfied: packaging>=20.0 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (23.2)\n",
"Requirement already satisfied: pillow>=8 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (10.2.0)\n",
"Requirement already satisfied: pyparsing>=2.3.1 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (3.1.1)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (2.9.0.post0)\n",
"Requirement already satisfied: protobuf>=3.20.2 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from onnx<2.0.0,>=1.15.0->zkstats==0.1.5) (4.25.3)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from requests<3.0.0,>=2.31.0->zkstats==0.1.5) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from requests<3.0.0,>=2.31.0->zkstats==0.1.5) (3.6)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from requests<3.0.0,>=2.31.0->zkstats==0.1.5) (2.2.1)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from requests<3.0.0,>=2.31.0->zkstats==0.1.5) (2024.2.2)\n",
"Requirement already satisfied: docutils>=0.3 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from statistics<2.0.0,>=1.0.3->zkstats==0.1.5) (0.20.1)\n",
"Requirement already satisfied: filelock in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from torch<3.0.0,>=2.1.1->zkstats==0.1.5) (3.13.1)\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from torch<3.0.0,>=2.1.1->zkstats==0.1.5) (4.10.0)\n",
"Requirement already satisfied: sympy in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from torch<3.0.0,>=2.1.1->zkstats==0.1.5) (1.12)\n",
"Requirement already satisfied: networkx in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from torch<3.0.0,>=2.1.1->zkstats==0.1.5) (3.2.1)\n",
"Requirement already satisfied: jinja2 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from torch<3.0.0,>=2.1.1->zkstats==0.1.5) (3.1.3)\n",
"Requirement already satisfied: fsspec in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from torch<3.0.0,>=2.1.1->zkstats==0.1.5) (2024.2.0)\n",
"Requirement already satisfied: six>=1.5 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from python-dateutil>=2.7->matplotlib<4.0.0,>=3.8.2->zkstats==0.1.5) (1.16.0)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from jinja2->torch<3.0.0,>=2.1.1->zkstats==0.1.5) (2.1.5)\n",
"Requirement already satisfied: mpmath>=0.19 in /Users/mhchia/Library/Caches/pypoetry/virtualenvs/zkstats-brXmXluj-py3.12/lib/python3.12/site-packages (from sympy->torch<3.0.0,>=2.1.1->zkstats==0.1.5) (1.3.0)\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install zkstats==0.1.5"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"cwd = os.getcwd()\n",
"\n",
"# FIXME: fill this in with the path to your data\n",
"data_path = f\"{cwd}/data.json\"\n",
"# Paths to the output files\n",
"output_dir = f\"{cwd}/out\"\n",
"os.makedirs(output_dir, exist_ok=True)\n",
"\n",
"data_commitment_path = f\"{output_dir}/data_commitment.json\"\n",
"# [0, 20) should be enough for most cases\n",
"possible_scales = list(range(20))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from zkstats.core import generate_data_commitment\n",
"\n",
"generate_data_commitment(data_path, possible_scales, data_commitment_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

View File

@@ -21,17 +21,35 @@ export async function verifyProof(
verificationKeyPath: string,
srsPath: string,
) {
const [proof, settings, verificationKey, srs] = await Promise.all([
loadFileToBuffer(proofPath),
loadFileToBuffer(settingsPath),
loadFileToBuffer(verificationKeyPath),
loadFileToBuffer(srsPath),
]);
const proof = await loadFileToBuffer(proofPath)
const settings = await loadFileToBuffer(settingsPath)
const verificationKey = await loadFileToBuffer(verificationKeyPath)
const srs = await loadFileToBuffer(srsPath)
const output = verify(
new Uint8ClampedArray(proof),
new Uint8ClampedArray(verificationKey),
new Uint8ClampedArray(settings),
new Uint8ClampedArray(srs),
);
)
// const [proof, settings, verificationKey, srs] = await Promise.all([
// loadFileToBuffer(proofPath),
// loadFileToBuffer(settingsPath),
// loadFileToBuffer(verificationKeyPath),
// loadFileToBuffer(srsPath),
// ]);
// // * @param {Uint8ClampedArray} proof_js
// // * @param {Uint8ClampedArray} vk
// // * @param {Uint8ClampedArray} settings
// // * @param {Uint8ClampedArray} srs
// console.log("!@# proof=", proof)
// console.log("!@# settings=", settings)
// console.log("!@# verificationKey=", verificationKey)
// console.log("!@# srs=", srs)
// const output = verify(
// new Uint8ClampedArray(proof),
// new Uint8ClampedArray(verificationKey),
// new Uint8ClampedArray(settings),
// new Uint8ClampedArray(srs),
// );
return output
}

View File

@@ -5,24 +5,36 @@ import { initialize, verifyProof } from "./ezkl";
const host = "http://localhost:3000";
const assetsURL = `${host}/assets`;
const proofPath = `${assetsURL}/model.pf`;
const settingsPath = `${assetsURL}/settings.json`;
const verificationKeyPath = `${assetsURL}/model.vk`;
const srsPath = `${assetsURL}/kzg.srs`;
const outDir = `${assetsURL}/out`;
const proofPath = `${outDir}/model.pf`;
const settingsPath = `${outDir}/settings.json`;
const verificationKeyPath = `${outDir}/model.vk`;
// NOTE: kzg.srs is printed from the jupyter notebook
const srsPath = `${outDir}/kzg.srs`;
// template notebook settings
const templateNotebook = `${assetsURL}/template.ipynb`;
const codeInCell = 2;
const cellForSelectedColumns = 3;
const cellForUserComputation = 5;
/**
* Generate the notebook for the computation
*
*/
export async function generateJupyterNotebookForComputation(computation: string, templateURL: string) {
export async function generateJupyterNotebookForComputation(
computation: string,
selectedColumns: string[],
templateURL: string,
) {
const splitCode = computation.split("\n").map((line) => `${line}\n`);
const response = await fetch(templateURL);
const template = await response.json();
const codeCell = template.cells[codeInCell];
// Fill the selected columns in the notebook
const selectedColumnsCell = template.cells[cellForSelectedColumns];
// Put `selected_columns = ["col1", "col2", ...]` in the notebook
selectedColumnsCell.source = `selected_columns = ${JSON.stringify(selectedColumns)}`;
// Fill the user computation in the notebook
const codeCell = template.cells[cellForUserComputation];
codeCell.source = splitCode;
return JSON.stringify(template);
}
@@ -30,15 +42,15 @@ export async function generateJupyterNotebookForComputation(computation: string,
async function exampleDownloadNotebook() {
const name = "mean";
const date = Date.now();
const selectedColumns = ["x", "y"];
const computation = `import torch
from zkstats.computation import State
def computation(state: State, x: list[torch.Tensor]):
x_0 = x[0]
out_0 = state.median(x_0)
out_1 = state.median(x_0)
out_0 = state.median(x[0])
out_1 = state.median(x[1])
return state.mean(torch.tensor([out_0, out_1]).reshape(1,-1,1))`
const notebook = await generateJupyterNotebookForComputation(computation, templateNotebook)
const notebook = await generateJupyterNotebookForComputation(computation, selectedColumns, templateNotebook)
// Download for testing
const element = document.createElement("a");
const file = new Blob([notebook], {type: 'text/plain'});
@@ -61,7 +73,6 @@ const Page = () => {
console.log("!@# proofPath=", proofPath)
console.log("!@# settingsPath=", settingsPath)
console.log("!@# verificationKeyPath=", verificationKeyPath)
console.log("!@# srsPath=", srsPath)
const result = await verifyProof(
proofPath,
settingsPath,