From f4da5bcde79ddf17c4eb2f70c41f55bcd6320f68 Mon Sep 17 00:00:00 2001 From: Damian Orzechowski Date: Wed, 18 Sep 2024 09:53:52 +0200 Subject: [PATCH] Added constant for single byte prefix content length in RLP --- src/Paprika/Merkle/ComputeMerkleBehavior.cs | 2 +- src/Paprika/RLP/Rlp.cs | 5 +++-- src/Paprika/RLP/RlpStream.cs | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Paprika/Merkle/ComputeMerkleBehavior.cs b/src/Paprika/Merkle/ComputeMerkleBehavior.cs index c1a2908d..bdaf61b1 100644 --- a/src/Paprika/Merkle/ComputeMerkleBehavior.cs +++ b/src/Paprika/Merkle/ComputeMerkleBehavior.cs @@ -650,7 +650,7 @@ private void EncodeBranch(scoped in Key key, scoped in ComputeContext ctx, scope var end = stream.Position; var actualLength = end - initialShift; var lengthOfLength = Rlp.LengthOfLength(actualLength); - if (actualLength >= 56) //to match StartSequence + if (actualLength >= Rlp.SmallPrefixBarrier) //to match StartSequence lengthOfLength++; var from = initialShift - lengthOfLength; stream.Position = from; diff --git a/src/Paprika/RLP/Rlp.cs b/src/Paprika/RLP/Rlp.cs index 1dadffad..e853e699 100644 --- a/src/Paprika/RLP/Rlp.cs +++ b/src/Paprika/RLP/Rlp.cs @@ -11,6 +11,7 @@ public static class Rlp { public const int LengthOfKeccakRlp = 33; public const int MaxLengthOfLength = 4; + public const int SmallPrefixBarrier = 56; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LengthOf(in UInt256 item) @@ -47,7 +48,7 @@ public static int LengthOf(ReadOnlySpan span) return 1; } - if (span.Length < 56) + if (span.Length < SmallPrefixBarrier) { return span.Length + 1; } @@ -57,7 +58,7 @@ public static int LengthOf(ReadOnlySpan span) public static int LengthOfSequence(int contentLength) { - if (contentLength < 56) + if (contentLength < SmallPrefixBarrier) { return 1 + contentLength; } diff --git a/src/Paprika/RLP/RlpStream.cs b/src/Paprika/RLP/RlpStream.cs index 645fc245..a164b77a 100644 --- a/src/Paprika/RLP/RlpStream.cs +++ b/src/Paprika/RLP/RlpStream.cs @@ -24,7 +24,7 @@ public RlpStream(Span data) public void StartSequence(int contentLength) { - if (contentLength < 56) + if (contentLength < Rlp.SmallPrefixBarrier) { byte prefix = (byte)(192 + contentLength); WriteByte(prefix); @@ -72,7 +72,7 @@ public void Encode(scoped ReadOnlySpan input) { WriteByte(input[0]); } - else if (input.Length < 56) + else if (input.Length < Rlp.SmallPrefixBarrier) { byte smallPrefix = (byte)(input.Length + 128); WriteByte(smallPrefix);