Reed-Solomon Golang テスト

序章

消去コード
リードソロモン

テスト

reedsolomonprojectの下にディレクトリがありexamples、その中のプログラムはテストに使用されます。
テスト プログラムをコンパイルします。

l@WIN10-901211241:reedsolomon$cd examples/
l@WIN10-901211241:examples$go build simple-encoder.go 
l@WIN10-901211241:examples$go build simple-decoder.go 

エンコード

シンプルエンコーダーテストプログラムのパラメーター:

パラメータ 説明
-データ データ ブロックの数を指定します。デフォルトは 4 です
-パー 冗長ブロックの数を指定します。デフォルトは 2 です
-外 分割後のファイル格納ディレクトリを指定
ファイル名 入力データのファイル名

テスト1

パラメータ設定 -data 4 -par 6

l@WIN10-901211241:examples$./simple-encoder -data 4 -par 6 -out . input
Opening input
File split into 10 data+parity shards with 358 bytes/shard.
Writing to input.0
Writing to input.1
Writing to input.2
Writing to input.3
Writing to input.4
Writing to input.5
Writing to input.6
Writing to input.7
Writing to input.8
Writing to input.9
l@WIN10-901211241:examples$ls -l input*
-rwxrwxrwx 1 luxq luxq 1432 Jul 15 15:25 input
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.0
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.1
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.2
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.3
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.4
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.5
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.6
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.7
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.8
-rwxrwxrwx 1 luxq luxq  358 Jul 15 15:52 input.9

元のファイル入力の長さは 1432 で、4 つのブロックに分割され、各ブロックの長さは 358 で、6 つの冗長ブロックが生成されます。
リードソロモン アルゴリズムでは、消去符号化されたデータの最初の M ブロックがファイルの元のデータであり、データの最後の N ブロックが冗長データです。
手動スプライシングは、元のデータと一致していることがわかります。

l@WIN10-901211241:examples$cat input.0 input.1 input.2 input.3 > restore
l@WIN10-901211241:examples$md5sum restore input
0076c987f6cc5e98919b0b9403b12b5f  restore
0076c987f6cc5e98919b0b9403b12b5f  input

テスト 2

パラメータ設定 -data 5 -par 4

l@WIN10-901211241:examples$./simple-encoder -data 5 -par 4 -out . input 
Opening input
File split into 9 data+parity shards with 287 bytes/shard.
Writing to input.0
Writing to input.1
Writing to input.2
Writing to input.3
Writing to input.4
Writing to input.5
Writing to input.6
Writing to input.7
Writing to input.8
l@WIN10-901211241:examples$ls -l input*
-rwxrwxrwx 1 luxq luxq 1432 Jul 15 15:25 input
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.0
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.1
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.2
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.3
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.4
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.5
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.6
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.7
-rwxrwxrwx 1 luxq luxq  287 Jul 15 15:33 input.8

元のファイル入力の長さは1432で、5ブロックに分割されています.長さが足りないため、5番目のブロックに3バイトの0データが埋められ、各ブロックは287の長さになり、4つの冗長ブロックが生成されます.

l@WIN10-901211241:examples$cat input.0 input.1 input.2 input.3 input.4 > restore 
l@WIN10-901211241:examples$ls -l restore input
-rwxrwxrwx 1 luxq luxq 1432 Jul 15 15:25 input
-rwxrwxrwx 1 luxq luxq 1435 Jul 15 15:43 restore

テスト 3

パラメータ設定 -data 3 -par 3、エンコードを 2 回繰り返して、エンコード結果がまったく同じかどうかを確認します。

l@WIN10-901211241:examples$./simple-encoder -data 3 -par 3 -out . input 
l@WIN10-901211241:examples$md5sum input*
0076c987f6cc5e98919b0b9403b12b5f  input
8b552e186349f17138b13115380e06a8  input.0
ec2696ca31e2fb44fc4e5145f4a4ba0f  input.1
c1b87d5e3a47b64375aa680e27e5126e  input.2
23b00199ea883baf56dd5237fe5d7200  input.3
a768cbeb3f6f4c74f1d4897e2e042137  input.4
42112dde5ae1f69497e481e3e9e139d7  input.5
l@WIN10-901211241:examples$rm input.*
l@WIN10-901211241:examples$./simple-encoder -data 3 -par 3 -out . input 
l@WIN10-901211241:examples$md5sum input*
0076c987f6cc5e98919b0b9403b12b5f  input
8b552e186349f17138b13115380e06a8  input.0
ec2696ca31e2fb44fc4e5145f4a4ba0f  input.1
c1b87d5e3a47b64375aa680e27e5126e  input.2
23b00199ea883baf56dd5237fe5d7200  input.3
a768cbeb3f6f4c74f1d4897e2e042137  input.4
42112dde5ae1f69497e481e3e9e139d7  input.5

同じデータとパラメータを入力すると、エンコードされたデータも同じになります。

デコード

simple-decoder テスト プログラムのパラメーター:

パラメータ 説明
-データ データ ブロックの数を指定します。デフォルトは 4 です
-パー 冗長ブロックの数を指定します。デフォルトは 2 です
-外 復元したファイル名を指定
ファイル名.拡張子 入力データ ブロックのファイル名プレフィックス

テスト1

パラメータ設定 -data 4 -par 6
期待値: 任意の 6 つ未満のデータ ブロックを削除すると、元のデータを復元できます

極端な場合、4 つの元のデータ ブロックを削除する input.0 input.1 input.2 input.3

l@WIN10-901211241:examples$rm input.0 input.1 input.2 input.3 
l@WIN10-901211241:examples$./simple-decoder -data 4 -par 6 -out restore input
Opening input.0
Error reading file open input.0: no such file or directory
Opening input.1
Error reading file open input.1: no such file or directory
Opening input.2
Error reading file open input.2: no such file or directory
Opening input.3
Error reading file open input.3: no such file or directory
Opening input.4
Opening input.5
Opening input.6
Opening input.7
Opening input.8
Opening input.9
Verification failed. Reconstructing data
Writing data to restore
l@WIN10-901211241:examples$md5sum input restore 
0076c987f6cc5e98919b0b9403b12b5f  input
0076c987f6cc5e98919b0b9403b12b5f  restore

データは正常に回復しました。

テスト 2

パラメータ設定 -data 5 -par 4
期待: 任意の 4 つ未満のデータ ブロックを削除すると、元のデータを復元できます

データブロック削除 input.0 input.1 input.2

l@WIN10-901211241:examples$rm input.0 input.1 input.2 
l@WIN10-901211241:examples$./simple-decoder -data 5 -par 4 -out restore input
Opening input.0
Error reading file open input.0: no such file or directory
Opening input.1
Error reading file open input.1: no such file or directory
Opening input.2
Error reading file open input.2: no such file or directory
Opening input.3
Opening input.4
Opening input.5
Opening input.6
Opening input.7
Opening input.8
Verification failed. Reconstructing data
Writing data to restore

を使用してファイルを比較するvimdiff input restoreと、復元されたファイルには、元のファイルよりも末尾に 3 バイト多くの 0 データがあることがわかります。これは、エンコード時にデータ長が断片化するのに十分でないため、正確に埋められたデータです。

ファイル比較

注: スライス時にアルゴリズムによってデータが埋められ、最終的に復元されたデータには埋められたデータが含まれます。呼び出し元は、事前に計算し、PKCS を使用して元のデータをパディングし、デコード後にパディングを削除して、完全な回復効果を実現できます。

おすすめ

転載: blog.csdn.net/mynameislu/article/details/107367285