/usr/share/doc/libghc-crypto-doc/html/src/Codec-Encryption-RSA-NumberTheory.html is in libghc-crypto-doc 4.2.5.1-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>Codec/Encryption/RSA/NumberTheory.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>--Copyright 2001, 2002, 2003 David J. Sankel</span>
<a name="line-2"></a><span class='hs-comment'>--</span>
<a name="line-3"></a><span class='hs-comment'>--This file is part of rsa-haskell.</span>
<a name="line-4"></a><span class='hs-comment'>--rsa-haskell is free software; you can redistribute it and/or modify</span>
<a name="line-5"></a><span class='hs-comment'>--it under the terms of the GNU General Public License as published by</span>
<a name="line-6"></a><span class='hs-comment'>--the Free Software Foundation; either version 2 of the License, or</span>
<a name="line-7"></a><span class='hs-comment'>--(at your option) any later version.</span>
<a name="line-8"></a><span class='hs-comment'>--</span>
<a name="line-9"></a><span class='hs-comment'>--rsa-haskell is distributed in the hope that it will be useful,</span>
<a name="line-10"></a><span class='hs-comment'>--but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="line-11"></a><span class='hs-comment'>--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<a name="line-12"></a><span class='hs-comment'>--GNU General Public License for more details.</span>
<a name="line-13"></a><span class='hs-comment'>--</span>
<a name="line-14"></a><span class='hs-comment'>--You should have received a copy of the GNU General Public License</span>
<a name="line-15"></a><span class='hs-comment'>--along with rsa-haskell; if not, write to the Free Software</span>
<a name="line-16"></a><span class='hs-comment'>--Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
<a name="line-17"></a>
<a name="line-18"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Codec</span><span class='hs-varop'>.</span><span class='hs-conid'>Encryption</span><span class='hs-varop'>.</span><span class='hs-conid'>RSA</span><span class='hs-varop'>.</span><span class='hs-conid'>NumberTheory</span><span class='hs-layout'>(</span>
<a name="line-19"></a><a name="inverse"></a><span class='hs-definition'>inverse</span><span class='hs-layout'>,</span> <span class='hs-varid'>extEuclGcd</span><span class='hs-layout'>,</span> <span class='hs-varid'>simplePrimalityTest</span><span class='hs-layout'>,</span> <span class='hs-varid'>getPrime</span><span class='hs-layout'>,</span> <span class='hs-varid'>pg</span><span class='hs-layout'>,</span> <span class='hs-varid'>isPrime</span><span class='hs-layout'>,</span>
<a name="line-20"></a><a name="rabinMillerPrimalityTest"></a><span class='hs-definition'>rabinMillerPrimalityTest</span><span class='hs-layout'>,</span> <span class='hs-varid'>expmod</span><span class='hs-layout'>,</span> <span class='hs-varid'>factor</span><span class='hs-layout'>,</span> <span class='hs-varid'>testInverse</span><span class='hs-layout'>,</span> <span class='hs-varid'>primes</span><span class='hs-layout'>,</span> <span class='hs-layout'>(</span><span class='hs-varop'>/|</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span>
<a name="line-21"></a><a name="randomOctet"></a><span class='hs-definition'>randomOctet</span>
<a name="line-22"></a><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-23"></a>
<a name="line-24"></a>
<a name="line-25"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Random</span><span class='hs-layout'>(</span><span class='hs-varid'>getStdRandom</span><span class='hs-layout'>,</span><span class='hs-varid'>randomR</span><span class='hs-layout'>)</span>
<a name="line-26"></a><span class='hs-comment'>--The following line is required for ghc optomized implementation</span>
<a name="line-27"></a><span class='hs-comment'>-- (see comments beginning with GHC):</span>
<a name="line-28"></a><span class='hs-comment'>-- import Bits(setBit)</span>
<a name="line-29"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>List</span><span class='hs-layout'>(</span><span class='hs-varid'>elemIndex</span><span class='hs-layout'>)</span>
<a name="line-30"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Maybe</span><span class='hs-layout'>(</span><span class='hs-varid'>fromJust</span><span class='hs-layout'>)</span>
<a name="line-31"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Char</span><span class='hs-layout'>(</span><span class='hs-varid'>chr</span><span class='hs-layout'>,</span><span class='hs-varid'>ord</span><span class='hs-layout'>)</span>
<a name="line-32"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Data</span><span class='hs-varop'>.</span><span class='hs-conid'>Bits</span><span class='hs-layout'>(</span><span class='hs-varid'>xor</span><span class='hs-layout'>)</span>
<a name="line-33"></a>
<a name="line-34"></a><span class='hs-comment'>--Precondition: the integer is >= 0</span>
<a name="line-35"></a><span class='hs-definition'>randomOctet</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span><span class='hs-layout'>(</span> <span class='hs-conid'>String</span> <span class='hs-layout'>)</span>
<a name="line-36"></a><span class='hs-definition'>randomOctet</span> <span class='hs-varid'>n</span>
<a name="line-37"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>n</span> <span class='hs-varop'><</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-str'>"randomOctet argument doesn't meet preconditions"</span>
<a name="line-38"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>sequence</span> <span class='hs-varop'>$</span> <span class='hs-varid'>take</span> <span class='hs-varid'>n</span> <span class='hs-varop'>$</span> <span class='hs-varid'>repeat</span> <span class='hs-varop'>$</span> <span class='hs-varid'>getStdRandom</span> <span class='hs-layout'>(</span><span class='hs-varid'>randomR</span><span class='hs-layout'>(</span> <span class='hs-num'>0</span><span class='hs-layout'>,</span><span class='hs-num'>255</span><span class='hs-layout'>)</span> <span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-39"></a> <span class='hs-varop'>>>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>return</span> <span class='hs-varop'>.</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-varid'>chr</span><span class='hs-layout'>)</span> <span class='hs-layout'>)</span>
<a name="line-40"></a>
<a name="line-41"></a><a name="factor"></a><span class='hs-comment'>--Returns a list [r_1,r_2,r_3,r_4, . . ., r_n ] where</span>
<a name="line-42"></a><span class='hs-comment'>-- a = p_1^r_1 * p_2^r_2 * p_3^r_3 * . . . * p_n^r_n</span>
<a name="line-43"></a><span class='hs-definition'>factor</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span>
<a name="line-44"></a><span class='hs-definition'>factor</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>factor_1</span>
<a name="line-45"></a>
<a name="line-46"></a><a name="factor_1"></a><span class='hs-comment'>--An implimentation of factor</span>
<a name="line-47"></a><span class='hs-definition'>factor_1</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Int</span><span class='hs-keyglyph'>]</span>
<a name="line-48"></a><span class='hs-definition'>factor_1</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>reverse</span> <span class='hs-varop'>.</span> <span class='hs-varid'>dropWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'>==</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>reverse</span>
<a name="line-49"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>x</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>largestPower</span> <span class='hs-varid'>x</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>takeWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'><=</span> <span class='hs-varid'>a</span> <span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>primes</span>
<a name="line-50"></a>
<a name="line-51"></a><a name="factor_2"></a><span class='hs-comment'>--Another implimentation of factor</span>
<a name="line-52"></a><span class='hs-definition'>factor_2</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Integer</span><span class='hs-keyglyph'>]</span>
<a name="line-53"></a><span class='hs-definition'>factor_2</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span>
<a name="line-54"></a> <span class='hs-keyword'>let</span>
<a name="line-55"></a> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromIntegral</span><span class='hs-layout'>)</span> <span class='hs-varop'>.</span> <span class='hs-varid'>reverse</span> <span class='hs-varop'>.</span> <span class='hs-varid'>dropWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'>==</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span>
<a name="line-56"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>reverse</span> <span class='hs-varop'>.</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>x</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>largestPower</span> <span class='hs-varid'>x</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-57"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>takeWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'><=</span> <span class='hs-varid'>a</span> <span class='hs-varop'>`div`</span> <span class='hs-num'>2</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-varid'>primes</span>
<a name="line-58"></a> <span class='hs-keyword'>in</span>
<a name="line-59"></a> <span class='hs-keyword'>if</span> <span class='hs-layout'>(</span><span class='hs-varid'>length</span> <span class='hs-varid'>p</span> <span class='hs-varop'>==</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span>
<a name="line-60"></a> <span class='hs-keyword'>then</span> <span class='hs-layout'>(</span><span class='hs-varid'>take</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>fromIntegral</span> <span class='hs-varop'>.</span> <span class='hs-varid'>fromJust</span> <span class='hs-varop'>$</span> <span class='hs-varid'>elemIndex</span> <span class='hs-varid'>a</span> <span class='hs-varid'>primes</span><span class='hs-layout'>)</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>repeat</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-61"></a> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>]</span>
<a name="line-62"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>p</span>
<a name="line-63"></a>
<a name="line-64"></a><span class='hs-comment'>--Find the inverse of x (mod n)</span>
<a name="line-65"></a><span class='hs-definition'>inverse</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span>
<a name="line-66"></a><span class='hs-definition'>inverse</span> <span class='hs-varid'>x</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>fst</span> <span class='hs-layout'>(</span><span class='hs-varid'>extEuclGcd</span> <span class='hs-varid'>x</span> <span class='hs-varid'>n</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>n</span>
<a name="line-67"></a>
<a name="line-68"></a><a name="testInverse"></a><span class='hs-definition'>testInverse</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span><span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-69"></a><span class='hs-definition'>testInverse</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>inverse</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span><span class='hs-varop'>*</span><span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>b</span> <span class='hs-varop'>==</span> <span class='hs-num'>1</span>
<a name="line-70"></a>
<a name="line-71"></a><a name="extEuclGcd"></a><span class='hs-comment'>--Extended Eucildean algorithm</span>
<a name="line-72"></a><span class='hs-comment'>--Returns (x,y) where gcd(a,b) = xa + yb</span>
<a name="line-73"></a><span class='hs-definition'>extEuclGcd</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span><span class='hs-layout'>,</span><span class='hs-conid'>Integer</span><span class='hs-layout'>)</span>
<a name="line-74"></a><span class='hs-definition'>extEuclGcd</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>extEuclGcd_iter</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span> <span class='hs-layout'>(</span><span class='hs-num'>1</span><span class='hs-layout'>,</span><span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-num'>0</span><span class='hs-layout'>,</span><span class='hs-num'>1</span><span class='hs-layout'>)</span>
<a name="line-75"></a>
<a name="line-76"></a><a name="extEuclGcd_iter"></a><span class='hs-definition'>extEuclGcd_iter</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span>
<a name="line-77"></a> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span><span class='hs-layout'>,</span><span class='hs-conid'>Integer</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span><span class='hs-layout'>,</span><span class='hs-conid'>Integer</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>Integer</span><span class='hs-layout'>,</span><span class='hs-conid'>Integer</span><span class='hs-layout'>)</span>
<a name="line-78"></a><span class='hs-definition'>extEuclGcd_iter</span> <span class='hs-varid'>a</span> <span class='hs-varid'>b</span> <span class='hs-layout'>(</span><span class='hs-varid'>c1</span><span class='hs-layout'>,</span><span class='hs-varid'>c2</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>d1</span><span class='hs-layout'>,</span><span class='hs-varid'>d2</span><span class='hs-layout'>)</span>
<a name="line-79"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-varop'>></span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span> <span class='hs-layout'>(</span><span class='hs-varid'>r1</span> <span class='hs-varop'>==</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>d1</span><span class='hs-layout'>,</span><span class='hs-varid'>d2</span><span class='hs-layout'>)</span>
<a name="line-80"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-varop'>></span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span> <span class='hs-layout'>(</span><span class='hs-varid'>r1</span> <span class='hs-varop'>/=</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>extEuclGcd_iter</span>
<a name="line-81"></a> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-comment'>-</span> <span class='hs-layout'>(</span><span class='hs-varid'>q1</span><span class='hs-varop'>*</span><span class='hs-varid'>b</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varid'>b</span> <span class='hs-layout'>(</span><span class='hs-varid'>c1</span> <span class='hs-comment'>-</span> <span class='hs-layout'>(</span><span class='hs-varid'>q1</span><span class='hs-varop'>*</span><span class='hs-varid'>d1</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-varid'>c2</span> <span class='hs-comment'>-</span> <span class='hs-layout'>(</span><span class='hs-varid'>q1</span><span class='hs-varop'>*</span><span class='hs-varid'>d2</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>d1</span><span class='hs-layout'>,</span><span class='hs-varid'>d2</span><span class='hs-layout'>)</span>
<a name="line-82"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-varop'><=</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span> <span class='hs-layout'>(</span><span class='hs-varid'>r2</span> <span class='hs-varop'>==</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>c1</span><span class='hs-layout'>,</span><span class='hs-varid'>c2</span><span class='hs-layout'>)</span>
<a name="line-83"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-varop'><=</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span> <span class='hs-layout'>(</span><span class='hs-varid'>r2</span> <span class='hs-varop'>/=</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>extEuclGcd_iter</span>
<a name="line-84"></a> <span class='hs-varid'>a</span> <span class='hs-layout'>(</span><span class='hs-varid'>b</span> <span class='hs-comment'>-</span> <span class='hs-layout'>(</span><span class='hs-varid'>q2</span><span class='hs-varop'>*</span><span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>c1</span><span class='hs-layout'>,</span><span class='hs-varid'>c2</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span> <span class='hs-varid'>d1</span> <span class='hs-comment'>-</span> <span class='hs-layout'>(</span><span class='hs-varid'>q2</span><span class='hs-varop'>*</span><span class='hs-varid'>c1</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-varid'>d2</span><span class='hs-comment'>-</span> <span class='hs-layout'>(</span><span class='hs-varid'>q2</span><span class='hs-varop'>*</span><span class='hs-varid'>c2</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-85"></a> <span class='hs-keyword'>where</span>
<a name="line-86"></a> <span class='hs-varid'>q1</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>a</span> <span class='hs-varop'>`div`</span> <span class='hs-varid'>b</span>
<a name="line-87"></a> <span class='hs-varid'>q2</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>b</span> <span class='hs-varop'>`div`</span> <span class='hs-varid'>a</span>
<a name="line-88"></a> <span class='hs-varid'>r1</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>a</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>b</span>
<a name="line-89"></a> <span class='hs-varid'>r2</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>b</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>a</span>
<a name="line-90"></a>
<a name="line-91"></a><span class='hs-comment'>-- This will return a random Integer of n bits. The highest order bit</span>
<a name="line-92"></a><span class='hs-comment'>-- will always be 1.</span>
<a name="line-93"></a>
<a name="line-94"></a><span class='hs-comment'>-- GHC optomized implementation</span>
<a name="line-95"></a><span class='hs-comment'>-- getNumber :: Int -> IO Integer</span>
<a name="line-96"></a><span class='hs-comment'>-- getNumber n = do </span>
<a name="line-97"></a><span class='hs-comment'>-- i <- getStdRandom ( randomR (0, a-1 ) )</span>
<a name="line-98"></a><span class='hs-comment'>-- return (setBit i (n-1))</span>
<a name="line-99"></a><span class='hs-comment'>-- where</span>
<a name="line-100"></a><span class='hs-comment'>-- a = (2^n) ::Integer</span>
<a name="line-101"></a>
<a name="line-102"></a><a name="getNumber"></a><span class='hs-comment'>--This is the portable version</span>
<a name="line-103"></a><span class='hs-definition'>getNumber</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>Integer</span>
<a name="line-104"></a><span class='hs-definition'>getNumber</span> <span class='hs-varid'>n</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-105"></a> <span class='hs-varid'>i</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>getStdRandom</span> <span class='hs-layout'>(</span> <span class='hs-varid'>randomR</span> <span class='hs-layout'>(</span><span class='hs-num'>0</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-comment'>-</span><span class='hs-num'>1</span> <span class='hs-layout'>)</span> <span class='hs-layout'>)</span>
<a name="line-106"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>i</span><span class='hs-varop'>+</span><span class='hs-layout'>(</span><span class='hs-num'>2</span><span class='hs-varop'>^</span><span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-107"></a> <span class='hs-keyword'>where</span>
<a name="line-108"></a> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-num'>2</span><span class='hs-varop'>^</span><span class='hs-layout'>(</span><span class='hs-varid'>n</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>::</span><span class='hs-conid'>Integer</span>
<a name="line-109"></a>
<a name="line-110"></a><span class='hs-comment'>--Returns a probable prime number of nBits bits</span>
<a name="line-111"></a>
<a name="line-112"></a><span class='hs-comment'>-- GHC optomized implementation</span>
<a name="line-113"></a><span class='hs-comment'>-- getPrime :: Int -> IO Integer</span>
<a name="line-114"></a><span class='hs-comment'>-- getPrime nBits = do</span>
<a name="line-115"></a><span class='hs-comment'>-- r <- getNumber nBits</span>
<a name="line-116"></a><span class='hs-comment'>-- let p = (setBit r 0) --Make it odd for speed</span>
<a name="line-117"></a><span class='hs-comment'>-- pIsPrime <- isPrime p</span>
<a name="line-118"></a><span class='hs-comment'>-- if( pIsPrime )</span>
<a name="line-119"></a><span class='hs-comment'>-- then return p</span>
<a name="line-120"></a><span class='hs-comment'>-- else getPrime nBits</span>
<a name="line-121"></a>
<a name="line-122"></a><a name="getPrime"></a><span class='hs-comment'>--This is the portable version</span>
<a name="line-123"></a><span class='hs-definition'>getPrime</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>Integer</span>
<a name="line-124"></a><span class='hs-definition'>getPrime</span> <span class='hs-varid'>nBits</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-125"></a> <span class='hs-varid'>r</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>getNumber</span> <span class='hs-varid'>nBits</span>
<a name="line-126"></a> <span class='hs-keyword'>let</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span><span class='hs-layout'>(</span> <span class='hs-num'>2</span> <span class='hs-varop'>/|</span> <span class='hs-varid'>r</span> <span class='hs-layout'>)</span> <span class='hs-keyword'>then</span> <span class='hs-varid'>r</span> <span class='hs-keyword'>else</span> <span class='hs-varid'>r</span><span class='hs-varop'>+</span><span class='hs-num'>1</span>
<a name="line-127"></a> <span class='hs-varid'>pIsPrime</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>isPrime</span> <span class='hs-varid'>p</span>
<a name="line-128"></a> <span class='hs-keyword'>if</span><span class='hs-layout'>(</span> <span class='hs-varid'>pIsPrime</span> <span class='hs-layout'>)</span>
<a name="line-129"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>return</span> <span class='hs-varid'>p</span>
<a name="line-130"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>getPrime</span> <span class='hs-varid'>nBits</span>
<a name="line-131"></a>
<a name="line-132"></a><a name="pg"></a><span class='hs-comment'>--Prime Generate:</span>
<a name="line-133"></a><span class='hs-comment'>--Generates a prime p | minimum <= p <= maximum and gcd p e == 1</span>
<a name="line-134"></a><span class='hs-definition'>pg</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span><span class='hs-layout'>(</span><span class='hs-conid'>Integer</span><span class='hs-layout'>)</span>
<a name="line-135"></a><span class='hs-definition'>pg</span> <span class='hs-varid'>minimum</span> <span class='hs-varid'>maximum</span> <span class='hs-varid'>e</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
<a name="line-136"></a> <span class='hs-varid'>p</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>getStdRandom</span><span class='hs-layout'>(</span> <span class='hs-varid'>randomR</span><span class='hs-layout'>(</span> <span class='hs-varid'>minimum</span><span class='hs-layout'>,</span> <span class='hs-varid'>maximum</span> <span class='hs-layout'>)</span> <span class='hs-layout'>)</span>
<a name="line-137"></a> <span class='hs-varid'>pIsPrime</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>isPrime</span> <span class='hs-varid'>p</span>
<a name="line-138"></a> <span class='hs-keyword'>if</span><span class='hs-layout'>(</span> <span class='hs-varid'>pIsPrime</span> <span class='hs-varop'>&&</span> <span class='hs-layout'>(</span><span class='hs-varid'>gcd</span> <span class='hs-varid'>p</span> <span class='hs-varid'>e</span><span class='hs-layout'>)</span> <span class='hs-varop'>==</span> <span class='hs-num'>1</span> <span class='hs-layout'>)</span>
<a name="line-139"></a> <span class='hs-keyword'>then</span> <span class='hs-varid'>return</span> <span class='hs-varid'>p</span>
<a name="line-140"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>pg</span> <span class='hs-varid'>minimum</span> <span class='hs-varid'>maximum</span> <span class='hs-varid'>e</span>
<a name="line-141"></a>
<a name="line-142"></a><a name="isPrime"></a><span class='hs-definition'>isPrime</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>Bool</span>
<a name="line-143"></a><span class='hs-definition'>isPrime</span> <span class='hs-varid'>a</span>
<a name="line-144"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-varop'><=</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-conid'>False</span>
<a name="line-145"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-varop'><=</span> <span class='hs-num'>2000</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>simplePrimalityTest</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-146"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>if</span> <span class='hs-layout'>(</span><span class='hs-varid'>simplePrimalityTest</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-147"></a> <span class='hs-keyword'>then</span> <span class='hs-keyword'>do</span> <span class='hs-comment'>--Do this 5 times for saftey</span>
<a name="line-148"></a> <span class='hs-varid'>test</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>mapM</span> <span class='hs-varid'>rabinMillerPrimalityTest</span> <span class='hs-varop'>$</span> <span class='hs-varid'>take</span> <span class='hs-num'>5</span> <span class='hs-varop'>$</span> <span class='hs-varid'>repeat</span> <span class='hs-varid'>a</span>
<a name="line-149"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>and</span> <span class='hs-varid'>test</span><span class='hs-layout'>)</span>
<a name="line-150"></a> <span class='hs-keyword'>else</span> <span class='hs-varid'>return</span> <span class='hs-conid'>False</span>
<a name="line-151"></a>
<a name="line-152"></a><a name="simplePrimalityTest"></a><span class='hs-definition'>simplePrimalityTest</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-153"></a><span class='hs-definition'>simplePrimalityTest</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldr</span> <span class='hs-layout'>(</span><span class='hs-varop'>&&</span><span class='hs-layout'>)</span> <span class='hs-conid'>True</span> <span class='hs-layout'>(</span><span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-varop'>/|</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>(</span><span class='hs-varid'>takeWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'><</span><span class='hs-varid'>it</span><span class='hs-layout'>)</span> <span class='hs-varid'>primes</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-154"></a> <span class='hs-keyword'>where</span> <span class='hs-varid'>it</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>min</span> <span class='hs-num'>2000</span> <span class='hs-varid'>a</span>
<a name="line-155"></a>
<a name="line-156"></a><a name="largestPower"></a><span class='hs-comment'>--returns greatest z where x^z | y</span>
<a name="line-157"></a><span class='hs-definition'>largestPower</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Int</span>
<a name="line-158"></a><span class='hs-definition'>largestPower</span> <span class='hs-varid'>x</span> <span class='hs-varid'>y</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fromJust</span> <span class='hs-varop'>.</span> <span class='hs-varid'>elemIndex</span> <span class='hs-conid'>False</span>
<a name="line-159"></a> <span class='hs-varop'>.</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-varid'>b</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-varid'>y</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>x</span><span class='hs-varop'>^</span><span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-varop'>==</span> <span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>1</span><span class='hs-keyglyph'>..</span><span class='hs-keyglyph'>]</span>
<a name="line-160"></a>
<a name="line-161"></a><span class='hs-definition'>rabinMillerPrimalityTest</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>Bool</span>
<a name="line-162"></a><span class='hs-definition'>rabinMillerPrimalityTest</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rabinMillerPrimalityTest_iter_1</span> <span class='hs-varid'>p</span> <span class='hs-varid'>b</span> <span class='hs-varid'>m</span>
<a name="line-163"></a> <span class='hs-keyword'>where</span>
<a name="line-164"></a> <span class='hs-varid'>b</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>fromIntegral</span> <span class='hs-varop'>$</span> <span class='hs-varid'>largestPower</span> <span class='hs-num'>2</span> <span class='hs-layout'>(</span><span class='hs-varid'>p</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span>
<a name="line-165"></a> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>p</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varop'>`div`</span> <span class='hs-layout'>(</span><span class='hs-num'>2</span><span class='hs-varop'>^</span><span class='hs-varid'>b</span><span class='hs-layout'>)</span>
<a name="line-166"></a>
<a name="line-167"></a><a name="rabinMillerPrimalityTest_iter_1"></a><span class='hs-comment'>--The ?prime? Number -> The amount of iterations -> b -> m</span>
<a name="line-168"></a><span class='hs-definition'>rabinMillerPrimalityTest_iter_1</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>Bool</span>
<a name="line-169"></a><span class='hs-definition'>rabinMillerPrimalityTest_iter_1</span> <span class='hs-varid'>p</span> <span class='hs-varid'>b</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>=</span>
<a name="line-170"></a> <span class='hs-keyword'>do</span>
<a name="line-171"></a> <span class='hs-varid'>a</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>getStdRandom</span> <span class='hs-layout'>(</span> <span class='hs-varid'>randomR</span> <span class='hs-layout'>(</span><span class='hs-num'>0</span><span class='hs-layout'>,</span> <span class='hs-num'>2000</span> <span class='hs-layout'>)</span> <span class='hs-layout'>)</span>
<a name="line-172"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-varid'>rabinMillerPrimalityTest_iter_2</span> <span class='hs-varid'>p</span> <span class='hs-varid'>b</span> <span class='hs-num'>0</span> <span class='hs-layout'>(</span><span class='hs-varid'>expmod</span> <span class='hs-varid'>a</span> <span class='hs-varid'>m</span> <span class='hs-varid'>p</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-173"></a>
<a name="line-174"></a><a name="rabinMillerPrimalityTest_iter_2"></a><span class='hs-definition'>rabinMillerPrimalityTest_iter_2</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span>
<a name="line-175"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-176"></a><span class='hs-definition'>rabinMillerPrimalityTest_iter_2</span> <span class='hs-varid'>p</span> <span class='hs-varid'>b</span> <span class='hs-varid'>j</span> <span class='hs-varid'>z</span>
<a name="line-177"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>z</span> <span class='hs-varop'>==</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varop'>||</span> <span class='hs-layout'>(</span><span class='hs-varid'>z</span> <span class='hs-varop'>==</span> <span class='hs-varid'>p</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-178"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>j</span> <span class='hs-varop'>></span> <span class='hs-num'>0</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span> <span class='hs-layout'>(</span><span class='hs-varid'>z</span> <span class='hs-varop'>==</span> <span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-179"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>j</span><span class='hs-varop'>+</span><span class='hs-num'>1</span> <span class='hs-varop'><</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span> <span class='hs-layout'>(</span><span class='hs-varid'>z</span> <span class='hs-varop'>/=</span> <span class='hs-varid'>p</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-180"></a> <span class='hs-layout'>(</span><span class='hs-varid'>rabinMillerPrimalityTest_iter_2</span> <span class='hs-varid'>p</span> <span class='hs-varid'>b</span> <span class='hs-layout'>(</span><span class='hs-varid'>j</span><span class='hs-varop'>+</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-layout'>(</span><span class='hs-varid'>z</span><span class='hs-varop'>^</span><span class='hs-num'>2</span><span class='hs-layout'>)</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>p</span> <span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-181"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>z</span> <span class='hs-varop'>==</span> <span class='hs-varid'>p</span> <span class='hs-comment'>-</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-182"></a> <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-varid'>j</span><span class='hs-varop'>+</span><span class='hs-num'>1</span> <span class='hs-varop'>==</span> <span class='hs-varid'>b</span><span class='hs-layout'>)</span> <span class='hs-varop'>&&</span> <span class='hs-layout'>(</span><span class='hs-varid'>z</span> <span class='hs-varop'>/=</span> <span class='hs-varid'>p</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-183"></a>
<a name="line-184"></a><a name="expmod"></a><span class='hs-comment'>--a^x (mod m)</span>
<a name="line-185"></a><span class='hs-definition'>expmod</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span>
<a name="line-186"></a><span class='hs-definition'>expmod</span> <span class='hs-varid'>a</span> <span class='hs-varid'>x</span> <span class='hs-varid'>m</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>x</span> <span class='hs-varop'>==</span> <span class='hs-num'>0</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>1</span>
<a name="line-187"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>x</span> <span class='hs-varop'>==</span> <span class='hs-num'>1</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>a</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>m</span>
<a name="line-188"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>even</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-varid'>p</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>expmod</span> <span class='hs-varid'>a</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span> <span class='hs-varop'>`div`</span> <span class='hs-num'>2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>m</span>
<a name="line-189"></a> <span class='hs-keyword'>in</span> <span class='hs-layout'>(</span><span class='hs-varid'>p</span><span class='hs-varop'>^</span><span class='hs-num'>2</span><span class='hs-layout'>)</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>m</span>
<a name="line-190"></a> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-layout'>(</span><span class='hs-varid'>a</span> <span class='hs-varop'>*</span> <span class='hs-varid'>expmod</span> <span class='hs-varid'>a</span> <span class='hs-layout'>(</span><span class='hs-varid'>x</span><span class='hs-comment'>-</span><span class='hs-num'>1</span><span class='hs-layout'>)</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>m</span>
<a name="line-191"></a>
<a name="line-192"></a><a name="intSqrt"></a><span class='hs-comment'>--Largest x where x^2 < i</span>
<a name="line-193"></a><span class='hs-definition'>intSqrt</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span>
<a name="line-194"></a><span class='hs-definition'>intSqrt</span> <span class='hs-varid'>i</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>floor</span> <span class='hs-layout'>(</span><span class='hs-varid'>sqrt</span> <span class='hs-layout'>(</span><span class='hs-varid'>fromIntegral</span> <span class='hs-varid'>i</span> <span class='hs-layout'>)</span> <span class='hs-layout'>)</span>
<a name="line-195"></a>
<a name="line-196"></a><a name="/%7c"></a><span class='hs-comment'>--The doesn't divide function</span>
<a name="line-197"></a><span class='hs-layout'>(</span><span class='hs-varop'>/|</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Integer</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Bool</span>
<a name="line-198"></a><a name="a"></a><span class='hs-definition'>a</span> <span class='hs-varop'>/|</span> <span class='hs-varid'>b</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>b</span> <span class='hs-varop'>`mod`</span> <span class='hs-varid'>a</span> <span class='hs-varop'>/=</span> <span class='hs-num'>0</span>
<a name="line-199"></a>
<a name="line-200"></a><a name="primes"></a><span class='hs-comment'>--List of primes</span>
<a name="line-201"></a><span class='hs-definition'>primes</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Integer</span><span class='hs-keyglyph'>]</span>
<a name="line-202"></a><span class='hs-definition'>primes</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>2</span><span class='hs-conop'>:</span><span class='hs-keyglyph'>[</span><span class='hs-varid'>x</span> <span class='hs-keyglyph'>|</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'><-</span> <span class='hs-keyglyph'>[</span><span class='hs-num'>3</span><span class='hs-layout'>,</span><span class='hs-num'>5</span><span class='hs-keyglyph'>..</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-varid'>foldr</span> <span class='hs-layout'>(</span><span class='hs-varop'>&&</span><span class='hs-layout'>)</span> <span class='hs-conid'>True</span>
<a name="line-203"></a> <span class='hs-layout'>(</span> <span class='hs-varid'>map</span> <span class='hs-layout'>(</span> <span class='hs-varop'>/|</span> <span class='hs-varid'>x</span> <span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-varid'>takeWhile</span> <span class='hs-layout'>(</span><span class='hs-varop'><=</span><span class='hs-layout'>(</span><span class='hs-varid'>intSqrt</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span> <span class='hs-varid'>primes</span> <span class='hs-layout'>)</span> <span class='hs-layout'>)</span> <span class='hs-keyglyph'>]</span>
</pre></body>
</html>
|