mirror of
https://github.com/zkonduit/ezkl.git
synced 2026-04-25 03:01:17 -04:00
765 lines
804 KiB
Plaintext
765 lines
804 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {
|
||
"id": "uKQPd7_5ANe-"
|
||
},
|
||
"source": [
|
||
"# Calculate the variance of an asset \n",
|
||
"\n",
|
||
"In this example we will calculate the variance of an asset using ezkl. Here's a diagram of the process:\n",
|
||
"\n",
|
||
""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {
|
||
"id": "2wIAHwqH2_mo"
|
||
},
|
||
"source": [
|
||
"**Import Dependencies**"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"metadata": {
|
||
"id": "9Byiv2Nc2MsK"
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"# check if notebook is in colab\n",
|
||
"try:\n",
|
||
" # install ezkl\n",
|
||
" import google.colab\n",
|
||
" import subprocess\n",
|
||
" import sys\n",
|
||
" subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", \"ezkl\"])\n",
|
||
" subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", \"onnx\"])\n",
|
||
"\n",
|
||
"# rely on local installation of ezkl if the notebook is not in colab\n",
|
||
"except:\n",
|
||
" pass\n",
|
||
"\n",
|
||
"import ezkl\n",
|
||
"import torch\n",
|
||
"import datetime\n",
|
||
"import pandas as pd\n",
|
||
"import requests\n",
|
||
"import json\n",
|
||
"import os\n",
|
||
"\n",
|
||
"import logging\n",
|
||
"\n",
|
||
"logging.basicConfig(level=logging.INFO)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {
|
||
"id": "osjj-0Ta3E8O"
|
||
},
|
||
"source": [
|
||
"**Create Computational Graph**"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {
|
||
"colab": {
|
||
"base_uri": "https://localhost:8080/"
|
||
},
|
||
"id": "x1vl9ZXF3EEW",
|
||
"outputId": "bda21d02-fe5f-4fb2-8106-f51a8e2e67aa"
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"cpu\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"from torch import nn\n",
|
||
"import torch\n",
|
||
"\n",
|
||
"\n",
|
||
"class Model(nn.Module):\n",
|
||
" def __init__(self):\n",
|
||
" super(Model, self).__init__()\n",
|
||
"\n",
|
||
" # x is a time series maybe the ETH/USDC price, that we get from Uni Oracle\n",
|
||
" def forward(self, x):\n",
|
||
" # return [torch.var(x, unbiased=False, dim=[1,2])]\n",
|
||
" return [torch.mean(x)]\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"circuit = Model()\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"x = 0.1*torch.rand(1,*[1,20], requires_grad=True)\n",
|
||
"\n",
|
||
"# # print(torch.__version__)\n",
|
||
"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
|
||
"\n",
|
||
"print(device)\n",
|
||
"\n",
|
||
"circuit.to(device)\n",
|
||
"\n",
|
||
"# Flips the neural net into inference mode\n",
|
||
"circuit.eval()\n",
|
||
"\n",
|
||
"# Export the model\n",
|
||
"torch.onnx.export(circuit, # model being run\n",
|
||
" x, # model input (or a tuple for multiple inputs)\n",
|
||
" \"lol.onnx\", # where to save the model (can be a file or file-like object)\n",
|
||
" export_params=True, # store the trained parameter weights inside the model file\n",
|
||
" opset_version=11, # the ONNX version to export the model to\n",
|
||
" do_constant_folding=True, # whether to execute constant folding for optimization\n",
|
||
" input_names = ['input'], # the model's input names\n",
|
||
" output_names = ['output'], # the model's output names\n",
|
||
" dynamic_axes={'input' : {0 : 'batch_size'}, # variable length axes\n",
|
||
" 'output' : {0 : 'batch_size'}})\n",
|
||
"\n",
|
||
"# export(circuit, input_shape=[1, 20])\n",
|
||
"\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {
|
||
"id": "E3qCeX-X5xqd"
|
||
},
|
||
"source": [
|
||
"**Set Data Source and Get Data**"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"metadata": {
|
||
"colab": {
|
||
"base_uri": "https://localhost:8080/"
|
||
},
|
||
"id": "6RAMplxk5xPk",
|
||
"outputId": "bd2158fe-0c00-44fd-e632-6a3f70cdb7c9"
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"1715422870\n",
|
||
"1714818070\n",
|
||
"https://api.coingecko.com/api/v3/coins/ethereum/market_chart/range?vs_currency=usd&from=1714818070&to=1715422870\n",
|
||
"<Response [200]>\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"\n",
|
||
"def get_url(coin, currency, start, end):\n",
|
||
" url = f\"https://api.coingecko.com/api/v3/coins/{coin}/market_chart/range?vs_currency={currency}&from={start}&to={end}\"\n",
|
||
" return url\n",
|
||
"\n",
|
||
"\n",
|
||
"timenow = datetime.datetime.now()\n",
|
||
"timenow_unix_sec = int(datetime.datetime.timestamp(timenow))\n",
|
||
"time7days = timenow - datetime.timedelta(days=7)\n",
|
||
"time7days_unix_sec = int(datetime.datetime.timestamp(time7days))\n",
|
||
"\n",
|
||
"print(timenow_unix_sec)\n",
|
||
"print(time7days_unix_sec)\n",
|
||
"\n",
|
||
"eth_price_url = get_url(\"ethereum\", \"usd\", time7days_unix_sec, timenow_unix_sec)\n",
|
||
"print(eth_price_url)\n",
|
||
"\n",
|
||
"data = requests.get(eth_price_url)\n",
|
||
"print(data)\n",
|
||
"\n",
|
||
"new_data = {\"time\": [], \"prices\": []}\n",
|
||
"\n",
|
||
"for k, v in data.json().items():\n",
|
||
" if k == \"prices\":\n",
|
||
" for x in v:\n",
|
||
" new_data[\"time\"].append(x[0])\n",
|
||
" new_data[\"prices\"].append(x[1])\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"metadata": {
|
||
"colab": {
|
||
"base_uri": "https://localhost:8080/",
|
||
"height": 424
|
||
},
|
||
"id": "WSj1Uxln65vf",
|
||
"outputId": "51422d71-9680-4b51-c4df-e400d20f988b"
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<div>\n",
|
||
"<style scoped>\n",
|
||
" .dataframe tbody tr th:only-of-type {\n",
|
||
" vertical-align: middle;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe tbody tr th {\n",
|
||
" vertical-align: top;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe thead th {\n",
|
||
" text-align: right;\n",
|
||
" }\n",
|
||
"</style>\n",
|
||
"<table border=\"1\" class=\"dataframe\">\n",
|
||
" <thead>\n",
|
||
" <tr style=\"text-align: right;\">\n",
|
||
" <th></th>\n",
|
||
" <th>time</th>\n",
|
||
" <th>prices</th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>1714820485367</td>\n",
|
||
" <td>3146.785806</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1</th>\n",
|
||
" <td>1714824033868</td>\n",
|
||
" <td>3127.968728</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2</th>\n",
|
||
" <td>1714828058243</td>\n",
|
||
" <td>3156.141681</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>3</th>\n",
|
||
" <td>1714831650751</td>\n",
|
||
" <td>3124.834064</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>4</th>\n",
|
||
" <td>1714834972229</td>\n",
|
||
" <td>3133.115333</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>...</th>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>163</th>\n",
|
||
" <td>1715407579346</td>\n",
|
||
" <td>2918.049749</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>164</th>\n",
|
||
" <td>1715411090715</td>\n",
|
||
" <td>2920.330834</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>165</th>\n",
|
||
" <td>1715414554830</td>\n",
|
||
" <td>2923.986611</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>166</th>\n",
|
||
" <td>1715418419843</td>\n",
|
||
" <td>2910.537671</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>167</th>\n",
|
||
" <td>1715421675338</td>\n",
|
||
" <td>2907.702307</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>168 rows × 2 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" time prices\n",
|
||
"0 1714820485367 3146.785806\n",
|
||
"1 1714824033868 3127.968728\n",
|
||
"2 1714828058243 3156.141681\n",
|
||
"3 1714831650751 3124.834064\n",
|
||
"4 1714834972229 3133.115333\n",
|
||
".. ... ...\n",
|
||
"163 1715407579346 2918.049749\n",
|
||
"164 1715411090715 2920.330834\n",
|
||
"165 1715414554830 2923.986611\n",
|
||
"166 1715418419843 2910.537671\n",
|
||
"167 1715421675338 2907.702307\n",
|
||
"\n",
|
||
"[168 rows x 2 columns]"
|
||
]
|
||
},
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"df = pd.DataFrame(new_data)\n",
|
||
"df\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {
|
||
"id": "eLJ7oirQ_HQR"
|
||
},
|
||
"source": [
|
||
"**EZKL Workflow**"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# make an input.json file from the df above\n",
|
||
"input_filename = os.path.join('input.json')\n",
|
||
"# get the last 20 prices and make a list\n",
|
||
"df_prices = df['prices'].tail(20).tolist()\n",
|
||
"\n",
|
||
"data = dict(input_data = [df_prices])\n",
|
||
"\n",
|
||
" # Serialize data into file:\n",
|
||
"json.dump( data, open(input_filename, 'w' ))"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"metadata": {
|
||
"colab": {
|
||
"base_uri": "https://localhost:8080/"
|
||
},
|
||
"id": "4MmE9SX66_Il",
|
||
"outputId": "16403639-66a4-4280-ac7f-6966b75de5a3"
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stderr",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"INFO:ezkl.execute:SRS already exists at that path\n",
|
||
"INFO:ezkl.execute:num calibration batches: 1\n",
|
||
"INFO:ezkl.execute:read 16777476 bytes from file (vector of len = 16777476)\n",
|
||
"WARNING:ezkl.execute:\n",
|
||
"\n",
|
||
" <------------- Numerical Fidelity Report (input_scale: 4, param_scale: 4, scale_input_multiplier: 10) ------------->\n",
|
||
"\n",
|
||
"+------------+--------------+-----------+-----------+----------------+------------------+---------------+---------------+--------------------+--------------------+------------------------+\n",
|
||
"| mean_error | median_error | max_error | min_error | mean_abs_error | median_abs_error | max_abs_error | min_abs_error | mean_squared_error | mean_percent_error | mean_abs_percent_error |\n",
|
||
"+------------+--------------+-----------+-----------+----------------+------------------+---------------+---------------+--------------------+--------------------+------------------------+\n",
|
||
"| -727.9929 | -727.9929 | -727.9929 | -727.9929 | 727.9929 | 727.9929 | 727.9929 | 727.9929 | 529973.7 | -0.24999964 | 0.24999964 |\n",
|
||
"+------------+--------------+-----------+-----------+----------------+------------------+---------------+---------------+--------------------+--------------------+------------------------+\n",
|
||
"\n",
|
||
"\n",
|
||
"INFO:ezkl.execute:file hash: 41509f380362a8d14401c5ae92073154922fe23e45459ce6f696f58607655db7\n"
|
||
]
|
||
},
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"{\n",
|
||
" \"run_args\": {\n",
|
||
" \"tolerance\": {\n",
|
||
" \"val\": 0.0,\n",
|
||
" \"scale\": 1.0\n",
|
||
" },\n",
|
||
" \"input_scale\": 4,\n",
|
||
" \"param_scale\": 4,\n",
|
||
" \"scale_rebase_multiplier\": 10,\n",
|
||
" \"lookup_range\": [\n",
|
||
" 0,\n",
|
||
" 0\n",
|
||
" ],\n",
|
||
" \"logrows\": 6,\n",
|
||
" \"num_inner_cols\": 2,\n",
|
||
" \"variables\": [\n",
|
||
" [\n",
|
||
" \"batch_size\",\n",
|
||
" 1\n",
|
||
" ]\n",
|
||
" ],\n",
|
||
" \"input_visibility\": \"Private\",\n",
|
||
" \"output_visibility\": \"Public\",\n",
|
||
" \"param_visibility\": \"Private\",\n",
|
||
" \"div_rebasing\": false,\n",
|
||
" \"rebase_frac_zero_constants\": false,\n",
|
||
" \"check_mode\": \"UNSAFE\",\n",
|
||
" \"commitment\": \"KZG\"\n",
|
||
" },\n",
|
||
" \"num_rows\": 21,\n",
|
||
" \"total_assignments\": 42,\n",
|
||
" \"total_const_size\": 0,\n",
|
||
" \"total_dynamic_col_size\": 0,\n",
|
||
" \"num_dynamic_lookups\": 0,\n",
|
||
" \"num_shuffles\": 0,\n",
|
||
" \"total_shuffle_col_size\": 0,\n",
|
||
" \"model_instance_shapes\": [\n",
|
||
" [\n",
|
||
" 1\n",
|
||
" ]\n",
|
||
" ],\n",
|
||
" \"model_output_scales\": [\n",
|
||
" 8\n",
|
||
" ],\n",
|
||
" \"model_input_scales\": [\n",
|
||
" 4\n",
|
||
" ],\n",
|
||
" \"module_sizes\": {\n",
|
||
" \"polycommit\": [],\n",
|
||
" \"poseidon\": [\n",
|
||
" 0,\n",
|
||
" [\n",
|
||
" 0\n",
|
||
" ]\n",
|
||
" ]\n",
|
||
" },\n",
|
||
" \"required_lookups\": [],\n",
|
||
" \"required_range_checks\": [],\n",
|
||
" \"check_mode\": \"UNSAFE\",\n",
|
||
" \"version\": \"0.0.0\",\n",
|
||
" \"num_blinding_factors\": null,\n",
|
||
" \"timestamp\": 1715422871248\n",
|
||
"}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# generate settings\n",
|
||
"onnx_filename = os.path.join('lol.onnx')\n",
|
||
"compiled_filename = os.path.join('lol.compiled')\n",
|
||
"settings_filename = os.path.join('settings.json')\n",
|
||
"srs_path = os.path.join('kzg.params')\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"ezkl.gen_settings(onnx_filename, settings_filename)\n",
|
||
"await ezkl.calibrate_settings(\n",
|
||
" input_filename, onnx_filename, settings_filename, \"resources\", scales = [4])\n",
|
||
"res = await ezkl.get_srs(settings_filename)\n",
|
||
"ezkl.compile_circuit(onnx_filename, compiled_filename, settings_filename)\n",
|
||
"\n",
|
||
"# show the settings.json\n",
|
||
"with open(\"settings.json\") as f:\n",
|
||
" data = json.load(f)\n",
|
||
" json_formatted_str = json.dumps(data, indent=2)\n",
|
||
"\n",
|
||
" print(json_formatted_str)\n",
|
||
"\n",
|
||
"assert os.path.exists(\"settings.json\")\n",
|
||
"assert os.path.exists(\"input.json\")\n",
|
||
"assert os.path.exists(\"lol.onnx\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"metadata": {
|
||
"id": "fULvvnK7_CMb"
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stderr",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"INFO:ezkl.pfsys.srs:loading srs from \"/Users/dante/.ezkl/srs/kzg6.srs\"\n",
|
||
"INFO:ezkl.execute:downsizing params to 6 logrows\n",
|
||
"INFO:ezkl.graph.model:model layout...\n",
|
||
"INFO:ezkl.pfsys:VK took 0.8\n",
|
||
"INFO:ezkl.graph.model:model layout...\n",
|
||
"INFO:ezkl.pfsys:PK took 0.2\n",
|
||
"INFO:ezkl.pfsys:saving verification key 💾\n",
|
||
"INFO:ezkl.pfsys:done saving verification key ✅\n",
|
||
"INFO:ezkl.pfsys:saving proving key 💾\n",
|
||
"INFO:ezkl.pfsys:done saving proving key ✅\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"pk_path = os.path.join('test.pk')\n",
|
||
"vk_path = os.path.join('test.vk')\n",
|
||
"\n",
|
||
"\n",
|
||
"# setup the proof\n",
|
||
"res = ezkl.setup(\n",
|
||
" compiled_filename,\n",
|
||
" vk_path,\n",
|
||
" pk_path,\n",
|
||
" )\n",
|
||
"\n",
|
||
"assert res == True\n",
|
||
"assert os.path.isfile(vk_path)\n",
|
||
"assert os.path.isfile(pk_path)\n",
|
||
"assert os.path.isfile(settings_filename)\n",
|
||
"\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"\n",
|
||
"witness_path = \"witness.json\"\n",
|
||
"\n",
|
||
"res = await ezkl.gen_witness(input_filename, compiled_filename, witness_path)\n",
|
||
"assert os.path.isfile(witness_path)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"metadata": {
|
||
"colab": {
|
||
"base_uri": "https://localhost:8080/"
|
||
},
|
||
"id": "Oog3j6Kd-Wed",
|
||
"outputId": "5839d0c1-5b43-476e-c2f8-6707de562260"
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stderr",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"INFO:ezkl.pfsys:loading proving key from \"test.pk\"\n",
|
||
"INFO:ezkl.pfsys:done loading proving key ✅\n",
|
||
"INFO:ezkl.pfsys.srs:loading srs from \"/Users/dante/.ezkl/srs/kzg6.srs\"\n",
|
||
"INFO:ezkl.execute:downsizing params to 6 logrows\n",
|
||
"INFO:ezkl.pfsys:proof started...\n",
|
||
"INFO:ezkl.graph.model:model layout...\n",
|
||
"INFO:ezkl.pfsys:proof took 0.15\n",
|
||
"INFO:ezkl.pfsys.srs:loading srs from \"/Users/dante/.ezkl/srs/kzg6.srs\"\n",
|
||
"INFO:ezkl.execute:downsizing params to 6 logrows\n",
|
||
"INFO:ezkl.pfsys:loading verification key from \"test.vk\"\n",
|
||
"INFO:ezkl.pfsys:done loading verification key ✅\n",
|
||
"INFO:ezkl.execute:verify took 0.2\n",
|
||
"INFO:ezkl.execute:verified: true\n"
|
||
]
|
||
},
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"verified\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# prove the zk circuit\n",
|
||
"# GENERATE A PROOF\n",
|
||
"proof_path = os.path.join('test.pf')\n",
|
||
"\n",
|
||
"\n",
|
||
"proof = ezkl.prove(\n",
|
||
" witness_path,\n",
|
||
" compiled_filename,\n",
|
||
" pk_path,\n",
|
||
" proof_path,\n",
|
||
" \"single\",\n",
|
||
" )\n",
|
||
"\n",
|
||
"\n",
|
||
"assert os.path.isfile(proof_path)\n",
|
||
"\n",
|
||
"# verify\n",
|
||
"res = ezkl.verify(\n",
|
||
" proof_path,\n",
|
||
" settings_filename,\n",
|
||
" vk_path,\n",
|
||
" )\n",
|
||
"\n",
|
||
"assert res == True\n",
|
||
"print(\"verified\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {
|
||
"id": "W7tAa-DFAtvS"
|
||
},
|
||
"source": [
|
||
"# Part 2 (Using the ZK Computational Graph Onchain!)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {
|
||
"id": "8Ym91kaVAIB6"
|
||
},
|
||
"source": [
|
||
"**Now How Do We Do It Onchain?????**"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"metadata": {
|
||
"colab": {
|
||
"base_uri": "https://localhost:8080/",
|
||
"height": 339
|
||
},
|
||
"id": "fodkNgwS70FM",
|
||
"outputId": "827b5efd-f74f-44de-c114-861b3a86daf2"
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stderr",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"INFO:ezkl.pfsys.srs:loading srs from \"/Users/dante/.ezkl/srs/kzg6.srs\"\n",
|
||
"INFO:ezkl.execute:downsizing params to 6 logrows\n",
|
||
"INFO:ezkl.pfsys:loading verification key from \"test.vk\"\n",
|
||
"INFO:ezkl.pfsys:done loading verification key ✅\n"
|
||
]
|
||
},
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"test.vk\n",
|
||
"settings.json\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# first we need to create evm verifier\n",
|
||
"print(vk_path)\n",
|
||
"print(settings_filename)\n",
|
||
"\n",
|
||
"\n",
|
||
"abi_path = 'test.abi'\n",
|
||
"sol_code_path = 'test.sol'\n",
|
||
"\n",
|
||
"res = await ezkl.create_evm_verifier(\n",
|
||
" vk_path,\n",
|
||
" settings_filename,\n",
|
||
" sol_code_path,\n",
|
||
" abi_path,\n",
|
||
" )\n",
|
||
"assert res == True"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stderr",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"INFO:ezkl.eth:using chain 31337\n",
|
||
"INFO:ezkl.execute:Contract deployed at: 0x998abeb3e57409262ae5b751f60747921b33613e\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# Make sure anvil is running locally first\n",
|
||
"# run with $ anvil -p 3030\n",
|
||
"# we use the default anvil node here\n",
|
||
"import json\n",
|
||
"\n",
|
||
"address_path = os.path.join(\"address.json\")\n",
|
||
"sol_code_path = 'test.sol'\n",
|
||
"# await\n",
|
||
"res = await ezkl.deploy_evm(\n",
|
||
" address_path,\n",
|
||
" sol_code_path,\n",
|
||
" 'http://127.0.0.1:3030'\n",
|
||
")\n",
|
||
"\n",
|
||
"assert res == True\n",
|
||
"\n",
|
||
"with open(address_path, 'r') as file:\n",
|
||
" addr = file.read().rstrip()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stderr",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"INFO:ezkl.eth:using chain 31337\n",
|
||
"INFO:ezkl.eth:estimated verify gas cost: 399775\n",
|
||
"INFO:ezkl.execute:Solidity verification result: true\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# read the address from addr_path\n",
|
||
"addr = None\n",
|
||
"with open(address_path, 'r') as f:\n",
|
||
" addr = f.read()\n",
|
||
"\n",
|
||
"res = await ezkl.verify_evm(\n",
|
||
" addr,\n",
|
||
" proof_path,\n",
|
||
" \"http://127.0.0.1:3030\"\n",
|
||
")\n",
|
||
"assert res == True"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {
|
||
"id": "mPFmUizd_gCp"
|
||
},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"colab": {
|
||
"provenance": []
|
||
},
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"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.2"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 0
|
||
}
|