<?xml version="1.0" encoding="UTF-8"?>
<Worksheet><Version major="6" minor="1"/><View-Properties><Zoom percentage="150"/></View-Properties><Styles><Layout alignment="left" bullet="none" linespacing="0.0" name="Heading 2" spaceabove="8.0" spacebelow="2.0"/><Layout alignment="left" bullet="none" linespacing="0.0" name="Heading 1" spaceabove="8.0" spacebelow="4.0"/><Layout alignment="left" firstindent="0.0" leftmargin="0.0" linespacing="0.0" name="Normal" rightmargin="0.0" spaceabove="0.0" spacebelow="0.0"/><Font background="[0,0,0]" bold="true" executable="true" family="Monospaced" foreground="[255,0,0]" name="Maple Input" readonly="false"/><Font background="[0,0,0]" bold="false" executable="false" family="Times New Roman" foreground="[0,0,0]" italic="false" name="Text" opaque="false" size="12" underline="false"/><Font background="[0,0,0]" bold="true" family="Serif" name="Heading 2" opaque="false" size="16"/><Font background="[0,0,0]" bold="true" family="Serif" name="Heading 1" opaque="false" size="18"/><Font background="[0,0,0]" executable="false" family="Times New Roman" name="Page Number" readonly="false" underline="false"/></Styles><Page-Numbers enabled="false" first-number="1" first-numbered-page="1" horizontal-location="right" style="Page Number" vertical-location="bottom"/><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">restart;</Text-field></Input></Group><Group><Input><Text-field layout="Heading 1" style="Heading 1">Mod Arithmetic</Text-field></Input></Group><Section><Title><Text-field layout="Heading 2" style="Heading 2">Checking if a List is a Permutation</Text-field></Title><Group><Input><Text-field layout="Normal" style="Text">This procedure tests if a given list of numbers is a permutation, i.e. all entries are different from each other.   In particular, if the length of the list is m and all members are integers in the range 0, ... , m-1 (as occurs when doing arithmetic mod m), then the list is a permutation iff it is a permutation of 0, .. , m-1. </Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">permutation := proc(num_array) 
local i, j, perm , m   :
m := nops(num_array) : perm := true :  
for i from 1 to m-1 do 
   for j from i+1 to m do 
      if num_array[i]=num_array[j] then perm := false: fi 
   od od:
perm;
end: </Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">Here is an example.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">permutation([2,3,1]); permutation([1,1,6,1,6,6]);</Text-field></Input></Group></Section><Section><Title><Text-field layout="Heading 2" style="Heading 2">Multiples of {1, ... , n-1} by a fixed factor, computed mod n</Text-field></Title><Group><Input><Text-field layout="Normal" style="Text">This procedure produces,  for W = 1, ... , n-1, a list of all multiples W*r mod n.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_mult := proc(r,n)
local mult, W :
for W from 1 to n-1 do mult[W]:= W*r mod n  od :
convert(mult, 'list');
end:</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">Here is an example.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input"> mod_mult(3,6) ;  mod_mult(5,8);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">permutation(mod_mult(3,6)); permutation(mod_mult(5,8));</Text-field></Input></Group></Section><Section><Title><Text-field layout="Heading 2" style="Heading 2">Powers of {1, ... , n-1} by a fixed exponent, computed mod n</Text-field></Title><Group><Input><Text-field layout="Normal" style="Text">This procedure produces,  for W = 1, ... , n-1, a list of all powers W^e mod n.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_power := proc(e,n)
local power, W :
for W from 1 to n-1 do power[W]:= W&amp;^e mod n    od :
convert(power, 'list');
end:</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">Here are some examples.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_power(3,7) ;  mod_power(17,33);</Text-field></Input></Group></Section><Section><Title><Text-field layout="Heading 2" style="Heading 2"><Font executable="false">Powers of members of any list by a fixed exponent, computed mod n</Font></Text-field></Title><Group><Input><Text-field layout="Normal" style="Text">This procedure produces, for any list L = {a,b, ...} and exponent d, a list of the powers (a^d, b^d, ... } computed mod n.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_power_list := proc(L,d,n)
local power, i :
for i from 1 to nops(L) do power[i]:= L[i]&amp;^d mod n  od :
convert(power, 'list');
end:</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">Here is an example.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_power_list([2,5,8],3,5);</Text-field></Input></Group></Section><Section><Title><Text-field layout="Heading 2" style="Heading 2">A Property of Mod Multiplication</Text-field></Title><Group><Input><Text-field layout="Normal" style="Text">This example demonstrates a property of mod multiplication.</Text-field><Text-field layout="Normal" style="Text">It first checks if  r  and  n  are relatively prime. </Text-field><Text-field layout="Normal" style="Text">If so, it shows that for the multiplication table mod n, the numbers in the row corresponding to multiplication by r each occur exactly once.   </Text-field><Text-field layout="Normal" style="Text">In other words, multiplication by r mod n produces a permutation (shuffle) of {1, ... , n-1}.  Moreover,  multiplication by the inverse s of r mod n reverse shuffles back to the original order. </Text-field><Text-field layout="Normal" style="Text">(Try some other examples for yourself.)</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">n := 25; r := 6;</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_mult(1,n);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">gcd(r,n);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_mult(r,n);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">permutation(mod_mult(r,n));</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">s := 1/r mod n;</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">s * mod_mult(r,n) mod n;</Text-field></Input></Group></Section><Section><Title><Text-field layout="Heading 2" style="Heading 2">Fermat's Little theorem</Text-field></Title><Group><Input><Text-field layout="Normal" style="Text">This is an example of Fermat's Little Theorem.  </Text-field><Text-field layout="Normal" style="Text">(Also try some other examples of your own.)</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_power(7,7); mod_power(6,7);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">Here is another example.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">mod_power(31,31); mod_power(30,31);</Text-field></Input></Group></Section><Section><Title><Text-field layout="Heading 2" style="Heading 2">RSA Cryptography and a Property of Mod Powers</Text-field></Title><Group><Input><Text-field layout="Normal" style="Text">This example looks at the type of powers W^e mod n that occur in RSA cryptography.</Text-field><Text-field layout="Normal" style="Text">We suppose that p and q are distinct primes, and let n = p*q,  m = (p-1)*(q-1).</Text-field><Text-field layout="Normal" style="Text">The example checks if e and m are relatively prime.   </Text-field><Text-field layout="Normal" style="Text">If they are, it then shows that taking powers W^e mod n of W produces a permutation (shuffle) of {1, ... , n-2}, and that taking powers again by the inverse d of e mod m reverse shuffles back to the original order.</Text-field><Text-field layout="Normal" style="Text">(Try some examples of your own.)</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">In RSA cryptography, p and q are random primes (MUCH bigger than the following example).</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">p:=17;<Font style="Text"> </Font> isprime(p); q:=5 ; isprime(q); </Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input">n := p*q; m := (p-1)*(q-1);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">The number e is the encoding exponent.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">e := 27; gcd(e,m);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">In RSA cryptography, and in this ``toy'' example, the secret number W is one of the following (but not too small).</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">W = mod_power(1,n);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">The secret number W is raised to the power e to give the corresponding coded number C as in the following list.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">C = mod_power(e,n);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">We check that this operation does indeed "shuffle" the set of all possible numbers.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">permutation(mod_power(e,n));</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">The number d is the decoding exponent.</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">d := 1/e mod m;</Text-field></Input></Group><Group><Input><Text-field layout="Normal" style="Text">Raising the coded number C to the power d gives the decoded number D. </Text-field><Text-field layout="Normal" style="Text">This should be the same as the original secret number W, which we now see it is!</Text-field><Text-field layout="Normal" prompt="&gt; " style="Maple Input">D = mod_power_list(mod_power(e,n),d,n);</Text-field></Input></Group><Group><Input><Text-field layout="Normal" prompt="&gt; " style="Maple Input"/></Input></Group></Section><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/><Text-field/></Worksheet>