Skip to content

Instantly share code, notes, and snippets.

@lepinekong
Last active January 4, 2018 14:04
Show Gist options
  • Save lepinekong/dcde4b5f4b36b5e9c04d4b11ad1d177c to your computer and use it in GitHub Desktop.
Save lepinekong/dcde4b5f4b36b5e9c04d4b11ad1d177c to your computer and use it in GitHub Desktop.
basse58.hexa
Red [
Title: "Base58 hexa"
Uses: https://gist.github.com/lepinekong/4fd51a2897b81f81d1257d5f694bdc7a
Description: {
This implementation deals with division in hexadecimal
as Red doesn't currently support Big Integer. For example:
to-integer #{48656C6C6F} ; -> 1214606444 BUG
should have been 310939249775 https://www.binaryhexconverter.com/hex-to-decimal-converter
to-binary 310939249775 -> #{4252195B1B1BC000} BUG
}
]
if exists? %.system.utils.debug.red [
do %.system.utils.debug.red
]
do %base58.lib.red
base58-alphabet: ["1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" "G" "H" "J" "K" "L" "M" "N" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"]
div58: func[hex-quotient-string /local quotient r1 r2][
?? "div58"
quotient: to-integer/hex hex-quotient-string
?? quotient
r1: divide quotient 58
?? r1
r2: mod quotient 58
?? r2
r1-hex: to-string/hex r1
?? r1-hex
r2-hex: to-string/hex r2
?? r2-hex
return reduce [r1-hex r2-hex]
]
set-dividend-splitted: func[dividend /local dividend-hex][
dividend-hex: to-string dividend
?? dividend-hex
dividend-hex: split/segment dividend-hex "" 6 ; ["48656C" "6C6F"]
?? dividend-hex
return dividend-hex
]
; div58-block/nested "436174"
div58-block: func[quotient-splitted [string! block!] /nested /local j
result-div58 remainder next-quotient-splitted quotient n string-base58][
?? "div58-block"
j: 0
?? quotient-splitted
if ((type? quotient-splitted) = string!) [
quotient-splitted: append copy [] quotient-splitted
]
foreach partial-quotient quotient-splitted [; block: "48656C" next round block: "6C6F"
j: j + 1
?? (rejoin [tab tab "start " j "." "partial-quotient: " partial-quotient])
result-div58: div58 partial-quotient
?? result-div58
remainder: result-div58/2
?? remainder
if nested [
?? j
?? quotient-splitted
n: length? quotient-splitted
?? n
either (j < n) [
print "j < n"
next-quotient-splitted: pick quotient-splitted (j + 1)
?? next-quotient-splitted
quotient: rejoin [remainder next-quotient-splitted]
?? quotient
next-quotient-splitted: set-dividend-splitted quotient
?? next-quotient-splitted
div58-block next-quotient-splitted
][
?? (rejoin ["NOT j < n " mold quotient-splitted])
]
]
append/only results-div58 result-div58
?? results-div58
?? (rejoin [tab tab "end " j "." "partial-quotient: " partial-quotient])
if nested [
if ((to-integer/hex result-div58/1) > 57) [
?? "next-quotient-splitted > 57"
next-quotient-splitted: result-div58/1
?? next-quotient-splitted
next-quotient-splitted: set-dividend-splitted next-quotient-splitted
?? next-quotient-splitted
div58-block/nested next-quotient-splitted
]
]
return results-div58
]
]
base58: func[ string "436174 for Cat" /local results-div58 string-base58 entry quotient quotient-splitted][
__TRACING__: true
system/words/results-div58: copy []
entry: to-string/hex to-binary string
??/start entry
string-base58: copy ""
results-div58: copy []
quotient: entry
quotient-splitted: set-dividend-splitted quotient
results-div58: div58-block/nested quotient-splitted
__TRACING__: true
?? results-div58
last-block: last results-div58
if ((to-integer/hex last-block/1) > 0) [
append/only results-div58 compose ["00" (last-block/1)]
]
?? results-div58
reverse results-div58
?? results-div58
string-base58: copy ""
foreach block results-div58 [
index: (block/2)
?? index
index: (to-integer/hex index) + 1
?? index
append string-base58 pick base58-alphabet index
]
?? string-base58
?? results-div58
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment