Skip to content

Commit

Permalink
Added constant for single byte prefix content length in RLP
Browse files Browse the repository at this point in the history
  • Loading branch information
damian-orzechowski committed Sep 18, 2024
1 parent 2206fa9 commit f4da5bc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Paprika/Merkle/ComputeMerkleBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Paprika/RLP/Rlp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -47,7 +48,7 @@ public static int LengthOf(ReadOnlySpan<byte> span)
return 1;
}

if (span.Length < 56)
if (span.Length < SmallPrefixBarrier)
{
return span.Length + 1;
}
Expand All @@ -57,7 +58,7 @@ public static int LengthOf(ReadOnlySpan<byte> span)

public static int LengthOfSequence(int contentLength)
{
if (contentLength < 56)
if (contentLength < SmallPrefixBarrier)
{
return 1 + contentLength;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Paprika/RLP/RlpStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public RlpStream(Span<byte> data)

public void StartSequence(int contentLength)
{
if (contentLength < 56)
if (contentLength < Rlp.SmallPrefixBarrier)
{
byte prefix = (byte)(192 + contentLength);
WriteByte(prefix);
Expand Down Expand Up @@ -72,7 +72,7 @@ public void Encode(scoped ReadOnlySpan<byte> input)
{
WriteByte(input[0]);
}
else if (input.Length < 56)
else if (input.Length < Rlp.SmallPrefixBarrier)
{
byte smallPrefix = (byte)(input.Length + 128);
WriteByte(smallPrefix);
Expand Down

0 comments on commit f4da5bc

Please sign in to comment.