{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# LLLplus Tutorial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lattice reduction and related lattice tools are used in cryptography, digital communication, and integer programming. The [LLLplus.jl](https://github.com/christianpeel/LLLplus.jl) package includes several lattice reduction tools, a CVP problem solver, and more. This notebook gives an introductory tutorial on lattices and lattice reduction and then shows the application of LLLplus tools in wireless communications, cryptography, and integer programming.\n", "\n", "### Intro to lattices\n", "\n", "What is a lattice? For an $m$ by $n$ matrix $H$ with $m\\ge n$, then the set of points $\\mathbf{y}=H\\mathbf{x}$ for any integer valued vector $\\mathbf{x}$ is a lattice.\n", "\n", "Let's illustrate this with an $m=n=2$ case. Rather than looking all possible vectors $\\mathbf{x}$, let's look at vectors with values from $-2,\\ldots,2$, represented by the columns of the following matrix $X$:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "X = [ -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2\n", " -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2];" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see what these points look like:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-2\n", "\n", "\n", "-1\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "-2\n", "\n", "\n", "-1\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Plots\n", "plot(X[1,:],X[2,:], linealpha = 0,markershape=:circle,legend=false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hopefully this plot looks like a subset of $\\mathbb{Z}^2$ to you.\n", "\n", "So what about a lattice? Let's look at a specially-chosen lattice, illustrated by the matrix $H$ below, and plot it." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×25 Array{Float64,2}:\n", " -2.0 -3.5 -5.0 -6.5 … 6.5 5.0 3.5 2.0\n", " 0.0 -0.866025 -1.73205 -2.59808 2.59808 1.73205 0.866025 0.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H = [ 2.5 -1.5 \n", " 0.866025 -0.866025]\n", "Y = H*X" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-6\n", "\n", "\n", "-3\n", "\n", "\n", "0\n", "\n", "\n", "3\n", "\n", "\n", "6\n", "\n", "\n", "-3\n", "\n", "\n", "-2\n", "\n", "\n", "-1\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "3\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(Y[1,:],Y[2,:], linealpha = 0,markershape=:circle,legend=false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Despite the different shapes, the first two figures above contain the *same* number of points.\n", "\n", "A *lattice* really means that we are spanning the entire $\\mathbb{Z}^2$ space; in the figure above we're only looking a subset of those points. To get a better feel for what's going on, we'll define the function 'count' below. The only thing you need to know is that it allows us to look at a larger set of 'lattice' points without writting the explicit `X` vector down." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×169 Array{Float64,2}:\n", " -6.0 -7.5 -9.0 -10.5 … 10.5 9.0 7.5 6.0\n", " 0.0 -0.866025 -1.73205 -2.59808 2.59808 1.73205 0.866025 0.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function count(M,N)\n", " s = zeros(M,N^M)\n", " z = (1:N^M) .-1\n", " for n = 0:M-1\n", " m = floor.(z/N)\n", " s[M-n,:] = z .- N.*m\n", " z = m\n", " end\n", " return s\n", "end\n", "X = Int.(count(2,13) .- 6)\n", "Y = H*X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll plot the same lattice, with the same plot window (same x and y domain), just with the larger number of points in X." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-6\n", "\n", "\n", "-3\n", "\n", "\n", "0\n", "\n", "\n", "3\n", "\n", "\n", "6\n", "\n", "\n", "-3\n", "\n", "\n", "-2\n", "\n", "\n", "-1\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "3\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(Y[1,:],Y[2,:], linealpha = 0,markershape=:circle,legend=false,xlims=(-8,8),ylims=(-3.5,3.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lattice Reduction\n", "Since the lattice is over the entire $\\mathbb{Z}^2$ space, it's natural to ask if there's a different matrix which will generate the same points. Hopefully, this new lattice will be less skew.\n", "\n", "This is what [Lenstra, Lenstra, Lovász](https://en.wikipedia.org/wiki/Lenstra%E2%80%93Lenstra%E2%80%93Lov%C3%A1sz_lattice_basis_reduction_algorithm) (LLL) lattice reduction does. It decomposes a matrix `H` into a matrix `B` that is still a basis for the lattice that `H` makes, yet has shorter vectors and is closer to orthogonal. It also returns a unimodular (determinant= +/-1) integer valued matrix `T` such that `H T= B`. \n", "\n", "Let's take the LLL decomposition of our `H` from above and plot the resulting lattice." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float64,2}:\n", " 1.0 -0.5 \n", " 0.0 0.866025" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using LLLplus\n", "B,T = lll(H); B" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "-6\n", "\n", "\n", "-3\n", "\n", "\n", "0\n", "\n", "\n", "3\n", "\n", "\n", "6\n", "\n", "\n", "-3\n", "\n", "\n", "-2\n", "\n", "\n", "-1\n", "\n", "\n", "0\n", "\n", "\n", "1\n", "\n", "\n", "2\n", "\n", "\n", "3\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y = B*X\n", "plot(Y[1,:],Y[2,:], linealpha = 0,markershape=:circle,legend=false,xlims=(-8,8),ylims=(-3.5,3.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you compare the two figures above, you'll see that they cover the same lattice points. The lattice basis is also less skew. \n", "\n", "What does that matrix `T` contain? Does `T` really have determinant of +/- 1? The lines below show that this is true. You can check yourself that the inverse of `T` is also integer-valued, and the determinant of the inverse is also +/- 1." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Int64,2}:\n", " 1 -2\n", " 1 -3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "det(T)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### CVP\n", "\n", "The \"closest vector problem\" is to find the closest point in a lattice to a point in the same underlying space. Specifically, given the lattice induced by a matrix `H`, and a vector `y`, find the integer-valued vector `x` that such that `Hx` is closest to `y`. \n", "\n", "$$\\hat{\\mathbf{x}} = \\arg\\min_{x \\in \\mathbf{Z}^n} ||y-Hx||_2$$\n", "\n", "Let's look at an example:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 0.029940685725328726\n", " 2.1336665272450257 " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "H=[1 2; 3 4]\n", "x=[2,-1]\n", "y=H*x + randn(2)/10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Without noise `Hx` would be `[0,2]`; the addition of noise moves it away from that baseline.\n", "\n", "The `cvp` function in LLLplus requires that we use a QR decomposition to prepare the data. Runing the code, we find we can recover the initial `x` vector." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float64,1}:\n", " 2.0\n", " -1.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q,R=qr(H)\n", "x̂ = cvp(Q'*y,R)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Application to Digital communication\n", "\n", "A wireless system with multiple antennas at both the transmitter and receiver is often referred to as a multiple-input, multiple-output (MIMO) system. Some engineers and researchers have proposed \"Massive-MIMO\" systems with hundreds of antennas. Let's look at decoding a binary signal sent on each antenna of such a system. *Some details that everyone but wireless gurus can ignore: we'll assume a synchronized, narrow-band system, with no correlation between antennas and no impairments other than thermal noise*. Let's assume `n` antennas at each transmitter and receiver, with the propogation channel between each transmit and each receive antenna represented by one entry in a `n` by `n` matrix `H`. The propgation matrix `H` reflects on how the transmitted signal bounced off of buildings and other metal surfaces between the transmitter and receiver. We will also add gaussian thermal noise `w`. The overal model is \n", " $$\\mathbf{y} = H\\mathbf{x} + \\mathbf{w}$$\n", "Let's simulate this system:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n=100 # Let's use 100 antennas\n", "x=Int.(rand(0:1,n)) # A binary signal is transmitted from each tx anteanna\n", "H=randn(n,n) # channel coefficients btwn each tx and rx antenna\n", "\n", "# send the signal through the channel\n", "w = rand(n)/10 # thermal noise\n", "y=H*x+w # received data\n", "\n", "# decode the signal\n", "Q,R=qr(H) # The receiver is assumed to know H via training (not modeled)\n", "x̂=cvp(Q'*y,R)\n", "\n", "# check if we decoded the signal correctly\n", "sum(abs.(x-x̂)) # Success!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The signal is decoded correctly.\n", "\n", "Use of lattice tools such as a cvp solver, or using lattice-reduction typically gives much better performance to a broadband wireless system than simple linear receivers (such as `round(inv(H)*y)` ). These principles apply to LTE, WiFi, and many other digital communications problems. It's possible that the difference between the figures in this [article](https://9to5mac.com/2016/10/20/iphone-7-qualcomm-intel-modem-cellular-performance/) is that Qualcomm is using a nonlinear (CVP, LLL, etc...) receiver, while Intel is using a simpler, but poorer-performance linear receiver." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Subset-sum problem\n", "\n", "In the subset-sum problem, one is given a set of numbers `a`, a sum `s`, with the goal of finding whether some subset of `a` sums to `s`. Specifically, if `a` is a vector, then is there a binary vector `x` such that `x'*a==s`? The `subsetsum` function implements the Lagarias and Odlyzko (1983) technique which uses the LLL algorithm to find a polynomial-time solution to the problem. The LLL has been used to crack the [Merkle-Hellman](https://en.wikipedia.org/wiki/Merkle%E2%80%93Hellman_knapsack_cryptosystem) cryptosystem, and has been part of many other cryptoanalytic attacks.\n", "\n", "We do not dive into the details of the `subsetsum` function, rather we give two examples, starting with an example that can be visually inspected to be correct." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6-element Array{Float64,1}:\n", " 1.0\n", " 0.0\n", " 0.0\n", " 1.0\n", " 1.0\n", " 0.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=[1,2,4,9,20,38]\n", "s=30\n", "x=subsetsum(a,s)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# check the result\n", "s==x'*a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The second example sets the vector `a` to be integers sampled uniformly from the range `[1:2^256]`. Despite the large bit-depth of the integers, the LLL-based technique solves the problem quickly." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "40-element Array{BigInt,1}:\n", " 52966019994847903947942438279253224710179175905054670645485359246712459937580\n", " 60284285807960226905685998406630132778973408018084486443877102388277646793029\n", " 88088434549054603795035220275158003859278583319576200298335243431186049890688\n", " 102216526612158525608427694592741849895831500557382123931052317507192769873414\n", " 111366934726704812336357159967275227136813435253283639477131741118741497414209\n", " 68312097531131194452312516287134976387408608625158155795157983708319827992525\n", " 55125975044263260301331673399718883155408719786422595646401499451424633490680\n", " 47238834081716529338009400277108320358882359342047456245665496785110661486235\n", " 72736875888792828199967782662535352416765087576704047857802048708869240140699\n", " 16782831561841572851701628030674265972955034348219805218945406149166119288657\n", " 111518864133851420097778624452034324362035413739427444854597266606003627923669\n", " 57831768993344032657816302725040745056254556148878139499279348917001800710818\n", " 97098252782077376706106816615328949582935885760188519504856906240934843729036\n", " ⋮\n", " 43617756509028935268065153506657982901346484655194542378962659172263286874567\n", " 24514787820482777097850819996878336822470282003929493899369788401994115725049\n", " 659019522081938406151199661909560306826152618189253797350605367892643425394\n", " 18524219368477146697571621538975472659957913040167655493576176702210093300777\n", " 19991360011325671962255145499577849636232056395638210789274105428863669504665\n", " 88153651723799965234202277829535942140168079285597169892962156845840816046269\n", " 28353066670915811088074600658821193560234484933732011087030237533483774640352\n", " 82601474594714678935549600454921758516384400812400780254743103044182810355919\n", " 63154568086128685299021557884272278280055552997286641122525772128245207399865\n", " 6834567513077213689931962590272659344149774329240293527553631454605963273832\n", " 69246004972092376302010948528430353858523542479157092028493846974578313098396\n", " 94103602193652048517365441015548431175193581166147658119497145544211926905650" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N=40\n", "a=rand(1:2^BigInt(256),N)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xtrue=rand(Bool,N)\n", "s=a'*xtrue\n", "setprecision(BigFloat,300)\n", "x=subsetsum(a,s)\n", "s==x'*a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integer Programming Feasibility Problem\n", "\n", "The integer programming feasibility problem is: given an `m` by `n` matrix `A`, and an `m`-dimensional vector `d`, determine if there is an `x` such that `Ax≤d`. For a fixed `n`, H.W. Lenstra (1983) gave a polynomial time solution, which we illustrate in the `integerfeasibility` function. We do not go into the details of the technique, rather illustrate it with a simple example." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A=[10 1 -9; 1 8 8]\n", "xtrue=[0; 2; 9]\n", "d=A*xtrue;\n", "x̂= integerfeasibility(A,d)\n", "xtrue==x̂" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In a more complex example, we set the values of the `20x30` matrix `A` to be uniform from `-10:10` and the `30`-dimensional vector `x` to have values taken uniformly from `0:10`. The `integerfeasibility` function finds the solution quickly." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20×30 Array{Int64,2}:\n", " -1 9 -2 4 -9 4 -4 -8 … 9 1 3 -3 -5 3 8 6\n", " 4 -5 5 8 6 -3 -3 -7 -9 2 1 -2 -6 7 3 -4\n", " 10 -2 6 9 -5 -5 -10 7 -1 -10 5 -4 -3 -1 1 -10\n", " -7 -10 -5 9 -7 10 9 -3 10 -6 -5 -4 1 2 0 5\n", " 8 -7 7 7 0 -7 2 -2 -2 3 4 1 -2 4 -7 -1\n", " -3 -4 -10 0 9 5 2 -7 … 6 7 1 -3 3 3 10 -5\n", " 4 -4 1 -10 6 2 -9 -3 -2 2 -8 7 -3 -5 -7 9\n", " -4 -6 -2 -6 -1 3 10 -4 -9 4 4 -7 -6 9 -5 8\n", " 3 -6 -2 8 5 1 -4 -2 3 8 9 -1 -7 10 0 -8\n", " -5 2 0 7 0 9 0 0 -3 -8 -2 7 -7 -7 6 4\n", " 5 -4 -3 8 4 -2 7 -7 … -5 6 -7 -9 5 -5 -6 -6\n", " -10 3 6 9 0 -6 8 6 -8 8 -7 5 -7 -1 -3 -6\n", " -3 -10 5 8 6 9 10 4 -5 -8 9 1 -8 8 10 -5\n", " 9 0 -4 -5 1 -3 8 10 -10 -4 -9 -3 -4 6 8 -3\n", " 2 5 6 -7 -6 -3 3 2 -3 3 6 8 5 -1 -9 -4\n", " 10 3 -10 -1 -9 -1 -8 5 … -8 6 8 8 10 -6 9 -5\n", " 4 -10 -7 -7 -4 -9 4 1 -5 -4 6 1 -7 2 9 -3\n", " 5 8 6 -1 5 0 5 4 -3 0 -9 0 10 3 1 -10\n", " 6 -5 7 10 -7 -3 9 4 -3 1 -6 4 -2 -10 -7 -6\n", " 9 -10 -5 0 6 9 10 3 -10 -10 9 10 -9 9 -7 9" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n=20\n", "m=30\n", "xtrue = rand(0:10,m)\n", "A = rand(-10:10,n,m)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d=A*xtrue;\n", "x̂ = integerfeasibility(A,d)\n", "xtrue == x̂" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Why Julia?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Why write LLLplus in Julia? The C++ library [fplll](https://github.com/fplll/fplll) is a widely-used, fast, and robust lattice toolbox and is a reasonable choice for many. Even so, there are several advantages to native Julia code. For example, the same Julia code can accept matrices of integers, floats, [DoubleFloats](https://github.com/JuliaMath/DoubleFloats.jl), Float128s from [Quadmath](https://github.com/JuliaMath/Quadmath.jl), BigInts, BigFloats, and likely others. Hopefully LLLplus can also be used in other ways as part of a composable Julia program.\n", "\n", "Want to learn more about lattice techniques? If you want a broad survey, try [\"The LLL Algorithm\"](https://www.springer.com/us/book/9783642022944). The algorithm pseudocode in the [monograph by Bremner](https://www.amazon.com/Lattice-Basis-Reduction-Introduction-Applications/dp/1439807027) and the [survey paper](http://www.ant.uni-bremen.de/sixcms/media.php/102/10740/SPM_2011_Wuebben.pdf) were helpful to me. " ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.1.0", "language": "julia", "name": "julia-1.1" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.1.0" } }, "nbformat": 4, "nbformat_minor": 2 }