diff --git a/src/Nethermind/Nethermind.Crypto/KzgPolynomialCommitments.cs b/src/Nethermind/Nethermind.Crypto/KzgPolynomialCommitments.cs index d6abf80311d..d9c06b978fd 100644 --- a/src/Nethermind/Nethermind.Crypto/KzgPolynomialCommitments.cs +++ b/src/Nethermind/Nethermind.Crypto/KzgPolynomialCommitments.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0-only using System; +using System.Buffers; using System.IO; using System.Security.Cryptography; using System.Threading; @@ -90,15 +91,23 @@ public static bool VerifyProof(ReadOnlySpan commitment, ReadOnlySpan public static bool AreProofsValid(byte[][] blobs, byte[][] commitments, byte[][] proofs) { - byte[] flatBlobs = new byte[blobs.Length * Ckzg.Ckzg.BytesPerBlob]; - byte[] flatCommitments = new byte[blobs.Length * Ckzg.Ckzg.BytesPerCommitment]; - byte[] flatProofs = new byte[blobs.Length * Ckzg.Ckzg.BytesPerProof]; + var length = blobs.Length * Ckzg.Ckzg.BytesPerBlob; + byte[] flatBlobsArray = ArrayPool.Shared.Rent(length); + var flatBlobs = new Span(flatBlobsArray, 0, length); + + length = blobs.Length * Ckzg.Ckzg.BytesPerCommitment; + byte[] flatCommitmentsArray = ArrayPool.Shared.Rent(length); + var flatCommitments = new Span(flatCommitmentsArray, 0, length); + + length = blobs.Length * Ckzg.Ckzg.BytesPerProof; + byte[] flatProofsArray = ArrayPool.Shared.Rent(length); + var flatProofs = new Span(flatProofsArray, 0, length); for (int i = 0; i < blobs.Length; i++) { - Array.Copy(blobs[i], 0, flatBlobs, i * Ckzg.Ckzg.BytesPerBlob, Ckzg.Ckzg.BytesPerBlob); - Array.Copy(commitments[i], 0, flatCommitments, i * Ckzg.Ckzg.BytesPerCommitment, Ckzg.Ckzg.BytesPerCommitment); - Array.Copy(proofs[i], 0, flatProofs, i * Ckzg.Ckzg.BytesPerProof, Ckzg.Ckzg.BytesPerProof); + blobs[i].CopyTo(flatBlobs.Slice(i * Ckzg.Ckzg.BytesPerBlob, Ckzg.Ckzg.BytesPerBlob)); + commitments[i].CopyTo(flatCommitments.Slice(i * Ckzg.Ckzg.BytesPerCommitment, Ckzg.Ckzg.BytesPerCommitment)); + proofs[i].CopyTo(flatProofs.Slice(i * Ckzg.Ckzg.BytesPerProof, Ckzg.Ckzg.BytesPerProof)); } try @@ -110,10 +119,16 @@ public static bool AreProofsValid(byte[][] blobs, byte[][] commitments, byte[][] { return false; } + finally + { + ArrayPool.Shared.Return(flatBlobsArray); + ArrayPool.Shared.Return(flatCommitmentsArray); + ArrayPool.Shared.Return(flatProofsArray); + } } /// - /// Method to genereate correct data for tests only, not safe + /// Method to generate correct data for tests only, not safe /// public static void KzgifyBlob(ReadOnlySpan blob, Span commitment, Span proof, Span hashV1) {