language: Ada (gnat-4.3.2)
date: 130 days 19 hours ago
link:
visibility: public
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
----------------------------------------------------------------
-- Montgomery Product (Montgomery_product.adb)
-- 
-- A simple example of
-- MP(x,y) = x.y/R mod M
--
----------------------------------------------------------------
with Gnat.Io; use Gnat.Io;
procedure Montgomery_product is
   R: constant Natural := 256;
   M: constant Natural := 239;
   --Inv_R: constant := 225;
   Minus_Inv_M: constant := 241;
   X, Y, Product, Q, Z: Natural;
begin
   loop
      Put("x = "); Get(X);     
      Put("y = "); Get(Y);     
      ---------------------------------------------------------------------
      Product := X*Y;
      Q := (Product + ((Product*Minus_Inv_M) mod R)*M) / R;
      if Q >= M then Z := Q-M; else Z := Q; end if; 
      ----------------------------------------------------------------------
      Put("x.y/R mod m = "); Put(Z); New_Line;
   end loop;
end Montgomery_product;