Files
zk-stats-demo/public/assets/template.ipynb

281 lines
18 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import torch\n",
"from zkstats.core import (\n",
" prover_gen_settings,\n",
" prover_gen_proof,\n",
" verifier_setup,\n",
" verifier_verify,\n",
")\n",
"from zkstats.computation import computation_to_model, State\n",
"\n",
"cwd = os.getcwd()\n",
"\n",
"# FIXME: fill this in with the path to your data\n",
"data_path = f\"{cwd}/data.json\"\n",
"\n",
"# Paths to the output files\n",
"output_dir = f\"{cwd}/out\"\n",
"os.makedirs(output_dir, exist_ok=True)\n",
"model_onnx_path = f\"{output_dir}/model.onnx\"\n",
"compiled_model_path = f\"{output_dir}/model.compiled\"\n",
"\n",
"pk_path = f\"{output_dir}/model.pk\"\n",
"vk_path = f\"{output_dir}/model.vk\"\n",
"proof_path = f\"{output_dir}/model.pf\"\n",
"settings_path = f\"{output_dir}/settings.json\"\n",
"witness_path = f\"{output_dir}/witness.json\"\n",
"comb_data_path = f\"{output_dir}/comb_data.json\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## User-defined Computation\n",
"\n",
"A computation should be of type `TComputation`. For example, the following code snippet defines a computation that computes the sum of the private data.\n",
"\n",
"```python\n",
"def computation(state: State, x: list[torch.Tensor]):\n",
" out_0 = state.median(x[0])\n",
" out_1 = state.median(x[1])\n",
" return state.mean(torch.tensor([out_0, out_1]).reshape(1,-1,1))\n",
"```\n",
"\n",
"FIXME: The following code snippet is entirely from the user. You MUST check\n",
"1. the code only performs zkstats-related operations.\n",
"2. the computation must not leak any information about the private data."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"# This is just a dummy computation. Replace it with user's computation\n",
"# def computation(state: State, x: list[torch.Tensor]):\n",
"# x_0 = x[0]\n",
"# out_0 = state.median(x_0)\n",
"# out_1 = state.median(x_0)\n",
"# return state.mean(torch.tensor([out_0, out_1]).reshape(1,-1,1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate settings and setup with user's computation."
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/t3/5psrvr1x0w1_6n9kx2n7d9700000gn/T/ipykernel_97835/3906390486.py:5: TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.\n",
" return state.mean(torch.tensor([out_0, out_1]).reshape(1,-1,1))\n",
"/var/folders/t3/5psrvr1x0w1_6n9kx2n7d9700000gn/T/ipykernel_97835/3906390486.py:5: TracerWarning: Converting a tensor to a Python float might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
" return state.mean(torch.tensor([out_0, out_1]).reshape(1,-1,1))\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"==== Generate & Calibrate Setting ====\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"spawning module 0\n",
"spawning module 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"scale: default\n",
"setting: {\"run_args\":{\"tolerance\":{\"val\":0.0,\"scale\":1.0},\"input_scale\":8,\"param_scale\":8,\"scale_rebase_multiplier\":10,\"lookup_range\":[-25112,24986],\"logrows\":16,\"num_inner_cols\":2,\"variables\":[[\"batch_size\",1]],\"input_visibility\":{\"Hashed\":{\"hash_is_public\":true,\"outlets\":[]}},\"output_visibility\":\"Public\",\"param_visibility\":\"Private\"},\"num_rows\":14432,\"total_assignments\":12049,\"total_const_size\":1816,\"model_instance_shapes\":[[1],[1]],\"model_output_scales\":[0,8],\"model_input_scales\":[8],\"module_sizes\":{\"kzg\":[],\"poseidon\":[14432,[1]],\"elgamal\":[0,[0]]},\"required_lookups\":[\"Abs\",{\"GreaterThan\":{\"a\":0.0}},\"KroneckerDelta\"],\"check_mode\":\"UNSAFE\",\"version\":\"7.0.0\",\"num_blinding_factors\":null}\n",
"==== setting up ezkl ====\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"spawning module 0\n",
"spawning module 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time setup: 12.489933013916016 seconds\n"
]
}
],
"source": [
"_, model = computation_to_model(computation)\n",
"prover_gen_settings(\n",
" [data_path],\n",
" comb_data_path,\n",
" model,\n",
" model_onnx_path,\n",
" \"default\",\n",
" \"resources\",\n",
" settings_path,\n",
")\n",
"verifier_setup(model_onnx_path, compiled_model_path, settings_path, vk_path, pk_path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generate proof with your data and user's computation."
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"==== Generating Witness ====\n",
"witness boolean: 1.0\n",
"witness result 1 : 49.55078125\n",
"==== Generating Proof ====\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"spawning module 0\n",
"spawning module 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"proof: {'instances': [[[3042937791208075219, 8157070662846698822, 3804781648660056856, 172406108020799675], [12436184717236109307, 3962172157175319849, 7381016538464732718, 1011752739694698287], [18341455175509539295, 12796101019039945164, 1607286914885633240, 1929881192315725821]]], 'proof': '01f9485e3484e27de162622863fdcf29cf81993a023d600b20173b37ca6d73392a2d6bee21aa3a00a12be030f57cf2fd67c3ba0182f1a879b53945993c2f20802eed1eeed24763f1d1070619ecbe97b47c3edab1d6f89a066dd6f6c6bfe0d3f60c26e2693859fb2be751d382d352ddb24570cba629a07bc55a58b33171825af1106e65e8bfb15161ac537b5a1463c993e8dbfb38dd3c410c64079587f6fe55c024f578ca70dd7700800e25cf651174e43fa3ddebd4e8036dbb4c8c2616fd2641084ea317141ecfab83be803e12254a766e35d526a9b6e1035d9c71b068ea8b4d125d355246396448552aaad9cd82c40568269f96acb66947c5d5b28c77331c420f816f2529b1b0d88b09c39a301c6665b04bdbeb406ffc42406017699ee7c2c42efc07fec604149bf8c787d91aca634ac1ae94b42655bcb480535ced9610e5311b28db6704f18b573cf2161321f75934a0e5ac7e51b1840543cbd4027471a391036bf7609d4068c2793f2bbb24b830bf8cac4518b53fc3288a598837bb07d60f2055334e7c906014440dd944368d39ee4511f27b8317c95b0decfc1f40d7b1282454499d2e9db5c8d8657bb7094c2eda4d9f2ecc58d59945f613829a826627671efdf9d998b59aa5e73e41c93d313ff2de88dd84d63efa56b6cabfe7225d30cd20aff7ef07e14b82d000ddcbc7f1e4bb0e9e916a8fdf19237d16e1541f93ca550c6116188b63393aa411cf08a8566453a6afdf19cb55f2f106b69589f8c21b2d0d4fe9c7cec2403a6bbae8221602e7a3ee452c9a2a20ae5af3edaa025b9d8b260bd4a25023bdc8ef5a969ca1c34d9be11a35bc1d48476bedaec4f0e798b8310013d0a0f699c8a5c654055b7d4a8fb219159650f52c7074cb8ccde6cb1a9f30cd2990f8b6126138656ef3b88f173e4dbf67d8d25fe2f4f4144e9737a6d27dd28c25249e0474681e7a64b861ea6641c04fb4c025744a17bdc443d8e1ec8c15da8417e8b2b254854fbed70a1c27057309925c74754d269cb136055db07a20fa930d111850770ec674aff132d600bcb4551c49359c06a02ee1312338620891856e4e1cda7159c1123918406f96df8d384fc07f26c0a4dedba490a713a4f8b8c11b9207427114eb97b638a5996484c1944770c3235e4d80b147801e42416a0ae28f30106e2dc75299499c0da0597a0e3f4871bb519061466a8667457b756f60735d8320fd67c09764871cdab5e843eeda1d2575f464be0d1e6ff7cf0186efa63c572810923fc17f250f67ea7d803fbab9e04874f72097b6846c22a3c3f63e8991af6e29fc13f6d413264d51b687e311ca85a42d45c84aa11ae401a823f20ff9c6ec410ecb534affac7032b4e5b46913f6d8154351020526298a2ad02e190d5ba01a2e18f7171e6f69d0564373e1e2b326c37d17478fe5658ca8e121a711f501caf7c7163bea69941180de33b5bfc9a888521caa9413e25e2b04f4ca8d2b3ef7d91647095976a3526e3fea2e0fb1e9921f0f002e6d2c66a27b72fb812963be6d1aa10f2bdd1f9b53adbd383ae5cbe59311d10813962bb9c06f1b3a9483574e90fad7250948a36c107042e29d26cc823f95c8b0c994ac45a6c5ae3f85f6c04e3e14516612da062979be4f3cb1ce506f1dcde1b2292ae8562e9a2cf02c806cafc8d5d3871728c175f1d1b1b37b108d7ab7a085b24661ec2d6de3c2e25dc3d9eb67fe09b422a7a76c39a637cdfcb9d7a8873f51e97b06d24b9dc8c19329c90e94f5bc54ea091a7db822e540f84edacad80bd8e5a3a34f09ed5eabadfa3e2d141c5c9e2f392a73fa961b6e23e7537874039c42b55472bef1115155fc34767d1bd04591b9fa15cc00fb52dda98c656a4ad522aab106bd04fa64161703026fe563489d48d71017c8a37a3158371157000dbdf6cf64fef8fd7727070f1aafcf531a1cc72256801252b4f05d499e2491a9bf0c42aff0f8c8b946187e7d81ef00e734976893910c0aefc8c4c7246a2857809fdce4b7b421c08b6e6cac8a26d0e73a3d57dbdd3a151d404bbe71993ad4497e971770dabc5edc5b6432cb4c4bdf23f8bce4291c85f21a95511e6639feb5f0428d2995092d1c1cd367adca4a7536d7f6f483a9777cca1b5652ea9e10ee913f6f32021437558ffbf23a04c7da46dca79e19430dd271df24d37ca4fe0206e18e27b764a09a6b588e5969227ec7300a76068a11f2a5960b2d14f10a7389de2d7a5ef1de9545726bc59a5405b5030e0966f55e40e5718ba1184bbdb832bc654d720f283ab875457db1bcafd13c75e581e3290d66324e0c80209621cf144340f025d0ce42a3b53843f2e619ca78dba74b785d3ec0a79ac7c015f723f286b9f37a24229eb621e87f30ec4ebba2081f1c9eca18fdcc22dd30182edbad86f8c81aeabb4ab9eee6464d8209c218ebe3b73160d176eb30ac3d34ad0235c6fad2f0507c4fec37bfa35d79539c819927402069b182f161a9392e3d6b134f6b105e4719028eac2d3e8a2ec9fd0bbbf63b03061ccd26667b4ce8aa10761eac4074cf58a67761bafdc29bf36880d54b0d54a13d2a5c6decd18a45d4e01c2aefee15491507bcb34d3eeeed40d3af0ae176e42aefa9a7e4f62733580fe3cb2da9528b7f865acb6dd9025f5305d2022247ebd911c2de81c4794b06bf04069117d11551ccfb4580722549bfafe77c50bf0a3890a5ea14f03ef7d7bbe46c3d620ef50df30ba57a3bfabd5463100e4776f1de35d78a71c6ee862ac52e8b9377a30d942a978c5a7c7891da51bf2713c87bfb171491af21b289b0c8d3c1f21a7ab80ceda6a0b2ed6c6568cbe76bccf8b26135725218106ac4562af6653235dd78cd007a378f1bd3370e5a4175bc612ad77a084b1dbb1a1afba8a4bd3fc38823d5d713d34a8e56c1024c6ef60de73adb8ad4a016e59238bb02fb24f001f8409af0c601bf11044f42f97eeb029e369cfebc55952e66cb4ab1d78d1336f4c3b151f84e1c894e08af0970e37f14f8f970b2e9454e6ea0d1ccd709b4935a8055e9b4764b3028ad81411ff42b0667cdabc00d83bea3d9c6927ed3e55e5db2273477b5da1f0b46c623e7c1866e12cc00e515edef6772d3c01036a1622eb2fb0b755b002a2714ede8d26266849311217becf72a733a80026144eccf3c7b3477fa2fe16f721707e955d55f12c7dc27f17314415eb5e687bf8c5b4ef40faedb54d94adefcea6b1a06a46a425a1305d3ef08482a0053a3cba33360213b517df1ed227f27bcb89f2550acc6af21b77d65053d0c9b52bbb7f8c069b47d1d77e8690c3507e84dee5f07e557a12b095c198226ef13c137fd3310aad82634a2df2660a9e0813b0e1bae0bf5055eb04e128111d8d4a55fdd7bd65596de50948631183f13b60ff06abef51fef50069a0a5da36a2c9783664514c3b8263e80c137ae406e8edf556d6e18d50669f249cf42873c78f482728248c7d89d3609814af56b46ed9657857191796003cecfd899b0646147dd4a2f8c0c7c5bb5123fb40706881eb7797453a092e9b126de7cf344da6fa02c7097680be9c1cc45f77ebee26427ea89403745fcf79a2118dba06b97f3b706697f9199a4ba1af68fa8b3cbc5038d25521bdb4b3889cea31cce576ae793d240105d9bb401ff3fbe8254fc3f0d43d21e2e1b5f9fb28090260a574e402286cca58f4891a3ad19923518a9b01f2abeb4bc526559245de10864174bb1fd1f62c28cb4385b0d2d7d7bc6d077cb3bccdb9b65689995271a460663093e54a8e40ecb5f2eb346d8f2649286d08ca4f4c15449621e02c16ec653dc450c6600562be79bd4f0630ab6c89da82808d4bff21dbc288b2e38a23b14aa35a72e9430a9f257dda54f79c64beb282edb02c3832b251459c4ca12548089e010de1a513341f4a49f8db6de79d12dfaa156e287cd714c88719c4fdef403cf2a80801822d1428a913f359e0f2614f04f7d41d9c251de03b24142daa6d389ae70cebf00000000000000000000000000000000000000000000000000000000000000002080e137f83dcd23d9f1a206c893964e11ff0f7cdac128cc3dae4a71a20ff849149d2d3a24bae872fbb8476cb5e60d55c1173a4deec7bb0b702d50e5e03f6a37273b47a811a7643fcda5641e6a061ced383d806a1db649288dfc315fa09698380044ec9b436e250a9b342410a337d01239ff83fb0c5f54fd8a85e7e443bbdec412ce90496a14557d9e91b00ef41f31b9839662b628ba3ef2e5a2431e836604371470b73bddc4b185b83cf60658bc160faa58c66ece5e7b237868cbb634b4bdab125a59743fd2b98d051d3b97f2c73b53b87c84f69244aa6d80684d34ec53ee5e2670f1ac56ae2931cc67b53f0adb763e831c2fbf1cee0f7e3a7c2d35608e612b29b18fe11bea774cb5a873f8015756e7fa28c8f91057ff3df9f7434c8f29e23906e3f8c2b78a28d54df6bc3dff17dc7cbc1702618a986265fdf89302a3978dbb155baa002fc62bb1fa6d6efa48a302a92b68e3e746893634816bd4e0e1d587f30d47279a4e47fba74a3e41b8092831721882fe6c2765225628c109fbaaf908b81abf501b83f72222562d78c7777008d7aa1f55e247d7a44fccc89e8f34951022270fda77667aea5570defe53f26ac9f24f5e3cc60c5f0c925cca339fe32d84e729c798b14f7ef41d29f082c71b59786eebed3ea92d6216278c2d9b58776d53431f5ceb40a19458c1c6e0c80a246fc8cdd954fa99c0eac250545792b7e691d9c20013b8352e6216156299754fbea34c889c99a7a7ad5ea64b963aabf14f4590361410cf1958be63724d4b5607e60249aa06e96c1ad4f684d20ef48b97b70dd9070624d67e1e66932b62e51e6302b97d6cd4208263d8b0028190a5a1103c40fad52f5a2a1479953313381839fe68e19e50f30c9f82f41166f3ac7402fe3b2a85152282ceb3af2e2f9578b85cbcc6e15b82cd0d85b468e8b6794886ef6e6c7fbe1c2ec83b1763532b8cf1f95a7e74e0fa997cfe6e69a4004a088e7b33002fee52fd0534c1ba9255f9715fdc1a12932c8cf8f9dfc1300e64d553137d21cf2a388f5d0f62f41edf3aca2f7619914b3f4c44eef67db71aa22897fd359142201f202fa424ea4bd8100e465e9003884b94e75d9126c7f7022177c6947c759e651d87ae561536a90ffce9c7577215219e4144df1a85f50f076097a96c3f5dffc1a53bda422739e58cf7dd03207bfa7a7edc6782b4924c0d873cd2e471ee1414154d5c3b8a2963e2cb999586b79338bcc69a9ed9b3ef8444f54e05037e042650638b70bbea2be754ed8c55f47b83c15e67feb1ddb7c7e076c4786e5f62219a201f441987c91c76c42e7981728a71cfbb4530bff633855c9172b776371576eaed18a972756408823ea37e7e325b3fe22719999ed6dd42cf4f4c61990ae5bdb66a9aa0833e22185854c50fd456f853a1ebebf00f6027e33ba40b9f4129aabfee0043edb1cde21ae11b7718df6fbe26e70780fc6d0f74e022efea795528c888d787733906358c12d0622618b47c565f924ce3f3e0ebaeab4c287f1cd29575f67710db99cb9df32162b1cfc0b99db356dc200b2cb1667437300b6e2eb58d670acdc3800526c35d09a74bce8602d9531bd9fb6101d48561f2d9bbd6a592cd4a5ec01256adc6fcdb019f4f781af8889260e6bb56b56ed3016783f7442ea334bc3042198d5269628008aba69fd8f9c46ce4688c21be3981dfa1c91f1382066e276a3995eca3c4ea153044a580ef1a738002e90c45a6e8a6908c57ef5a2dde2c95cbe411bab0ce1a0c01111cb6b3aa8fedec159e6f6e3dedcaf83afa3137086c9f795cab0c286f6d4e29a710e02b02ef1730c11202046af989ebbd5e945a211eacbafa2102856ac9340ab527886b834790897fdd47d4060dc48899721177aa3e7e92e5c9a6ad2474e7118db1484878c60fd2ed8775f59e889cea780819be13ace3a6d9e1034b60fbfa048e61c47023605fb6e11085a12df0ffe5480960054efa15d6bb1de71b8bd3cf1ec58ae65b52b9441043933a89311f9319fa4fb6251e82cf6d3c799c7301b241026e97345fd3c95ea63924423c249cea9fd87aabe12a8e5877ed47ff5b240e2b1310b06f6a147c2dd8043377325f9410f3dc1eb733d2a464ae283e8ea06baeb03025c763c0ea92a669f33747fdbdd2ba4fa1f8e51db27417b3a4794d33015c2b288831e539c75a458253a3212888b571e035c88f671c1b17bf7a2ceb088d6c432162e84a821c23828806bfb5e934521363ec93152cefde77e7f62442781abf00213fe3263f4527449cbcd267bfdc249c2fc6ff19039ac4e648a35fa53912677c', 'transcript_type': 'EVM'}\n",
"Time gen prf: 17.092010021209717 seconds\n"
]
}
],
"source": [
"prover_gen_proof(\n",
" model_onnx_path,\n",
" comb_data_path,\n",
" witness_path,\n",
" compiled_model_path,\n",
" settings_path,\n",
" proof_path,\n",
" pk_path,\n",
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Verify the proof to ensure it is correct"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"!@# res: True\n",
"prf instances: [[[3042937791208075219, 8157070662846698822, 3804781648660056856, 172406108020799675], [12436184717236109307, 3962172157175319849, 7381016538464732718, 1011752739694698287], [18341455175509539295, 12796101019039945164, 1607286914885633240, 1929881192315725821]]]\n",
"num_inputs: 1\n",
"proof boolean: 1.0\n",
"proof result 0 : 49.55078125\n"
]
}
],
"source": [
"verifier_verify(proof_path, settings_path, vk_path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the file paths. You should share the following files back to the user for them to verify the proof. You **SHOULD NOT** share more files otherwise data might be leaked."
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model onnx:\t\t /Users/mhchia/projects/work/pse/demo-next/public/assets/out/model.onnx\n",
"Settings:\t\t /Users/mhchia/projects/work/pse/demo-next/public/assets/out/settings.json\n",
"Proof:\t\t\t /Users/mhchia/projects/work/pse/demo-next/public/assets/out/model.pf\n",
"Verification key:\t /Users/mhchia/projects/work/pse/demo-next/public/assets/out/model.vk\n"
]
}
],
"source": [
"model_onnx_path, settings_path, proof_path, vk_path\n",
"print(\"Model onnx:\\t\\t\", model_onnx_path)\n",
"print(\"Settings:\\t\\t\", settings_path)\n",
"print(\"Proof:\\t\\t\\t\", proof_path)\n",
"print(\"Verification key:\\t\", vk_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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}