Reed-Solomon error correction over GF(2^8)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

reed-solomon: add Gf256 tests and fuzz mli (E605)

Cover the GF(2^8) functor with property-based tests (additive identity,
commutativity, inverse roundtrip, pow boundaries, table consistency)
and add the missing fuzz module interface.

+111 -1
+4
fuzz/fuzz_reed_solomon.mli
··· 1 + (** Fuzz tests for {!Reed_solomon}. *) 2 + 3 + val suite : string * Alcobar.test_case list 4 + (** Test suite. *)
+1
test/dune
··· 1 1 (test 2 2 (name test) 3 + (modules test test_reed_solomon test_gf256) 3 4 (libraries reed_solomon alcotest))
+2 -1
test/test.ml
··· 1 - let () = Alcotest.run "reed-solomon" [ Test_reed_solomon.suite ] 1 + let () = 2 + Alcotest.run "reed-solomon" [ Test_reed_solomon.suite; Test_gf256.suite ]
+100
test/test_gf256.ml
··· 1 + (** Tests for [Gf256] — GF(2^8) finite field arithmetic. 2 + 3 + Vectors checked against the CCSDS 131.0-B-4 Annex F field (primitive 4 + polynomial x^8+x^7+x^2+x+1 = 0x187, primitive element 11). *) 5 + 6 + module F = Reed_solomon.Gf256.Make (struct 7 + let primitive_poly = 0x187 8 + let primitive_element = 11 9 + end) 10 + 11 + let add_self_zero () = 12 + for a = 0 to 255 do 13 + Alcotest.(check int) (Fmt.str "%d + %d = 0" a a) 0 (F.add a a) 14 + done 15 + 16 + let sub_equals_add () = 17 + for a = 0 to 255 do 18 + for b = 0 to 255 do 19 + Alcotest.check Alcotest.int (Fmt.str "sub %d %d" a b) (F.add a b) 20 + (F.sub a b) 21 + done 22 + done 23 + 24 + let mul_zero () = 25 + for a = 0 to 255 do 26 + Alcotest.(check int) (Fmt.str "0 * %d" a) 0 (F.mul 0 a); 27 + Alcotest.(check int) (Fmt.str "%d * 0" a) 0 (F.mul a 0) 28 + done 29 + 30 + let mul_one () = 31 + for a = 0 to 255 do 32 + Alcotest.(check int) (Fmt.str "1 * %d" a) a (F.mul 1 a); 33 + Alcotest.(check int) (Fmt.str "%d * 1" a) a (F.mul a 1) 34 + done 35 + 36 + let mul_commutative () = 37 + for a = 1 to 255 do 38 + for b = 1 to 255 do 39 + Alcotest.(check int) 40 + (Fmt.str "%d * %d = %d * %d" a b b a) 41 + (F.mul a b) (F.mul b a) 42 + done 43 + done 44 + 45 + let inv_roundtrip () = 46 + for a = 1 to 255 do 47 + let inv = F.inv a in 48 + Alcotest.(check int) (Fmt.str "%d * inv %d = 1" a a) 1 (F.mul a inv) 49 + done 50 + 51 + let inv_zero_raises () = 52 + Alcotest.check_raises "inv 0 raises" 53 + (Invalid_argument "Gf256.inv: cannot invert zero") (fun () -> 54 + ignore (F.inv 0)) 55 + 56 + let pow_zero_exponent () = 57 + for a = 0 to 255 do 58 + Alcotest.(check int) (Fmt.str "%d^0 = 1" a) 1 (F.pow a 0) 59 + done 60 + 61 + let pow_one_exponent () = 62 + for a = 0 to 255 do 63 + Alcotest.(check int) (Fmt.str "%d^1 = %d" a a) a (F.pow a 1) 64 + done 65 + 66 + let pow_negative () = 67 + for a = 1 to 255 do 68 + Alcotest.(check int) (Fmt.str "%d^-1 = inv %d" a a) (F.inv a) (F.pow a (-1)) 69 + done 70 + 71 + let exp_table_size () = 72 + Alcotest.(check int) "exp_table length" 512 (Array.length F.exp_table); 73 + Alcotest.(check int) "exp_table[0]" 1 F.exp_table.(0) 74 + 75 + let log_table_size () = 76 + Alcotest.(check int) "log_table length" 256 (Array.length F.log_table) 77 + 78 + let log_exp_inverse () = 79 + for i = 0 to 254 do 80 + let v = F.exp_table.(i) in 81 + Alcotest.(check int) (Fmt.str "log(exp(%d)) = %d" i i) i F.log_table.(v) 82 + done 83 + 84 + let suite = 85 + ( "gf256", 86 + [ 87 + Alcotest.test_case "add self yields zero" `Quick add_self_zero; 88 + Alcotest.test_case "sub equals add" `Quick sub_equals_add; 89 + Alcotest.test_case "mul by zero" `Quick mul_zero; 90 + Alcotest.test_case "mul by one" `Quick mul_one; 91 + Alcotest.test_case "mul commutative" `Quick mul_commutative; 92 + Alcotest.test_case "inv roundtrip" `Quick inv_roundtrip; 93 + Alcotest.test_case "inv 0 raises" `Quick inv_zero_raises; 94 + Alcotest.test_case "pow exponent 0" `Quick pow_zero_exponent; 95 + Alcotest.test_case "pow exponent 1" `Quick pow_one_exponent; 96 + Alcotest.test_case "pow negative" `Quick pow_negative; 97 + Alcotest.test_case "exp_table size" `Quick exp_table_size; 98 + Alcotest.test_case "log_table size" `Quick log_table_size; 99 + Alcotest.test_case "log/exp inverse" `Quick log_exp_inverse; 100 + ] )
+4
test/test_gf256.mli
··· 1 + (** Tests for [Gf256]. *) 2 + 3 + val suite : string * unit Alcotest.test_case list 4 + (** [suite] is the alcotest suite for the [Gf256] functor. *)