Created
December 24, 2016 07:57
-
-
Save swenson/1231675bd2617060540c056687428ca8 to your computer and use it in GitHub Desktop.
This notebook performs computations from "Fully Homomorphic Encryption over the Integers" by van Dijk, Gentry, Halevi, and Vaikuntanathanm, which can be found at https://eprint.iacr.org/2009/616.pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This notebook performs computations from \"Fully Homomorphic Encryption over the Integers\" by van Dijk, Gentry, Halevi, and Vaikuntanathanm, which can be found at https://eprint.iacr.org/2009/616.pdf\n", | |
"\n", | |
"First, let's do some basic Python imports and initialization." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 426, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from math import ceil, floor, log\n", | |
"import random\n", | |
"random.seed(1234)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Next, we setup our parameters for the R-LWE bootstrap encryption.\n", | |
"\n", | |
"We want a small example, so the parameters are way too small to provide any real security." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 427, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Public key integer bit size: 9\n", | |
"Secret key bit size: 9\n", | |
"Noise bit size: 2\n", | |
"Secondary noise parameter: 3\n", | |
"Number of integers in public key: 7\n", | |
"Maximum error: 28\n" | |
] | |
} | |
], | |
"source": [ | |
"rho = 2\n", | |
"rho_prime = 3\n", | |
"nu = 9\n", | |
"gamma = 9\n", | |
"tau = 6\n", | |
"\n", | |
"print 'Public key integer bit size:', gamma\n", | |
"print 'Secret key bit size:', nu\n", | |
"print 'Noise bit size:', rho\n", | |
"print 'Secondary noise parameter:', rho_prime\n", | |
"print 'Number of integers in public key:', tau + 1\n", | |
"print 'Maximum error:', ((2**rho) - 1) * (tau + 1) + ((2**rho_prime) - 1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Next, we define some functions to help us sample random values and calculate smaller, potentially negative remainders." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 428, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def D(p):\n", | |
" q = random.randint(0, int(floor((2**gamma) / p)))\n", | |
" r = random.randint(-(2**rho) + 1, (2**rho) - 1)\n", | |
" x = p * q + r\n", | |
" return x\n", | |
"\n", | |
"def rem(a, b):\n", | |
" q = int(round(float(a) / b))\n", | |
" return a - q * b" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We generate a secret key, a random odd integer." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 429, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Secret key 505\n" | |
] | |
} | |
], | |
"source": [ | |
"p = random.randint(2**(nu-2), (2**(nu-1))) * 2 + 1\n", | |
"print 'Secret key', p" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We generate a public key from our secret key: a list of integers that are zero mod p with some random noise added." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 430, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Public key [507, 503, 503, 502, -3, 2, -2]\n" | |
] | |
} | |
], | |
"source": [ | |
"while True:\n", | |
" xi = [D(p) for _ in range(tau + 1)]\n", | |
" maxx = xi[0]\n", | |
" maxi = 0\n", | |
" for i in range(1, len(xi)):\n", | |
" x = xi[i]\n", | |
" if x > maxx:\n", | |
" maxx = x\n", | |
" maxi = i\n", | |
" xi[0], xi[maxi] = xi[maxi], xi[0]\n", | |
" # x0 must be odd\n", | |
" if xi[0] % 2 == 0:\n", | |
" continue\n", | |
" # x0 % p must be even\n", | |
" if rem(xi[0], p) % 2 == 1:\n", | |
" continue\n", | |
" # avoid edge case of x0 divisible by p\n", | |
" if xi[0] % p == 0:\n", | |
" continue\n", | |
" break\n", | |
"print 'Public key', xi\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We encrypt by adding a random subset of the public key, along with some extra noise, all multiplied by 2 (so that it is zero mod 2), and adding in the message bit.\n", | |
"\n", | |
"Decrypting is just taking extracting the bottom bit from the created ciphertext mod the secret key." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 431, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def Encrypt(pk, m):\n", | |
" # random subset\n", | |
" S = [a for a in range(1, tau + 1) if bool(round(random.random()))]\n", | |
" r = random.randint(-(2**rho_prime) + 1, (2**rho_prime) - 1)\n", | |
" c = m + 2 * r + 2 * sum(pk[i] for i in S)\n", | |
" c = rem(c, pk[0])\n", | |
" return c\n", | |
"\n", | |
"def Decrypt(p, c):\n", | |
" return rem(c, p) % 2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here we run 1,000 random tests of the code, and print a small example." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 432, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Message: 1\n", | |
"Ciphertext: -7\n", | |
"Decrypted to: 1\n" | |
] | |
} | |
], | |
"source": [ | |
"# send the message 0 or 1\n", | |
"failed = 0\n", | |
"for i in range(1000):\n", | |
" m = int(round(random.random()))\n", | |
" ciphertext = Encrypt(xi, m)\n", | |
" decrypted = Decrypt(p, ciphertext)\n", | |
" if m != decrypted:\n", | |
" if not failed:\n", | |
" print 'Message:', m\n", | |
" print 'Ciphertext:', ciphertext\n", | |
" print 'Decrypted to:', decrypted\n", | |
" failed += 1\n", | |
"if failed:\n", | |
" print 'Failed:', failed\n", | |
"else:\n", | |
" print 'Message:', m\n", | |
" print 'Ciphertext:', ciphertext\n", | |
" print 'Decrypted to:', decrypted\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"License: MIT\n", | |
"\n", | |
"Copyright (c) 2016 Christopher Swenson\n", | |
"\n", | |
"Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n", | |
"\n", | |
"The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n", | |
"\n", | |
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE." | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment