Advertisement
noler89

Untitled

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