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

Issue11 #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 27 additions & 20 deletions src/_ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

void signZZ_p(Sig * sig, char * msg, mpz_t d, mpz_t k, const CurveZZ_p * curve) {
mpz_t e, kinv;

int orderBits, digestBits;
// R = k * G, r = R[x]
PointZZ_p R;
pointZZ_pMul(&R, curve->g, k, curve);
Expand All @@ -14,8 +14,8 @@ void signZZ_p(Sig * sig, char * msg, mpz_t d, mpz_t k, const CurveZZ_p * curve)

// convert digest to integer (digest is computed as hex in ecdsa.py)
mpz_init_set_str(e, msg, 16);
int orderBits = mpz_sizeinbase(curve->q, 2);
int digestBits = strlen(msg) * 4;
orderBits = mpz_sizeinbase(curve->q, 2);
digestBits = strlen(msg) * 4;

if(digestBits > orderBits) {
mpz_fdiv_q_2exp(e, e, digestBits - orderBits);
Expand All @@ -36,12 +36,14 @@ void signZZ_p(Sig * sig, char * msg, mpz_t d, mpz_t k, const CurveZZ_p * curve)
int verifyZZ_p(Sig * sig, char * msg, PointZZ_p * Q, const CurveZZ_p * curve) {
mpz_t e, w, u1, u2;
PointZZ_p tmp;
int orderBits, digestBits, equal;

mpz_inits(w, u1, u2, tmp.x, tmp.y, NULL);

// convert digest to integer (digest is computed as hex in ecdsa.py)
mpz_init_set_str(e, msg, 16);
int orderBits = mpz_sizeinbase(curve->q, 2);
int digestBits = strlen(msg) * 4;
orderBits = mpz_sizeinbase(curve->q, 2);
digestBits = strlen(msg) * 4;

if(digestBits > orderBits) {
mpz_fdiv_q_2exp(e, e, digestBits - orderBits);
Expand All @@ -56,37 +58,40 @@ int verifyZZ_p(Sig * sig, char * msg, PointZZ_p * Q, const CurveZZ_p * curve) {
pointZZ_pShamirsTrick(&tmp, curve->g, u1, Q, u2, curve);
mpz_mod(tmp.x, tmp.x, curve->q);

int equal = (mpz_cmp(tmp.x, sig->r) == 0);
equal = (mpz_cmp(tmp.x, sig->r) == 0);
mpz_clears(e, w, u1, u2, tmp.x, tmp.y, NULL);
return equal;
}


/******************************************************************************
PYTHON BINDINGS
******************************************************************************/
PYTHON BINDINGS
******************************************************************************/
static PyObject * _ecdsa_sign(PyObject *self, PyObject *args) {
char * msg, * d, * k, * p, * a, * b, * q, * gx, * gy;

char * resultR;
char * resultS;
mpz_t privKey, nonce;
Sig sig;
CurveZZ_p * curve;
PyObject * ret;
if (!PyArg_ParseTuple(args, "sssssssss", &msg, &d, &k, &p, &a, &b, &q, &gx, &gy)) {
return NULL;
}

mpz_t privKey, nonce;
CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);
Sig sig;
curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);

mpz_init_set_str(privKey, d, 10);
mpz_init_set_str(nonce, k, 10);

signZZ_p(&sig, msg, privKey, nonce, curve);
destroyCurveZZ_p(curve);

char * resultR = mpz_get_str(NULL, 10, sig.r);
char * resultS = mpz_get_str(NULL, 10, sig.s);
resultR = mpz_get_str(NULL, 10, sig.r);
resultS = mpz_get_str(NULL, 10, sig.s);
mpz_clears(sig.r, sig.s, privKey, NULL);

PyObject * ret = Py_BuildValue("ss", resultR, resultS);
ret = Py_BuildValue("ss", resultR, resultS);
free(resultR);
free(resultS);
return ret;
Expand All @@ -95,19 +100,21 @@ static PyObject * _ecdsa_sign(PyObject *self, PyObject *args) {

static PyObject * _ecdsa_verify(PyObject *self, PyObject *args) {
char * r, * s, * msg, * qx, * qy, * p, * a, * b, * q, * gx, * gy;

Sig sig;
CurveZZ_p * curve;
int valid = 0;
PointZZ_p * Q;

if (!PyArg_ParseTuple(args, "sssssssssss", &r, &s, &msg, &qx, &qy, &p, &a, &b, &q, &gx, &gy)) {
return NULL;
}

Sig sig;
mpz_init_set_str(sig.r, r, 10);
mpz_init_set_str(sig.s, s, 10);

CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);
int valid = 0;
curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);

PointZZ_p * Q = buildPointZZ_p(qx, qy, 10);
Q = buildPointZZ_p(qx, qy, 10);
valid = verifyZZ_p(&sig, msg, Q, curve);

destroyCurveZZ_p(curve);
Expand Down
59 changes: 36 additions & 23 deletions src/curveMath.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ void pointZZ_pAdd(PointZZ_p * rop, const PointZZ_p * op1, const PointZZ_p * op2,

void pointZZ_pMul(PointZZ_p * rop, const PointZZ_p * point, const mpz_t scalar, const CurveZZ_p * curve) {
PointZZ_p R0, R1, tmp;
int dbits, i;
mpz_inits(R1.x, R1.y, tmp.x, tmp.y, NULL);
mpz_init_set(R0.x, point->x);
mpz_init_set(R0.y, point->y);
pointZZ_pDouble(&R1, point, curve);

int dbits = mpz_sizeinbase(scalar, 2), i;
dbits = mpz_sizeinbase(scalar, 2), i;

for(i = dbits - 2; i >= 0; i--) {
if(mpz_tstbit(scalar, i)) {
Expand Down Expand Up @@ -106,15 +107,16 @@ void pointZZ_pMul(PointZZ_p * rop, const PointZZ_p * point, const mpz_t scalar,


void pointZZ_pShamirsTrick(PointZZ_p * rop, const PointZZ_p * point1, const mpz_t scalar1,
const PointZZ_p * point2, const mpz_t scalar2, const CurveZZ_p * curve)
const PointZZ_p * point2, const mpz_t scalar2, const CurveZZ_p * curve)
{
PointZZ_p sum, tmp;
int scalar1Bits, scalar2Bits, l;
mpz_inits(sum.x, sum.y, tmp.x, tmp.y, NULL);
pointZZ_pAdd(&sum, point1, point2, curve);

int scalar1Bits = mpz_sizeinbase(scalar1, 2);
int scalar2Bits = mpz_sizeinbase(scalar2, 2);
int l = (scalar1Bits > scalar2Bits ? scalar1Bits : scalar2Bits) - 1;
scalar1Bits = mpz_sizeinbase(scalar1, 2);
scalar2Bits = mpz_sizeinbase(scalar2, 2);
l = (scalar1Bits > scalar2Bits ? scalar1Bits : scalar2Bits) - 1;

if(mpz_tstbit(scalar1, l) && mpz_tstbit(scalar2, l)) {
mpz_set(rop->x, sum.x);
Expand Down Expand Up @@ -150,48 +152,59 @@ void pointZZ_pShamirsTrick(PointZZ_p * rop, const PointZZ_p * point1, const mpz_


/******************************************************************************
PYTHON BINDINGS
******************************************************************************/
PYTHON BINDINGS
******************************************************************************/
static PyObject * curvemath_mul(PyObject *self, PyObject *args) {
char * x, * y, * d, * p, * a, * b, * q, * gx, * gy;

char * resultX;
char * resultY;
CurveZZ_p * curve;
PointZZ_p * point;
PointZZ_p result;
mpz_t scalar;
PyObject * ret;

if (!PyArg_ParseTuple(args, "sssssssss", &x, &y, &d, &p, &a, &b, &q, &gx, &gy)) {
return NULL;
}

PointZZ_p result;
mpz_t scalar;
mpz_init_set_str(scalar, d, 10);
CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);;
curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);;

PointZZ_p * point = buildPointZZ_p(x, y, 10);
point = buildPointZZ_p(x, y, 10);
pointZZ_pMul(&result, point, scalar, curve);
destroyPointZZ_p(point);
destroyCurveZZ_p(curve);

char * resultX = mpz_get_str(NULL, 10, result.x);
char * resultY = mpz_get_str(NULL, 10, result.y);
resultX = mpz_get_str(NULL, 10, result.x);
resultY = mpz_get_str(NULL, 10, result.y);
mpz_clears(result.x, result.y, scalar, NULL);

PyObject * ret = Py_BuildValue("ss", resultX, resultY);
ret = Py_BuildValue("ss", resultX, resultY);
free(resultX);
free(resultY);
return ret;
}

static PyObject * curvemath_add(PyObject *self, PyObject *args) {
char * px, * py, * qx, * qy, * p, * a, * b, * q, * gx, * gy;

PointZZ_p result;
CurveZZ_p * curve;
PointZZ_p * P;
PointZZ_p * Q;
char * resultX;
char * resultY;
PyObject * ret;

if (!PyArg_ParseTuple(args, "ssssssssss", &px, &py, &qx, &qy, &p, &a, &b, &q, &gx, &gy)) {
return NULL;
}

PointZZ_p result;
mpz_inits(result.x, result.y, NULL);
CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);;
curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);;

PointZZ_p * P = buildPointZZ_p(px, py, 10);
PointZZ_p * Q = buildPointZZ_p(qx, qy, 10);
P = buildPointZZ_p(px, py, 10);
Q = buildPointZZ_p(qx, qy, 10);

if(pointZZ_pEqual(P, Q)) {
pointZZ_pDouble(&result, P, curve);
Expand All @@ -204,11 +217,11 @@ static PyObject * curvemath_add(PyObject *self, PyObject *args) {
destroyPointZZ_p(Q);
destroyCurveZZ_p(curve);

char * resultX = mpz_get_str(NULL, 10, result.x);
char * resultY = mpz_get_str(NULL, 10, result.y);
resultX = mpz_get_str(NULL, 10, result.x);
resultY = mpz_get_str(NULL, 10, result.y);
mpz_clears(result.x, result.y, NULL);

PyObject * ret = Py_BuildValue("ss", resultX, resultY);
ret = Py_BuildValue("ss", resultX, resultY);
free(resultX);
free(resultY);
return ret;
Expand Down
70 changes: 70 additions & 0 deletions windows_make.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
if not exist aria2c.exe (
echo "Download aria2c.exe from https://github.com/aria2/aria2/releases/tag/release-1.34.0"
goto :exit
)

if exist "C:\Program Files (x86)\" (
set programfiles = "C:\Program Files (x86)\"
) else ( set programfiles = "C:\Program Files\" )


if not exist "%programfiles%\WinRAR\WinRAR.exe" (
echo "Need some tool to extract bz2 and zip archives"
goto :exit
) else ( set unrar="%programfiles%\WinRAR\WinRAR.exe")


if exist "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\bin\cl.exe" (
goto :getyasm
) else (
aria2c.exe https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59FBF6D907B/VCForPython27.msi
msiexec.exe /i VCForPython27.msi
)


:getyasm
if not exist "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\bin\yasm.exe" (
aria2c.exe http://www.tortall.net/projects/yasm/releases/yasm-1.3.0-win32.exe
copy yasm-1.3.0-win32.exe "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\bin\yasm.exe"
)

:getmpir
if exist "mpir-2.6.0.tar.bz2" (
goto :make_mpir
) else (
aria2c.exe http://www.mpir.org/mpir-2.6.0.tar.bz2
%unrar% x mpir-2.6.0.tar.bz2 mpir-2.6.0
)

:make_mpir
if not exist "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\lib\gmp.lib" (
cd mpir-2.6.0\win
call "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat"
call configure.bat ABI 32
call make.bat
REM make check
call gen_mpir_h.bat
copy mpir.lib "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\lib\gmp.lib"
cd ..
copy gmp.h "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\include\gmp.h"
cd ..
)

REM have to be replaced to https://github.com/AntonKueltz/fastecdsa/archive/master.zip if commited
aria2c.exe https://github.com/shikuk/fastecdsa/archive/master.zip
%unrar% x fastecdsa-master.zip
cd fastecdsa-master
python setup.py build
python setup.py install

cd..

python -m fastecdsa.test

:exit
exit /b 1