raw
ffa_ch6_simplest_...    1 ------------------------------------------------------------------------------
ffa_ch6_simplest_... 2 ------------------------------------------------------------------------------
ffa_ch6_simplest_... 3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
ffa_ch6_simplest_... 4 -- --
ffa_ch6_simplest_... 5 -- (C) 2017 Stanislav Datskovskiy ( www.loper-os.org ) --
ffa_ch6_simplest_... 6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
ffa_ch6_simplest_... 7 -- --
ffa_ch6_simplest_... 8 -- You do not have, nor can you ever acquire the right to use, copy or --
ffa_ch6_simplest_... 9 -- distribute this software ; Should you use this software for any purpose, --
ffa_ch6_simplest_... 10 -- or copy and distribute it to anyone or in any manner, you are breaking --
ffa_ch6_simplest_... 11 -- the laws of whatever soi-disant jurisdiction, and you promise to --
ffa_ch6_simplest_... 12 -- continue doing so for the indefinite future. In any case, please --
ffa_ch6_simplest_... 13 -- always : read and understand any software ; verify any PGP signatures --
ffa_ch6_simplest_... 14 -- that you use - for any purpose. --
ffa_ch6_simplest_... 15 -- --
ffa_ch6_simplest_... 16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
ffa_ch6_simplest_... 17 ------------------------------------------------------------------------------
ffa_ch6_simplest_... 18 ------------------------------------------------------------------------------
ffa_ch6_simplest_... 19
ffa_ch6_simplest_... 20 with FZ_Basic; use FZ_Basic;
ffa_ch6_simplest_... 21 with FZ_Pred; use FZ_Pred;
ffa_ch6_simplest_... 22 with FZ_Shift; use FZ_Shift;
ffa_ch6_simplest_... 23 with FZ_Mul; use FZ_Mul;
ffa_ch12_karatsub... 24 with FZ_Sqr; use FZ_Sqr;
ffa_ch6_simplest_... 25 with FZ_Divis; use FZ_Divis;
ffa_ch6_simplest_... 26
ffa_ch6_simplest_... 27
ffa_ch6_simplest_... 28 package body FZ_ModEx is
ffa_ch6_simplest_... 29
ffa_ch6_simplest_... 30 -- Modular Multiply: Product := X*Y mod Modulus
ffa_ch6_simplest_... 31 procedure FZ_Mod_Mul(X : in FZ;
ffa_ch6_simplest_... 32 Y : in FZ;
ffa_ch6_simplest_... 33 Modulus : in FZ;
ffa_ch6_simplest_... 34 Product : out FZ) is
ffa_ch6_simplest_... 35
ffa_ch6_simplest_... 36 -- The wordness of all three operands is equal:
ffa_ch6_simplest_... 37 L : constant Indices := X'Length;
ffa_ch6_simplest_... 38
ffa_ch6_simplest_... 39 -- Double-width register for multiplication and modulus operations
ffa_ch6_simplest_... 40 XY : FZ(1 .. L * 2);
ffa_ch6_simplest_... 41
ffa_ch6_simplest_... 42 -- To refer to the lower and upper halves of the working register:
ffa_ch6_simplest_... 43 XY_Lo : FZ renames XY(1 .. L);
ffa_ch6_simplest_... 44 XY_Hi : FZ renames XY(L + 1 .. XY'Last);
ffa_ch6_simplest_... 45
ffa_ch6_simplest_... 46 begin
ffa_ch6_simplest_... 47
ffa_ch6_simplest_... 48 -- XY_Lo:XY_Hi := X * Y
ffa_ch11_tuning_a... 49 FZ_Multiply_Buffered(X, Y, XY_Lo, XY_Hi);
ffa_ch6_simplest_... 50
ffa_ch7_turbo_egy... 51 -- Product := XY mod M
ffa_ch7_turbo_egy... 52 FZ_Mod(XY, Modulus, Product);
ffa_ch6_simplest_... 53
ffa_ch6_simplest_... 54 end FZ_Mod_Mul;
ffa_ch6_simplest_... 55
ffa_ch6_simplest_... 56
ffa_ch12_karatsub... 57 -- Modular Squaring: Product := X*X mod Modulus
ffa_ch12_karatsub... 58 procedure FZ_Mod_Sqr(X : in FZ;
ffa_ch12_karatsub... 59 Modulus : in FZ;
ffa_ch12_karatsub... 60 Product : out FZ) is
ffa_ch12_karatsub... 61
ffa_ch12_karatsub... 62 -- The wordness of both operands is equal:
ffa_ch12_karatsub... 63 L : constant Indices := X'Length;
ffa_ch12_karatsub... 64
ffa_ch12_karatsub... 65 -- Double-width register for squaring and modulus operations
ffa_ch12_karatsub... 66 XX : FZ(1 .. L * 2);
ffa_ch12_karatsub... 67
ffa_ch12_karatsub... 68 -- To refer to the lower and upper halves of the working register:
ffa_ch12_karatsub... 69 XX_Lo : FZ renames XX(1 .. L);
ffa_ch12_karatsub... 70 XX_Hi : FZ renames XX(L + 1 .. XX'Last);
ffa_ch12_karatsub... 71
ffa_ch12_karatsub... 72 begin
ffa_ch12_karatsub... 73
ffa_ch12_karatsub... 74 -- XX_Lo:XX_Hi := X^2
ffa_ch12_karatsub... 75 FZ_Square_Buffered(X, XX_Lo, XX_Hi);
ffa_ch12_karatsub... 76
ffa_ch12_karatsub... 77 -- Product := XX mod M
ffa_ch12_karatsub... 78 FZ_Mod(XX, Modulus, Product);
ffa_ch12_karatsub... 79
ffa_ch12_karatsub... 80 end FZ_Mod_Sqr;
ffa_ch12_karatsub... 81
ffa_ch12_karatsub... 82
ffa_ch6_simplest_... 83 -- Modular Exponent: Result := Base^Exponent mod Modulus
ffa_ch6_simplest_... 84 procedure FZ_Mod_Exp(Base : in FZ;
ffa_ch6_simplest_... 85 Exponent : in FZ;
ffa_ch6_simplest_... 86 Modulus : in FZ;
ffa_ch6_simplest_... 87 Result : out FZ) is
ffa_ch6_simplest_... 88
ffa_ch8_randomism.kv 89 -- Working register for the squaring; initially is copy of Base
ffa_ch6_simplest_... 90 B : FZ(Base'Range) := Base;
ffa_ch6_simplest_... 91
ffa_ch8_randomism.kv 92 -- Copy of Exponent, for cycling through its bits
ffa_ch6_simplest_... 93 E : FZ(Exponent'Range) := Exponent;
ffa_ch6_simplest_... 94
ffa_ch6_simplest_... 95 -- Register for the Mux operation
ffa_ch6_simplest_... 96 T : FZ(Result'Range);
ffa_ch6_simplest_... 97
ffa_ch8_randomism.kv 98 -- Buffer register for the Result
ffa_ch8_randomism.kv 99 R : FZ(Result'Range);
ffa_ch8_randomism.kv 100
ffa_ch6_simplest_... 101 begin
ffa_ch6_simplest_... 102 -- Result := 1
ffa_ch8_randomism.kv 103 WBool_To_FZ(1, R);
ffa_ch6_simplest_... 104
ffa_ch8_randomism.kv 105 -- For each bit of R width:
ffa_ch8_randomism.kv 106 for i in 1 .. FZ_Bitness(R) loop
ffa_ch6_simplest_... 107
ffa_ch6_simplest_... 108 -- T := Result * B mod Modulus
ffa_ch8_randomism.kv 109 FZ_Mod_Mul(X => R, Y => B, Modulus => Modulus, Product => T);
ffa_ch6_simplest_... 110
ffa_ch6_simplest_... 111 -- Sel is the current low bit of E;
ffa_ch6_simplest_... 112 -- When Sel=0 -> Result := Result;
ffa_ch6_simplest_... 113 -- When Sel=1 -> Result := T
ffa_ch8_randomism.kv 114 FZ_Mux(X => R, Y => T, Result => R, Sel => FZ_OddP(E));
ffa_ch6_simplest_... 115
ffa_ch6_simplest_... 116 -- Advance to the next bit of E
ffa_ch6_simplest_... 117 FZ_ShiftRight(E, E, 1);
ffa_ch6_simplest_... 118
ffa_ch12_karatsub... 119 -- B := B^2 mod Modulus
ffa_ch12_karatsub... 120 FZ_Mod_Sqr(X => B, Modulus => Modulus, Product => B);
ffa_ch6_simplest_... 121
ffa_ch6_simplest_... 122 end loop;
ffa_ch6_simplest_... 123
ffa_ch8_randomism.kv 124 -- Output the Result:
ffa_ch8_randomism.kv 125 Result := R;
ffa_ch8_randomism.kv 126
ffa_ch6_simplest_... 127 end FZ_Mod_Exp;
ffa_ch6_simplest_... 128
ffa_ch6_simplest_... 129 end FZ_ModEx;