summaryrefslogtreecommitdiff
path: root/src/crypto-types-big_numbers-mod_utils.adb
blob: 3c02df1d470e5cd00eedf2a2e1135ec810a1f053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 2 of the
-- License, or (at your option) any later version.

-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-- General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.

-- As a special exception, if other files instantiate generics from
-- this unit, or you link this unit with other files to produce an
-- executable, this unit does not by itself cause the resulting
-- executable to be covered by the GNU General Public License. This
-- exception does not however invalidate any other reasons why the
-- executable file might be covered by the GNU Public License.

with Crypto.Types.Random;
with Crypto.Asymmetric.Prime_Tables;
--with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO;

separate(Crypto.Types.Big_Numbers)

package body Mod_Utils is

   pragma Optimize (Time);
   use Crypto.Asymmetric.Prime_Tables;


   ---------------------------------------------------------------------------

   function Patch(Item, N : Big_Unsigned)
                 return Big_Unsigned is
      Diff : constant Big_Unsigned:=((Big_Unsigned_Last - N) + 1) mod N;
   begin
      return Add(Item,Diff,N);
   end Patch; pragma Inline(Patch);

   ---------------------------------------------------------------------------

   function Add(Left, Right, N : Big_Unsigned) return Big_Unsigned is
      L : constant Big_Unsigned := Left mod N;
      R : constant Big_Unsigned := Right mod N;
      Result : constant Big_Unsigned := L + R;
   begin
      if Result < Max(L,R) then
         return Patch(Result,N);
      else return
        Result mod N;
      end if;
   end Add;

   ---------------------------------------------------------------------------

   function Sub(Left, Right, N : Big_Unsigned) return Big_Unsigned is
      L : constant Big_Unsigned := Left  mod N;
      R : constant Big_Unsigned := Right mod N;
   begin
      if R > L then
         return N - R + L;
      else return L-R;
      end if;
   end Sub;

   ---------------------------------------------------------------------------

   function Div(Left, Right, N : Big_Unsigned) return Big_Unsigned is
   begin
      return Mult(Left,Inverse(Right,N),N);
   end Div; pragma Inline(Div);


   ---------------------------------------------------------------------------

   --from Erik-Zenners handout "Zahlentheoretische Algorithmen"
   function Pow(Base, Exponent, N : Big_Unsigned) return Big_Unsigned is
      L : constant Big_Unsigned := Base mod N;
      R : constant Big_Unsigned := Exponent;
      Result : Big_Unsigned := Big_Unsigned_One;
   begin
      if L = Big_Unsigned_Zero or L = Big_Unsigned_One then
         return L;
      elsif R = Big_Unsigned_Zero then return Big_Unsigned_One;
      else
         -- Square_And_Muliply
         for I in reverse  0..Bit_Length(R)-1 loop
            Result :=  Mult(Result,Result,N);
            if (Shift_Right(R, I) mod 2) = Big_Unsigned_One then
               Result := Mult(Result,L,N);
            end if;
         end loop;
         return Result mod N;
      end if;
   end Pow;

   ---------------------------------------------------------------------------

   --based on  Erik-Zenners handout "Zahlentheoretische Algorithmen"
   -- (ext. Euklid)
   -- This function returns Big_unsigned_Zero if X have no inverse mod n
   function Inverse(X, N : Big_Unsigned) return Big_Unsigned is
      B : Big_Unsigned := X mod N;
      A : Big_Unsigned := N;
   begin
      -- if gcd(A,B) /= 1 then A have no inverse mod B
      if B = Big_Unsigned_Zero or A = Big_Unsigned_Zero or
        Gcd(A,B) /= Big_Unsigned_One then
         return  Big_Unsigned_Zero;
      end if;

      declare
         T : Big_Unsigned := Big_Unsigned_One;
         Tstrich,  Tempt : Big_Unsigned;
         Q, R : Big_Unsigned;
      begin
         loop
            Big_Div(A,B,Q,R);
            if(R = Big_Unsigned_Zero) then
               return T;
            end if;

            A:=B;
            B:=R;

            Tempt:=T;

            T:=Sub(Tstrich,Mult(Q,T,N),N);

            Tstrich:=Tempt;
         end loop;
      end;
   end Inverse;

   ---------------------------------------------------------------------------

   function Get_Random(N : Big_Unsigned) return Big_Unsigned is
      Result : Big_Unsigned;
   begin
      Random.Read(Result.Number);

      for I in reverse 0..N.Last_Index loop
         if Result.Number(I) /= 0 then
            Result.Last_Index := I;
            exit;
         end if;
      end loop;
      return Result mod N ;
   end Get_Random;

   ---------------------------------------------------------------------------

   -- this function returns true if X is a Mersenne prim number
   function Lucas_Lehmer_Test(X : Big_Unsigned) return Boolean is
      Is_Mp : Boolean := false;
   begin

      if X.Last_Index = 0 then
         for I in 2..Word'Size-1 loop
            if  X.Number(0) = Shift_Left(2,I)-1 then
               Is_Mp := True;
               exit;
            end if;
         end loop;
         if Is_Mp = False then return False;
         end if;
      else
         for I in 0..X.Last_Index loop
            if X.Number(I) /= Word'Last then return False;
            end if;
         end loop;
      end if;

      declare
         P : constant Word := Word(Bit_Length(X)-1);
         S : Big_Unsigned := Big_Unsigned_Two+2; --S(1) = 4;
      begin
         for I in 2..P-1 loop
            S := (Mult(S,S,X) - 2) mod X;
         end loop;

         if S = Big_Unsigned_Zero then return True;
         else return False;
         end if;
      end;
   end Lucas_Lehmer_Test;

   ---------------------------------------------------------------------------

   --from Erik-Zenners handout "Zahlentheoretische Algorithmen"
   function Is_Miller_Rabin_Witness(Wit, X : Big_Unsigned) return Boolean is

      B : constant Big_Unsigned := X-1;
      Result : Big_Unsigned := Big_Unsigned_One;
      Root : Big_Unsigned;
   begin
      for I in reverse 0..Bit_Length(B)-1 loop
         Root := Result;
         Result := Mult(Result, Result, X);
         if ((Result = Big_Unsigned_One) and
             (Root /= Big_Unsigned_One and Root /= B)) then return True;
         elsif (Shift_Right(B,I) mod 2) = Big_Unsigned_One then
            Result := Mult(Result, Wit, X);
         end if;
      end loop;
      if Result /= Big_Unsigned_One then return True;
      else return False;
      end if;
   end Is_Miller_Rabin_Witness;

   ---------------------------------------------------------------------------

   -- Test if Wit is a witness for N
   -- If Wit is a wittness then N is no prime
    function Is_Simple_Witness(Wit, N : Big_Unsigned) return Boolean is
    begin
       -- is Wit a "Miller-Rabin"-witness
       if (Wit /= (N-Big_Unsigned_One)) and  (Wit /= Big_Unsigned_One) and
         Mult(Wit,Wit,N) = Big_Unsigned_One  then return True;

       elsif Gcd(Wit,N) /= Big_Unsigned_One then  return True;

       -- is Wit a "Fermat-Witness"
       -- elsif Pow(Wit,N-1,N) /= Big_Unsigned_One then  return True;
       else return False;
       end if;
    end Is_Simple_Witness;

    ---------------------------------------------------------------------------


     -- Returns true if N passes the specified number of Miller-Rabin tests.
   function Passed_Miller_Rabin_Test(X : Big_Unsigned; S : Positive)
                                    return Boolean is
      Witness : Big_Unsigned;
   begin
      -- Do the tests
      for I in 1..S loop
         -- Generate a uniform random on (1, X)
         loop
            Witness := Get_Random(X);
            exit when Witness > Big_Unsigned_One;
         end loop;
         if Is_Miller_Rabin_Witness(Witness, X) then
            return False;
         end if;
         end loop;
      return true;
   end Passed_Miller_Rabin_Test;

   ---------------------------------------------------------------------------

   function Pass_Prime_Test(X : Big_Unsigned; Status : Hardness)
                           return Boolean is
      Rounds : Natural;
      X_Bit_Size : constant Natural := Bit_Length(X);
   begin
       if X < Big_Unsigned_Two then return False;
       elsif Is_Even(X) then
          if X = Big_Unsigned_Two then return True;
          else return False;
          end if;
       end if;

       --X is odd

       for I in One_Digit_Primes'First+1..One_Digit_Primes'Last loop
         if X = Word(One_Digit_Primes(I)) then return true;
         elsif X mod Word(One_Digit_Primes(I)) = Big_Unsigned_Zero then
            return False;
         end if;
       end loop;

      for I in Two_Digit_Primes'Range loop
         if X = Word(Two_Digit_Primes(I)) then return true;
         elsif X mod Word(Two_Digit_Primes(I)) = Big_Unsigned_Zero then
            return False;
         end if;
      end loop;

      if Lucas_Lehmer_Test(X) then
         return True;
      end if;

      for I in Three_Digit_Primes'Range loop
         if X = Word(Three_Digit_Primes(I)) then return true;
         elsif X mod Word(Three_Digit_Primes(I)) = Big_Unsigned_Zero then
            return False;
         end if;
      end loop;

       -- The relationship between the certainty and the number of rounds
      -- we perform is given in the draft standard ANSI X9.80, "PRIME
      -- NUMBER GENERATION, PRIMALITY TESTING, AND PRIMALITY CERTIFICATES".
      -- Comment:
      -- I don't have a look on this paper. =:) I borrowed this
      -- "algorithmen" from the j2sdk1.4.1 library (java/math/BigInteger.java)
      -- If you have the permission to send me the draft standard ANSI X9.80
      -- then send it, please!
      -- I'm a student. I have no money for ANSI or IEEE drafts. :-(
      -- It's right to require money to read a draft?
      -- This really really sucks! SCNR!

      if    (X_Bit_Size <  100) then Rounds := 50;
      elsif (X_Bit_Size <  256) then Rounds := 27;
      elsif (X_Bit_Size <  512) then Rounds := 15;
      elsif (X_Bit_Size <  768) then Rounds :=  8;
      elsif (X_Bit_Size < 1024) then Rounds :=  4;
      else                           Rounds :=  2;
      end if;

      declare
         Witness : Big_Unsigned;
      begin
         if Status = Weak then
            for I in 1..Rounds loop
               loop
                  Witness := Get_Random(X);
                  exit when Witness > Big_Unsigned_Two;
               end loop;
               if Is_Simple_Witness(Witness,X) then return False;
               end if;
            end loop;
         else
            for I in 1..Rounds loop
               loop
                  Witness := Get_Random(X);
                  exit when Witness > Big_Unsigned_Two;
               end loop;
               if Is_Miller_Rabin_Witness(Witness,X) then return False;
               end if;
            end loop;
         end if;
      end;
      return True;
    end Pass_Prime_Test;

    ---------------------------------------------------------------------------


    function Is_Prime(X : Big_Unsigned) return Boolean is
    begin
       return Pass_Prime_Test(X, Strong);
    end Is_Prime; pragma Inline (Is_Prime);

    ---------------------------------------------------------------------------

    -- This function is faster then Is_prime but a lot of no strong pseudo
    -- primes pass this test
    function Looks_Like_A_Prime(X : Big_Unsigned) return Boolean is
    begin
      return Pass_Prime_Test(X, Weak);
    end Looks_Like_A_Prime; pragma Inline(Looks_Like_A_Prime);

   ---------------------------------------------------------------------------

    function Get_Prime(N : Big_Unsigned) return Big_Unsigned is
       Result : Big_Unsigned := Get_Random(N);
   begin
      if N <= Big_Unsigned_Two then
         raise Constraint_Error;
      end if;

      -- make sure that Result is odd
      Result.Number(0) := Result.Number(0) or 1;
      loop
         if Is_Prime(Result) then  return Result;
         else  Result := (Result+2) mod N ;
         end if;
      end loop;
   end Get_Prime;


   ---------------------------------------------------------------------------


   function "mod"(Left :  D_Big_Unsigned; Right : Big_Unsigned)
                 return Big_Unsigned;

   ---------------------------------------------------------------------------

   -- Result = Left * Right (mod N)
   function Mult(Left, Right, N : Big_Unsigned) return Big_Unsigned is
      T : DWord;
      Carry : Word := 0;
      R : D_Big_Unsigned;
   begin
      for I in 0..Left.Last_Index loop
         for J in 0..Right.Last_Index loop
            T := DWord(Left.Number(I)) *  DWord(Right.Number(J))
               + DWord(R.Number(I+J)) + DWord(Carry);

            R.Number(I+J) := Word(T and DWord(Word'Last));

            Carry:= Word(Shift_Right(T,Word'Size));
         end loop;
         R.Number(I+Right.Last_Index+1) := Carry +
          R.Number(I+Right.Last_Index+1);
         Carry := 0;
      end loop;

     for I in reverse 0..D_Max_Length loop
        if R.Number(I) /= 0 then
            R.Last_Index := I;
            exit;
         end if;
      end loop;
     return R mod N;
   end Mult;

 ---------------------------------------------------------------------------


   -- Returns a probability  N-bit prime (Result).
   function Get_N_Bit_Prime(N : Positive) return Big_Unsigned  is
      J : Big_Unsigned := Get_Random(Shift_Left(Big_Unsigned_One,N-2));
      Index : constant Natural := (N-1)/Word'Size;
      Amount : constant Natural := (N-1) mod Word'Size;
      Result : Big_Unsigned := Shift_Left(J,1);

   begin
      if N = 1 or N > Size then
         raise Constraint_Error;
      end if;

      loop
         -- Make sure that Result is an odd
         Set_Least_Significant_Bit (Result);

         -- Make sure that Result is a N-Bit-Number;
         Result.Number (Index) := Result.Number (Index) or
           Shift_Left (Word (1), Amount);

         if Amount = 0 then
	    Result.Last_Index := Index;
         end if;

         if Is_Prime(Result) then
	    return Result;
         else
            Result:=Result-2;
            if Is_Prime(Result) then 
	       return Result;
            end if;
         end if;

         J := Get_Random (Shift_Left (Big_Unsigned_One, N - 2));
         Result := Shift_Left (J, 1);
      end loop;

   end Get_N_Bit_Prime;

   ---------------------------------------------------------------------------

   -- computes the jacobi-symbol
   -- return value:
   --   0 : if X mod N = 0
   --   1 : if X is a quadratic resuide mod N
   --  -1 : if X is a quadratic non-resuide mod N

   function Jacobi(X, N : Big_Unsigned) return Integer is
      A : Big_Unsigned :=  X mod N;
   begin

      if Is_Even(N) then
         raise Constraint_Error;
      end if;

      if N = Big_Unsigned_One then return 1;
      elsif A = Big_Unsigned_Zero then return 0;
      elsif A =  Big_Unsigned_One then return 1;
      end if;

      while (A mod 4) = Big_Unsigned_Zero loop
         exit when (A mod 4) = Big_Unsigned_Zero;
         A := Shift_Right(A,2);
      end loop;

      if Is_Even(A) then
         if (N mod 8 = 1) or (N mod 8 = 7) then
            return Jacobi(Shift_Right(A,1),N);
         else return -1*Jacobi(Shift_Right(A,1),N);
         end if;
      else
         if (A mod 4 = 1)  or (N mod 4 = 1) then
            return Jacobi(N mod A, A);
         else  return -1*Jacobi(N mod A, A);
         end if;
      end if;
   end Jacobi;

  ----------------------------------------------------------------------------
  -----------------------------DOUBLE_SIZE------------------------------------
  ----------------------------------------------------------------------------

   --only needed for multiplication mod N
   --here we need 2*Size-bit numbers to avoid an overflow because
   --if one of our provisional result t > BIG_Unsigned_Last
   --then there ist no well known algortihm to compute the
   -- result of an multiplication mod m

   -- same algorithm for D_Big_Unsigned as for Big_Unsigned

   function "="(Left, Right : D_Big_Unsigned) return Boolean is
   begin
      if Left.Last_Index = Right.Last_Index then
         for I in 0..Left.Last_Index loop
            if  Left.Number(I) /= Right.Number(I) then return False;
            end if;
         end loop;
      else return False;
      end if;
      return True;
   end"=";

   ----------------------------------------------------------------------------

   function Shift_Left(Value : D_Big_Unsigned; Amount : Natural)
                      return D_Big_Unsigned is
   begin
      if Amount >= (D_Max_Length+1)*Word'Size or
        Value = D_Big_Unsigned_Zero
      then  return D_Big_Unsigned_Zero;
      elsif Amount = 0 then return Value;
      end if;

      declare
         Result : D_Big_Unsigned;
         Temp : DLimbs :=(others => 0);
         L : constant Natural := Amount mod Word'Size;
         R : constant Natural := Word'Size-L;
         M : constant Natural := Amount/Word'Size;
      begin
         Temp(0) := Shift_Left(Value.Number(0), L);

         for I in 1..Value.Last_Index loop
            Temp(I) := Shift_Right(Value.Number(I-1), R) +
              Shift_Left(Value.Number(I), L);
         end loop;

         if Value.Last_Index /= D_Max_Length then
            Temp(Value.Last_Index+1):=
              Shift_Right(Value.Number(Value.Last_Index), R);
         end if;

         for I in Temp'Range loop
            if (I+M) > D_Max_Length then
               exit;
            end if;
            Result.Number(I+M):= Temp(I);
         end loop;

         for I in reverse 0..D_Max_Length loop
            if Result.Number(I) /=0 then
               Result.Last_Index:=I;
               exit;
            end if;
         end loop;
         return Result;
      end;
   end Shift_Left; pragma Inline (Shift_Left);

   ---------------------------------------------------------------------------


   function Bit_Length(X : D_Big_Unsigned) return Natural is
   begin
      if X = D_Big_Unsigned_Zero then
         return 0;
      end if;

      for I in reverse 0..Word'Size-1 loop
         if Shift_Left(1,I) <= X.Number(X.Last_Index) then
            return  Word'Size * X.Last_Index + I + 1 ;
         end if;
      end loop;
      return X.Last_Index * Word'Size;
   end Bit_Length; pragma Inline (Bit_Length);


   ---------------------------------------------------------------------------

   function "<"(Left, Right : D_Big_Unsigned) return Boolean is
   begin
      if Left.Last_Index < Right.Last_Index then return True;
      elsif Left.Last_Index > Right.Last_Index then  return False;
      else
         for I in reverse  0..Left.Last_Index loop
            if  Left.Number(I) < Right.Number(I) then return True;
            elsif Left.Number(I) > Right.Number(I) then return False;
            end if;
         end loop;
      end if;
      return False;
   end "<"; pragma Inline ("<");

   ---------------------------------------------------------------------------

   function ">"(Left, Right : D_Big_Unsigned) return Boolean is
   begin
      return Right < Left;
   end ">"; pragma Inline (">");



   ---------------------------------------------------------------------------

   function ">="(Left, Right : D_Big_Unsigned) return Boolean is
   begin
      return not(Left < Right);
   end ">="; pragma Inline (">=");

   ---------------------------------------------------------------------------

   function "+"(Left, Right : D_Big_Unsigned) return D_Big_Unsigned;

   function "-"(Left, Right : D_Big_Unsigned) return D_Big_Unsigned is
   begin
      if Left = Right then  return D_Big_Unsigned_Zero;
      elsif Left = Right+D_Big_Unsigned_One then return D_Big_Unsigned_One;
      elsif Left+D_Big_Unsigned_One = Right then return D_Big_Unsigned_Last;

      -- add the modulus if Right > Left
      elsif Right > Left then
         return D_Big_Unsigned_Last - Right + Left + D_Big_Unsigned_One;
      else
         declare
            Result : D_Big_Unsigned;
            Carry : Word:=0;
         begin
            -- Remember Left > Right
            for I in 0..Left.Last_Index loop
               Result.Number(I) := Left.Number(I) - Right.Number(I) - Carry;
               if (Right.Number(I) >  Left.Number(I)) or
                 (Carry= 1 and Right.Number(I) =  Left.Number(I))
               then Carry :=1;
               else Carry :=0;
               end if;
               if Result.Number(I) /= 0 then
                  Result.Last_Index := I;
               end if;
            end loop;
            return Result;
         end;
      end if;
   end "-";


   ---------------------------------------------------------------------------

   function "mod"(Left :  D_Big_Unsigned; Right : Big_Unsigned)
                 return Big_Unsigned is
   begin
      if Left.Last_Index <= Max_Length then
         declare
            L : Big_Unsigned;
         begin
            L.Last_Index := Left.Last_Index;
            L.Number(0..Left.Last_Index) := Left.Number(0..Left.Last_Index);
         return L mod Right;
         end;
      end if;

      if Right = Big_Unsigned_Zero then raise Division_By_Zero;
      --elsif Right = Big_Unsigned_One then return Big_Unsigned_Zero;
      end if;

      -- Now, there is only the case where (Left > Right), (Right /= 0)
      -- and |Left|>|Right|.

      declare
         Remainder : D_Big_Unsigned:=Left;
         Temp_Right, R : D_Big_Unsigned;
         Result : Big_Unsigned;
         Diff: Natural;

      begin
         Temp_Right.Last_Index := Right.Last_Index;
         Temp_Right.Number(0..Right.Last_Index) :=
           Right.Number(0..Right.Last_Index);
         R:=Temp_Right;

         while(Remainder >= R) loop
            Diff := Bit_Length(Remainder) - Bit_Length(R);
            if Diff = 0 then
               Remainder := Remainder-R;
               exit;
            else Diff:=Diff-1;
            end if;
            Temp_Right := Shift_Left(R, Diff);
            Remainder :=  Remainder-Temp_Right;
         end loop;

         Result.Last_Index := Remainder.Last_Index;
         Result.Number(0..Result.Last_Index) :=
           Remainder.Number(0..Result.Last_Index);
         return Result;
      end;
   end "mod";

   ---------------------------------------------------------------------------

   function "+"(Left, Right : D_Big_Unsigned) return D_Big_Unsigned is
      Result : D_Big_Unsigned;
      M : constant Natural  := Natural'Max(Left.Last_Index, Right.Last_Index);
      Temp : Word;
      Carry : Word :=0;
   begin

        for I in 0..M loop
         Temp :=Carry;
         Result.Number(I) := Left.Number(I) + Right.Number(I) +Temp;
         if Result.Number(I) < Word'Max(Left.Number(I), Right.Number(I))
         then  Carry := 1;
         else Carry := 0;
         end if;
      end loop;

      if Carry  =1 and M < Max_Length then
         Result.Number(M+1) := 1;
         Result.Last_Index := M+1;
      else
         -- Set Result.Last_Index
         for I in reverse 0..M loop
            if Result.Number(I) /= 0 then
               Result.Last_Index := I;
               return Result;
            end if;
         end loop;
      end if;
      return Result;
   end "+";

   ---------------------------------------------------------------------------

end Mod_Utils;