raw
ffa_ch11_tuning_a...    1 ------------------------------------------------------------------------------
ffa_ch11_tuning_a... 2 ------------------------------------------------------------------------------
ffa_ch11_tuning_a... 3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
ffa_ch11_tuning_a... 4 -- --
ffa_ch15_gcd.kv 5 -- (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) --
ffa_ch11_tuning_a... 6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
ffa_ch11_tuning_a... 7 -- --
ffa_ch11_tuning_a... 8 -- You do not have, nor can you ever acquire the right to use, copy or --
ffa_ch11_tuning_a... 9 -- distribute this software ; Should you use this software for any purpose, --
ffa_ch11_tuning_a... 10 -- or copy and distribute it to anyone or in any manner, you are breaking --
ffa_ch11_tuning_a... 11 -- the laws of whatever soi-disant jurisdiction, and you promise to --
ffa_ch11_tuning_a... 12 -- continue doing so for the indefinite future. In any case, please --
ffa_ch11_tuning_a... 13 -- always : read and understand any software ; verify any PGP signatures --
ffa_ch11_tuning_a... 14 -- that you use - for any purpose. --
ffa_ch11_tuning_a... 15 -- --
ffa_ch11_tuning_a... 16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
ffa_ch11_tuning_a... 17 ------------------------------------------------------------------------------
ffa_ch11_tuning_a... 18 ------------------------------------------------------------------------------
ffa_ch11_tuning_a... 19
ffa_ch11_tuning_a... 20 with FZ_Arith;
ffa_ch11_tuning_a... 21 with FZ_Shift;
ffa_ch11_tuning_a... 22 with FZ_Mul;
ffa_ch12_karatsub... 23 with FZ_Sqr;
ffa_ch15_gcd.kv 24 with FZ_GCD;
ffa_ch11_tuning_a... 25
ffa_ch11_tuning_a... 26
ffa_ch11_tuning_a... 27 -- Wrapper bodies for routines that we inline, but must enforce preconditions
ffa_ch11_tuning_a... 28 -- on when called by FFA user.
ffa_ch11_tuning_a... 29 package body FFA is
ffa_ch11_tuning_a... 30
ffa_ch11_tuning_a... 31 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 32 --- FZ Basics
ffa_ch11_tuning_a... 33 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 34
ffa_ch11_tuning_a... 35 -- Exchange X and Y
ffa_ch11_tuning_a... 36 procedure FFA_FZ_Swap(X : in out FZ; Y : in out FZ) is
ffa_ch11_tuning_a... 37 begin
ffa_ch11_tuning_a... 38 FZ_Basic.FZ_Swap(X => X, Y => Y);
ffa_ch11_tuning_a... 39 end FFA_FZ_Swap;
ffa_ch11_tuning_a... 40
ffa_ch11_tuning_a... 41 -- Constant-time MUX: Sel = 0: Result := X; Sel = 1: Result := Y
ffa_ch11_tuning_a... 42 procedure FFA_FZ_Mux(X : in FZ; Y : in FZ;
ffa_ch11_tuning_a... 43 Result : out FZ; Sel : in WBool) is
ffa_ch11_tuning_a... 44 begin
ffa_ch11_tuning_a... 45 FZ_Basic.FZ_Mux(X => X, Y => Y, Result => Result, Sel => Sel);
ffa_ch11_tuning_a... 46 end FFA_FZ_Mux;
ffa_ch11_tuning_a... 47
ffa_ch11_tuning_a... 48 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 49 --- Bitwise Operations on FZ
ffa_ch11_tuning_a... 50 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 51
ffa_ch11_tuning_a... 52 -- Result := X & Y
ffa_ch11_tuning_a... 53 procedure FFA_FZ_And(X : in FZ; Y : in FZ; Result : out FZ) is
ffa_ch11_tuning_a... 54 begin
ffa_ch11_tuning_a... 55 FZ_BitOp.FZ_And(X => X, Y => Y, Result => Result);
ffa_ch11_tuning_a... 56 end FFA_FZ_And;
ffa_ch11_tuning_a... 57
ffa_ch11_tuning_a... 58 -- Result := X | Y
ffa_ch11_tuning_a... 59 procedure FFA_FZ_Or(X : in FZ; Y : in FZ; Result : out FZ) is
ffa_ch11_tuning_a... 60 begin
ffa_ch11_tuning_a... 61 FZ_BitOp.FZ_Or(X => X, Y => Y, Result => Result);
ffa_ch11_tuning_a... 62 end FFA_FZ_Or;
ffa_ch11_tuning_a... 63
ffa_ch11_tuning_a... 64 -- Result := X ^ Y
ffa_ch11_tuning_a... 65 procedure FFA_FZ_Xor(X : in FZ; Y : in FZ; Result : out FZ) is
ffa_ch11_tuning_a... 66 begin
ffa_ch11_tuning_a... 67 FZ_BitOp.FZ_Xor(X => X, Y => Y, Result => Result);
ffa_ch11_tuning_a... 68 end FFA_FZ_Xor;
ffa_ch11_tuning_a... 69
ffa_ch11_tuning_a... 70 -- NotN := ~N ('ones complement')
ffa_ch11_tuning_a... 71 procedure FFA_FZ_Not(N : in FZ; NotN : out FZ) is
ffa_ch11_tuning_a... 72 begin
ffa_ch11_tuning_a... 73 FZ_BitOp.FZ_Not(N => N, NotN => NotN);
ffa_ch11_tuning_a... 74 end FFA_FZ_Not;
ffa_ch11_tuning_a... 75
ffa_ch11_tuning_a... 76 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 77 --- Arithmetic on FZ
ffa_ch11_tuning_a... 78 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 79
ffa_ch11_tuning_a... 80 -- Sum := X + Y; Overflow := Carry
ffa_ch11_tuning_a... 81 procedure FFA_FZ_Add(X : in FZ;
ffa_ch11_tuning_a... 82 Y : in FZ;
ffa_ch11_tuning_a... 83 Sum : out FZ;
ffa_ch11_tuning_a... 84 Overflow : out WBool) is
ffa_ch11_tuning_a... 85 begin
ffa_ch11_tuning_a... 86 FZ_Arith.FZ_Add(X => X, Y => Y, Sum => Sum, Overflow => Overflow);
ffa_ch11_tuning_a... 87 end FFA_FZ_Add;
ffa_ch11_tuning_a... 88
ffa_ch11_tuning_a... 89 -- Difference := X - Y; Underflow := Borrow
ffa_ch11_tuning_a... 90 procedure FFA_FZ_Subtract(X : in FZ;
ffa_ch11_tuning_a... 91 Y : in FZ;
ffa_ch11_tuning_a... 92 Difference : out FZ;
ffa_ch11_tuning_a... 93 Underflow : out WBool) is
ffa_ch11_tuning_a... 94 begin
ffa_ch11_tuning_a... 95 FZ_Arith.FZ_Sub(X => X, Y => Y, Difference => Difference,
ffa_ch11_tuning_a... 96 Underflow => Underflow);
ffa_ch11_tuning_a... 97 end FFA_FZ_Subtract;
ffa_ch11_tuning_a... 98
ffa_ch11_tuning_a... 99 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 100 --- Multiplication on FZ
ffa_ch11_tuning_a... 101 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 102
ffa_ch11_tuning_a... 103 procedure FFA_FZ_Multiply(X : in FZ;
ffa_ch11_tuning_a... 104 Y : in FZ;
ffa_ch11_tuning_a... 105 XY_Lo : out FZ;
ffa_ch11_tuning_a... 106 XY_Hi : out FZ) is
ffa_ch11_tuning_a... 107 begin
ffa_ch11_tuning_a... 108 FZ_Mul.FZ_Multiply_Buffered(X => X, Y => Y,
ffa_ch11_tuning_a... 109 XY_Lo => XY_Lo, XY_Hi => XY_Hi);
ffa_ch11_tuning_a... 110 end FFA_FZ_Multiply;
ffa_ch11_tuning_a... 111
ffa_ch12_karatsub... 112
ffa_ch12_karatsub... 113 -- Square. Preserves the inputs.
ffa_ch12_karatsub... 114 procedure FFA_FZ_Square(X : in FZ;
ffa_ch12_karatsub... 115 XX_Lo : out FZ;
ffa_ch12_karatsub... 116 XX_Hi : out FZ) is
ffa_ch12_karatsub... 117 begin
ffa_ch12_karatsub... 118 FZ_Sqr.FZ_Square_Buffered(X => X, XX_Lo => XX_Lo, XX_Hi => XX_Hi);
ffa_ch12_karatsub... 119 end FFA_FZ_Square;
ffa_ch12_karatsub... 120
ffa_ch15_gcd.kv 121 ----------------------------------------------------------------------------
ffa_ch15_gcd.kv 122 --- Other Operations on FZ
ffa_ch15_gcd.kv 123 ----------------------------------------------------------------------------
ffa_ch15_gcd.kv 124
ffa_ch15_gcd.kv 125 -- Find Greatest Common Divisor (GCD) of X and Y.
ffa_ch15_gcd.kv 126 procedure FFA_FZ_Greatest_Common_Divisor(X : in FZ;
ffa_ch15_gcd.kv 127 Y : in FZ;
ffa_ch15_gcd.kv 128 Result : out FZ) is
ffa_ch15_gcd.kv 129 begin
ffa_ch15_gcd.kv 130 FZ_GCD.FZ_Greatest_Common_Divisor(X => X, Y => Y, Result => Result);
ffa_ch15_gcd.kv 131 end FFA_FZ_Greatest_Common_Divisor;
ffa_ch15_gcd.kv 132
ffa_ch11_tuning_a... 133 end FFA;