Skip to content

Commit 5966565

Browse files
committed
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.
1 parent 0408ab8 commit 5966565

File tree

2 files changed

+7
-22
lines changed

2 files changed

+7
-22
lines changed

src/gf256.erl

+6-16
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ add(F, [H|T], [], Acc) ->
7575
add(F, T, [], [H|Acc]);
7676
add(F, [], [H|T], Acc) ->
7777
add(F, [], T, [H|Acc]);
78-
add(F, [], [], [0|Acc]) ->
79-
add(F, [], [], Acc);
8078
add(_, [], [], Acc) ->
8179
Acc.
8280

@@ -85,17 +83,13 @@ subtract(F = #gf256{}, A, B) ->
8583
add(F, A, B).
8684

8785
%%
88-
multiply(#gf256{}, 0, _) ->
89-
0;
90-
multiply(#gf256{}, _, 0) ->
91-
0;
9286
multiply(#gf256{}, 1, B) ->
9387
B;
9488
multiply(#gf256{}, A, 1) ->
9589
A;
96-
multiply(#gf256{exponent = E, log = L}, A, B) ->
97-
X = (lists:nth(A + 1, L) + lists:nth(B + 1, L)) rem ?RANGE,
98-
lists:nth(X + 1, E).
90+
multiply(F = #gf256{}, A, B) ->
91+
X = (log(F, A) + log(F, B)) rem ?RANGE,
92+
exponent(F, X).
9993

10094
%%
10195
exponent(#gf256{exponent = E}, X) ->
@@ -106,8 +100,8 @@ log(#gf256{log = L}, X) ->
106100
lists:nth(X + 1, L).
107101

108102
%%
109-
inverse(#gf256{exponent = E, log = L}, X) ->
110-
lists:nth(256 - lists:nth(X + 1, L), E).
103+
inverse(F = #gf256{}, X) ->
104+
exponent(F, ?RANGE - log(F, X)).
111105

112106
%%
113107
value(#gf256{}, Poly, 0) ->
@@ -131,8 +125,6 @@ monomial(#gf256{}, Coeff, Degree) when Degree >= 0 ->
131125
[Coeff|lists:duplicate(Degree, 0)].
132126

133127
%%
134-
monomial_product(#gf256{}, _, 0, _) ->
135-
[0];
136128
monomial_product(F, Poly, Coeff, Degree) ->
137129
monomial_product(F, Poly, Coeff, Degree, []).
138130
%
@@ -184,9 +176,7 @@ divide(F, IDLT, B, Q, R = [H|_]) when length(R) >= length(B), R =/= [0] ->
184176
M = monomial(F, Scale, Diff),
185177
Q0 = add(F, Q, M),
186178
Coeffs = monomial_product(F, B, Scale, Diff),
187-
R0 = add(F, R, Coeffs),
179+
[_|R0] = add(F, R, Coeffs),
188180
divide(F, IDLT, B, Q0, R0);
189181
divide(_, _, _, Q, R) ->
190182
{Q, R}.
191-
192-

src/qrcode_reedsolomon.erl

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,9 @@ encode(Bin, Degree) when Degree > 0 ->
2525
Data = binary_to_list(Bin),
2626
Coeffs = gf256:monomial_product(Field, Data, 1, Degree),
2727
{_Quotient, Remainder} = gf256:divide(Field, Coeffs, Generator),
28-
Remainder0 = zero_pad(Degree, Remainder),
29-
ErrorCorrectionBytes = list_to_binary(Remainder0),
28+
ErrorCorrectionBytes = list_to_binary(Remainder),
3029
<<ErrorCorrectionBytes/binary>>.
3130

32-
zero_pad(Length, R) when length(R) < Length ->
33-
zero_pad(Length, [0|R]);
34-
zero_pad(_, R) ->
35-
R.
3631
%%
3732
bch_code(Byte, Poly) ->
3833
MSB = msb(Poly),

0 commit comments

Comments
 (0)