Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
hanyul-ryu committed Nov 22, 2023
1 parent 851ffd1 commit 9206fc1
Show file tree
Hide file tree
Showing 7 changed files with 10,012 additions and 0 deletions.
1,247 changes: 1,247 additions & 0 deletions examples/[Example] CKKS engine.ipynb

Large diffs are not rendered by default.

5,569 changes: 5,569 additions & 0 deletions examples/[Example] Evaluators (auto).ipynb

Large diffs are not rendered by default.

1,143 changes: 1,143 additions & 0 deletions examples/[Example] Evaluators.ipynb

Large diffs are not rendered by default.

266 changes: 266 additions & 0 deletions examples/[Example] MultiParty Enc & Dec.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "6a1a630e",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-19T17:02:59.461251Z",
"iopub.status.busy": "2023-11-19T17:02:59.461093Z",
"iopub.status.idle": "2023-11-19T17:03:01.780708Z",
"shell.execute_reply": "2023-11-19T17:03:01.779397Z",
"shell.execute_reply.started": "2023-11-19T17:02:59.461233Z"
}
},
"outputs": [],
"source": [
"import math\n",
"import numpy as np\n",
"import torch\n",
"from tqdm.notebook import tqdm\n",
"\n",
"from liberate import fhe\n",
"from liberate.fhe import presets\n",
"import liberate"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9044dcbf",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-19T17:03:01.784378Z",
"iopub.status.busy": "2023-11-19T17:03:01.783571Z",
"iopub.status.idle": "2023-11-19T17:03:03.805008Z",
"shell.execute_reply": "2023-11-19T17:03:03.803922Z",
"shell.execute_reply.started": "2023-11-19T17:03:01.784351Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2023-11-20 02:03:01.832507] I have received the context:\n",
"\n",
"\n",
"I have received inputs:\n",
" buffer_bit_length\t\t= 62\n",
" scale_bits\t\t\t= 40\n",
" logN\t\t\t\t= 15\n",
" N\t\t\t\t= 32,768\n",
" Number of special primes\t= 2\n",
" Number of scales\t\t= 16\n",
" Cache folder\t\t\t= '/home/hanyul/.pyenv/versions/liberate/lib/python3.11/site-packages/liberate/fhe/cache/resources'\n",
" Security bits\t\t\t= 128\n",
" Quantum security model\t\t= post_quantum\n",
" Security sampling distribution\t= uniform\n",
" Number of message bits\t\t= 60\n",
" In total I will be using '821' bits out of available maximum '829' bits.\n",
" And is it secured?\t\t= True\n",
"My RNS primes are [1099510054913, 1099515691009, 1099507695617, 1099516280833, 1099506515969, 1099520606209, 1099504549889, 1099523555329, 1099503894529, 1099527946241, 1099503370241, 1099529060353, 1099498258433, 1099531223041, 1099469684737, 1099532009473, 1152921504606584833, 1152921504598720513, 1152921504597016577].\n",
"[2023-11-20 02:03:01.832784] Requested devices for computation are [0].\n",
"[2023-11-20 02:03:01.834690] I have generated a partitioning scheme.\n",
"[2023-11-20 02:03:01.834711] I have in total 17 levels available.\n",
"[2023-11-20 02:03:01.834719] I have 17 ordinary primes.\n",
"[2023-11-20 02:03:01.834726] I have 2 special primes.\n",
"[2023-11-20 02:03:03.443537] I prepared ntt parameters.\n",
"[2023-11-20 02:03:03.443634] ntt initialization took 1.61 seconds.\n",
">>\tGenerate CKKS engine\n",
"\n",
"CPU times: user 1.5 s, sys: 271 ms, total: 1.77 s\n",
"Wall time: 2.01 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"params = presets.params[\"silver\"]\n",
"engine = fhe.ckks_engine(verbose=True, **params)\n",
"print(\">>\\tGenerate CKKS engine\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "393b2beb",
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-19T17:03:03.806996Z",
"iopub.status.busy": "2023-11-19T17:03:03.806547Z",
"iopub.status.idle": "2023-11-19T17:03:04.917914Z",
"shell.execute_reply": "2023-11-19T17:03:04.917456Z",
"shell.execute_reply.started": "2023-11-19T17:03:03.806956Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"====================================================================================================\n",
"|\tnum_of_parties\t:\t5\n",
"----------------------------------------------------------------------------------------------------\n",
"|\t>> level : 0\t2.547722033341415e-10+9.599448347330508e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 1\t2.101305796031738e-10+9.494556252320763e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 2\t2.354454409214668e-10+9.415256130296257e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 3\t2.594617853901582e-10+9.473609452470555e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 4\t2.623892214614898e-10+9.480544349571574e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 5\t2.331130843913343e-10+9.515161991657806e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 6\t2.282902755723626e-10+9.453273719373101e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 7\t2.281979050167138e-10+9.418428703611426e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 8\t2.355307060497580e-10+9.458005933993263e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 9\t2.190176928706933e-10+9.469051320820654e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 10\t2.359001882723533e-10+9.589333771486963e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 11\t2.620694772303978e-10+9.447646220905881e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 12\t2.345359462196939e-10+9.484324436925817e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 13\t2.331432824576041e-10+9.474071305248799e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 14\t2.204672000516439e-10+9.561034630678478e-09j\t|\n",
"====================================================================================================\n",
"|\t>> level : 15\t2.389981546002673e-10+9.441151860301034e-09j\t|\n",
"====================================================================================================\n",
"CPU times: user 182 ms, sys: 61.5 ms, total: 244 ms\n",
"Wall time: 1.1 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"print(\"=====\"*20)\n",
"num_of_parties = 5\n",
"print(f\"|\\tnum_of_parties\\t:\\t{num_of_parties}\")\n",
"print(\"-----\"*20)\n",
"for level in range(engine.num_levels):\n",
"\n",
" delta = 5\n",
" amin, amax = -255, 255\n",
" m = engine.example(amin=amin, amax=amax)\n",
" ############################\n",
" #### generate sks ####\n",
" ############################\n",
" sks = [engine.create_secret_key() for _ in range(num_of_parties)]\n",
"\n",
" ############################\n",
" #### generate pks ####\n",
" ############################\n",
" pks = [engine.create_public_key(sk=sks[0])]\n",
" crs = engine.clone(pks[0]).data[1]\n",
" for sk in sks[1:]:\n",
" pks.append(engine.multiparty_create_public_key(sk, a=crs))\n",
"\n",
" cpk = engine.multiparty_create_collective_public_key(pks=pks)\n",
"\n",
" ###################\n",
" #### enc ####\n",
" ###################\n",
" ct = engine.encorypt(m, cpk, level=level)\n",
"\n",
" ###################\n",
" #### dec ####\n",
" ###################\n",
" pcts = [engine.multiparty_decrypt_head(ct, sks[0])]\n",
" for sk in sks[1:]:\n",
" pcts.append(engine.multiparty_decrypt_partial(ct, sk))\n",
" m_ = engine.multiparty_decrypt_fusion(pcts, level=level)\n",
"\n",
" print(f\"|\\t>> level : {level:2d}\\t{engine.absmax_error(m_, m):.15e}\\t|\")\n",
" print(\"=====\"*20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a90fcdd",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d11fe5f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c9914f2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b034866",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "44dc6812",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "95f87df2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a242138",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "liberate",
"language": "python",
"name": "liberate"
},
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 9206fc1

Please sign in to comment.