From 753d4634f2089faf0d854bd44979271fca239acb Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 9 Apr 2024 22:51:59 +0400 Subject: [PATCH 1/2] verify encoding of shares in case when axis is fully reconstructed from orthogonal --- extendeddatacrossword.go | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/extendeddatacrossword.go b/extendeddatacrossword.go index dbd7621..7490791 100644 --- a/extendeddatacrossword.go +++ b/extendeddatacrossword.go @@ -138,10 +138,7 @@ func (eds *ExtendedDataSquare) solveCrosswordRow( // Prepare shares shares := make([][]byte, eds.width) - vectorData := eds.row(uint(r)) - for c := 0; c < int(eds.width); c++ { - shares[c] = vectorData[c] - } + copy(shares, eds.row(uint(r))) // Attempt rebuild the row rebuiltShares, isDecoded, err := eds.rebuildShares(shares) @@ -177,6 +174,10 @@ func (eds *ExtendedDataSquare) solveCrosswordRow( } return false, false, err } + + if eds.verifyEncoding(col, r, rebuiltShares[c]) != nil { + return false, false, &ErrByzantineData{Col, uint(c), col} + } } } @@ -211,10 +212,7 @@ func (eds *ExtendedDataSquare) solveCrosswordCol( // Prepare shares shares := make([][]byte, eds.width) - vectorData := eds.col(uint(c)) - for r := 0; r < int(eds.width); r++ { - shares[r] = vectorData[r] - } + copy(shares, eds.col(uint(c))) // Attempt rebuild rebuiltShares, isDecoded, err := eds.rebuildShares(shares) @@ -250,6 +248,10 @@ func (eds *ExtendedDataSquare) solveCrosswordCol( } return false, false, err } + + if eds.verifyEncoding(row, c, rebuiltShares[r]) != nil { + return false, false, &ErrByzantineData{Row, uint(r), row} + } } } @@ -468,3 +470,22 @@ func (eds *ExtendedDataSquare) computeSharesRootWithRebuiltShare(shares [][]byte } return tree.Root() } + +func (eds *ExtendedDataSquare) verifyEncoding(data [][]byte, rebuiltIndex int, rebuiltShare []byte) error { + data[rebuiltIndex] = rebuiltShare + half := len(data) / 2 + original := data[:half] + parity, err := eds.codec.Encode(original) + if err != nil { + return err + } + + for i := half; i < len(data); i++ { + if !bytes.Equal(data[i], parity[i-half]) { + data[rebuiltIndex] = nil + return errors.New("parity data does not match encoded data") + } + } + + return nil +} From ff65800c450f9ac90392abcec9f3e8b441dda14d Mon Sep 17 00:00:00 2001 From: Vlad Date: Sat, 13 Apr 2024 20:04:28 +0400 Subject: [PATCH 2/2] add comment for verifyEncoding --- extendeddatacrossword.go | 1 + 1 file changed, 1 insertion(+) diff --git a/extendeddatacrossword.go b/extendeddatacrossword.go index 7490791..7ff301c 100644 --- a/extendeddatacrossword.go +++ b/extendeddatacrossword.go @@ -471,6 +471,7 @@ func (eds *ExtendedDataSquare) computeSharesRootWithRebuiltShare(shares [][]byte return tree.Root() } +// verifyEncoding checks the Reed-Solomon encoding of the provided data. func (eds *ExtendedDataSquare) verifyEncoding(data [][]byte, rebuiltIndex int, rebuiltShare []byte) error { data[rebuiltIndex] = rebuiltShare half := len(data) / 2