Satoshi Compact Varint CVARINT

4 replies 180 views
tom2017Member
Posts: 17 · Reputation: 230
#1May 28, 2022, 05:24 AM
Hey there, I need my transformation to be exactly reversible. I’m using Satoshi's compact CVarint (not Varint) for this. I’ve got a Python version of it, but it looks like it’s generating some characters that aren’t in the ASCII range, so I can't really check it. The serialize.h from back in the day doesn’t cover this completely, so any guidance or resources would be super helpful! Here’s the code I’m working with: def encode_camount(amount): # CAmount transformation if amount == 0: transformed_value = 0 else: e = 0 while amount % 10 == 0 and e < 9: amount //= 10 e += 1 if e < 9: d = amount % 10 n = amount // 10 transformed_value = 1 + 10 * (9 * n + d 1) + e else: transformed_value = 1 + 10 * (amount 1) + 9 # MSB base-128 encoding encoded_bytes = [] while transformed_value > 0: byte = transformed_value & 0x7F transformed_value >>= 7 if transformed_value > 0: byte |= 0x80 byte -= 1 encoded_bytes.insert(0, byte) return encoded_bytes def decode_camount(encoded_bytes): # Decode the variable-length integer decoded_value = 0 for i, byte in enumerate(reversed(encoded_bytes)): if i > 0: decoded_value += (byte + 1) * (128 ** i) else: decoded_value += byte # Reverse the CAmount transformation if decoded_value == 0: return 0 e = decoded_value % 10 decoded_value = (decoded_value 1) // 10 if e < 9: n = decoded_value // 9 d = decoded_value % 9 + 1 original_amount = n * 10 + d
2 Reply Quote Share
Posts: 35 · Reputation: 189
#2May 28, 2022, 09:39 AM
You could try encode the input. Since the ord function returns the unicode point for the given charater though if your outside the range for ASCII then it might not fit in a single byte. What you can do is try encode the string like UTF-8 before processing it. Keep in mind if you modify the encode decode function to accpet the unicode points you may need to transform the scheme.
4 Reply Quote Share
tom2017Member
Posts: 17 · Reputation: 230
#3May 28, 2022, 11:41 AM
some merit there, did that already but I have DM 'ed the string to test,  see if you can work it out?
0 Reply Quote Share
LuckyCoinLegendary
Posts: 832 · Reputation: 4795
#4May 28, 2022, 12:50 PM
There is a CVarint type used somewhere in Bitcoin code or is this something you made? I am aware of a class called "CVarint" in the Bitcoin Core codebase but that is just an implementation of varint. I've never heard of compact varint type though. From googling just now, there is a compact varint type outside of Bitcoin though (https://github.com/protocolbuffers/protobuf/issues/4376), that serializes arbitrary-sized integers but uses more space. Are you referring to that by any chance?
0 Reply Quote Share
tom2017Member
Posts: 17 · Reputation: 230
#5May 28, 2022, 02:25 PM
Thx for replying, sorry I missed your reply as I have been away. I have mastered VARINT now and get it , but there was in the past more to this, especially a printed ASCII variant translation of Blockchain data. I am now drilling down to understanding FDF%F%F% etc found in Satoshi ascii text scripts of blockchain data from 2007-2010. Once I can resolve "F%" then I may be closer to understanding my project. Thx again
4 Reply Quote Share

Related topics