www.pudn.com > rsa.zip > GInt.pas


{License, info, etc 
 ------------------ 
 
This implementation is made by Walied Othman, to contact me 
mail to Walied.Othman@Student.KULeuven.ac.be or 
Triade@ace.Ulyssis.Student.KULeuven.ac.be, or ICQ me on 20388046. 
If you 're going to use these implementations, at least mention my 
name or something and notify me so I may even put a link on my page. 
This implementation is freeware and according to the coderpunks' 
manifesto it should remain so, so don 't use these implementations 
in commercial applications.  Encryption, as a tool to ensure privacy 
should be free and accessible for anyone. 
If any algorithm is patented in your country, you should acquire a 
license before using this software.  Modified versions of this 
software must remain in the public domain and must contain an 
acknowledgement of the original author (=me). 
This implementaion is available at 
http://ace.ulyssis.student.kuleuven.ac.be/~triade/GInt/index.htm 
 
 
copyright 1999, Walied Othman 
This header may not be removed. 
 
Updates: 
-------- 
 9/1/99: Division algorithm speeded up, 3 times faster now 
22/1/99: Procedure Added to compute the Legendre symbol 
26/2/99: The Procedure GIntToBinStr optimized 
28/2/99: signtype changed 
19/3/99: TrialDiv range expanded 
23/3/99: BinStrToGInt optimized, 4 times faster now 
         GIntMulByInt and GIntMulByIntBis procedures added 
} 
 
Unit GInt; 
 
Interface 
 
Uses Windows, SysUtils, Controls; 
 
Type 
   TCompare = (Lt, St, Eq, Er); 
   Tsign = (negative, positive); 
   TGInt = ^cont; 
   cont = Record 
      sign : Tsign; 
      value : longint; 
      next, prev : TGInt; 
   End; 
 
Procedure zeronetochar8(Var g : char; x : String); 
Procedure zeronetochar6(Var g : integer; x : String); 
Procedure initialize8(Var trans : Array Of String); 
Procedure initialize6(Var trans : Array Of String); 
Procedure Convert8to6bit(str8 : String; Var str6 : String); 
Procedure Convert6to8bit(str6 : String; Var str8 : String); 
Procedure Convert8to1bit(str8 : String; Var str1 : String); 
Procedure Convert6to1bit(str6 : String; Var str1 : String); 
Procedure Convert1to8bit(str1 : String; Var str8 : String); 
Procedure Convert1to6bit(str1 : String; Var str6 : String); 
Procedure decstrtogint(GIntstr : String; Var GInt : TGInt); 
Procedure ginttodecstr(Var GIntstr : String; GInt : TGInt); 
Procedure InttoGInt(Int : integer; Var GInt : TGInt); 
Procedure gintdestroy(Var GInt : TGInt); 
Procedure GIntcopy(GInt1 : TGInt; Var GInt2 : TGInt); 
Procedure GIntdivbyint(GInt : TGInt; Var res : TGInt; by : longint; Var m : longint); 
Procedure GIntmodbyint(GInt : TGInt; by : longint; Var m : longint); 
Function GIntCompareAbs(GInt1, GInt2 : TGInt) : TCompare; 
Procedure GIntchangesign(Var GInt : TGInt); 
Procedure GIntabs(Var GInt : TGInt); 
Procedure GIntadd(GInt1, GInt2 : TGInt; Var sum : TGInt); 
Procedure GIntsub(GInt1, GInt2 : TGInt; Var dif : TGInt); 
Procedure GIntmul(GInt1, GInt2 : TGInt; Var prod : TGInt); 
Procedure GIntMulByInt(GInt1 : TGInt; By : Longint; Var prod : TGInt); 
Procedure GIntMulByIntBis(Var GInt : TGInt; By : Longint); 
Procedure GIntSquare(GInt : TGInt; Var Square : TGInt); 
Procedure GInttobinstr(GInt : TGint; Var S : String); 
Procedure Binstrtogint(S : String; Var GInt : TGInt); 
Procedure GInttostr(GInt : TGInt; Var str : String); 
Procedure strtoGInt(str : String; Var GInt : TGInt); 
Procedure GIntExp(GInt, exp : TGInt; Var res : TGInt); 
Procedure GIntfac(GInt : TGInt; Var res : TGint); 
Procedure GIntdivmod(GInt1, GInt2 : TGInt; Var divres, modres : TGInt); 
Procedure GIntdiv(GInt1, GInt2 : TGInt; Var divres : TGInt); 
Procedure GIntmod(GInt1, GInt2 : TGInt; Var modres : TGInt); 
Procedure GIntSquareMod(GInt, Modb : TGInt; Var GIntSM : TGInt); 
Procedure GIntAddMod(GInt1, GInt2, base : TGInt; Var GIntres : TGInt); 
Procedure GIntMulMod(GInt1, GInt2, base : TGInt; Var GIntres : TGInt); 
Procedure GIntmodExp(GInt, exp, modb : TGInt; Var res : TGInt); 
Procedure GIntGCD(GInt1, GInt2 : TGint; Var GCD : TGInt); 
Procedure GIntLCM(GInt1, GInt2 : TGInt; Var LCM : TGInt); 
Procedure GIntTrialdiv9999(GInt : TGInt; Var ok : boolean); 
Procedure GIntRandom1(Seed : TGInt; Var RandomGInt : TGInt); 
Procedure GIntRabinMiller(GIntp : TGInt; nrtest : integer; Var ok : boolean); 
Procedure GIntBezoutBachet(GInt1, GInt2 : TGInt; Var a, b : TGInt); 
Procedure GIntModInv(GInt1, base : TGInt; Var Inverse : TGInt); 
Procedure GIntPrimetest(GIntp : TGInt; nrRMtests : integer; Var ok : boolean); 
Procedure GIntLegendreSymbol(a, p : TGInt; Var L : integer); 
 
 
Implementation 
 
Var 
   primes : Array[1..1227] Of integer = 
      (3, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 
      131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 
      257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 
      397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 
      547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 
      683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 
      853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 
      1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 
      1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 
      1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 
      1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 
      1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 
      1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 
      1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 
      1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 
      2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 
      2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 
      2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 
      2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 
      2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 
      2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 
      3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 
      3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 
      3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 
      3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 
      3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 
      3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 
      3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 
      4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 
      4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 
      4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 
      4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 
      4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 
      4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 
      5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 
      5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 
      5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 
      5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 
      5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 
      5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 
      6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 
      6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 
      6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 
      6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 
      6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 
      6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 
      7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 
      7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 
      7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 
      7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 
      7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 
      7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 
      8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 
      8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 
      8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 
      8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 
      8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 
      9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 
      9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 
      9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 
      9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 
      9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 
      9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973); 
   chr64 : Array[1..64] Of char = ('a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 
      'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 
      'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 
      'z', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '='); 
 
{$H+} 
 
 
Procedure zeronetochar8(Var g : char; x : String); 
Begin 
   If x[1] = '0' Then 
   Begin 
      If x[2] = '0' Then 
      Begin 
         If x[3] = '0' Then 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(1) Else g := chr(2); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(4) Else g := chr(3); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(5) Else g := chr(6); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(9) Else g := chr(8); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(7) Else g := chr(10); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(11) Else g := chr(13); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(14) Else g := chr(12); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(15) Else g := chr(16); 
                     End 
                  End 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(18) Else g := chr(20); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(17) Else g := chr(21); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(19) Else g := chr(25); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(24) Else g := chr(22); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(23) Else g := chr(26); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(28) Else g := chr(31); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(27) Else g := chr(29); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(30) Else g := chr(32); 
                     End 
                  End 
               End 
            End 
         End 
         Else 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(0) Else g := chr(33); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(34) Else g := chr(36); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(35) Else g := chr(37); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(38) Else g := chr(40); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(39) Else g := chr(41); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(42) Else g := chr(43); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(44) Else g := chr(45); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(46) Else g := chr(47); 
                     End 
                  End 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(48) Else g := chr(49); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(50) Else g := chr(51); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(52) Else g := chr(53); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(54) Else g := chr(55); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(56) Else g := chr(57); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(58) Else g := chr(59); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(60) Else g := chr(90); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(89) Else g := chr(88); 
                     End 
                  End 
               End 
            End 
         End 
      End 
      Else 
      Begin 
         If x[3] = '0' Then 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(87) Else g := chr(86); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(85) Else g := chr(84); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(83) Else g := chr(82); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(81) Else g := chr(80); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(79) Else g := chr(78); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(77) Else g := chr(76); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(75) Else g := chr(74); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(73) Else g := chr(72); 
                     End 
                  End 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(71) Else g := chr(70); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(69) Else g := chr(68); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(67) Else g := chr(66); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(65) Else g := chr(64); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(63) Else g := chr(62); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(61) Else g := chr(95); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(94) Else g := chr(93); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(91) Else g := chr(92); 
                     End 
                  End 
               End 
            End 
         End 
         Else 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(96) Else g := chr(97); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(98) Else g := chr(99); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(100) Else g := chr(101); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(102) Else g := chr(105); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(103) Else g := chr(104); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(106) Else g := chr(107); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(108) Else g := chr(109); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(110) Else g := chr(111); 
                     End 
                  End 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(112) Else g := chr(113); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(114) Else g := chr(115); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(116) Else g := chr(117); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(118) Else g := chr(119); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(120) Else g := chr(121); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(125) Else g := chr(124); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(123) Else g := chr(126); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(122) Else g := chr(127); 
                     End 
                  End 
               End 
            End 
         End 
      End 
   End 
   Else 
   Begin 
      If x[2] = '0' Then 
      Begin 
         If x[3] = '0' Then 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(128) Else g := chr(130); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(129) Else g := chr(131); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(132) Else g := chr(133); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(134) Else g := chr(135); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(136) Else g := chr(137); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(138) Else g := chr(139); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(140) Else g := chr(141); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(142) Else g := chr(143); 
                     End 
                  End 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(144) Else g := chr(145); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(150) Else g := chr(149); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(148) Else g := chr(147); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(146) Else g := chr(151); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(152) Else g := chr(154); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(153) Else g := chr(155); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(156) Else g := chr(157); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(158) Else g := chr(159); 
                     End 
                  End 
               End 
            End 
         End 
         Else 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(160) Else g := chr(161); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(162) Else g := chr(170); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(165) Else g := chr(166); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(167) Else g := chr(168); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(169) Else g := chr(163); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(164) Else g := chr(171); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(172) Else g := chr(173); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(174) Else g := chr(175); 
                     End 
                  End 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(176) Else g := chr(177); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(178) Else g := chr(179); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(180) Else g := chr(181); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(200) Else g := chr(199); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(198) Else g := chr(197); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(196) Else g := chr(195); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(194) Else g := chr(193); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(192) Else g := chr(191); 
                     End 
                  End 
               End 
            End 
         End 
      End 
      Else 
      Begin 
         If x[3] = '0' Then 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(190) Else g := chr(189); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(188) Else g := chr(182); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(183) Else g := chr(184); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(185) Else g := chr(186); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(187) Else g := chr(201); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(202) Else g := chr(203); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(204) Else g := chr(205); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(206) Else g := chr(207); 
                     End 
                  End 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(208) Else g := chr(209); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(210) Else g := chr(220); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(211) Else g := chr(219); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(212) Else g := chr(218); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(213) Else g := chr(217); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(214) Else g := chr(216); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(215) Else g := chr(221); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(222) Else g := chr(223); 
                     End 
                  End 
               End 
            End 
         End 
         Else 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(224) Else g := chr(225); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(226) Else g := chr(227); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(228) Else g := chr(229); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(230) Else g := chr(231); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(232) Else g := chr(233); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(234) Else g := chr(235); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(236) Else g := chr(237); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(238) Else g := chr(240); 
                     End 
                  End 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(239) Else g := chr(241); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(242) Else g := chr(243); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(244) Else g := chr(245); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(255) Else g := chr(254); 
                     End 
                  End 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(246) Else g := chr(253); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(247) Else g := chr(252); 
                     End 
                  End 
                  Else 
                  Begin 
                     If x[7] = '0' Then 
                     Begin 
                        If x[8] = '0' Then g := chr(248) Else g := chr(251); 
                     End 
                     Else 
                     Begin 
                        If x[8] = '0' Then g := chr(249) Else g := chr(250); 
                     End 
                  End 
               End 
            End 
         End 
      End 
   End 
End; 
 
 
 
Procedure zeronetochar6(Var g : integer; x : String); 
Begin 
   If x[1] = '0' Then 
   Begin 
      If x[2] = '0' Then 
      Begin 
         If x[3] = '0' Then 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 1 Else g := 2; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 3 Else g := 4; 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 5 Else g := 6; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 7 Else g := 8; 
               End 
            End 
         End 
         Else 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 9 Else g := 10; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 11 Else g := 12; 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 13 Else g := 14; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 15 Else g := 16; 
               End 
            End 
         End 
      End 
      Else 
      Begin 
         If x[3] = '0' Then 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 17 Else g := 18; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 19 Else g := 20; 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 21 Else g := 22; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 23 Else g := 24; 
               End 
            End 
         End 
         Else 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 25 Else g := 26; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 27 Else g := 28; 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 29 Else g := 30; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 31 Else g := 32; 
               End 
            End 
         End 
      End 
   End 
   Else 
   Begin 
      If x[2] = '0' Then 
      Begin 
         If x[3] = '0' Then 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 33 Else g := 34; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 35 Else g := 36; 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 37 Else g := 38; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 39 Else g := 40; 
               End 
            End 
         End 
         Else 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 41 Else g := 42; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 43 Else g := 44; 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 45 Else g := 46; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 47 Else g := 48; 
               End 
            End 
         End 
      End 
      Else 
      Begin 
         If x[3] = '0' Then 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 49 Else g := 50; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 51 Else g := 52; 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 53 Else g := 54; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 55 Else g := 56; 
               End 
            End 
         End 
         Else 
         Begin 
            If x[4] = '0' Then 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 57 Else g := 58; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 59 Else g := 60; 
               End 
            End 
            Else 
            Begin 
               If x[5] = '0' Then 
               Begin 
                  If x[6] = '0' Then g := 61 Else g := 62; 
               End 
               Else 
               Begin 
                  If x[6] = '0' Then g := 63 Else g := 64; 
               End 
            End 
         End 
      End 
   End 
End; 
 
 
 
Procedure initialize8(Var trans : Array Of String); 
Var 
   c1, c2, c3, c4, c5, c6, c7, c8 : integer; 
   x : String; 
   g : char; 
Begin 
   For c1 := 0 To 1 Do 
      For c2 := 0 To 1 Do 
         For c3 := 0 To 1 Do 
            For c4 := 0 To 1 Do 
               For c5 := 0 To 1 Do 
                  For c6 := 0 To 1 Do 
                     For c7 := 0 To 1 Do 
                        For c8 := 0 To 1 Do 
                        Begin 
                           x := ''; 
                           x := inttostr(c1) + inttostr(c2) + inttostr(c3) + inttostr(c4) + inttostr(c5) + inttostr(c6) + inttostr(c7) + inttostr(c8); 
                           zeronetochar8(g, x); 
                           trans[ord(g)] := x; 
                        End; 
End; 
 
 
 
Procedure initialize6(Var trans : Array Of String); 
Var 
   c1, c2, c3, c4, c5, c6 : integer; 
   x : String; 
   g : integer; 
Begin 
   For c1 := 0 To 1 Do 
      For c2 := 0 To 1 Do 
         For c3 := 0 To 1 Do 
            For c4 := 0 To 1 Do 
               For c5 := 0 To 1 Do 
                  For c6 := 0 To 1 Do 
                  Begin 
                     x := ''; 
                     x := inttostr(c1) + inttostr(c2) + inttostr(c3) + inttostr(c4) + inttostr(c5) + inttostr(c6); 
                     zeronetochar6(g, x); 
                     trans[ord(chr64[g])] := x; 
                  End; 
End; 
 
 
// Convert 8 bit strings to 6 bit strings and visa versa 
 
Procedure Convert8to6bit(str8 : String; Var str6 : String); 
Var 
   temp : String; 
   trans : Array[0..255] Of String; 
   i, len6 : longint; 
   g : integer; 
Begin 
   initialize8(trans); 
   temp := ''; 
   For i := 1 To length(str8) Do temp := temp + trans[ord(str8[i])]; 
   While (length(temp) Mod 6) <> 0 Do temp := temp + '0'; 
   len6 := length(temp) Div 6; 
   str6 := ''; 
   For i := 1 To len6 Do 
   Begin 
      zeronetochar6(g, copy(temp, 1, 6)); 
      str6 := str6 + chr64[g]; 
      delete(temp, 1, 6); 
   End; 
End; 
 
 
Procedure Convert6to8bit(str6 : String; Var str8 : String); 
Var 
   temp : String; 
   trans : Array[0..255] Of String; 
   i, len8 : longint; 
   g : char; 
Begin 
   initialize6(trans); 
   temp := ''; 
   For i := 1 To length(str6) Do temp := temp + trans[ord(str6[i])]; 
   str8 := ''; 
   len8 := length(temp) Div 8; 
   For i := 1 To len8 Do 
   Begin 
      zeronetochar8(g, copy(temp, 1, 8)); 
      str8 := str8 + g; 
      delete(temp, 1, 8); 
   End; 
End; 
 
 
// Convert 8 & 6 bit strings to 1 bit strings and visa versa 
 
Procedure Convert8to1bit(str8 : String; Var str1 : String); 
Var 
   trans : Array[0..255] Of String; 
   i : longint; 
Begin 
   str1 := ''; 
   initialize8(trans); 
   For i := 1 To length(str8) Do str1 := str1 + trans[ord(str8[i])]; 
End; 
 
 
Procedure Convert6to1bit(str6 : String; Var str1 : String); 
Var 
   trans : Array[0..255] Of String; 
   i : longint; 
Begin 
   str1 := ''; 
   initialize6(trans); 
   For i := 1 To length(str6) Do str1 := str1 + trans[ord(str6[i])]; 
End; 
 
 
Procedure Convert1to8bit(str1 : String; Var str8 : String); 
Var 
   i, len8 : longint; 
   g : char; 
Begin 
   str8 := ''; 
   While (length(str1) Mod 8) <> 0 Do str1 := '0' + str1; 
   len8 := length(str1) Div 8; 
   For i := 1 To len8 Do 
   Begin 
      zeronetochar8(g, copy(str1, 1, 8)); 
      str8 := str8 + g; 
      delete(str1, 1, 8); 
   End; 
End; 
 
 
Procedure Convert1to6bit(str1 : String; Var str6 : String); 
Var 
   i, len6 : longint; 
   g : integer; 
Begin 
   str6 := ''; 
   While (length(str1) Mod 6) <> 0 Do str1 := '0' + str1; 
   len6 := length(str1) Div 6; 
   For i := 1 To len6 Do 
   Begin 
      zeronetochar6(g, copy(str1, 1, 6)); 
      str6 := str6 + chr64[g]; 
      delete(str1, 1, 6); 
   End; 
End; 
 
 
// convert a base 10 string to a GInt and visa versa 
 
Procedure DecStrToGInt(GIntstr : String; Var GInt : TGInt); 
Var 
   temp1, temp2 : TGInt; 
   p : Tsign; 
Begin 
 
   While Not (GIntstr[1] In ['-', '0'..'9']) Do delete(GIntstr, 1, 1); 
 
   If GIntstr[1] = '-' Then 
   Begin 
      delete(GIntstr, 1, 1); 
      p := negative; 
   End 
   Else p := positive; 
 
   While (GIntstr[1] = '0') And (length(GIntstr) > 1) Do delete(GIntstr, 1, 1); 
 
   new(temp2); 
   temp2^.next := Nil; 
   If (length(GIntstr) Mod 4) = 0 Then 
   Begin 
      temp2^.value := strtoint(copy(GIntstr, 1, 4)); 
      delete(GIntstr, 1, 4); 
   End 
   Else 
   Begin 
      temp2^.value := strtoint(copy(GIntstr, 1, (length(GIntstr) Mod 4))); 
      delete(GIntstr, 1, (length(GIntstr) Mod 4)); 
   End; 
 
   While length(GIntstr) > 0 Do 
   Begin 
      new(temp1); 
      temp1^.next := temp2; 
      temp2^.prev := temp1; 
      temp1^.value := strtoint(copy(GIntstr, 1, 4)); 
      delete(GIntstr, 1, 4); 
      temp2 := temp1; 
   End; 
 
   temp2^.prev := Nil; 
   temp2^.sign := p; 
   GInt := temp2; 
 
End; 
 
 
Procedure GIntToDecStr(Var GIntstr : String; GInt : TGInt); 
Var 
   s : String; 
   p : TSign; 
Begin 
   GIntstr := ''; 
   p := GInt^.sign; 
 
   s := inttostr(GInt^.value); 
   While length(s) < 4 Do s := '0' + s; 
   GIntstr := s + GIntstr; 
   While GInt^.next <> Nil Do 
   Begin 
      GInt := GInt^.next; 
      s := inttostr(abs(GInt^.value)); 
      While length(s) < 4 Do s := '0' + s; 
      GIntstr := s + GIntstr; 
   End; 
 
   While (GIntstr[1] = '0') And (length(GIntstr) > 1) Do delete(GIntstr, 1, 1); 
 
   If p = negative Then GIntstr := '-' + GIntstr; 
 
End; 
 
 
// Convert an integer to a GInt 
 
Procedure IntToGInt(Int : integer; Var GInt : TGInt); 
Begin 
   DecstrtoGInt(inttostr(Int), GInt); 
End; 
 
 
// Destroy a GInt, in order to free memory 
 
Procedure GIntDestroy(Var GInt : TGInt); 
Begin 
 
   While GInt^.next <> Nil Do GInt := GInt^.next; 
 
   While GInt^.prev <> Nil Do 
   Begin 
      GInt := GInt^.prev; 
      dispose(GInt^.next); 
   End; 
   dispose(GInt); 
End; 
 
 
// Make a copy of a GInt 
 
Procedure GIntCopy(GInt1 : TGInt; Var GInt2 : TGInt); 
Var 
   temp1, temp2 : TGInt; 
Begin 
   new(GInt2); 
   GInt2^.sign := GInt1^.sign; 
   GInt2^.prev := Nil; 
   GInt2^.value := GInt1^.value; 
   temp1 := GInt2; 
   While GInt1^.next <> Nil Do 
   Begin 
      GInt1 := GInt1^.next; 
      new(temp2); 
      temp2^.value := GInt1^.value; 
      temp2^.prev := temp1; 
      temp1^.next := temp2; 
      temp1 := temp2; 
   End; 
   temp1^.next := Nil; 
End; 
 
 
// Divide a GInt by an integer, GInt = res * by + m 
 
Procedure GIntDivByInt(GInt : TGInt; Var res : TGInt; by : longint; Var m : longint); 
Var 
   S, S1 : String; 
Begin 
   If (by Div 10000) = 0 Then 
   Begin 
      S := ''; 
      While GInt^.next <> Nil Do GInt := GInt^.next; 
      S := inttostr(GInt^.value Div by); 
      m := (GInt^.value Mod by); 
      While GInt^.prev <> Nil Do 
      Begin 
         m := m * 10000; 
         GInt := GInt^.prev; 
         S1 := inttostr((GInt^.value + m) Div by); 
         While length(S1) < 4 Do S1 := '0' + S1; 
         S := S + S1; 
         m := ((GInt^.value + m) Mod by); 
      End; 
      decstrtogint(S, res); 
   End; 
End; 
 
 
// GInt modulo an integer, GInt mod by = m 
 
Procedure GIntModByInt(GInt : TGInt; by : longint; Var m : longint); 
Begin 
   If (by Div 10000) = 0 Then 
   Begin 
      While GInt^.next <> Nil Do GInt := GInt^.next; 
      m := (GInt^.value Mod by); 
      While GInt^.prev <> Nil Do 
      Begin 
         m := m * 10000; 
         GInt := GInt^.prev; 
         m := ((GInt^.value + m) Mod by); 
      End; 
   End; 
End; 
 
 
// Compare two GInts in absolute value, GInt1 < : St, > : Lt, = : Eq, Error : Er GInt2 
 
Function GIntCompareAbs(GInt1, GInt2 : TGInt) : TCompare; 
Begin 
 
   GIntCompareAbs := Er; 
 
   While (GInt1^.next <> Nil) And (GInt2^.next <> Nil) Do 
   Begin 
      GInt1 := GInt1^.next; 
      GInt2 := GInt2^.next; 
   End; 
 
   If (GInt1^.next = Nil) And (GInt2^.next <> Nil) Then GIntCompareAbs := St; 
 
   If (GInt2^.next = Nil) And (GInt1^.next <> Nil) Then GIntCompareAbs := Lt; 
 
   If (GInt1^.next = Nil) And (GInt2^.next = Nil) Then 
   Begin 
      While (GInt1^.value = GInt2^.value) And (GInt1^.prev <> Nil) Do 
      Begin 
         GInt1 := GInt1^.prev; 
         GInt2 := GInt2^.prev; 
      End; 
 
      If (GInt1^.value > GInt2^.value) Then GIntCompareAbs := Lt 
      Else If (GInt1^.value < GInt2^.value) Then GIntCompareAbs := St Else GIntCompareAbs := Eq; 
   End 
 
End; 
 
 
// Change the sign of a GInt 
 
Procedure GIntChangeSign(Var GInt : TGInt); 
Begin 
   If GInt^.sign = negative Then GInt^.sign := positive Else GInt^.sign := negative; 
End; 
 
 
// Returns the GInt in its absolute value 
 
Procedure GIntAbs(Var GInt : TGInt); 
Begin 
   GInt^.sign := positive; 
End; 
 
 
// Add 2 GInts, GInt1 + GInt2 = sum 
 
Procedure GIntAdd(GInt1, GInt2 : TGInt; Var sum : TGInt); 
Var 
   temp1, temp2 : TGInt; 
   rest : integer; 
   Tres : Longint; 
Begin 
   If (GInt1^.sign = GInt2^.sign) Then 
   Begin 
 
      new(temp2); 
      temp2^.prev := Nil; 
      Tres := GInt1^.value + GInt2^.value; 
      temp2^.value := Tres Mod 10000; 
      temp2^.sign := GInt1^.sign; 
      If Tres >= 10000 Then rest := 1 Else rest := 0; 
 
      While (GInt1^.next <> Nil) And (GInt2^.next <> Nil) Do 
      Begin 
         GInt1 := GInt1^.next; 
         GInt2 := GInt2^.next; 
         new(temp1); 
         Tres := GInt1^.value + GInt2^.value + rest; 
         temp1^.value := Tres Mod 10000; 
         If Tres >= 10000 Then rest := 1 Else rest := 0; 
         temp1^.prev := temp2; 
         temp2^.next := temp1; 
         temp2 := temp1; 
      End; 
 
      While (GInt1^.next) <> Nil Do 
      Begin 
         GInt1 := GInt1^.next; 
         new(temp1); 
         Tres := GInt1^.value + rest; 
         temp1^.value := Tres Mod 10000; 
         If Tres >= 10000 Then rest := 1 Else rest := 0; 
         temp1^.prev := temp2; 
         temp2^.next := temp1; 
         temp2 := temp1; 
      End; 
 
      While (GInt2^.next) <> Nil Do 
      Begin 
         GInt2 := GInt2^.next; 
         new(temp1); 
         Tres := GInt2^.value + rest; 
         temp1^.value := Tres Mod 10000; 
         If Tres >= 10000 Then rest := 1 Else rest := 0; 
         temp1^.prev := temp2; 
         temp2^.next := temp1; 
         temp2 := temp1; 
      End; 
 
      If rest <> 0 Then 
      Begin 
         new(temp1); 
         temp1^.value := (rest) Mod 10000; 
         temp1^.prev := temp2; 
         temp2^.next := temp1; 
         temp2 := temp1; 
      End; 
 
      temp2^.next := Nil; 
      sum := temp2; 
      While sum^.prev <> Nil Do sum := sum^.prev; 
 
   End 
   Else 
   Begin 
      If (GIntCompareAbs(GInt1, GInt2) = Lt) Or (GIntCompareAbs(GInt1, GInt2) = Eq) Then 
      Begin 
 
         new(temp2); 
         temp2^.prev := Nil; 
         temp2^.sign := GInt1^.sign; 
         Tres := 10000 + GInt1^.value - GInt2^.value; 
         temp2^.value := Tres Mod 10000; 
         If (GInt1^.value - GInt2^.value) < 0 Then rest := -1 Else rest := 0; 
 
         While (GInt1^.next <> Nil) And (GInt2^.next <> Nil) Do 
         Begin 
            GInt1 := GInt1^.next; 
            GInt2 := GInt2^.next; 
            new(temp1); 
            Tres := GInt1^.value - GInt2^.value + rest; 
            temp1^.value := (10000 + Tres) Mod 10000; 
            If Tres < 0 Then rest := -1 Else rest := 0; 
            temp1^.prev := temp2; 
            temp2^.next := temp1; 
            temp2 := temp1; 
         End; 
 
         While (GInt1^.next) <> Nil Do 
         Begin 
            GInt1 := GInt1^.next; 
            new(temp1); 
            Tres := GInt1^.value + rest; 
            temp1^.value := (10000 + Tres) Mod 10000; 
            If Tres < 0 Then rest := -1 Else rest := 0; 
            temp1^.prev := temp2; 
            temp2^.next := temp1; 
            temp2 := temp1; 
         End; 
 
 
         If rest <> 0 Then 
         Begin 
            new(temp1); 
            temp1^.value := (10000 + rest) Mod 10000; 
            temp1^.prev := temp2; 
            temp2^.next := temp1; 
            temp2 := temp1; 
         End; 
 
         While (temp2^.value = 0) And (temp2^.prev <> Nil) Do 
         Begin 
            temp2 := temp2^.prev; 
            dispose(temp2^.next); 
            temp2^.next := Nil; 
         End; 
         temp2^.next := Nil; 
         sum := temp2; 
         While sum^.prev <> Nil Do sum := sum^.prev; 
 
      End 
 
      Else 
         GIntadd(GInt2, GInt1, sum); 
 
   End 
 
End; 
 
 
// Subtract 2 GInts, GInt1 - GInt2 = dif 
 
Procedure GIntSub(GInt1, GInt2 : TGInt; Var dif : TGInt); 
Begin 
   GIntchangesign(GInt2); 
   GIntadd(GInt1, GInt2, dif); 
   GIntchangesign(GInt2); 
End; 
 
 
// Multiply 2 GInts, GInt1 * GInt2 = prod 
 
Procedure GIntMul(GInt1, GInt2 : TGInt; Var prod : TGInt); 
Var 
   zero, temp1, temp2, temp : TGInt; 
   sign : Tsign; 
   rest, Trest : longint; 
Begin 
   decstrtogint('0', zero); 
   If Not ((GIntcompareabs(zero, GInt1) = Eq) Or (GIntcompareabs(zero, GInt2) = Eq)) Then 
   Begin 
      If GInt1^.sign = GInt2^.sign Then sign := positive Else sign := negative; 
      temp1 := GInt1; 
 
      new(temp2); 
      temp2^.sign := sign; 
      temp2^.prev := Nil; 
      temp2^.value := (GInt2^.value * temp1^.value) Mod 10000; 
      rest := (GInt2^.value * temp1^.value) Div 10000; 
      temp2^.next := Nil; 
      prod := temp2; 
      While temp1^.next <> Nil Do 
      Begin 
         temp1 := temp1^.next; 
         new(temp); 
         temp^.value := (GInt2^.value * temp1^.value + rest) Mod 10000; 
         rest := (GInt2^.value * temp1^.value + rest) Div 10000; 
         temp^.next := Nil; 
         temp2^.next := temp; 
         temp^.prev := temp2; 
         temp2 := temp2^.next; 
      End; 
      If rest <> 0 Then 
      Begin 
         new(temp); 
         temp^.value := rest; 
         temp^.next := Nil; 
         temp^.prev := temp2; 
         temp2^.next := temp; 
      End; 
 
      While GInt2^.next <> Nil Do 
      Begin 
         If prod^.next = Nil Then 
         Begin 
            new(temp2); 
            temp2^.value := 0; 
            temp2^.prev := prod; 
            prod^.next := temp2; 
            temp2^.next := Nil; 
         End; 
         prod := prod^.next; 
         GInt2 := GInt2^.next; 
         temp1 := GInt1; 
         temp2 := prod; 
         rest := (GInt2^.value * temp1^.value + temp2^.value) Div 10000; 
         temp2^.value := (GInt2^.value * temp1^.value + temp2^.value) Mod 10000; 
 
         While temp1^.next <> Nil Do 
         Begin 
            temp1 := temp1^.next; 
            If temp2^.next = Nil Then 
            Begin 
               new(temp); 
               temp^.value := 0; 
               temp^.next := Nil; 
            End 
            Else temp := temp2^.next; 
            trest := (GInt2^.value * temp1^.value + rest + temp^.value) Div 10000; 
            temp^.value := (GInt2^.value * temp1^.value + rest + temp^.value) Mod 10000; 
            rest := trest; 
            temp2^.next := temp; 
            temp^.prev := temp2; 
            temp2 := temp2^.next; 
         End; 
         If rest <> 0 Then 
         Begin 
            If temp2^.next = Nil Then 
            Begin 
               new(temp); 
               temp^.value := 0; 
               temp^.next := Nil; 
            End 
            Else temp := temp2^.next; 
            temp^.value := temp^.value + rest; 
            temp^.next := Nil; 
            temp^.prev := temp2; 
            temp2^.next := temp; 
         End; 
 
      End; 
 
      While prod^.prev <> Nil Do prod := prod^.prev; 
   End 
   Else decstrtogint('0', prod); 
   GIntdestroy(zero); 
 
End; 
 
 
// Prod = GInt1 * By, By < 10000 
 
Procedure GIntMulByInt(GInt1 : TGInt; By : Longint; Var prod : TGInt); 
Var 
   temp2, temp : TGInt; 
   sign : Tsign; 
   rest : longint; 
Begin 
   If By < 0 Then sign := negative Else sign := positive; 
   If GInt1^.sign = sign Then sign := positive Else sign := negative; 
   by := abs(by); 
 
   new(temp2); 
   temp2^.sign := sign; 
   temp2^.prev := Nil; 
   temp2^.value := (GInt1^.value * by) Mod 10000; 
   rest := (GInt1^.value * by) Div 10000; 
   temp2^.next := Nil; 
   prod := temp2; 
 
   While GInt1^.next <> Nil Do 
   Begin 
      GInt1 := GInt1^.next; 
      new(temp); 
      temp^.value := (GInt1^.value * By + rest) Mod 10000; 
      rest := (GInt1^.value * By + rest) Div 10000; 
      temp^.next := Nil; 
      temp2^.next := temp; 
      temp^.prev := temp2; 
      temp2 := temp2^.next; 
   End; 
 
   If rest <> 0 Then 
   Begin 
      new(temp); 
      temp^.value := rest; 
      temp^.next := Nil; 
      temp^.prev := temp2; 
      temp2^.next := temp; 
   End; 
 
 
   While prod^.prev <> Nil Do prod := prod^.prev; 
 
End; 
 
 
// GInt = GInt * By, By < 10000 
 
Procedure GIntMulByIntBis(Var GInt : TGInt; By : Longint); 
Var 
   temp1, temp : TGInt; 
   sign : Tsign; 
   rest, TRest : longint; 
Begin 
   If By < 0 Then sign := negative Else sign := positive; 
   If GInt^.sign = sign Then sign := positive Else sign := negative; 
   by := abs(by); 
 
   GInt^.sign := sign; 
   Trest := GInt^.value * By; 
   GInt^.value := Trest Mod 10000; 
   rest := Trest Div 10000; 
   temp1 := GInt; 
 
   While GInt^.next <> Nil Do 
   Begin 
      GInt := GInt^.next; 
      Trest := GInt^.value * By + rest; 
      GInt^.value := Trest Mod 10000; 
      rest := Trest Div 10000; 
   End; 
 
   If rest <> 0 Then 
   Begin 
      new(temp); 
      temp^.value := rest; 
      temp^.next := Nil; 
      temp^.prev := GInt; 
      GInt^.next := temp; 
   End; 
   GInt := temp1; 
End; 
 
 
// Square a GInt, GInt^2 = Square 
 
Procedure GIntSquare(GInt : TGInt; Var Square : TGInt); 
Begin 
   GIntMul(GInt, GInt, square); 
End; 
 
 
// Convert a GInt to a binary string (base 2) & visa versa 
 
Procedure GIntToBinStr(GInt : TGint; Var S : String); 
Var 
   zero, temp, temp1 : TGInt; 
   i : integer; 
Begin 
   DecStrToGInt('0', zero); 
   GIntCopy(GInt, temp); 
   S := ''; 
   While GIntCompareAbs(zero, temp) <> Eq Do 
   Begin 
      GIntDivByInt(temp, temp1, 2, i); 
      S := inttostr(i) + S; 
      GIntDestroy(temp); 
      temp := temp1; 
   End; 
   If S = '' Then S := '0'; 
   GIntDestroy(temp); 
   GIntDestroy(zero); 
End; 
 
 
Procedure BinStrToGInt(S : String; Var GInt : TGInt); 
Var 
   temp, temp2 : TGInt; 
   i : longint; 
Begin 
   While copy(S, 1, 1) = '0' Do delete(S, 1, 1); 
   decstrtogint('0', GInt); 
   decstrtogint('1', temp); 
   For i := length(S) Downto 1 Do 
   Begin 
      If S[i] = '1' Then 
      Begin 
         GIntadd(GInt, temp, temp2); 
         GIntdestroy(GInt); 
         GInt := temp2; 
      End; 
      GIntmulByIntBis(temp, 2); 
   End; 
   GIntdestroy(temp); 
End; 
 
 
// Convert a GInt to an 8 bit string & visa versa 
 
Procedure GIntToStr(GInt : TGInt; Var str : String); 
Var 
   temp1 : String; 
   i, len8 : longint; 
   g : char; 
Begin 
   GInttobinstr(GInt, temp1); 
   While (length(temp1) Mod 8) <> 0 Do temp1 := '0' + temp1; 
   len8 := length(temp1) Div 8; 
   str := ''; 
   For i := 1 To len8 Do 
   Begin 
      zeronetochar8(g, copy(temp1, 1, 8)); 
      str := str + g; 
      delete(temp1, 1, 8); 
   End; 
End; 
 
 
Procedure StrToGInt(str : String; Var GInt : TGInt); 
Var 
   temp1 : String; 
   i : longint; 
   trans : Array[0..255] Of String; 
Begin 
   temp1 := ''; 
   initialize8(trans); 
   For i := 1 To length(str) Do temp1 := temp1 + trans[ord(str[i])]; 
   While temp1[1] = '0' Do delete(temp1, 1, 1); 
   binstrtoGInt(temp1, GInt); 
End; 
 
 
// Exponentiate a GInt, GInt^exp = res 
 
Procedure GIntExp(GInt, exp : TGInt; Var res : TGInt); 
Var 
   temp2, temp3 : TGInt; 
   S : String; 
   i : longint; 
Begin 
   GInttobinstr(exp, S); 
   If S[length(S)] = '0' Then decstrtogint('1', res) Else GIntcopy(GInt, res); 
   GIntcopy(GInt, temp2); 
   If length(S) > 1 Then 
      For i := (length(S) - 1) Downto 1 Do 
      Begin 
         GIntSquare(temp2, temp3); 
         GIntdestroy(temp2); 
         temp2 := temp3; 
         If S[i] = '1' Then 
         Begin 
            GIntmul(res, temp2, temp3); 
            GIntdestroy(res); 
            res := temp3; 
         End; 
      End; 
End; 
 
 
// Compute GInt! = GInt * (GInt - 1) * (GInt - 2) * ... * 3 * 2 * 1 
 
Procedure GIntFac(GInt : TGInt; Var res : TGint); 
Var 
   one, temp, temp1 : TGInt; 
Begin 
   GIntcopy(GInt, temp); 
   decstrtogint('1', res); 
   decstrtogint('1', one); 
 
   While Not ((temp^.next = Nil) And (temp^.value = 1)) Do 
   Begin 
      GIntmul(temp, res, temp1); 
      GIntdestroy(res); 
      res := temp1; 
      GIntsub(temp, one, temp1); 
      GIntdestroy(temp); 
      temp := temp1; 
   End; 
 
   GIntdestroy(one); 
   GIntdestroy(temp); 
 
End; 
 
 
// Divide 2 GInts, GInt1 = GInt2 * divres + modres, modres is always positive 
 
Procedure GIntDivMod(GInt1, GInt2 : TGInt; Var divres, modres : TGInt); 
Var 
   s1, s2 : TSign; 
   Tempstr1, tempstr2, tempstr, QStr : String; 
   lend, k, i : longint; 
   temp1, temp2, one, zero, temp : TGInt; 
   QCnt : integer; 
Begin 
   s1 := GInt1^.sign; 
   s2 := GInt2^.sign; 
   GIntabs(GInt1); 
   GIntabs(GInt2); 
   GInttodecstr(tempstr1, GInt1); 
   GInttodecstr(tempstr2, GInt2); 
   lend := length(tempstr2); 
   QStr := '0'; 
   decstrtogint('0', zero); 
 
 
   If Not ((GInt1^.value = 0) And (GInt1^.next = Nil)) Then 
   Begin 
 
      GIntcopy(GInt1, temp); 
      tempstr := tempstr1; 
      k := lend; 
      While (GIntcompareabs(temp, GInt2) <> St) Do 
      Begin 
         GIntDestroy(temp); 
         tempstr := copy(tempstr1, 1, k); 
         delete(tempstr1, 1, k); 
 
         While (length(tempstr) < lend) And (length(tempstr1) > 0) Do 
         Begin 
            tempstr := tempstr + copy(tempstr1, 1, 1); 
            delete(tempstr1, 1, 1); 
            QStr := QStr + '0'; 
         End; 
 
         decstrtoGInt(tempstr, temp1); 
         QCnt := 0; 
         While GIntcompareabs(temp1, GInt2) <> St Do 
         Begin 
            GIntSub(temp1, GInt2, temp2); 
            GIntdestroy(temp1); 
            temp1 := temp2; 
            QCnt := QCnt + 1; 
         End; 
         QStr := QStr + inttostr(QCnt); 
         GInttodecstr(tempstr, temp1); 
 
         k := length(tempstr) + 1; 
         tempstr1 := tempstr + tempstr1; 
         GIntdestroy(temp1); 
 
         DecStrToGInt(tempstr1, temp); 
      End; 
      If (GIntcompareabs(temp, GInt2) = St) And ((k - 1) <> length(tempstr1)) Then 
         For i := 1 To (length(tempstr1) - k + 1) Do QStr := QStr + '0'; 
      GIntDestroy(temp); 
 
      DecstrtoGInt(tempstr1, modres); 
      DecstrtoGInt(QStr, divres); 
 
      decstrtogint('1', one); 
      If s1 = negative Then 
      Begin 
         If GIntcompareabs(modres, zero) <> Eq Then 
         Begin 
            GIntadd(divres, one, temp1); 
            GIntdestroy(divres); 
            divres := temp1; 
            GIntAbs(GInt2); 
            GIntsub(GInt2, modres, temp1); 
            GIntdestroy(modres); 
            GInt2^.sign := s2; 
            modres := temp1; 
         End; 
         If s2 = positive Then divres^.sign := negative; 
      End 
      Else divres^.sign := s2; 
 
      GIntdestroy(one); 
   End 
   Else 
   Begin 
      GIntcopy(zero, divres); 
      GIntcopy(zero, modres); 
   End; 
   GIntdestroy(zero); 
 
   GInt1^.sign := s1; 
   GInt2^.sign := s2; 
End; 
 
 
 
// Same as above but doesn't compute modres 
 
Procedure GIntDiv(GInt1, GInt2 : TGInt; Var divres : TGInt); 
Var 
   s1, s2 : TSign; 
   Tempstr1, tempstr2, tempstr, QStr : String; 
   lend, k, i : longint; 
   temp1, temp2, one, zero, temp, modres : TGInt; 
   QCnt : integer; 
Begin 
   s1 := GInt1^.sign; 
   s2 := GInt2^.sign; 
   GIntabs(GInt1); 
   GIntabs(GInt2); 
   GInttodecstr(tempstr1, GInt1); 
   GInttodecstr(tempstr2, GInt2); 
   lend := length(tempstr2); 
   QStr := '0'; 
   decstrtogint('0', zero); 
 
 
   If Not ((GInt1^.value = 0) And (GInt1^.next = Nil)) Then 
   Begin 
 
      GIntcopy(GInt1, temp); 
      tempstr := tempstr1; 
      k := lend; 
      While (GIntcompareabs(temp, GInt2) <> St) Do 
      Begin 
         GIntDestroy(temp); 
         tempstr := copy(tempstr1, 1, k); 
         delete(tempstr1, 1, k); 
 
         While (length(tempstr) < lend) And (length(tempstr1) > 0) Do 
         Begin 
            tempstr := tempstr + copy(tempstr1, 1, 1); 
            delete(tempstr1, 1, 1); 
            QStr := QStr + '0'; 
         End; 
 
         decstrtoGInt(tempstr, temp1); 
         QCnt := 0; 
         While GIntcompareabs(temp1, GInt2) <> St Do 
         Begin 
            GIntSub(temp1, GInt2, temp2); 
            GIntdestroy(temp1); 
            temp1 := temp2; 
            QCnt := QCnt + 1; 
         End; 
         QStr := QStr + inttostr(QCnt); 
         GInttodecstr(tempstr, temp1); 
 
         k := length(tempstr) + 1; 
         tempstr1 := tempstr + tempstr1; 
         GIntdestroy(temp1); 
 
         DecStrToGInt(tempstr1, temp); 
      End; 
      If (GIntcompareabs(temp, GInt2) = St) And ((k - 1) <> length(tempstr1)) Then 
         For i := 1 To (length(tempstr1) - k + 1) Do QStr := QStr + '0'; 
      GIntDestroy(temp); 
 
      DecstrtoGInt(tempstr1, modres); 
      DecstrtoGInt(QStr, divres); 
 
      decstrtogint('1', one); 
      If s1 = negative Then 
      Begin 
         If GIntcompareabs(modres, zero) <> Eq Then 
         Begin 
            GIntadd(divres, one, temp1); 
            GIntdestroy(divres); 
            divres := temp1; 
            GIntAbs(GInt2); 
            GIntsub(GInt2, modres, temp1); 
            GIntdestroy(modres); 
            GInt2^.sign := s2; 
            modres := temp1; 
         End; 
         If s2 = positive Then divres^.sign := negative; 
      End 
      Else divres^.sign := s2; 
 
      GIntdestroy(one); 
   End 
   Else 
   Begin 
      GIntcopy(zero, divres); 
      GIntcopy(zero, modres); 
   End; 
   GIntdestroy(zero); 
 
   GInt1^.sign := s1; 
   GInt2^.sign := s2; 
   GIntDestroy(modres); 
End; 
 
 
// Same as above but computes modres and not divres 
 
Procedure GIntMod(GInt1, GInt2 : TGInt; Var modres : TGInt); 
Var 
   s1, s2 : TSign; 
   Tempstr1, tempstr2, tempstr, QStr : String; 
   lend, k : longint; 
   temp1, temp2, zero, temp : TGInt; 
Begin 
   s1 := GInt1^.sign; 
   s2 := GInt2^.sign; 
   GIntabs(GInt1); 
   GIntabs(GInt2); 
   GInttodecstr(tempstr1, GInt1); 
   GInttodecstr(tempstr2, GInt2); 
   lend := length(tempstr2); 
   QStr := '0'; 
   decstrtogint('0', zero); 
 
 
   If Not ((GInt1^.value = 0) And (GInt1^.next = Nil)) Then 
   Begin 
 
      GIntcopy(GInt1, temp); 
      tempstr := tempstr1; 
      k := lend; 
      While (GIntcompareabs(temp, GInt2) <> St) Do 
      Begin 
         GIntDestroy(temp); 
         tempstr := copy(tempstr1, 1, k); 
         delete(tempstr1, 1, k); 
 
         While (length(tempstr) < lend) And (length(tempstr1) > 0) Do 
         Begin 
            tempstr := tempstr + copy(tempstr1, 1, 1); 
            delete(tempstr1, 1, 1); 
         End; 
 
         decstrtoGInt(tempstr, temp1); 
         While GIntcompareabs(temp1, GInt2) <> St Do 
         Begin 
            GIntSub(temp1, GInt2, temp2); 
            GIntdestroy(temp1); 
            temp1 := temp2; 
         End; 
         GInttodecstr(tempstr, temp1); 
 
         k := length(tempstr) + 1; 
         tempstr1 := tempstr + tempstr1; 
         GIntdestroy(temp1); 
 
         DecStrToGInt(tempstr1, temp); 
      End; 
      GIntDestroy(temp); 
 
      DecstrtoGInt(tempstr1, modres); 
 
      If s1 = negative Then 
      Begin 
         If GIntcompareabs(modres, zero) <> Eq Then 
         Begin 
            GIntAbs(GInt2); 
            GIntsub(GInt2, modres, temp1); 
            GIntdestroy(modres); 
            GInt2^.sign := s2; 
            modres := temp1; 
         End; 
      End; 
 
   End 
   Else 
   Begin 
      GIntcopy(zero, modres); 
   End; 
   GIntdestroy(zero); 
 
   GInt1^.sign := s1; 
   GInt2^.sign := s2; 
End; 
 
 
 
// Square a GInt modulo Modb, GInt^2 mod Modb = GIntSM 
 
Procedure GIntSquareMod(GInt, Modb : TGInt; Var GIntSM : TGInt); 
Var 
   temp : TGInt; 
Begin 
   GIntSquare(GInt, temp); 
   GIntMod(temp, Modb, GIntSM); 
   GIntDestroy(temp); 
End; 
 
 
// Add 2 GInts modulo base, (GInt1 + GInt2) mod base = GIntres 
 
Procedure GIntAddMod(GInt1, GInt2, base : TGInt; Var GIntres : TGInt); 
Var 
   temp : TGInt; 
Begin 
   GIntadd(GInt1, GInt2, temp); 
   GIntMod(temp, base, GIntres); 
   GIntdestroy(temp); 
End; 
 
 
// Multiply 2 GInts modulo base, (GInt1 * GInt2) mod base = GIntres 
 
Procedure GIntMulMod(GInt1, GInt2, base : TGInt; Var GIntres : TGInt); 
Var 
   temp : TGInt; 
Begin 
   GIntMul(GInt1, GInt2, temp); 
   GIntMod(temp, base, GIntres); 
   GIntdestroy(temp); 
End; 
 
 
// Exponentiate 2 GInts modulo base, (GInt1 ^ GInt2) mod modb = res 
 
Procedure GIntModExp(GInt, exp, modb : TGInt; Var res : TGInt); 
Var 
   temp2, temp3 : TGInt; 
   S : String; 
   i : longint; 
Begin 
   GInttobinstr(exp, S); 
   If S[length(S)] = '0' Then decstrtogint('1', res) Else GIntcopy(GInt, res); 
   GIntcopy(GInt, temp2); 
   If length(S) > 1 Then 
      For i := (length(S) - 1) Downto 1 Do 
      Begin 
         GIntSquareMod(temp2, Modb, temp3); 
         GIntdestroy(temp2); 
         temp2 := temp3; 
         If S[i] = '1' Then 
         Begin 
            GIntmulMod(res, temp2, modb, temp3); 
            GIntdestroy(res); 
            res := temp3; 
         End; 
      End; 
End; 
 
 
// Compute the Greatest Common Divisor of 2 GInts 
 
Procedure GIntGCD(GInt1, GInt2 : TGint; Var GCD : TGInt); 
Var 
   k : TCompare; 
   zero, temp1, temp2, temp3 : TGInt; 
Begin 
   k := GIntcompareabs(GInt1, GInt2); 
   If (k = Eq) Then GIntCopy(GInt1, GCD) Else 
      If (k = St) Then GIntGCD(GInt2, GInt1, GCD) Else 
      Begin 
         decstrtogint('0', zero); 
         GIntCopy(GInt1, temp1); 
         GIntCopy(GInt2, temp2); 
         While GIntcompareabs(temp2, zero) <> Eq Do 
         Begin 
            GIntmod(temp1, temp2, temp3); 
            GIntdestroy(temp1); 
            temp1 := temp2; 
            temp2 := temp3; 
         End; 
         GCD := temp1; 
         GIntdestroy(temp2); 
         GIntdestroy(zero); 
      End; 
End; 
 
 
// Compute the Least Common Multiple of 2 GInts 
 
Procedure GIntLCM(GInt1, GInt2 : TGInt; Var LCM : TGInt); 
Var 
   temp1, temp2 : TGInt; 
Begin 
   GIntGCD(GInt1, GInt2, temp1); 
   GIntmul(GInt1, GInt2, temp2); 
   GIntdiv(temp2, temp1, LCM); 
   GIntDestroy(temp1); 
   GIntDestroy(temp2); 
End; 
 
 
// Trialdivision of a GInt upto 8192 and stopping when a divisor is found, returning ok=false 
 
Procedure GIntTrialDiv9999(GInt : TGInt; Var ok : boolean); 
Var 
   i, j : integer; 
Begin 
   If ((GInt^.value Mod 2) = 0) Or ((GInt^.value Mod 5) = 0) Then ok := false 
   Else 
   Begin 
      i := 0; 
      ok := true; 
      While ok And (i < 1227) Do 
      Begin 
         i := i + 1; 
         GIntmodbyint(GInt, primes[i], j); 
         If j = 0 Then ok := false; 
      End; 
   End; 
End; 
 
 
// A prng 
 
Procedure GIntRandom1(Seed : TGInt; Var RandomGInt : TGInt); 
Var 
   temp, base : TGInt; 
Begin 
   decstrtoGInt('281474976710656', base); 
   decstrtoGInt('44485709377909', temp); 
   GIntMulMod(seed, temp, base, RandomGInt); 
   GIntdestroy(temp); 
   GIntdestroy(base); 
End; 
 
 
// Perform a Rabin Miller Primality Test nrtest times on GIntp, returns ok=true when GIntp passes the test 
 
Procedure GIntRabinMiller(GIntp : TGInt; nrtest : integer; Var ok : boolean); 
Var 
   j, b, i : longint; 
   m, z, temp1, temp2, temp3, zero, one, two, pmin1 : TGInt; 
   ok1, ok2 : boolean; 
Begin 
   randomize; 
   j := 0; 
   b := 0; 
   decstrtogint('0', zero); 
   decstrtogint('1', one); 
   decstrtogint('2', two); 
   GIntsub(GIntp, one, temp1); 
   GIntsub(GIntp, one, pmin1); 
 
   While (temp1^.value Mod 2) = 0 Do 
   Begin 
      b := b + 1; 
      GIntdivbyint(temp1, temp2, 2, i); 
      GIntdestroy(temp1); 
      temp1 := temp2; 
   End; 
   m := temp1; 
 
   i := 0; 
   ok := true; 
   Randomize; 
   While (i < nrtest) And ok Do 
   Begin 
      i := i + 1; 
      DecStrToGInt(inttostr(Primes[Random(1227) + 1]), temp2); 
      GIntmodexp(temp2, m, GIntp, z); 
      GIntdestroy(temp2); 
      ok1 := (GIntcompareabs(z, one) = Eq); 
      ok2 := (GIntcompareabs(z, pmin1) = Eq); 
      If Not (ok1 Or ok2) Then 
      Begin 
 
         While (ok And (j < b)) Do 
         Begin 
            If (j > 0) And ok1 Then ok := false 
            Else 
            Begin 
               j := j + 1; 
               If (j < b) And (Not ok2) Then 
               Begin 
                  GIntSquaremod(z, GIntp, temp3); 
                  GIntdestroy(z); 
                  z := temp3; 
                  ok1 := (GIntcompareabs(z, one) = Eq); 
                  ok2 := (GIntcompareabs(z, pmin1) = Eq); 
                  If ok2 Then j := b; 
               End 
               Else If (Not ok2) And (j >= b) Then ok := false; 
            End; 
         End; 
 
      End 
   End; 
 
   GIntdestroy(zero); 
   GIntdestroy(one); 
   GIntdestroy(two); 
   GIntdestroy(m); 
   GIntdestroy(z); 
   GIntdestroy(pmin1); 
 
End; 
 
 
// Compute the coefficients from the Bezout Bachet theorem, GInt1 * a + GInt2 * b = GCD(GInt1, GInt2) 
 
Procedure GIntBezoutBachet(GInt1, GInt2 : TGInt; Var a, b : TGInt); 
Var 
   zero, r1, r2, r3, ta, gcd, temp, temp1, temp2 : TGInt; 
Begin 
   If GIntcompareabs(GInt1, GInt2) <> St Then 
   Begin 
      GIntcopy(GInt1, r1); 
      GIntcopy(GInt2, r2); 
      decstrtogint('0', zero); 
      decstrtogint('1', a); 
      decstrtogint('0', ta); 
 
      Repeat 
         GIntdivmod(r1, r2, temp, r3); 
         GIntdestroy(r1); 
         r1 := r2; 
         r2 := r3; 
 
         GIntmul(ta, temp, temp1); 
         GIntsub(a, temp1, temp2); 
         GIntdestroy(a); 
         GIntdestroy(temp1); 
         a := ta; 
         ta := temp2; 
 
         GIntdestroy(temp); 
      Until GIntcompareabs(r3, zero) = Eq; 
 
      GIntGCD(GInt1, GInt2, gcd); 
      GIntmul(a, GInt1, temp1); 
      GIntsub(gcd, temp1, temp2); 
      GIntDestroy(temp1); 
      GIntdiv(temp2, GInt2, b); 
      GIntDestroy(temp2); 
 
      GIntdestroy(ta); 
      GIntdestroy(r1); 
      GIntdestroy(r2); 
      GIntdestroy(gcd); 
   End 
   Else GIntBezoutBachet(GInt2, GInt1, b, a); 
End; 
 
 
// Find the (multiplicative) Modular inverse of a GInt in a finite ring of additive order base 
 
Procedure GIntModInv(GInt1, base : TGInt; Var Inverse : TGInt); 
Var 
   zero, one, r1, r2, r3, tb, gcd, temp, temp1, temp2 : TGInt; 
Begin 
   decstrtogint('1', one); 
   GIntGCD(GInt1, base, gcd); 
   If GIntcompareabs(one, gcd) = Eq Then 
   Begin 
      GIntcopy(base, r1); 
      GIntcopy(GInt1, r2); 
      decstrtogint('0', zero); 
      decstrtogint('0', inverse); 
      decstrtogint('1', tb); 
 
      Repeat 
         GIntdivmod(r1, r2, temp, r3); 
         GIntdestroy(r1); 
         r1 := r2; 
         r2 := r3; 
 
         GIntmul(tb, temp, temp1); 
         GIntsub(inverse, temp1, temp2); 
         GIntdestroy(inverse); 
         GIntdestroy(temp1); 
         inverse := tb; 
         tb := temp2; 
 
         GIntdestroy(temp); 
      Until GIntcompareabs(r3, zero) = Eq; 
 
      If inverse^.sign = negative Then 
      Begin 
         GIntadd(base, inverse, temp); 
         GIntdestroy(inverse); 
         inverse := temp; 
      End; 
 
      GIntdestroy(tb); 
      GIntdestroy(r1); 
      GIntdestroy(r2); 
   End; 
   GIntdestroy(gcd); 
   GIntdestroy(one); 
End; 
 
 
// Perform a (combined) primality test on GIntp consisting of a trialdivision upto 8192, 
// if the GInt passes perform nrRMtests Rabin Miller primality tests, returns ok when a 
// GInt is probably prime 
 
Procedure GIntPrimetest(GIntp : TGInt; nrRMtests : integer; Var ok : boolean); 
Begin 
   GIntTrialdiv9999(GIntp, ok); 
   If ok Then GIntRabinMiller(GIntp, nrRMtests, ok); 
End; 
 
// Computes the Legendre symbol for a any number and 
// p a prime, returns 0 if p divides a, 1 if a is a 
// quadratic residu mod p, -1 if a is a quadratic 
// nonresidu mod p 
 
Procedure GIntLegendreSymbol(a, p : TGInt; Var L : integer); 
Var 
   temp1, temp2, temp3, temp4, temp5, zero, one : TGInt; 
   i : integer; 
   ok1, ok2 : boolean; 
Begin 
   DecStrToGInt('0', zero); 
   DecStrToGInt('1', one); 
   GIntMod(a, p, temp1); 
   If GIntCompareabs(zero, temp1) = Eq Then 
   Begin 
      GIntDestroy(temp1); 
      L := 0; 
   End 
   Else 
   Begin 
      GIntDestroy(temp1); 
      GIntCopy(p, temp1); 
      GIntCopy(a, temp2); 
      L := 1; 
      While GIntCompareAbs(temp2, one) <> Eq Do 
      Begin 
         If (temp2^.value Mod 2) = 0 Then 
         Begin 
            GIntSquare(temp1, temp3); 
            GIntSub(temp3, one, temp4); 
            GIntDestroy(temp3); 
            GIntDivByInt(temp4, temp3, 8, i); 
            If (temp3^.value Mod 2) = 0 Then ok1 := false Else ok1 := true; 
            GIntDestroy(temp3); 
            GIntDestroy(temp4); 
            If ok1 = true Then L := L * (-1); 
            GIntDivByInt(temp2, temp3, 2, i); 
            GIntDestroy(temp2); 
            temp2 := temp3; 
         End 
         Else 
         Begin 
            GIntSub(temp1, one, temp3); 
            GIntSub(temp2, one, temp4); 
            GIntMul(temp3, temp4, temp5); 
            GIntDestroy(temp3); 
            GIntDestroy(temp4); 
            GIntDivByInt(temp5, temp3, 4, i); 
            If (temp3^.value Mod 2) = 0 Then ok2 := false Else ok2 := true; 
            GIntDestroy(temp5); 
            GIntDestroy(temp3); 
            If ok2 = true Then L := L * (-1); 
            GIntMod(temp1, temp2, temp3); 
            GIntDestroy(temp1); 
            temp1 := temp2; 
            temp2 := temp3; 
         End; 
      End; 
      GIntDestroy(temp1); 
      GIntDestroy(temp2); 
   End; 
   GIntDestroy(zero); 
   GIntDestroy(one); 
End; 
 
End.