3
3
% Licensed under the Apache License, Version 2.0 (the "License");
4
4
% you may not use this file except in compliance with the License.
5
5
% You may obtain a copy of the License at
6
- %
6
+ %
7
7
% http://www.apache.org/licenses/LICENSE-2.0
8
- %
8
+ %
9
9
% Unless required by applicable law or agreed to in writing, software
10
10
% distributed under the License is distributed on an "AS IS" BASIS,
11
11
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
% See the License for the specific language governing permissions and
13
13
% limitations under the License.
14
14
15
- % % NOTE: This module implements finite field arithmetic over the galois field
16
- % GF(256) with a specified prime modulus.
15
+ % % NOTE: This module implements finite field arithmetic over the galois field
16
+ % GF(256) with a specified prime modulus.
17
17
18
18
-module (gf256 ).
19
19
26
26
% UNUSED
27
27
% -record(gf256poly, {field, coefficients}).
28
28
% NOTE: Implementation and use are greatly simplified by expressing polynomials
29
- % simply as lists of coefficient values, rather than explicit reification of
29
+ % simply as lists of coefficient values, rather than explicit reification of
30
30
% polynomial "objects".
31
31
32
32
-define (RANGE , 255 ).
@@ -36,7 +36,7 @@ field(PrimeModulus) ->
36
36
Exponent = exponent_table (1 , PrimeModulus , []),
37
37
Log = log_table (Exponent , 1 , [0 ]),
38
38
# gf256 {exponent = Exponent , log = Log }.
39
- %
39
+ %
40
40
exponent_table (X , Modulus , Acc ) when length (Acc ) =< ? RANGE ->
41
41
case X bsl 1 of
42
42
V when V > ? RANGE ->
@@ -47,7 +47,7 @@ exponent_table(X, Modulus, Acc) when length(Acc) =< ?RANGE ->
47
47
exponent_table (X0 , Modulus , [X |Acc ]);
48
48
exponent_table (_ , _ , Acc ) ->
49
49
lists :reverse (Acc ).
50
- %
50
+ %
51
51
log_table (E , Count , Acc ) when Count =< ? RANGE ->
52
52
X = index_of (Count , 0 , E ),
53
53
log_table (E , Count + 1 , [X |Acc ]);
@@ -59,7 +59,7 @@ index_of(X, Count, [X|_]) ->
59
59
index_of (X , Count , [_ |T ]) ->
60
60
index_of (X , Count + 1 , T ).
61
61
62
- % %
62
+ % %
63
63
add (# gf256 {}, A , B ) when is_integer (A ), is_integer (B ) ->
64
64
A bxor B ;
65
65
add (# gf256 {}, [0 ], B ) when is_list (B ) ->
@@ -68,7 +68,7 @@ add(#gf256{}, A, [0]) when is_list(A) ->
68
68
A ;
69
69
add (F = # gf256 {}, A , B ) when is_list (A ), is_list (B ) ->
70
70
add (F , lists :reverse (A ), lists :reverse (B ), []).
71
-
71
+
72
72
add (F , [H |T ], [H0 |T0 ], Acc ) ->
73
73
add (F , T , T0 , [H bxor H0 | Acc ]);
74
74
add (F , [H |T ], [], Acc ) ->
@@ -83,10 +83,10 @@ subtract(F = #gf256{}, A, B) ->
83
83
add (F , A , B ).
84
84
85
85
% %
86
- multiply (# gf256 {}, 1 , B ) ->
87
- B ;
88
- multiply (# gf256 {}, A , 1 ) ->
89
- A ;
86
+ multiply (# gf256 {}, 0 , _ ) ->
87
+ 0 ;
88
+ multiply (# gf256 {}, _ , 0 ) ->
89
+ 0 ;
90
90
multiply (F = # gf256 {}, A , B ) ->
91
91
X = (log (F , A ) + log (F , B )) rem ? RANGE ,
92
92
exponent (F , X ).
@@ -98,7 +98,7 @@ exponent(#gf256{exponent = E}, X) ->
98
98
% %
99
99
log (# gf256 {log = L }, X ) ->
100
100
lists :nth (X + 1 , L ).
101
-
101
+
102
102
% %
103
103
inverse (F = # gf256 {}, X ) ->
104
104
exponent (F , ? RANGE - log (F , X )).
@@ -127,7 +127,7 @@ monomial(#gf256{}, Coeff, Degree) when Degree >= 0 ->
127
127
% %
128
128
monomial_product (F , Poly , Coeff , Degree ) ->
129
129
monomial_product (F , Poly , Coeff , Degree , []).
130
- %
130
+ %
131
131
monomial_product (F , [H |T ], C , D , Acc ) ->
132
132
P = gf256 :multiply (F , H , C ),
133
133
monomial_product (F , T , C , D , [P |Acc ]);
@@ -143,7 +143,7 @@ polynomial_product(_, _, [0]) ->
143
143
[0 ];
144
144
polynomial_product (F , P0 , P1 ) ->
145
145
polynomial_product0 (F , P0 , P1 , [], []).
146
- %
146
+ %
147
147
polynomial_product0 (F , [H |T ], P1 , P2 , Acc ) ->
148
148
[H0 |T0 ] = polynomial_product1 (F , H , P1 , P2 , []),
149
149
polynomial_product0 (F , T , P1 , T0 , [H0 |Acc ]);
0 commit comments