Advertisement
noler89

Untitled

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