Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize publicKey method by precomputing G powers #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions ec.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@ contract EC {
uint256 constant n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;
uint256 constant a = 0;
uint256 constant b = 7;
uint256[] public gxs;
uint256[] public gys;
uint256[] public gzs;

function EC()
{
gxs.push(gx);
gys.push(gy);
gzs.push(1);
}

function prepare(uint count) public
{
require(gxs.length < 256);
uint256 x = gxs[gxs.length - 1];
uint256 y = gys[gys.length - 1];
uint256 z = gzs[gzs.length - 1];
for (uint j = 0; j < count && gxs.length < 256; j++) {
(x,y,z) = _ecDouble(x,y,z);
gxs.push(x);
gys.push(y);
gzs.push(z);
}
}

function _jAdd( uint256 x1,uint256 z1,
Expand Down Expand Up @@ -154,13 +174,22 @@ contract EC {
function publicKey(uint256 privKey) constant
returns(uint256 qx, uint256 qy)
{
uint256 x;
uint256 y;
uint256 z;
(x,y,z) = _ecMul(privKey, gx, gy, 1);
z = _inverse(z);
qx = mulmod(x , z ,n);
qy = mulmod(y , z ,n);
uint256 acx = 0;
uint256 acy = 0;
uint256 acz = 1;

if (privKey == 0) {
return (0,0);
}

for (uint i = 0; i < 256; i++) {
if (((privKey >> i) & 1) != 0) {
(acx,acy,acz) = _ecAdd(acx,acy,acz, gxs[i],gys[i],gzs[i]);
}
}

acz = _inverse(acz);
(qx,qy) = (mulmod(acx,acz,n),mulmod(acy,acz,n));
}

function deriveKey(uint256 privKey, uint256 pubX, uint256 pubY) constant
Expand Down