NTT-learning/notebooks/foundations/02_negative_wrapped_ntt/lab.ipynb

166 lines
7.4 KiB
Text
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"pedagogy": {
"role": "meta",
"difficulty": 1,
"kind": "orientation",
"title": "Lab Goals"
}
},
"source": "## META | difficulty 1 | Lab Goals\n\nThe lab is about prediction inside the direct transform matrix.\nDo not run the next cells until you name the powers and products you expect to matter.\n"
},
{
"cell_type": "markdown",
"metadata": {
"pedagogy": {
"role": "mandatory",
"difficulty": 2,
"kind": "exercise",
"title": "Exercise 1"
}
},
"source": "## MANDATORY | difficulty 2 | Exercise 1\n\nFor `signal = [1,2,3,4]`, `n = 4`, `q = 17`, predict:\n\n- which powers of `\u03c8` appear in row `j = 1`\n- whether the inverse should need both `\u03c8^-1` and `n^-1`\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pedagogy": {
"role": "mandatory",
"difficulty": 2,
"kind": "exercise",
"title": "Prediction Check"
}
},
"outputs": [],
"source": "# MANDATORY | difficulty 2 | Prediction Check\n\nfrom ntt_learning.toy_ntt import find_psi, ntt_psi_exponent_grid\n\npsi = find_psi(4, 17)\nprint(\"psi:\", psi)\nfor row_index, row in enumerate(ntt_psi_exponent_grid(4)):\n print(\"row\", row_index, row)\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pedagogy": {
"role": "mandatory",
"difficulty": 3,
"kind": "exercise",
"title": "Interactive Signal Explorer"
}
},
"outputs": [],
"source": "# MANDATORY | difficulty 3 | Interactive Signal Explorer\n\nimport ipywidgets as widgets\nfrom IPython.display import display\n\nfrom ntt_learning.toy_ntt import find_psi, forward_ntt_psi, inverse_ntt_psi\n\nmodulus = 17\npsi = find_psi(4, modulus)\n\ndef preview(a0=1, a1=2, a2=3, a3=4):\n signal = [a0, a1, a2, a3]\n spectrum = forward_ntt_psi(signal, modulus, psi)\n print(\"signal:\", signal)\n print(\"spectrum:\", spectrum)\n print(\"inverse:\", inverse_ntt_psi(spectrum, modulus, psi))\n\ndisplay(\n widgets.interact(\n preview,\n a0=(0, 16),\n a1=(0, 16),\n a2=(0, 16),\n a3=(0, 16),\n )\n)\n"
},
{
"cell_type": "markdown",
"metadata": {
"pedagogy": {
"role": "mandatory",
"difficulty": 2,
"kind": "exercise",
"title": "Exercise 2"
}
},
"source": "## MANDATORY | difficulty 2 | Exercise 2\n\nExplain what the pointwise multiplication in the transform domain is buying you.\nUse the words \u201creplace convolution by slotwise multiplication\u201d in your own sentence.\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pedagogy": {
"role": "mandatory",
"difficulty": 2,
"kind": "exercise",
"title": "Compare Raw Convolution And Slotwise Multiplication"
}
},
"outputs": [],
"source": "# MANDATORY | difficulty 2 | Compare Raw Convolution And Slotwise Multiplication\n\nfrom ntt_learning.toy_ntt import (\n find_psi,\n forward_ntt_psi,\n inverse_ntt_psi,\n pointwise_multiply,\n schoolbook_convolution,\n)\n\nleft = [2, 1, 0, 3]\nright = [4, 0, 1, 2]\npsi = find_psi(4, 17)\nleft_hat = forward_ntt_psi(left, 17, psi)\nright_hat = forward_ntt_psi(right, 17, psi)\n\nprint(\"schoolbook raw:\", schoolbook_convolution(left, right))\nprint(\"left_hat:\", left_hat)\nprint(\"right_hat:\", right_hat)\nprint(\"pointwise product:\", pointwise_multiply(left_hat, right_hat, 17))\nprint(\"inverse:\", inverse_ntt_psi(pointwise_multiply(left_hat, right_hat, 17), 17, psi))\n"
},
{
"cell_type": "markdown",
"metadata": {
"pedagogy": {
"role": "mandatory",
"difficulty": 2,
"kind": "reflection",
"title": "Reflection"
}
},
"source": "## MANDATORY | difficulty 2 | Reflection\n\nReflection prompt:\n\n- What feels concrete in the direct transform matrix?\n- What still feels too expensive or repetitive?\n- Why are butterflies the obvious next step?\n"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pedagogy": {
"role": "facultative",
"difficulty": 4,
"kind": "exploration",
"title": "Optional: Try A Different Modulus"
}
},
"outputs": [],
"source": "# FACULTATIVE | difficulty 4 | Optional: Try A Different Modulus\n\nfrom ntt_learning.toy_ntt import find_psi, forward_ntt_psi\n\nmodulus = 97\npsi = find_psi(4, modulus)\nsignal = [1, 2, 3, 4]\n\nprint(\"modulus:\", modulus)\nprint(\"psi:\", psi)\nprint(\"spectrum:\", forward_ntt_psi(signal, modulus, psi))\n"
},
{
"cell_type": "markdown",
"metadata": {
"pedagogy": {
"role": "meta",
"difficulty": 1,
"kind": "handoff",
"title": "Next Notebook"
}
},
"source": "## META | difficulty 1 | Next Notebook\n\nNext notebook: `problems.ipynb`\n"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
},
"ntt_learning": {
"title": "Lab: Negative-Wrapped NTT",
"contract_version": "0.2",
"sequence": [
"notebooks/START_HERE.ipynb",
"notebooks/COURSE_BLUEPRINT.ipynb",
"notebooks/foundations/01_convolution_to_toy_ntt/lecture.ipynb",
"notebooks/foundations/01_convolution_to_toy_ntt/lab.ipynb",
"notebooks/foundations/01_convolution_to_toy_ntt/problems.ipynb",
"notebooks/foundations/01_convolution_to_toy_ntt/studio.ipynb",
"notebooks/foundations/02_negative_wrapped_ntt/lecture.ipynb",
"notebooks/foundations/02_negative_wrapped_ntt/lab.ipynb",
"notebooks/foundations/02_negative_wrapped_ntt/problems.ipynb",
"notebooks/foundations/02_negative_wrapped_ntt/studio.ipynb",
"notebooks/butterfly_mechanics/03_fast_forward_ct/lecture.ipynb",
"notebooks/butterfly_mechanics/03_fast_forward_ct/lab.ipynb",
"notebooks/butterfly_mechanics/03_fast_forward_ct/problems.ipynb",
"notebooks/butterfly_mechanics/03_fast_forward_ct/studio.ipynb",
"notebooks/butterfly_mechanics/04_fast_inverse_gs/lecture.ipynb",
"notebooks/butterfly_mechanics/04_fast_inverse_gs/lab.ipynb",
"notebooks/butterfly_mechanics/04_fast_inverse_gs/problems.ipynb",
"notebooks/butterfly_mechanics/04_fast_inverse_gs/studio.ipynb",
"notebooks/kyber_mapping/05_kyber_ntt_and_base_multiplication/lecture.ipynb",
"notebooks/kyber_mapping/05_kyber_ntt_and_base_multiplication/lab.ipynb",
"notebooks/kyber_mapping/05_kyber_ntt_and_base_multiplication/problems.ipynb",
"notebooks/kyber_mapping/05_kyber_ntt_and_base_multiplication/studio.ipynb",
"notebooks/professional/06_debugging_ntt_failures/lecture.ipynb",
"notebooks/professional/06_debugging_ntt_failures/lab.ipynb",
"notebooks/professional/06_debugging_ntt_failures/problems.ipynb",
"notebooks/professional/06_debugging_ntt_failures/studio.ipynb",
"notebooks/COURSE_COMPLETE.ipynb"
]
}
},
"nbformat": 4,
"nbformat_minor": 5
}