Issue #2 - correct resolution to RFC3339 date encoding

Reverts provisional commit and applies the correct fixes to gf256 math
module. This resolves incorrect ECC generation for a large number of
cases where the most significant polynomial in a remainder resolves to
0 at any point during the generation of intermediate remainder values.
This commit is contained in:
Steven Charles Davis 2014-01-20 11:45:31 -06:00
parent 0408ab8857
commit 596656536a
2 changed files with 7 additions and 22 deletions

View File

@ -75,8 +75,6 @@ add(F, [H|T], [], Acc) ->
add(F, T, [], [H|Acc]); add(F, T, [], [H|Acc]);
add(F, [], [H|T], Acc) -> add(F, [], [H|T], Acc) ->
add(F, [], T, [H|Acc]); add(F, [], T, [H|Acc]);
add(F, [], [], [0|Acc]) ->
add(F, [], [], Acc);
add(_, [], [], Acc) -> add(_, [], [], Acc) ->
Acc. Acc.
@ -85,17 +83,13 @@ subtract(F = #gf256{}, A, B) ->
add(F, A, B). add(F, A, B).
%% %%
multiply(#gf256{}, 0, _) ->
0;
multiply(#gf256{}, _, 0) ->
0;
multiply(#gf256{}, 1, B) -> multiply(#gf256{}, 1, B) ->
B; B;
multiply(#gf256{}, A, 1) -> multiply(#gf256{}, A, 1) ->
A; A;
multiply(#gf256{exponent = E, log = L}, A, B) -> multiply(F = #gf256{}, A, B) ->
X = (lists:nth(A + 1, L) + lists:nth(B + 1, L)) rem ?RANGE, X = (log(F, A) + log(F, B)) rem ?RANGE,
lists:nth(X + 1, E). exponent(F, X).
%% %%
exponent(#gf256{exponent = E}, X) -> exponent(#gf256{exponent = E}, X) ->
@ -106,8 +100,8 @@ log(#gf256{log = L}, X) ->
lists:nth(X + 1, L). lists:nth(X + 1, L).
%% %%
inverse(#gf256{exponent = E, log = L}, X) -> inverse(F = #gf256{}, X) ->
lists:nth(256 - lists:nth(X + 1, L), E). exponent(F, ?RANGE - log(F, X)).
%% %%
value(#gf256{}, Poly, 0) -> value(#gf256{}, Poly, 0) ->
@ -131,8 +125,6 @@ monomial(#gf256{}, Coeff, Degree) when Degree >= 0 ->
[Coeff|lists:duplicate(Degree, 0)]. [Coeff|lists:duplicate(Degree, 0)].
%% %%
monomial_product(#gf256{}, _, 0, _) ->
[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, []).
% %
@ -184,9 +176,7 @@ divide(F, IDLT, B, Q, R = [H|_]) when length(R) >= length(B), R =/= [0] ->
M = monomial(F, Scale, Diff), M = monomial(F, Scale, Diff),
Q0 = add(F, Q, M), Q0 = add(F, Q, M),
Coeffs = monomial_product(F, B, Scale, Diff), Coeffs = monomial_product(F, B, Scale, Diff),
R0 = add(F, R, Coeffs), [_|R0] = add(F, R, Coeffs),
divide(F, IDLT, B, Q0, R0); divide(F, IDLT, B, Q0, R0);
divide(_, _, _, Q, R) -> divide(_, _, _, Q, R) ->
{Q, R}. {Q, R}.

View File

@ -25,14 +25,9 @@ encode(Bin, Degree) when Degree > 0 ->
Data = binary_to_list(Bin), Data = binary_to_list(Bin),
Coeffs = gf256:monomial_product(Field, Data, 1, Degree), Coeffs = gf256:monomial_product(Field, Data, 1, Degree),
{_Quotient, Remainder} = gf256:divide(Field, Coeffs, Generator), {_Quotient, Remainder} = gf256:divide(Field, Coeffs, Generator),
Remainder0 = zero_pad(Degree, Remainder), ErrorCorrectionBytes = list_to_binary(Remainder),
ErrorCorrectionBytes = list_to_binary(Remainder0),
<<ErrorCorrectionBytes/binary>>. <<ErrorCorrectionBytes/binary>>.
zero_pad(Length, R) when length(R) < Length ->
zero_pad(Length, [0|R]);
zero_pad(_, R) ->
R.
%% %%
bch_code(Byte, Poly) -> bch_code(Byte, Poly) ->
MSB = msb(Poly), MSB = msb(Poly),