Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.13;
- contract bitcoinkeys {
- uint256 constant gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798;
- uint256 constant gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8;
- uint256 constant n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;
- uint256 constant a = 0;
- uint256 constant b = 7;
- bytes32 constant mask4 = 0xffffffff00000000000000000000000000000000000000000000000000000000;
- bytes1 constant version;
- bytes1 constant network;
- function bitcoinkeys(){}
- function set(bytes1 _version, bytes1 _network){
- version=_version;
- network=_network;
- }
- function _add (uint256 x1,uint256 z1, uint256 x2,uint256 z2) private constant returns(uint256 x3,uint256 z3){
- (x3, z3) = (addmod(mulmod(z2, x1, n), mulmod(x2, z1, n), n), mulmod(z1, z2, n));
- }
- function _sub (uint256 x1,uint256 z1, uint256 x2,uint256 z2) private constant returns(uint256 x3,uint256 z3){
- (x3, z3) = (addmod(mulmod(z2, x1, n),mulmod(n-x2, z1, n), n), mulmod(z1, z2 , n));
- }
- function _multiply (uint256 x1,uint256 z1, uint256 x2,uint256 z2) private constant returns(uint256 x3,uint256 z3){
- (x3, z3) = (mulmod(x1, x2 , n), mulmod(z1, z2 , n));
- }
- function _divide (uint256 x1,uint256 z1, uint256 x2,uint256 z2) private constant returns(uint256 x3,uint256 z3){
- (x3, z3) = (mulmod(x1, z2 , n), mulmod(z1 , x2 , n));
- }
- function inv (uint256 a) private constant returns(uint256 invA){
- uint256 t=0;
- uint256 newT=1;
- uint256 r=n;
- uint256 newR=a;
- uint256 q;
- while (newR != 0) {
- q = r / newR;
- (t, newT) = (newT, addmod(t , (n - mulmod(q, newT,n)) , n));
- (r, newR) = (newR, r - q * newR );
- }
- return t;
- }
- function Add (uint256 x1,uint256 y1,uint256 z1, uint256 x2,uint256 y2,uint256 z2) private constant returns(uint256 x3,uint256 y3,uint256 z3) {
- uint256 l;
- uint256 lz;
- uint256 da;
- uint256 db;
- if ((x1==0)&&(y1==0)) {
- return (x2,y2,z2);
- }
- if ((x2==0)&&(y2==0)) {
- return (x1,y1,z1);
- }
- if ((x1==x2)&&(y1==y2)) {
- (l,lz) = _multiply(x1, z1, x1, z1);
- (l,lz) = _multiply(l, lz, 3, 1);
- (l,lz) = _add(l, lz, a, 1);
- (da,db) = _multiply(y1, z1, 2, 1);
- }
- else {
- (l,lz) = _sub(y2, z2, y1, z1);
- (da,db) = _sub(x2, z2, x1, z1);
- }
- (l, lz) = _divide(l, lz, da, db);
- (x3, da) = _multiply(l, lz, l, lz);
- (x3, da) = _sub(x3, da, x1, z1);
- (x3, da) = _sub(x3, da, x2, z2);
- (y3, db) = _sub(x1, z1, x3, da);
- (y3, db) = _multiply(y3, db, l, lz );
- (y3, db) = _sub(y3, db, y1, z1 );
- if (da != db) {
- x3 = mulmod(x3, db, n);
- y3 = mulmod(y3, da, n);
- z3 = mulmod(da, db, n);
- } else {
- z3 = da;
- }
- }
- function Double(uint256 x1,uint256 y1,uint256 z1) private constant returns(uint256 x3,uint256 y3,uint256 z3){
- (x3,y3,z3) = Add(x1,y1,z1,x1,y1,z1);
- }
- function Mulultiply(uint256 d, uint256 x1,uint256 y1,uint256 z1) private constant returns(uint256 x3,uint256 y3,uint256 z3){
- uint256 remaining = d;
- uint256 px = x1;
- uint256 py = y1;
- uint256 pz = z1;
- uint256 acx = 0;
- uint256 acy = 0;
- uint256 acz = 1;
- if (d==0) {
- return (0,0,1);
- }
- while (remaining != 0) {
- if ((remaining & 1) != 0) {
- (acx,acy,acz) = Add(acx,acy,acz, px,py,pz);
- }
- remaining = remaining / 2;
- (px,py,pz) = Double(px,py,pz);
- }
- (x3,y3,z3) = (acx,acy,acz);
- }
- function privkey_to_public(uint256 privKey) constant returns(uint256 qx, uint256 qy){
- uint256 x;
- uint256 y;
- uint256 z;
- (x,y,z) = Mulultiply(privKey, gx, gy, 1);
- z = inv(z);
- qx = mulmod(x , z ,n);
- qy = mulmod(y , z ,n);
- }
- function randomPriv() constant returns (uint256){
- uint256 lastBlockNumber = block.number - 1;
- uint256 hashVal = uint256(block.blockhash(lastBlockNumber));
- return uint256(hashVal) + 1;
- }
- function hex_dec(string _s) public returns(uint ans){
- ans=0;
- bytes memory s = bytes(_s);
- string memory i_hate_solidity = new string(1);
- bytes memory solidity_is_shit = bytes(i_hate_solidity);
- // for(uint i= s.length-1;i>=0;i=i-1){
- for(uint i= 0;i<s.length;i++){
- solidity_is_shit[0]=s[i];
- ans=ans+ (16**(s.length-1-i))*dec_val(solidity_is_shit);
- }
- }
- function hex_val(uint a) private returns(string){
- if(a==0) return "0";
- if(a==1) return "1";
- if(a==2) return "2";
- if(a==3) return "3";
- if(a==4) return "4";
- if(a==5) return "5";
- if(a==6) return "6";
- if(a==7) return "7";
- if(a==8) return "8";
- if(a==9) return "9";
- if(a==10) return "A";
- if(a==11) return "B";
- if(a==12) return "C";
- if(a==13) return "D";
- if(a==14) return "E";
- if(a==15) return "F";
- }
- function dec_val(bytes a)private returns(uint){
- if(strequal(a,"0")) return 0;
- if(strequal(a,"1")) return 1;
- if(strequal(a,"2")) return 2;
- if(strequal(a,"3")) return 3;
- if(strequal(a,"4")) return 4;
- if(strequal(a,"5")) return 5;
- if(strequal(a,"6")) return 6;
- if(strequal(a,"7")) return 7;
- if(strequal(a,"8")) return 8;
- if(strequal(a,"9")) return 9;
- if(strequal(a,"a")) return 10;
- if(strequal(a,"b")) return 11;
- if(strequal(a,"c")) return 12;
- if(strequal(a,"d")) return 13;
- if(strequal(a,"e")) return 14;
- if(strequal(a,"f")) return 15;
- }
- function strequal(bytes a,string _b) private returns(bool){
- bytes memory b = bytes(_b);
- if(a[0]==b[0]) return true;
- return false;
- }
- function stringAdd(string _a,string _b) private returns(string){
- bytes memory a = bytes(_a);
- bytes memory b = bytes(_b);
- string memory _ab = new string(a.length + b.length);
- bytes memory ab = bytes(_ab);
- uint k = 0;
- for (uint i = 0; i < a.length; i++) ab[k++] = a[i];
- for (i = 0; i < b.length; i++) ab[k++] = b[i];
- return string(ab);
- }
- // function randomKeys() public constant returns(string , string , string){
- // var privkey=randomPriv();
- // var (pubkeyX,pubkeyY) = privkey_to_public(privkey);
- // var hexpriv=deΡ_hex(privkey);
- // return (hexpriv, deΡ_hex(pubkeyX), deΡ_hex(pubkeyY));
- // }
- function dec_hex(uint256 a) public returns(string ans){
- string memory num;
- ans="";
- if(a==0) return "0";
- while(a>0){
- num=hex_val(a%16);
- ans=stringAdd(num,ans);
- a=a/16;
- }
- return ans;
- }
- function getUintAdress(bytes20 a,bytes4 b,bytes1 c) private returns(uint){
- bytes memory ans = new bytes(a.length+b.length+c.length);
- uint k=0;
- for(uint i=0;i<c.length;++i){
- ans[k++]=c[i];
- }
- for( i=0;i<a.length;++i){
- ans[k++]=a[i];
- }
- for(i=0;i<b.length;++i){
- ans[k++]=b[i];
- }
- uint preBase58=0;
- for(i=0;i<ans.length;++i){
- preBase58=preBase58+uint(ans[i])*(256**(ans.length-1-i));
- }
- return preBase58;
- }
- function AddressFromUint(uint preBase58) private returns(string) {
- string memory num="";
- if(preBase58==0) return "0";
- while(preBase58>0){
- var add=base58Alph(preBase58%58);
- num = stringAdd(add,num);
- preBase58=preBase58/58;
- }
- add=base58Alph(0); num = stringAdd(add,num);
- return num;
- }
- function getAdress(uint _x,uint _y)
- constant returns(string)
- {
- bytes32 _xPoint = bytes32(_x);
- bytes32 _yPoint = bytes32(_y);
- bytes20 hashedPubKey = PubKeyHash(_xPoint, _yPoint);
- bytes4 checkSum = CheckSum(hashedPubKey);
- var ans = getUintAdress(hashedPubKey,checkSum,network);
- var addressFromPublic = AddressFromUint(ans);
- return addressFromPublic;
- }
- function PubKeyHash( bytes32 _xPoint,bytes32 _yPoint)
- private returns(bytes20)
- {
- return ripemd160(sha256(version, _xPoint, _yPoint));
- }
- function CheckSum(bytes20 _hashedPubKey)
- private returns(bytes4 checkSum)
- {
- var full = sha256((sha256(network, _hashedPubKey)));
- return bytes4(full&mask4);
- }
- function base58Alph(uint a) private returns(string){
- if(a==0) return "1";
- if(a==1) return "2";
- if(a==2) return "3";
- if(a==3) return "4";
- if(a==4) return "5";
- if(a==5) return "6";
- if(a==6) return "7";
- if(a==7) return "8";
- if(a==8) return "9";
- if(a==9) return "A";
- if(a==10) return "B";
- if(a==11) return "C";
- if(a==12) return "D";
- if(a==13) return "E";
- if(a==14) return "F";
- if(a==15) return "G";
- if(a==16) return "H";
- if(a==17) return "J";
- if(a==18) return "K";
- if(a==19) return "L";
- if(a==20) return "M";
- if(a==21) return "N";
- if(a==22) return "P";
- if(a==23) return "Q";
- if(a==24) return "R";
- if(a==25) return "S";
- if(a==26) return "T";
- if(a==27) return "U";
- if(a==28) return "V";
- if(a==29) return "W";
- if(a==30) return "X";
- if(a==31) return "Y";
- if(a==32) return "Z";
- if(a==33) return "a";
- if(a==34) return "b";
- if(a==35) return "c";
- if(a==36) return "d";
- if(a==37) return "e";
- if(a==38) return "f";
- if(a==39) return "g";
- if(a==40) return "h";
- if(a==41) return "i";
- if(a==42) return "j";
- if(a==43) return "k";
- if(a==44) return "m";
- if(a==45) return "n";
- if(a==46) return "o";
- if(a==47) return "p";
- if(a==48) return "q";
- if(a==49) return "r";
- if(a==50) return "s";
- if(a==51) return "t";
- if(a==52) return "u";
- if(a==53) return "v";
- if(a==54) return "w";
- if(a==55) return "x";
- if(a==56) return "y";
- if(a==57) return "z";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement