Debian Codegraph @

gf256/blog_test.go

raw symbols SCIP
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // This file contains a straightforward implementation of
6 // Reed-Solomon encoding, along with a benchmark.
7 // It goes with http://research.swtch.com/field.
8 //
9 // For an optimized implementation, see gf256.go.
10
11 package gf256
12
13 import (
14 "bytes"
15 "fmt"
16 "testing"
17 )
18
19 // BlogECC writes to check the error correcting code bytes
20 // for data using the given Reed-Solomon parameters.
21 func BlogECC(rs *RSEncoder, m []byte, check []byte) {
22 if len(check) < rs.c {
23 panic("gf256: invalid check byte length")
24 }
25 if rs.c == 0 {
26 return
27 }
28
29 // The check bytes are the remainder after dividing
30 // data padded with c zeros by the generator polynomial.
31
32 // p = data padded with c zeros.
33 var p []byte
34 n := len(m) + rs.c
35 if len(rs.p) >= n {
36 p = rs.p
37 } else {
38 p = make([]byte, n)
39 }
40 copy(p, m)
41 for i := len(m); i < len(p); i++ {
42 p[i] = 0
43 }
44
45 gen := rs.gen
46
47 // Divide p by gen, leaving the remainder in p[len(data):].
48 // p[0] is the most significant term in p, and
49 // gen[0] is the most significant term in the generator.
50 for i := 0; i < len(m); i++ {
51 k := f.Mul(p[i], f.Inv(gen[0])) // k = pi / g0
52 // p -= k·g
53 for j, g := range gen {
54 p[i+j] = f.Add(p[i+j], f.Mul(k, g))
55 }
56 }
57
58 copy(check, p[len(m):])
59 rs.p = p
60 }
61
62 func BenchmarkBlogECC(b *testing.B) {
63 data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
64 check := []byte{0x29, 0x41, 0xb3, 0x93, 0x8, 0xe8, 0xa3, 0xe7, 0x63, 0x8f}
65 out := make([]byte, len(check))
66 rs := NewRSEncoder(f, len(check))
67 for i := 0; i < b.N; i++ {
68 BlogECC(rs, data, out)
69 }
70 b.SetBytes(int64(len(data)))
71 if !bytes.Equal(out, check) {
72 fmt.Printf("have %#v want %#v\n", out, check)
73 }
74 }
75
76 func TestBlogECC(t *testing.T) {
77 data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
78 check := []byte{0xa5, 0x24, 0xd4, 0xc1, 0xed, 0x36, 0xc7, 0x87, 0x2c, 0x55}
79 out := make([]byte, len(check))
80 rs := NewRSEncoder(f, len(check))
81 BlogECC(rs, data, out)
82 if !bytes.Equal(out, check) {
83 t.Errorf("have %x want %x", out, check)
84 }
85 }