use a static buffer for big endian

This commit is contained in:
chirag-parmar 2024-12-19 10:42:38 +05:30
parent 01125d3775
commit 20b8ee7acc
1 changed files with 11 additions and 14 deletions

View File

@ -12,6 +12,7 @@ type
listLengths*: seq[int] listLengths*: seq[int]
prefixLengths*: seq[int] prefixLengths*: seq[int]
listCount: int listCount: int
bigEndianBuf: array[8, byte]
template update(writer: var RlpHashWriter, data: byte) = template update(writer: var RlpHashWriter, data: byte) =
writer.keccak.update([data]) writer.keccak.update([data])
@ -19,6 +20,11 @@ template update(writer: var RlpHashWriter, data: byte) =
template update(writer: var RlpHashWriter, data: openArray[byte]) = template update(writer: var RlpHashWriter, data: openArray[byte]) =
writer.keccak.update(data) writer.keccak.update(data)
template updateBigEndian(writer: var RlpHashWriter, i: SomeUnsignedInt,
length: int) =
writer.bigEndianBuf.writeBigEndian(i, length - 1, length)
writer.update(writer.bigEndianBuf.toOpenArray(0, length - 1))
func writeCount(writer: var RlpHashWriter, count: int, baseMarker: byte) = func writeCount(writer: var RlpHashWriter, count: int, baseMarker: byte) =
if count < THRESHOLD_LIST_LEN: if count < THRESHOLD_LIST_LEN:
writer.update(baseMarker + byte(count)) writer.update(baseMarker + byte(count))
@ -27,10 +33,7 @@ func writeCount(writer: var RlpHashWriter, count: int, baseMarker: byte) =
writer.update baseMarker + (THRESHOLD_LIST_LEN - 1) + byte(lenPrefixBytes) writer.update baseMarker + (THRESHOLD_LIST_LEN - 1) + byte(lenPrefixBytes)
var buf = newSeqOfCap[byte](lenPrefixBytes) writer.updateBigEndian(uint64(count), lenPrefixBytes)
buf.setLen(lenPrefixBytes)
buf.writeBigEndian(uint64(count), buf.len - 1, lenPrefixBytes)
writer.update(buf)
func writeInt*(writer: var RlpHashWriter, i: SomeUnsignedInt) = func writeInt*(writer: var RlpHashWriter, i: SomeUnsignedInt) =
if i == typeof(i)(0): if i == typeof(i)(0):
@ -41,10 +44,7 @@ func writeInt*(writer: var RlpHashWriter, i: SomeUnsignedInt) =
let bytesNeeded = i.bytesNeeded let bytesNeeded = i.bytesNeeded
writer.writeCount(bytesNeeded, BLOB_START_MARKER) writer.writeCount(bytesNeeded, BLOB_START_MARKER)
var buf = newSeqOfCap[byte](bytesNeeded) writer.updateBigEndian(uint64(i), bytesNeeded)
buf.setLen(bytesNeeded)
buf.writeBigEndian(uint64(i), buf.len - 1, bytesNeeded)
writer.update(buf)
template appendRawBytes*(self: var RlpHashWriter, bytes: openArray[byte]) = template appendRawBytes*(self: var RlpHashWriter, bytes: openArray[byte]) =
self.update(bytes) self.update(bytes)
@ -72,10 +72,7 @@ proc startList*(self: var RlpHashWriter, listSize: int) =
let listLenBytes = prefixLen - 1 let listLenBytes = prefixLen - 1
self.update(LEN_PREFIXED_LIST_MARKER + byte(listLenBytes)) self.update(LEN_PREFIXED_LIST_MARKER + byte(listLenBytes))
var buf = newSeqOfCap[byte](listLenBytes) self.updateBigEndian(uint64(listLen), listLenBytes)
buf.setLen(listLenBytes)
buf.writeBigEndian(uint64(listLen), buf.len - 1, listLenBytes)
self.update(buf)
template finish*(self: var RlpHashWriter): MDigest[self.keccak.bits] = template finish*(self: var RlpHashWriter): MDigest[self.keccak.bits] =
self.listLengths.setLen(0) self.listLengths.setLen(0)