From 8ddaf58a3af627a1d97113eb8f8b2454493f77e0 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 11 Feb 2019 01:43:47 +0000 Subject: [PATCH] Add OutputInputByteTableCodingLoop class. --- .../Claunia.ReedSolomon.csproj | 1 + Claunia.ReedSolomon/CodingLoopBase.cs | 4 +- .../OutputInputByteTableCodingLoop.cs | 82 +++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 Claunia.ReedSolomon/OutputInputByteTableCodingLoop.cs diff --git a/Claunia.ReedSolomon/Claunia.ReedSolomon.csproj b/Claunia.ReedSolomon/Claunia.ReedSolomon.csproj index 7950f4a..cc69839 100644 --- a/Claunia.ReedSolomon/Claunia.ReedSolomon.csproj +++ b/Claunia.ReedSolomon/Claunia.ReedSolomon.csproj @@ -53,6 +53,7 @@ + diff --git a/Claunia.ReedSolomon/CodingLoopBase.cs b/Claunia.ReedSolomon/CodingLoopBase.cs index ce3a177..f008c3c 100644 --- a/Claunia.ReedSolomon/CodingLoopBase.cs +++ b/Claunia.ReedSolomon/CodingLoopBase.cs @@ -25,12 +25,12 @@ namespace Claunia.ReedSolomon [SuppressMessage("ReSharper", "InconsistentNaming")] public static readonly ICodingLoop[] ALL_CODING_LOOPS = { - /*new ByteInputOutputExpCodingLoop(), new ByteInputOutputTableCodingLoop(), + new ByteInputOutputExpCodingLoop(), new ByteInputOutputTableCodingLoop(), new ByteOutputInputExpCodingLoop(), new ByteOutputInputTableCodingLoop(), new InputByteOutputExpCodingLoop(), new InputByteOutputTableCodingLoop(), new InputOutputByteExpCodingLoop(), new InputOutputByteTableCodingLoop(), new OutputByteInputExpCodingLoop(), new OutputByteInputTableCodingLoop(), - new OutputInputByteExpCodingLoop(), new OutputInputByteTableCodingLoop()*/ + new OutputInputByteExpCodingLoop(), new OutputInputByteTableCodingLoop() }; public abstract void CodeSomeShards(byte[][] matrixRows, byte[][] inputs, int inputCount, byte[][] outputs, diff --git a/Claunia.ReedSolomon/OutputInputByteTableCodingLoop.cs b/Claunia.ReedSolomon/OutputInputByteTableCodingLoop.cs new file mode 100644 index 0000000..f296b4f --- /dev/null +++ b/Claunia.ReedSolomon/OutputInputByteTableCodingLoop.cs @@ -0,0 +1,82 @@ +/** + * One specific ordering/nesting of the coding loops. + * + * Copyright 2015, Backblaze, Inc. All rights reserved. + * Copyright © 2019 Natalia Portillo + */ + +namespace Claunia.ReedSolomon +{ + public class OutputInputByteTableCodingLoop : CodingLoopBase + { + public override void CodeSomeShards(byte[][] matrixRows, byte[][] inputs, int inputCount, byte[][] outputs, + int outputCount, int offset, int byteCount) + { + byte[][] table = Galois.MULTIPLICATION_TABLE; + + for(int iOutput = 0; iOutput < outputCount; iOutput++) + { + byte[] outputShard = outputs[iOutput]; + byte[] matrixRow = matrixRows[iOutput]; + + { + int iInput = 0; + byte[] inputShard = inputs[iInput]; + byte[] multTableRow = table[matrixRow[iInput] & 0xFF]; + + for(int iByte = offset; iByte < offset + byteCount; iByte++) + outputShard[iByte] = multTableRow[inputShard[iByte] & 0xFF]; + } + + for(int iInput = 1; iInput < inputCount; iInput++) + { + byte[] inputShard = inputs[iInput]; + byte[] multTableRow = table[matrixRow[iInput] & 0xFF]; + + for(int iByte = offset; iByte < offset + byteCount; iByte++) + outputShard[iByte] ^= multTableRow[inputShard[iByte] & 0xFF]; + } + } + } + + public override bool CheckSomeShards(byte[][] matrixRows, byte[][] inputs, int inputCount, byte[][] toCheck, + int checkCount, int offset, int byteCount, byte[] tempBuffer) + { + if(tempBuffer == null) + return base.CheckSomeShards(matrixRows, inputs, inputCount, toCheck, checkCount, offset, byteCount, + null); + + byte[][] table = Galois.MULTIPLICATION_TABLE; + + for(int iOutput = 0; iOutput < checkCount; iOutput++) + { + byte[] outputShard = toCheck[iOutput]; + byte[] matrixRow = matrixRows[iOutput]; + + { + int iInput = 0; + byte[] inputShard = inputs[iInput]; + byte[] multTableRow = table[matrixRow[iInput] & 0xFF]; + + for(int iByte = offset; iByte < offset + byteCount; iByte++) + tempBuffer[iByte] = multTableRow[inputShard[iByte] & 0xFF]; + } + + for(int iInput = 1; iInput < inputCount; iInput++) + { + byte[] inputShard = inputs[iInput]; + byte[] multTableRow = table[matrixRow[iInput] & 0xFF]; + + for(int iByte = offset; iByte < offset + byteCount; iByte++) + tempBuffer[iByte] ^= multTableRow[inputShard[iByte] & 0xFF]; + } + + for(int iByte = offset; iByte < offset + byteCount; iByte++) + if(tempBuffer[iByte] != outputShard[iByte]) + return false; + } + + return true; + } + } +} \ No newline at end of file