noler89

Untitled

Aug 31st, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 KB | None | 0 0
  1. pragma solidity ^0.4.13;
  2.  
  3. contract bitcoinkeys {
  4.  
  5. uint256 constant gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798;
  6. uint256 constant gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8;
  7. uint256 constant n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;
  8. uint256 constant a = 0;
  9. uint256 constant b = 7;
  10. bytes32 constant mask4 = 0xffffffff00000000000000000000000000000000000000000000000000000000;
  11. bytes1 network;
  12.  
  13. function bitcoinkeys(){}
  14.  
  15. function set(bytes1 _network){
  16. network=_network;
  17. }
  18.  
  19. function _add (uint256 x1,uint256 z1, uint256 x2,uint256 z2) private constant returns(uint256 x3,uint256 z3){
  20. (x3, z3) = (addmod(mulmod(z2, x1, n), mulmod(x2, z1, n), n), mulmod(z1, z2, n));
  21. }
  22.  
  23. function _sub (uint256 x1,uint256 z1, uint256 x2,uint256 z2) private constant returns(uint256 x3,uint256 z3){
  24. (x3, z3) = (addmod(mulmod(z2, x1, n),mulmod(n-x2, z1, n), n), mulmod(z1, z2 , n));
  25. }
  26.  
  27. function _multiply (uint256 x1,uint256 z1, uint256 x2,uint256 z2) private constant returns(uint256 x3,uint256 z3){
  28. (x3, z3) = (mulmod(x1, x2 , n), mulmod(z1, z2 , n));
  29. }
  30.  
  31. function _divide (uint256 x1,uint256 z1, uint256 x2,uint256 z2) private constant returns(uint256 x3,uint256 z3){
  32. (x3, z3) = (mulmod(x1, z2 , n), mulmod(z1 , x2 , n));
  33. }
  34.  
  35. function inv (uint256 a) private constant returns(uint256 invA){
  36. uint256 t=0;
  37. uint256 newT=1;
  38. uint256 r=n;
  39. uint256 newR=a;
  40. uint256 q;
  41. while (newR != 0) {
  42. q = r / newR;
  43.  
  44. (t, newT) = (newT, addmod(t , (n - mulmod(q, newT,n)) , n));
  45. (r, newR) = (newR, r - q * newR );
  46. }
  47.  
  48. return t;
  49. }
  50.  
  51. function Add (uint256 x1,uint256 y1,uint256 z1, uint256 x2,uint256 y2,uint256 z2) private constant returns(uint256 x3,uint256 y3,uint256 z3) {
  52. uint256 l;
  53. uint256 lz;
  54. uint256 da;
  55. uint256 db;
  56.  
  57. if ((x1==0)&&(y1==0)) {
  58. return (x2,y2,z2);
  59. }
  60.  
  61. if ((x2==0)&&(y2==0)) {
  62. return (x1,y1,z1);
  63. }
  64.  
  65. if ((x1==x2)&&(y1==y2)) {
  66. (l,lz) = _multiply(x1, z1, x1, z1);
  67. (l,lz) = _multiply(l, lz, 3, 1);
  68. (l,lz) = _add(l, lz, a, 1);
  69.  
  70. (da,db) = _multiply(y1, z1, 2, 1);
  71. }
  72. else {
  73. (l,lz) = _sub(y2, z2, y1, z1);
  74. (da,db) = _sub(x2, z2, x1, z1);
  75. }
  76.  
  77. (l, lz) = _divide(l, lz, da, db);
  78.  
  79. (x3, da) = _multiply(l, lz, l, lz);
  80. (x3, da) = _sub(x3, da, x1, z1);
  81. (x3, da) = _sub(x3, da, x2, z2);
  82.  
  83. (y3, db) = _sub(x1, z1, x3, da);
  84. (y3, db) = _multiply(y3, db, l, lz );
  85. (y3, db) = _sub(y3, db, y1, z1 );
  86.  
  87.  
  88. if (da != db) {
  89. x3 = mulmod(x3, db, n);
  90. y3 = mulmod(y3, da, n);
  91. z3 = mulmod(da, db, n);
  92. } else {
  93. z3 = da;
  94. }
  95.  
  96. }
  97.  
  98. function Double(uint256 x1,uint256 y1,uint256 z1) private constant returns(uint256 x3,uint256 y3,uint256 z3){
  99. (x3,y3,z3) = Add(x1,y1,z1,x1,y1,z1);
  100. }
  101.  
  102. function Mulultiply(uint256 d, uint256 x1,uint256 y1,uint256 z1) private constant returns(uint256 x3,uint256 y3,uint256 z3){
  103. uint256 remaining = d;
  104. uint256 px = x1;
  105. uint256 py = y1;
  106. uint256 pz = z1;
  107. uint256 acx = 0;
  108. uint256 acy = 0;
  109. uint256 acz = 1;
  110.  
  111. if (d==0) {
  112. return (0,0,1);
  113. }
  114.  
  115. while (remaining != 0) {
  116. if ((remaining & 1) != 0) {
  117. (acx,acy,acz) = Add(acx,acy,acz, px,py,pz);
  118. }
  119. remaining = remaining / 2;
  120. (px,py,pz) = Double(px,py,pz);
  121. }
  122.  
  123. (x3,y3,z3) = (acx,acy,acz);
  124. }
  125.  
  126. function privkey_to_public(uint256 privKey) constant returns(uint256 qx, uint256 qy){
  127. uint256 x;
  128. uint256 y;
  129. uint256 z;
  130. (x,y,z) = Mulultiply(privKey, gx, gy, 1);
  131. z = inv(z);
  132. qx = mulmod(x , z ,n);
  133. qy = mulmod(y , z ,n);
  134. }
  135.  
  136. function randomPriv() constant returns (uint256){
  137. uint256 lastBlockNumber = block.number - 1;
  138. uint256 hashVal = uint256(block.blockhash(lastBlockNumber));
  139. return uint256(hashVal) + 1;
  140. }
  141.  
  142. function hex_dec(string _s) public returns(uint ans){
  143. ans=0;
  144. bytes memory s = bytes(_s);
  145. string memory i_hate_solidity = new string(1);
  146. bytes memory solidity_is_shit = bytes(i_hate_solidity);
  147. // for(uint i= s.length-1;i>=0;i=i-1){
  148. for(uint i= 0;i<s.length;i++){
  149. solidity_is_shit[0]=s[i];
  150. ans=ans+ (16**(s.length-1-i))*dec_val(solidity_is_shit);
  151.  
  152. }
  153. }
  154.  
  155. function hex_val(uint a) private returns(string){
  156. if(a==0) return "0";
  157. if(a==1) return "1";
  158. if(a==2) return "2";
  159. if(a==3) return "3";
  160. if(a==4) return "4";
  161. if(a==5) return "5";
  162. if(a==6) return "6";
  163. if(a==7) return "7";
  164. if(a==8) return "8";
  165. if(a==9) return "9";
  166. if(a==10) return "A";
  167. if(a==11) return "B";
  168. if(a==12) return "C";
  169. if(a==13) return "D";
  170. if(a==14) return "E";
  171. if(a==15) return "F";
  172. }
  173.  
  174. function dec_val(bytes a)private returns(uint){
  175. if(strequal(a,"0")) return 0;
  176. if(strequal(a,"1")) return 1;
  177. if(strequal(a,"2")) return 2;
  178. if(strequal(a,"3")) return 3;
  179. if(strequal(a,"4")) return 4;
  180. if(strequal(a,"5")) return 5;
  181. if(strequal(a,"6")) return 6;
  182. if(strequal(a,"7")) return 7;
  183. if(strequal(a,"8")) return 8;
  184. if(strequal(a,"9")) return 9;
  185. if(strequal(a,"a")) return 10;
  186. if(strequal(a,"b")) return 11;
  187. if(strequal(a,"c")) return 12;
  188. if(strequal(a,"d")) return 13;
  189. if(strequal(a,"e")) return 14;
  190. if(strequal(a,"f")) return 15;
  191. }
  192.  
  193. function strequal(bytes a,string _b) private returns(bool){
  194. bytes memory b = bytes(_b);
  195. if(a[0]==b[0]) return true;
  196. return false;
  197. }
  198.  
  199. function stringAdd(string _a,string _b) private returns(string){
  200. bytes memory a = bytes(_a);
  201. bytes memory b = bytes(_b);
  202. string memory _ab = new string(a.length + b.length);
  203. bytes memory ab = bytes(_ab);
  204. uint k = 0;
  205. for (uint i = 0; i < a.length; i++) ab[k++] = a[i];
  206. for (i = 0; i < b.length; i++) ab[k++] = b[i];
  207. return string(ab);
  208.  
  209. }
  210.  
  211. // function randomKeys() public constant returns(string , string , string){
  212. // var privkey=randomPriv();
  213. // var (pubkeyX,pubkeyY) = privkey_to_public(privkey);
  214. // var hexpriv=deс_hex(privkey);
  215. // return (hexpriv, deс_hex(pubkeyX), deс_hex(pubkeyY));
  216. // }
  217.  
  218. function dec_hex(uint256 a) public returns(string ans){
  219. string memory num;
  220. ans="";
  221. if(a==0) return "0";
  222. while(a>0){
  223. num=hex_val(a%16);
  224. ans=stringAdd(num,ans);
  225. a=a/16;
  226. }
  227. return ans;
  228. }
  229.  
  230. function getUintAdress(bytes20 a,bytes4 b,bytes1 c) private returns(uint){
  231. bytes memory ans = new bytes(a.length+b.length+c.length);
  232. uint k=0;
  233. for(uint i=0;i<c.length;++i){
  234. ans[k++]=c[i];
  235. }
  236. for( i=0;i<a.length;++i){
  237. ans[k++]=a[i];
  238. }
  239. for(i=0;i<b.length;++i){
  240. ans[k++]=b[i];
  241. }
  242.  
  243. uint preBase58=0;
  244. for(i=0;i<ans.length;++i){
  245. preBase58=preBase58+uint(ans[i])*(256**(ans.length-1-i));
  246. }
  247. return preBase58;
  248.  
  249.  
  250. }
  251.  
  252. function AddressFromUint(uint preBase58) private returns(string) {
  253. string memory num="";
  254. if(preBase58==0) return "0";
  255. while(preBase58>0){
  256. var add=base58Alph(preBase58%58);
  257. num = stringAdd(add,num);
  258. preBase58=preBase58/58;
  259. }
  260. add=base58Alph(0); num = stringAdd(add,num);
  261. return num;
  262. }
  263.  
  264. function getAdress(uint _x,uint _y)
  265. constant returns(string)
  266. {
  267. bytes32 _xPoint = bytes32(_x);
  268. bytes32 _yPoint = bytes32(_y);
  269. bytes20 hashedPubKey = PubKeyHash(_xPoint, _yPoint);
  270. bytes4 checkSum = CheckSum(hashedPubKey);
  271. var ans = getUintAdress(hashedPubKey,checkSum,network);
  272. var addressFromPublic = AddressFromUint(ans);
  273. return addressFromPublic;
  274.  
  275. }
  276.  
  277. function PubKeyHash( bytes32 _xPoint,bytes32 _yPoint)
  278. private returns(bytes20)
  279. {
  280. return ripemd160(sha256(0x04, _xPoint, _yPoint));
  281. }
  282.  
  283. function CheckSum(bytes20 _hashedPubKey)
  284. private returns(bytes4 checkSum)
  285. {
  286. var full = sha256((sha256(network, _hashedPubKey)));
  287. return bytes4(full&mask4);
  288. }
  289.  
  290.  
  291. function base58Alph(uint a) private returns(string){
  292. if(a==0) return "1";
  293. if(a==1) return "2";
  294. if(a==2) return "3";
  295. if(a==3) return "4";
  296. if(a==4) return "5";
  297. if(a==5) return "6";
  298. if(a==6) return "7";
  299. if(a==7) return "8";
  300. if(a==8) return "9";
  301. if(a==9) return "A";
  302. if(a==10) return "B";
  303. if(a==11) return "C";
  304. if(a==12) return "D";
  305. if(a==13) return "E";
  306. if(a==14) return "F";
  307. if(a==15) return "G";
  308. if(a==16) return "H";
  309. if(a==17) return "J";
  310. if(a==18) return "K";
  311. if(a==19) return "L";
  312. if(a==20) return "M";
  313. if(a==21) return "N";
  314. if(a==22) return "P";
  315. if(a==23) return "Q";
  316. if(a==24) return "R";
  317. if(a==25) return "S";
  318. if(a==26) return "T";
  319. if(a==27) return "U";
  320. if(a==28) return "V";
  321. if(a==29) return "W";
  322. if(a==30) return "X";
  323. if(a==31) return "Y";
  324. if(a==32) return "Z";
  325. if(a==33) return "a";
  326. if(a==34) return "b";
  327. if(a==35) return "c";
  328. if(a==36) return "d";
  329. if(a==37) return "e";
  330. if(a==38) return "f";
  331. if(a==39) return "g";
  332. if(a==40) return "h";
  333. if(a==41) return "i";
  334. if(a==42) return "j";
  335. if(a==43) return "k";
  336. if(a==44) return "m";
  337. if(a==45) return "n";
  338. if(a==46) return "o";
  339. if(a==47) return "p";
  340. if(a==48) return "q";
  341. if(a==49) return "r";
  342. if(a==50) return "s";
  343. if(a==51) return "t";
  344. if(a==52) return "u";
  345. if(a==53) return "v";
  346. if(a==54) return "w";
  347. if(a==55) return "x";
  348. if(a==56) return "y";
  349. if(a==57) return "z";
  350. }
  351. }
Add Comment
Please, Sign In to add comment