Skip to content

Instantly share code, notes, and snippets.

@pcpthm

pcpthm/xor.ipynb Secret

Created October 26, 2019 15:24
Show Gist options
  • Save pcpthm/9bf91ed750cb2b4abc4009475680d908 to your computer and use it in GitHub Desktop.
Save pcpthm/9bf91ed750cb2b4abc4009475680d908 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [],
"source": [
"def bit_indices(n):\n",
" i = 0\n",
" res = []\n",
" while n > 0:\n",
" if (n & 1) != 0:\n",
" res.append(i)\n",
" n //= 2\n",
" i += 1\n",
" return res\n",
"def parity(n): return len(bit_indices(n)) % 2"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
"R = IntegerModRing(8)\n",
"n = 4"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
"alphas = [chr(ord('a') + i) for i in range(n)]\n",
"var_names = [''.join(alphas[i] for i in bit_indices(S)) for S in range(1, 2**n - 1)]"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [],
"source": [
"A = Matrix(R, [[parity(S & T) for T in range(1, 2**n - 1)] for S in range(0, 2**n)])\n",
"b = vector(R, [parity(S) for S in range(0, 2**n)])"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"sol = A.solve_right(b)"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-3*a - 3*b - 3*ab - 3*c + ac + bc + abc + 3*d + 3*ad - bd + 3*abd - cd - acd + 3*bcd"
]
},
"execution_count": 171,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"R_with_vars = R[[var(name) for name in var_names]]\n",
"expr = sum(R_with_vars(a) * x for a, x in zip(sol, R_with_vars.gens()))\n",
"expr"
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0^0^0^0: ref(0) =?= f(0): True\n",
"0^0^0^1: ref(1) =?= f(1): True\n",
"0^0^1^0: ref(1) =?= f(1): True\n",
"0^0^1^1: ref(0) =?= f(0): True\n",
"0^1^0^0: ref(1) =?= f(1): True\n",
"0^1^0^1: ref(0) =?= f(0): True\n",
"0^1^1^0: ref(0) =?= f(0): True\n",
"0^1^1^1: ref(1) =?= f(1): True\n",
"1^0^0^0: ref(1) =?= f(1): True\n",
"1^0^0^1: ref(0) =?= f(0): True\n",
"1^0^1^0: ref(0) =?= f(0): True\n",
"1^0^1^1: ref(1) =?= f(1): True\n",
"1^1^0^0: ref(0) =?= f(0): True\n",
"1^1^0^1: ref(1) =?= f(1): True\n",
"1^1^1^0: ref(1) =?= f(1): True\n",
"1^1^1^1: ref(0) =?= f(0): True\n"
]
}
],
"source": [
"def evalxor3(f):\n",
" for (i, j, k, l) in [(i, j, k, l) for i in [0, 1] for j in [0, 1] for k in [0, 1] for l in [0, 1]]:\n",
" ref = i^^j^^k^^l\n",
" eval = f.substitute(a=i, b=j, c=k, d=l, ab=i^^j, ac=i^^k, ad=i^^l, bc=j^^k, bd=j^^l, cd=k^^l, abc=i^^j^^k, abd=i^^j^^l,\n",
" acd=i^^k^^l, bcd=j^^k^^l)\n",
" print(\"%s^%s^%s^%s: ref(%s) =?= f(%s): %s\" % \n",
" (i, j, k, l, ref, eval, ref == eval))\n",
"evalxor3(expr)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 8.8",
"language": "sage",
"name": "sagemath"
},
"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.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment