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_ch11_tuning_a... 5 -- (C) 2018 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_ch11_tuning_a... 24
ffa_ch11_tuning_a... 25
ffa_ch11_tuning_a... 26 -- Wrapper bodies for routines that we inline, but must enforce preconditions
ffa_ch11_tuning_a... 27 -- on when called by FFA user.
ffa_ch11_tuning_a... 28 package body FFA is
ffa_ch11_tuning_a... 29
ffa_ch11_tuning_a... 30 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 31 --- FZ Basics
ffa_ch11_tuning_a... 32 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 33
ffa_ch11_tuning_a... 34 -- Exchange X and Y
ffa_ch11_tuning_a... 35 procedure FFA_FZ_Swap(X : in out FZ; Y : in out FZ) is
ffa_ch11_tuning_a... 36 begin
ffa_ch11_tuning_a... 37 FZ_Basic.FZ_Swap(X => X, Y => Y);
ffa_ch11_tuning_a... 38 end FFA_FZ_Swap;
ffa_ch11_tuning_a... 39
ffa_ch11_tuning_a... 40 -- Constant-time MUX: Sel = 0: Result := X; Sel = 1: Result := Y
ffa_ch11_tuning_a... 41 procedure FFA_FZ_Mux(X : in FZ; Y : in FZ;
ffa_ch11_tuning_a... 42 Result : out FZ; Sel : in WBool) is
ffa_ch11_tuning_a... 43 begin
ffa_ch11_tuning_a... 44 FZ_Basic.FZ_Mux(X => X, Y => Y, Result => Result, Sel => Sel);
ffa_ch11_tuning_a... 45 end FFA_FZ_Mux;
ffa_ch11_tuning_a... 46
ffa_ch11_tuning_a... 47 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 48 --- Bitwise Operations on FZ
ffa_ch11_tuning_a... 49 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 50
ffa_ch11_tuning_a... 51 -- Result := X & Y
ffa_ch11_tuning_a... 52 procedure FFA_FZ_And(X : in FZ; Y : in FZ; Result : out FZ) is
ffa_ch11_tuning_a... 53 begin
ffa_ch11_tuning_a... 54 FZ_BitOp.FZ_And(X => X, Y => Y, Result => Result);
ffa_ch11_tuning_a... 55 end FFA_FZ_And;
ffa_ch11_tuning_a... 56
ffa_ch11_tuning_a... 57 -- Result := X | Y
ffa_ch11_tuning_a... 58 procedure FFA_FZ_Or(X : in FZ; Y : in FZ; Result : out FZ) is
ffa_ch11_tuning_a... 59 begin
ffa_ch11_tuning_a... 60 FZ_BitOp.FZ_Or(X => X, Y => Y, Result => Result);
ffa_ch11_tuning_a... 61 end FFA_FZ_Or;
ffa_ch11_tuning_a... 62
ffa_ch11_tuning_a... 63 -- Result := X ^ Y
ffa_ch11_tuning_a... 64 procedure FFA_FZ_Xor(X : in FZ; Y : in FZ; Result : out FZ) is
ffa_ch11_tuning_a... 65 begin
ffa_ch11_tuning_a... 66 FZ_BitOp.FZ_Xor(X => X, Y => Y, Result => Result);
ffa_ch11_tuning_a... 67 end FFA_FZ_Xor;
ffa_ch11_tuning_a... 68
ffa_ch11_tuning_a... 69 -- NotN := ~N ('ones complement')
ffa_ch11_tuning_a... 70 procedure FFA_FZ_Not(N : in FZ; NotN : out FZ) is
ffa_ch11_tuning_a... 71 begin
ffa_ch11_tuning_a... 72 FZ_BitOp.FZ_Not(N => N, NotN => NotN);
ffa_ch11_tuning_a... 73 end FFA_FZ_Not;
ffa_ch11_tuning_a... 74
ffa_ch11_tuning_a... 75 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 76 --- Arithmetic on FZ
ffa_ch11_tuning_a... 77 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 78
ffa_ch11_tuning_a... 79 -- Sum := X + Y; Overflow := Carry
ffa_ch11_tuning_a... 80 procedure FFA_FZ_Add(X : in FZ;
ffa_ch11_tuning_a... 81 Y : in FZ;
ffa_ch11_tuning_a... 82 Sum : out FZ;
ffa_ch11_tuning_a... 83 Overflow : out WBool) is
ffa_ch11_tuning_a... 84 begin
ffa_ch11_tuning_a... 85 FZ_Arith.FZ_Add(X => X, Y => Y, Sum => Sum, Overflow => Overflow);
ffa_ch11_tuning_a... 86 end FFA_FZ_Add;
ffa_ch11_tuning_a... 87
ffa_ch11_tuning_a... 88 -- Difference := X - Y; Underflow := Borrow
ffa_ch11_tuning_a... 89 procedure FFA_FZ_Subtract(X : in FZ;
ffa_ch11_tuning_a... 90 Y : in FZ;
ffa_ch11_tuning_a... 91 Difference : out FZ;
ffa_ch11_tuning_a... 92 Underflow : out WBool) is
ffa_ch11_tuning_a... 93 begin
ffa_ch11_tuning_a... 94 FZ_Arith.FZ_Sub(X => X, Y => Y, Difference => Difference,
ffa_ch11_tuning_a... 95 Underflow => Underflow);
ffa_ch11_tuning_a... 96 end FFA_FZ_Subtract;
ffa_ch11_tuning_a... 97
ffa_ch11_tuning_a... 98 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 99 --- Multiplication on FZ
ffa_ch11_tuning_a... 100 ----------------------------------------------------------------------------
ffa_ch11_tuning_a... 101
ffa_ch11_tuning_a... 102 procedure FFA_FZ_Multiply(X : in FZ;
ffa_ch11_tuning_a... 103 Y : in FZ;
ffa_ch11_tuning_a... 104 XY_Lo : out FZ;
ffa_ch11_tuning_a... 105 XY_Hi : out FZ) is
ffa_ch11_tuning_a... 106 begin
ffa_ch11_tuning_a... 107 FZ_Mul.FZ_Multiply_Buffered(X => X, Y => Y,
ffa_ch11_tuning_a... 108 XY_Lo => XY_Lo, XY_Hi => XY_Hi);
ffa_ch11_tuning_a... 109 end FFA_FZ_Multiply;
ffa_ch11_tuning_a... 110
ffa_ch12_karatsub... 111
ffa_ch12_karatsub... 112 -- Square. Preserves the inputs.
ffa_ch12_karatsub... 113 procedure FFA_FZ_Square(X : in FZ;
ffa_ch12_karatsub... 114 XX_Lo : out FZ;
ffa_ch12_karatsub... 115 XX_Hi : out FZ) is
ffa_ch12_karatsub... 116 begin
ffa_ch12_karatsub... 117 FZ_Sqr.FZ_Square_Buffered(X => X, XX_Lo => XX_Lo, XX_Hi => XX_Hi);
ffa_ch12_karatsub... 118 end FFA_FZ_Square;
ffa_ch12_karatsub... 119
ffa_ch11_tuning_a... 120 end FFA;