Convert uint256 to compact encoding
(v)
| 316 | return v |
| 317 | |
| 318 | def compact_from_uint256(v): |
| 319 | """Convert uint256 to compact encoding |
| 320 | """ |
| 321 | nbytes = (v.bit_length() + 7) >> 3 |
| 322 | compact = 0 |
| 323 | if nbytes <= 3: |
| 324 | compact = (v & 0xFFFFFF) << 8 * (3 - nbytes) |
| 325 | else: |
| 326 | compact = v >> 8 * (nbytes - 3) |
| 327 | compact = compact & 0xFFFFFF |
| 328 | |
| 329 | # If the sign bit (0x00800000) is set, divide the mantissa by 256 and |
| 330 | # increase the exponent to get an encoding without it set. |
| 331 | if compact & 0x00800000: |
| 332 | compact >>= 8 |
| 333 | nbytes += 1 |
| 334 | |
| 335 | return compact | nbytes << 24 |
| 336 | |
| 337 | def uint256_to_str(u): |
| 338 | r = b"" |
no outgoing calls