Advertisement
noler89

Untitled

Aug 17th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. pragma solidity ^0.4.2;
  2.  
  3. contract EC {
  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.  
  11. function EC(){}
  12.  
  13. function _add (uint256 x1,uint256 z1, uint256 x2,uint256 z2) constant returns(uint256 x3,uint256 z3){
  14. (x3, z3) = (addmod(mulmod(z2, x1, n), mulmod(x2, z1, n), n), mulmod(z1, z2, n));
  15. }
  16.  
  17. function _sub (uint256 x1,uint256 z1, uint256 x2,uint256 z2) constant returns(uint256 x3,uint256 z3){
  18. (x3, z3) = (addmod(mulmod(z2, x1, n),mulmod(n-x2, z1, n), n), mulmod(z1, z2 , n));
  19. }
  20.  
  21. function _multiply (uint256 x1,uint256 z1, uint256 x2,uint256 z2) constant returns(uint256 x3,uint256 z3){
  22. (x3, z3) = (mulmod(x1, x2 , n), mulmod(z1, z2 , n));
  23. }
  24.  
  25. function _divide (uint256 x1,uint256 z1, uint256 x2,uint256 z2) constant returns(uint256 x3,uint256 z3){
  26. (x3, z3) = (mulmod(x1, z2 , n), mulmod(z1 , x2 , n));
  27. }
  28.  
  29. function inv (uint256 a) constant returns(uint256 invA){
  30. uint256 t=0;
  31. uint256 newT=1;
  32. uint256 r=n;
  33. uint256 newR=a;
  34. uint256 q;
  35. while (newR != 0) {
  36. q = r / newR;
  37.  
  38. (t, newT) = (newT, addmod(t , (n - mulmod(q, newT,n)) , n));
  39. (r, newR) = (newR, r - q * newR );
  40. }
  41.  
  42. return t;
  43. }
  44.  
  45. function Add (uint256 x1,uint256 y1,uint256 z1, uint256 x2,uint256 y2,uint256 z2) constant returns(uint256 x3,uint256 y3,uint256 z3) {
  46. uint256 l;
  47. uint256 lz;
  48. uint256 da;
  49. uint256 db;
  50.  
  51. if ((x1==0)&&(y1==0)) {
  52. return (x2,y2,z2);
  53. }
  54.  
  55. if ((x2==0)&&(y2==0)) {
  56. return (x1,y1,z1);
  57. }
  58.  
  59. if ((x1==x2)&&(y1==y2)) {
  60. (l,lz) = _multiply(x1, z1, x1, z1);
  61. (l,lz) = _multiply(l, lz, 3, 1);
  62. (l,lz) = _add(l, lz, a, 1);
  63.  
  64. (da,db) = _multiply(y1, z1, 2, 1);
  65. }
  66. else {
  67. (l,lz) = _sub(y2, z2, y1, z1);
  68. (da,db) = _sub(x2, z2, x1, z1);
  69. }
  70.  
  71. (l, lz) = _divide(l, lz, da, db);
  72.  
  73. (x3, da) = _multiply(l, lz, l, lz);
  74. (x3, da) = _sub(x3, da, x1, z1);
  75. (x3, da) = _sub(x3, da, x2, z2);
  76.  
  77. (y3, db) = _sub(x1, z1, x3, da);
  78. (y3, db) = _multiply(y3, db, l, lz );
  79. (y3, db) = _sub(y3, db, y1, z1 );
  80.  
  81.  
  82. if (da != db) {
  83. x3 = mulmod(x3, db, n);
  84. y3 = mulmod(y3, da, n);
  85. z3 = mulmod(da, db, n);
  86. } else {
  87. z3 = da;
  88. }
  89.  
  90. }
  91.  
  92. function Double(uint256 x1,uint256 y1,uint256 z1) constant returns(uint256 x3,uint256 y3,uint256 z3){
  93. (x3,y3,z3) = Add(x1,y1,z1,x1,y1,z1);
  94. }
  95.  
  96. function Mulultiply(uint256 d, uint256 x1,uint256 y1,uint256 z1) constant returns(uint256 x3,uint256 y3,uint256 z3){
  97. uint256 remaining = d;
  98. uint256 px = x1;
  99. uint256 py = y1;
  100. uint256 pz = z1;
  101. uint256 acx = 0;
  102. uint256 acy = 0;
  103. uint256 acz = 1;
  104.  
  105. if (d==0) {
  106. return (0,0,1);
  107. }
  108.  
  109. while (remaining != 0) {
  110. if ((remaining & 1) != 0) {
  111. (acx,acy,acz) = Add(acx,acy,acz, px,py,pz);
  112. }
  113. remaining = remaining / 2;
  114. (px,py,pz) = Double(px,py,pz);
  115. }
  116.  
  117. (x3,y3,z3) = (acx,acy,acz);
  118. }
  119.  
  120. function privkey_to_public(uint256 privKey) constant returns(uint256 qx, uint256 qy){
  121. uint256 x;
  122. uint256 y;
  123. uint256 z;
  124. (x,y,z) = Mulultiply(privKey, gx, gy, 1);
  125. z = inv(z);
  126. qx = mulmod(x , z ,n);
  127. qy = mulmod(y , z ,n);
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement