question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BigInteger RLP bug

See original GitHub issue
@Test
    fun testBigIntRLP() {
        val bigInt = BigInteger.valueOf(54408193066555392L)
        val rlp = bigInt.toRLP()
        println(rlp.bytes.size)
        val bitInt1 = rlp.toBigIntegerFromRLP()
        Assert.assertEquals(bigInt, bitInt1)
}
7

java.lang.AssertionError: 
Expected :54408193066555392
Actual   :-17649400971372544

same as #49

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ligicommented, Dec 27, 2018

I think I will go this way for now. So it is more correct - but we loose one feature for now. Did not yet find a way to support signed bigintegers - happy about ideas there.

0reactions
ligicommented, Dec 27, 2018

thanks for the follow-up. Yea unsigned solves the problem - but still not yet sure how a good fix looks like. Perhaps just allowing unsigned … So I would propose this:

diff --git a/rlp/src/main/kotlin/org/kethereum/functions/rlp/RLPTypeConverter.kt b/rlp/src/main/kotlin/org/kethereum/functions/rlp/RLPTypeConverter.kt
index d7da0ab..936409a 100644
--- a/rlp/src/main/kotlin/org/kethereum/functions/rlp/RLPTypeConverter.kt
+++ b/rlp/src/main/kotlin/org/kethereum/functions/rlp/RLPTypeConverter.kt
@@ -24,7 +24,6 @@ fun RLPElement.toIntFromRLP() = bytes
         .mapIndexed { index, byte -> (byte.toInt() and 0xff).shl((bytes.size - 1 - index) * 8) }
         .reduce { acc, i -> acc + i }
 
-fun RLPElement.toBigIntegerFromRLP(): BigInteger = if (bytes.isEmpty()) ZERO else BigInteger(bytes)
 fun RLPElement.toUnsignedBigIntegerFromRLP(): BigInteger = if (bytes.isEmpty()) ZERO else BigInteger(1, bytes)
 fun RLPElement.toByteFromRLP(): Byte {
     if (bytes.size != 1) {
diff --git a/rlp/src/test/kotlin/org/kethereum/functions/rlp/TheRLPTypeConverter.kt b/rlp/src/test/kotlin/org/kethereum/functions/rlp/TheRLPTypeConverter.kt
index 1d7dc3d..46b04cc 100644
--- a/rlp/src/test/kotlin/org/kethereum/functions/rlp/TheRLPTypeConverter.kt
+++ b/rlp/src/test/kotlin/org/kethereum/functions/rlp/TheRLPTypeConverter.kt
@@ -2,6 +2,7 @@ package org.kethereum.functions.rlp
 
 import org.assertj.core.api.Assertions.assertThat
 import org.junit.jupiter.api.Test
+import org.walleth.khex.toHexString
 import java.math.BigInteger
 import java.math.BigInteger.*
 
@@ -14,11 +15,12 @@ class TheRLPTypeConverter {
             assertThat(it.toRLP().toIntFromRLP()).isEqualTo(it)
         }
 
-        assertThat(ZERO.toRLP().toBigIntegerFromRLP()).isEqualTo(ZERO)
-        assertThat(ONE.toRLP().toBigIntegerFromRLP()).isEqualTo(ONE)
-        assertThat(TEN.toRLP().toBigIntegerFromRLP()).isEqualTo(TEN)
-        assertThat(BigInteger.valueOf(Long.MAX_VALUE).toRLP().toBigIntegerFromRLP())
-                .isEqualTo(BigInteger.valueOf(Long.MAX_VALUE))
+        arrayOf(ZERO, ONE, TEN,  BigInteger.valueOf(70_000),BigInteger.valueOf(Long.MAX_VALUE),BigInteger.valueOf(54408193066555392L) ).forEach {
+            assertThat(it.toRLP().toUnsignedBigIntegerFromRLP()).isEqualTo(it)
+        }

what do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

A big bug in TransactionEncode.encode()!!! #562 - GitHub
public static RlpString create(BigInteger value) { // RLP encoding only supports positive integer values if (value.signum() < 1) { return ...
Read more >
rlp - Go Packages
If the RLP string is larger than the bit size of the type, decoding will return an error. Decode also supports *big.Int. There...
Read more >
rlp::DecoderError - Rust - Docs.rs
Error concerning the RLP decoder. Variants. RlpIsTooBig. [−] Expand description. Data has additional bytes at the end of the valid ...
Read more >
net.consensys.cava.rlp.RLPReader.readValue java code examples ...
Read a UInt256 value from the RLP source. isLenient. Determine if this reader is lenient by default. A non-lenient reader will throw InvalidRLPEncodingEx....
Read more >
rlp - crates.io: Rust Package Registry
rlp. Recursive-length prefix encoding, decoding, and compression ... Pure Rust implementation of a big integer library which has been designed from the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found