]> gitweb.factorcode.org Git - factor.git/commitdiff
base58: adding base58 encoding (used by bitcoin).
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 4 Dec 2020 18:32:13 +0000 (10:32 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 4 Dec 2020 18:32:35 +0000 (10:32 -0800)
extra/base58/authors.txt [new file with mode: 0644]
extra/base58/base58-tests.factor [new file with mode: 0644]
extra/base58/base58.factor [new file with mode: 0644]
extra/base58/summary.txt [new file with mode: 0644]

diff --git a/extra/base58/authors.txt b/extra/base58/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/base58/base58-tests.factor b/extra/base58/base58-tests.factor
new file mode 100644 (file)
index 0000000..90a4643
--- /dev/null
@@ -0,0 +1,25 @@
+USING: base58 math.parser strings tools.test ;
+
+{ "" } [ "" >base58 >string ] unit-test
+{ "" } [ "" base58> >string ] unit-test
+
+{ "1" } [ B{ 0 } >base58 >string ] unit-test
+{ B{ 0 } } [ "1" base58> ] unit-test
+
+{ "11" } [ B{ 0 0 } >base58 >string ] unit-test
+{ B{ 0 0 } } [ "11" base58> ] unit-test
+
+{ "111233QC4" }
+[ "000000287fb4cd" hex-string>bytes >base58 >string ] unit-test
+
+{ "Hello World!" }
+[ "2NEpo7TZRRrLZSi2U" base58> >string ] unit-test
+
+{ "2NEpo7TZRRrLZSi2U" }
+[ "Hello World!" >base58 >string ] unit-test
+
+{ "The quick brown fox jumps over the lazy dog." }
+[ "USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z" base58> >string ] unit-test
+
+{ "USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z" }
+[ "The quick brown fox jumps over the lazy dog." >base58 >string ] unit-test
diff --git a/extra/base58/base58.factor b/extra/base58/base58.factor
new file mode 100644 (file)
index 0000000..ee2124e
--- /dev/null
@@ -0,0 +1,44 @@
+! Copyright (C) 2020 John Benediktsson.
+! See http://factorcode.org/license.txt for BSD license.
+
+USING: base64.private byte-arrays io.binary kernel
+kernel.private literals math sequences ;
+
+IN: base58
+
+ERROR: malformed-base58 ;
+
+<PRIVATE
+
+<<
+CONSTANT: alphabet $[
+    "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
+    >byte-array
+]
+>>
+
+PRIVATE>
+
+: ch>base58 ( ch -- ch )
+    alphabet nth ; inline
+
+: base58>ch ( ch -- ch )
+    $[ alphabet alphabet-inverse ] nth
+    [ malformed-base58 ] unless* { fixnum } declare ; inline
+
+:: >base58 ( seq -- base58 )
+    BV{ } clone :> accum
+    seq [ zero? not ] find [ drop seq length ] unless :> i
+    seq i tail-slice be>
+    [ dup zero? ] [ 58 /mod ch>base58 accum push ] until drop
+    i alphabet first '[ _ accum push ] times
+    accum reverse! B{ } like ;
+
+:: base58> ( base58 -- seq )
+    BV{ } clone :> accum
+    base58 alphabet first '[ _ = not ] find
+    [ drop base58 length ] unless :> i
+    0 base58 [ [ 58 * ] dip base58>ch + ] i each-from
+    [ dup zero? ] [ 256 /mod accum push ] until drop
+    i [ 0 accum push ] times
+    accum reverse! B{ } like ;
diff --git a/extra/base58/summary.txt b/extra/base58/summary.txt
new file mode 100644 (file)
index 0000000..e295a25
--- /dev/null
@@ -0,0 +1 @@
+Base58 encoding/decoding