Skip to content

Instantly share code, notes, and snippets.

@assafshomer
Last active August 29, 2015 14:22
Show Gist options
  • Save assafshomer/9c35623a732ee8689924 to your computer and use it in GitHub Desktop.
Save assafshomer/9c35623a732ee8689924 to your computer and use it in GitHub Desktop.
Colored Coins Precision Encoding Scheme
require 'terminal-table'
MAX_SATOSHIS = 21*(10**6)*(10**8)
def max_num(mantis_bits,available_bits)
mantis(mantis_bits)*(10**exponent(mantis_bits,available_bits))
end
def mantis(mantis_bits)
2**mantis_bits
end
def exponent(mantis_bits,available_bits)
2**(available_bits-mantis_bits)
end
def threshold_mantis?(mantis_bits,available_bits)
max_num(mantis_bits,available_bits) > MAX_SATOSHIS &&
max_num(mantis_bits+1,available_bits) < MAX_SATOSHIS
end
table = Terminal::Table.new do |t|
t.title = "Encoding for maximum #{MAX_SATOSHIS} Satoshis"
t << ['Bytes','Bits','Available','Mantisa','Digits', 'Exponent']
t.add_separator
mantis_bits = 0
(2..10).each do |total_bytes|
total_bits = 8*total_bytes
available_bits = total_bits -3
until threshold_mantis?(mantis_bits,available_bits) do
break if mantis_bits == available_bits -1
mantis_bits +=1
end
significant = (2**mantis_bits).to_s.size-1
t<<[total_bytes,total_bits, available_bits, mantis_bits,significant, available_bits - mantis_bits]
end
end
puts table
=begin
+-------+------+-----------+---------+--------+----------+
| Encoding for maximum 2100000000000000 Satoshis |
+-------+------+-----------+---------+--------+----------+
| Bytes | Bits | Available | Mantisa | Digits | Exponent |
+-------+------+-----------+---------+--------+----------+
| 2 | 16 | 13 | 9 | 2 | 4 |
| 3 | 24 | 21 | 17 | 5 | 4 |
| 4 | 32 | 29 | 26 | 7 | 3 |
| 5 | 40 | 37 | 34 | 10 | 3 |
| 6 | 48 | 45 | 43 | 12 | 2 |
| 7 | 56 | 53 | 52 | 15 | 1 |
| 8 | 64 | 61 | 60 | 18 | 1 |
| 9 | 72 | 69 | 68 | 20 | 1 |
| 10 | 80 | 77 | 76 | 22 | 1 |
+-------+------+-----------+---------+--------+----------+
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment