root/trunk/etc/bigint/modinv.d

Revision 13, 3.3 kB (checked in by Arcane Jill, 5 years ago)

--

Line 
1 module etc.bigint.modinv;
2 import etc.bigint.bigint_int;
3 import etc.bigint.gcd;
4
5 /*
6
7 Copyright (c) 2004, Arcane Jill
8
9 All rights reserved. Intellectual Property Me Arse!
10
11 Redistribution and use in source and binary forms, with or without modification, are permitted
12 provided that the following conditions are met:
13
14    * Redistributions of source code must retain the above copyright notice, the phrase
15      "Intellectual Property Me Arse!", this list of conditions, and the following disclaimer.
16    * Redistributions in binary form must reproduce the above copyright notice, the phrase
17      "Intellectual Property Me Arse!", this list of conditions and the following disclaimer
18      in the documentation and/or other materials provided with the distribution.
19    * The name Arcane Jill may not be used to endorse or promote products derived from this
20      software without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
23 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER,
25 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED, AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 OF SUCH DAMAGE.
31
32 */
33
34 /*  ------------------------------------------------------------------------------------
35     Returns z such that ((a*z)%p == 1
36 */
37 Int modInv(Int a, Int b)
38 in
39 {
40     assert(a > 0);
41     assert(b > 0);
42 }
43 out(z)
44 {
45     assert((a*z)%b == 1);
46 }
47 body
48 {
49     Int x, y;
50     Int d = gcd(a, b, x, y);
51     if (d != 1) throw new IntException("modInv(a,b) not defined for a, b such that gcd(a,b) != 1");
52     if (x.negative) x = x + b;
53     return x;
54 }
55
56 /*  ------------------------------------------------------------------------------------
57     Returns z such that ((a*z)%p == 1 (given p is prime)
58
59     WARNING - If you call this function with p not prime then you have a BUG! Do not do this.
60     The in contract cannot test for this, because primality testing is a higher-level function and could lead to an infinite loop
61 */
62 Int modInvPrime(Int a, Int p)
63 in
64 {
65     assert(a > 0);
66     assert(a < p);
67     assert((p & 1u) == 1u); // NOTE: The REAL in condition is that p must be PRIME, not p must be odd.
68                             // Unfortunately we can't test that here without creating a circular dependency.
69 }
70 out(z)
71 {
72     assert((a*z)%p == 1);
73 }
74 body
75 {
76     Int u = a;
77     Int v = p;
78     Int x1 = Int.ONE;
79     Int x2 = Int.ZERO;
80     while (u != 1u && v != 1u)
81     {
82         modInvInternal(u, x1, p);
83         modInvInternal(v, x2, p);
84         if (u >= v)
85         {
86             u = u - v;
87             x1 = x1 - x2;
88         }
89         else
90         {
91             v = v - u;
92             x2 = x2 - x1;
93         }
94     }
95     if (u == 1u)
96     {
97         return (x1 % p);
98     }
99     else
100     {
101         return (x2 % p);
102     }
103 }
104
105 private
106 {
107     void modInvInternal(inout Int u, inout Int x, in Int p)
108     {
109         while ((u & 1u) == 0)
110         {
111             u = u >> 1;
112             if ((x & 1u) == 0)
113             {
114                 x = x >> 1;
115             }
116             else
117             {
118                 x = (x + p) >> 1;
119             }
120         }
121     }
122 }
Note: See TracBrowser for help on using the browser.