Bitcoin/public point to address: Difference between revisions
Crate ripemd160 is deprecated - Update
(Add Rust implementation) |
(Crate ripemd160 is deprecated - Update) |
||
(11 intermediate revisions by 4 users not shown) | |||
Line 22:
''Extra credit:'' add a verification procedure about the public point, making sure it belongs to the secp256k1 elliptic curve
=={{header|C}}==
<
#include <string.h>
#include <ctype.h>
Line 98 ⟶ 97:
0));
return 0;
}</
=={{header|Common Lisp}}==
{{libheader|Quicklisp}}
{{libheader|Ironclad}}
<
;;;; This is a revised version, inspired by a throwaway script originally
;;;; published at http://deedbot.org/bundle-381528.txt by the same Adlai.
Line 206 ⟶ 204:
(base58enc (checksum (sovcat '(0) (digest '(ripemd-160 sha256) point)))))))
</syntaxhighlight>
Here's an example of how to feed a point into the functions defined above:
<syntaxhighlight lang="text">
;; ? (pubkey-to-p2pkh
Line 218 ⟶ 216:
;; "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
</syntaxhighlight>
=={{header|D}}==
Requires the second D module from the SHA-256 task.
{{trans|C}}
{{trans|Go}}
<
// A Bitcoin public point.
Line 281 ⟶ 278:
p.bitcoinEncode.writeln;
}</
{{out}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
Line 290 ⟶ 287:
{{libheader| DCPripemd160}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Public_point_to_address;
Line 453 ⟶ 450:
writeln(a.ToBase58);
readln;
end.</
=={{header|Factor}}==
<
math sequences ;
IN: rosetta-code.bitcoin.point-address
Line 478 ⟶ 474:
dup 0 prefix btc-checksum
append 0 prefix >base58 ;
</syntaxhighlight>
{{out}}
<pre>
Line 485 ⟶ 481:
point>address . ! "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
</pre>
=={{header|Go}}==
{{libheader|Go sub-repositories}}
<
import (
Line 588 ⟶ 583:
// show base58 representation
fmt.Println(string(a.A58()))
}</
{{out}}
<pre>
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
=={{header|Haskell}}==
<
import Data.List (unfoldr)
import Data.Binary (Word8)
Line 615 ⟶ 609:
0x50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352
0x2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
</syntaxhighlight>
{{out}}
<pre>"6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
</pre>
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 625 ⟶ 618:
'''Main functions''':
<
function encodebase58(b::Vector{<:Integer})
out = Vector{Char}(34)
Line 652 ⟶ 645:
end
pubpoint2address(x::AbstractString, y::AbstractString) =
pubpoint2address(hex2bytes(x), hex2bytes(y))</
'''Main''':
<
y = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"
println(pubpoint2address(x, y)))</
{{out}}
<pre>6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
=={{header|Nim}}==
Line 672 ⟶ 664:
The “bignum” library is used to check if the public point belongs to the “secp256k1” elliptic curve.
<
import nimcrypto
import bignum
Line 739 ⟶ 731:
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6")
echo "Coordinates are valid."
echo "Address is: ", address</
{{out}}
<pre>Coordinates are valid.
Address is: 16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
=={{header|Perl}}==
Here we'll use the standard Digest::SHA module, and the CPAN-available Crypt::RIPEMD160 and Encode::Base58::GMP.
<
use Crypt::RIPEMD160;
use Digest::SHA qw(sha256);
Line 768 ⟶ 759:
'2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'
;
</syntaxhighlight>
{{out}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- no ripemd160.js as yet</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #7060A8;">sha256</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">ripemd160</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">b58</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">base58</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">out</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">25</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">34</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">25</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">*</span><span style="color: #000000;">256</span><span style="color: #0000FF;">+</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">/</span><span style="color: #000000;">58</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">58</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">out</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">b58</span><span style="color: #0000FF;">[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">out</span><span style="color: #0000FF;">[$]=</span><span style="color: #008000;">'1'</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">out</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">out</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">'1'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">out</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">out</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">out</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">coin_encode</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">32</span>
<span style="color: #008080;">or</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">32</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #008000;">"bad public point string"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\x04"</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">y</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">rmd</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'\0'</span><span style="color: #0000FF;">&</span><span style="color: #000000;">ripemd160</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sha256</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">rmd</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sha256</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sha256</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rmd</span><span style="color: #0000FF;">))[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">base58</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rmd</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">coin_encode</span><span style="color: #0000FF;">(</span>x"<span style="color: #0000FF;">50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352</span>"<span style="color: #0000FF;">,</span>
x"<span style="color: #0000FF;">2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6</span>"<span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
There is actually an sha256.js included, not that I recommend it.<br>
You could probably get this to work in a browser if you provide a suitable ripemd160.js and tweak p2js to use it.
{{out}}
<pre>
"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"
</pre>
=={{header|PicoLisp}}==
<
(load "sha256.l")
Line 859 ⟶ 854:
(point2address
"50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352"
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6" ) )</
{{out}}
<pre>"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"</pre>
=={{header|Python}}==
<
import binascii
Line 886 ⟶ 880:
print(public_point_to_address(
b'50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352',
b'2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'))</
{{out}}
<pre>
b'6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM'
</pre>
=={{header|Racket}}==
Uses code from [[SHA-256#Racket]] (which is isolated in a submodule).
<
(module sha256 racket/base
;; define a quick SH256 FFI interface, similar to the Racket's default
Line 1,015 ⟶ 1,008:
(bytes->HEX-STRING s7) => "D61967F6"
(bytes->HEX-STRING s8) => "00010966776006953D5567439E5E39F86A0D273BEED61967F6"
s9 => #"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"))</
{{out}}
Line 1,021 ⟶ 1,014:
1 test passed
8 tests passed</pre>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub dgst(blob8 $b, Str :$dgst) returns blob8 {
given run «openssl dgst "-$dgst" -binary», :in, :out, :bin {
.in.write: $b;
.in.close;
return .out.slurp;
}
}
sub sha256($b) { dgst $b, :dgst<sha256> }
sub rmd160($b) { dgst $b, :dgst<rmd160> }
sub public_point_to_address( UInt $x, UInt $y ) {
my @bytes = flat ($y,$x).map: *.polymod( 256 xx * )[^32];
my $hash = rmd160 sha256 blob8.new: 4, @bytes.reverse;
my $checksum = sha256(sha256 blob8.new: 0, $hash.list).subbuf: 0, 4;
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
>[ .polymod: 58 xx * ] given
reduce * * 256 + * , flat 0, ($hash, $checksum)».list
}
say public_point_to_address
0x50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352,
0x2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6;
</syntaxhighlight>
{{out}}
<pre>6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
=={{header|Ruby}}==
<
# Translate public point to Bitcoin address
#
Line 1,077 ⟶ 1,068:
end
puts res.reverse
</syntaxhighlight>
{{out}}
<pre>
6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
=={{header|Rust}}==
<
use ring::digest::{digest, SHA256};
use
use hex::FromHex;
Line 1,151 ⟶ 1,141:
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,165 ⟶ 1,155:
which encodes a string with the Base58 encoding used by Bitcoin. No external library is needed.
<
include "bytedata.s7i";
include "msgdigest.s7i";
Line 1,184 ⟶ 1,174:
writeln(publicPointToAddress("50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352",
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"));
end func;</
{{out}}
Line 1,190 ⟶ 1,180:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
=={{header|Tcl}}==
{{tcllib|ripemd160}}
{{tcllib|sha256}}
<
package require sha256
Line 1,222 ⟶ 1,211:
append addr [binary format "a4" [string range $hash 0 3]]
return [base58encode $addr]
}</
Demonstrating
<
0x50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352 \
0x2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6]</
{{out}}
<pre>
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
</pre>
=={{header|Wolfram Language}}/{{header|Mathematica}}==
<syntaxhighlight lang="mathematica">BlockchainKeyEncode[
PublicKey[
<|
"Type"->"EllipticCurve",
"PublicCurvePoint"-> {
16^^50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352,
16^^2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6
}
|>
],
"Address",
BlockchainBase-> "Bitcoin"
]</syntaxhighlight>
{{output}}
<pre>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-crypto}}
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./crypto" for Sha256, Ripemd160
import "./str" for Str
import "./fmt" for Conv
// converts an hexadecimal string to a byte list.
var HexToBytes = Fn.new { |s| Str.chunks(s, 2).map { |c| Conv.atoi(c, 16) }.toList }
// Point is a class for a bitcoin public point
class Point {
construct new() {
_x = List.filled(32, 0)
_y = List.filled(32, 0)
}
x { _x }
y { _y }
// setHex takes two hexadecimal strings and decodes them into the receiver.
setHex(s, t) {
if (s.count != 64 || t.count != 64) Fiber.abort("Invalid hex string length.")
_x = HexToBytes.call(s)
_y = HexToBytes.call(t)
}
}
// Represents a bitcoin address.
class Address {
static tmpl_ { "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".bytes }
construct new() {
_a = List.filled(25, 0)
}
a { _a }
// returns a base58 encoded bitcoin address corresponding to the receiver.
a58 {
var out = List.filled(34, 0)
for (n in 33..0) {
var c = 0
for (i in 0..24) {
c = c * 256 + _a[i]
_a[i] = (c/58).floor
c = c % 58
}
out[n] = Address.tmpl_[c]
}
var i = 1
while (i < 34 && out[i] == 49) i = i + 1
return out[i-1..-1]
}
// doubleSHA256 computes a double sha256 hash of the first 21 bytes of the
// address, returning the full 32 byte sha256 hash.
doubleSHA256() {
var d = Sha256.digest(_a[0..20])
d = HexToBytes.call(d)
d = Sha256.digest(d)
return HexToBytes.call(d)
}
// updateChecksum computes the address checksum on the first 21 bytes and
// stores the result in the last 4 bytes.
updateCheckSum() {
var d = doubleSHA256()
for (i in 21..24) _a[i] = d[i-21]
}
// setPoint takes a public point and generates the corresponding address
// into the receiver, complete with valid checksum.
setPoint(p) {
var c = List.filled(65, 0)
c[0] = 4
for (i in 1..32) c[i] = p.x[i - 1]
for (i in 33..64) c[i] = p.y[i - 33]
var s = Sha256.digest(c)
s = HexToBytes.call(s)
var h = Ripemd160.digest(s)
h = HexToBytes.call(h)
for (i in 0...h.count) _a[i+1] = h[i]
updateCheckSum()
}
}
// parse hex into Point object
var p = Point.new()
p.setHex(
"50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352",
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"
)
// generate Address object from Point
var a = Address.new()
a.setPoint(p)
// show base58 representation
System.print(a.a58.map { |b| String.fromByte(b) }.join())</syntaxhighlight>
{{out}}
<pre>
Line 1,234 ⟶ 1,344:
=={{header|zkl}}==
Uses shared library zklMsgHash.
<
const symbols = "123456789" // 58 characters: no cap i,o; ell, zero
Line 1,260 ⟶ 1,370:
d,chkSum := Data(), MsgHash.SHA256(bytes,1,d) : MsgHash.SHA256(_,1,d);
base58Encode(bytes.append(chkSum.del(4,*))); // first 4 bytes of hashed hash
}</
<
"50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352",
"2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6");
(e=="16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM").println();</
{{out}}
<pre>True</pre>
{{omit from|Brlcad}}
{{omit from|GUISS}}
|