Skip to content

Commit 6eb5d48

Browse files
committed
Resolution to #7
Fault in gf256 multiply function. Atom removed a bunch of trailing spaces, so these are in here too.
1 parent 9da3a8d commit 6eb5d48

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/gf256.erl

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
% Licensed under the Apache License, Version 2.0 (the "License");
44
% you may not use this file except in compliance with the License.
55
% You may obtain a copy of the License at
6-
%
6+
%
77
% http://www.apache.org/licenses/LICENSE-2.0
8-
%
8+
%
99
% Unless required by applicable law or agreed to in writing, software
1010
% distributed under the License is distributed on an "AS IS" BASIS,
1111
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
% See the License for the specific language governing permissions and
1313
% limitations under the License.
1414

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.
1717

1818
-module(gf256).
1919

@@ -26,7 +26,7 @@
2626
% UNUSED
2727
%-record(gf256poly, {field, coefficients}).
2828
% 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
3030
% polynomial "objects".
3131

3232
-define(RANGE, 255).
@@ -36,7 +36,7 @@ field(PrimeModulus) ->
3636
Exponent = exponent_table(1, PrimeModulus, []),
3737
Log = log_table(Exponent, 1, [0]),
3838
#gf256{exponent = Exponent, log = Log}.
39-
%
39+
%
4040
exponent_table(X, Modulus, Acc) when length(Acc) =< ?RANGE ->
4141
case X bsl 1 of
4242
V when V > ?RANGE ->
@@ -47,7 +47,7 @@ exponent_table(X, Modulus, Acc) when length(Acc) =< ?RANGE ->
4747
exponent_table(X0, Modulus, [X|Acc]);
4848
exponent_table(_, _, Acc) ->
4949
lists:reverse(Acc).
50-
%
50+
%
5151
log_table(E, Count, Acc) when Count =< ?RANGE ->
5252
X = index_of(Count, 0, E),
5353
log_table(E, Count + 1, [X|Acc]);
@@ -59,7 +59,7 @@ index_of(X, Count, [X|_]) ->
5959
index_of(X, Count, [_|T]) ->
6060
index_of(X, Count + 1, T).
6161

62-
%%
62+
%%
6363
add(#gf256{}, A, B) when is_integer(A), is_integer(B) ->
6464
A bxor B;
6565
add(#gf256{}, [0], B) when is_list(B) ->
@@ -68,7 +68,7 @@ add(#gf256{}, A, [0]) when is_list(A) ->
6868
A;
6969
add(F = #gf256{}, A, B) when is_list(A), is_list(B) ->
7070
add(F, lists:reverse(A), lists:reverse(B), []).
71-
71+
7272
add(F, [H|T], [H0|T0], Acc) ->
7373
add(F, T, T0, [H bxor H0 | Acc]);
7474
add(F, [H|T], [], Acc) ->
@@ -83,10 +83,10 @@ subtract(F = #gf256{}, A, B) ->
8383
add(F, A, B).
8484

8585
%%
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;
9090
multiply(F = #gf256{}, A, B) ->
9191
X = (log(F, A) + log(F, B)) rem ?RANGE,
9292
exponent(F, X).
@@ -98,7 +98,7 @@ exponent(#gf256{exponent = E}, X) ->
9898
%%
9999
log(#gf256{log = L}, X) ->
100100
lists:nth(X + 1, L).
101-
101+
102102
%%
103103
inverse(F = #gf256{}, X) ->
104104
exponent(F, ?RANGE - log(F, X)).
@@ -127,7 +127,7 @@ monomial(#gf256{}, Coeff, Degree) when Degree >= 0 ->
127127
%%
128128
monomial_product(F, Poly, Coeff, Degree) ->
129129
monomial_product(F, Poly, Coeff, Degree, []).
130-
%
130+
%
131131
monomial_product(F, [H|T], C, D, Acc) ->
132132
P = gf256:multiply(F, H, C),
133133
monomial_product(F, T, C, D, [P|Acc]);
@@ -143,7 +143,7 @@ polynomial_product(_, _, [0]) ->
143143
[0];
144144
polynomial_product(F, P0, P1) ->
145145
polynomial_product0(F, P0, P1, [], []).
146-
%
146+
%
147147
polynomial_product0(F, [H|T], P1, P2, Acc) ->
148148
[H0|T0] = polynomial_product1(F, H, P1, P2, []),
149149
polynomial_product0(F, T, P1, T0, [H0|Acc]);

0 commit comments

Comments
 (0)