Resolution to #7

Fault in gf256 multiply function.
Atom removed a bunch of trailing spaces, so these are in here too.
This commit is contained in:
Steven Charles Davis 2015-12-23 15:06:14 -06:00
parent 9da3a8d73c
commit 6eb5d48faf

View File

@ -3,17 +3,17 @@
% Licensed under the Apache License, Version 2.0 (the "License"); % Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License. % you may not use this file except in compliance with the License.
% You may obtain a copy of the License at % You may obtain a copy of the License at
% %
% http://www.apache.org/licenses/LICENSE-2.0 % http://www.apache.org/licenses/LICENSE-2.0
% %
% Unless required by applicable law or agreed to in writing, software % Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, % distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and % See the License for the specific language governing permissions and
% limitations under the License. % limitations under the License.
%% NOTE: This module implements finite field arithmetic over the galois field %% NOTE: This module implements finite field arithmetic over the galois field
% GF(256) with a specified prime modulus. % GF(256) with a specified prime modulus.
-module(gf256). -module(gf256).
@ -26,7 +26,7 @@
% UNUSED % UNUSED
%-record(gf256poly, {field, coefficients}). %-record(gf256poly, {field, coefficients}).
% NOTE: Implementation and use are greatly simplified by expressing polynomials % NOTE: Implementation and use are greatly simplified by expressing polynomials
% simply as lists of coefficient values, rather than explicit reification of % simply as lists of coefficient values, rather than explicit reification of
% polynomial "objects". % polynomial "objects".
-define(RANGE, 255). -define(RANGE, 255).
@ -36,7 +36,7 @@ field(PrimeModulus) ->
Exponent = exponent_table(1, PrimeModulus, []), Exponent = exponent_table(1, PrimeModulus, []),
Log = log_table(Exponent, 1, [0]), Log = log_table(Exponent, 1, [0]),
#gf256{exponent = Exponent, log = Log}. #gf256{exponent = Exponent, log = Log}.
% %
exponent_table(X, Modulus, Acc) when length(Acc) =< ?RANGE -> exponent_table(X, Modulus, Acc) when length(Acc) =< ?RANGE ->
case X bsl 1 of case X bsl 1 of
V when V > ?RANGE -> V when V > ?RANGE ->
@ -47,7 +47,7 @@ exponent_table(X, Modulus, Acc) when length(Acc) =< ?RANGE ->
exponent_table(X0, Modulus, [X|Acc]); exponent_table(X0, Modulus, [X|Acc]);
exponent_table(_, _, Acc) -> exponent_table(_, _, Acc) ->
lists:reverse(Acc). lists:reverse(Acc).
% %
log_table(E, Count, Acc) when Count =< ?RANGE -> log_table(E, Count, Acc) when Count =< ?RANGE ->
X = index_of(Count, 0, E), X = index_of(Count, 0, E),
log_table(E, Count + 1, [X|Acc]); log_table(E, Count + 1, [X|Acc]);
@ -59,7 +59,7 @@ index_of(X, Count, [X|_]) ->
index_of(X, Count, [_|T]) -> index_of(X, Count, [_|T]) ->
index_of(X, Count + 1, T). index_of(X, Count + 1, T).
%% %%
add(#gf256{}, A, B) when is_integer(A), is_integer(B) -> add(#gf256{}, A, B) when is_integer(A), is_integer(B) ->
A bxor B; A bxor B;
add(#gf256{}, [0], B) when is_list(B) -> add(#gf256{}, [0], B) when is_list(B) ->
@ -68,7 +68,7 @@ add(#gf256{}, A, [0]) when is_list(A) ->
A; A;
add(F = #gf256{}, A, B) when is_list(A), is_list(B) -> add(F = #gf256{}, A, B) when is_list(A), is_list(B) ->
add(F, lists:reverse(A), lists:reverse(B), []). add(F, lists:reverse(A), lists:reverse(B), []).
add(F, [H|T], [H0|T0], Acc) -> add(F, [H|T], [H0|T0], Acc) ->
add(F, T, T0, [H bxor H0 | Acc]); add(F, T, T0, [H bxor H0 | Acc]);
add(F, [H|T], [], Acc) -> add(F, [H|T], [], Acc) ->
@ -83,10 +83,10 @@ subtract(F = #gf256{}, A, B) ->
add(F, A, B). add(F, A, B).
%% %%
multiply(#gf256{}, 1, B) -> multiply(#gf256{}, 0, _) ->
B; 0;
multiply(#gf256{}, A, 1) -> multiply(#gf256{}, _, 0) ->
A; 0;
multiply(F = #gf256{}, A, B) -> multiply(F = #gf256{}, A, B) ->
X = (log(F, A) + log(F, B)) rem ?RANGE, X = (log(F, A) + log(F, B)) rem ?RANGE,
exponent(F, X). exponent(F, X).
@ -98,7 +98,7 @@ exponent(#gf256{exponent = E}, X) ->
%% %%
log(#gf256{log = L}, X) -> log(#gf256{log = L}, X) ->
lists:nth(X + 1, L). lists:nth(X + 1, L).
%% %%
inverse(F = #gf256{}, X) -> inverse(F = #gf256{}, X) ->
exponent(F, ?RANGE - log(F, X)). exponent(F, ?RANGE - log(F, X)).
@ -127,7 +127,7 @@ monomial(#gf256{}, Coeff, Degree) when Degree >= 0 ->
%% %%
monomial_product(F, Poly, Coeff, Degree) -> monomial_product(F, Poly, Coeff, Degree) ->
monomial_product(F, Poly, Coeff, Degree, []). monomial_product(F, Poly, Coeff, Degree, []).
% %
monomial_product(F, [H|T], C, D, Acc) -> monomial_product(F, [H|T], C, D, Acc) ->
P = gf256:multiply(F, H, C), P = gf256:multiply(F, H, C),
monomial_product(F, T, C, D, [P|Acc]); monomial_product(F, T, C, D, [P|Acc]);
@ -143,7 +143,7 @@ polynomial_product(_, _, [0]) ->
[0]; [0];
polynomial_product(F, P0, P1) -> polynomial_product(F, P0, P1) ->
polynomial_product0(F, P0, P1, [], []). polynomial_product0(F, P0, P1, [], []).
% %
polynomial_product0(F, [H|T], P1, P2, Acc) -> polynomial_product0(F, [H|T], P1, P2, Acc) ->
[H0|T0] = polynomial_product1(F, H, P1, P2, []), [H0|T0] = polynomial_product1(F, H, P1, P2, []),
polynomial_product0(F, T, P1, T0, [H0|Acc]); polynomial_product0(F, T, P1, T0, [H0|Acc]);