feat: finish module methods definition

This commit is contained in:
sangge 2023-12-09 17:22:33 +08:00
parent 8a4059906c
commit 11c53ebb89

View File

@ -38,6 +38,17 @@ const TPRE_BN SM2_P = {
0xfffffffe, 0xfffffffe,
}; };
const TPRE_BN SM2_A = {
0xfffffffc,
0xffffffff,
0x00000000,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff,
0xfffffffe,
};
const TPRE_BN SM2_B = { const TPRE_BN SM2_B = {
0x4d940e93, 0x4d940e93,
0xddbcbd41, 0xddbcbd41,
@ -112,15 +123,15 @@ const TPRE_BN SM2_ONE = {1, 0, 0, 0, 0, 0, 0, 0};
const TPRE_BN SM2_TWO = {2, 0, 0, 0, 0, 0, 0, 0}; const TPRE_BN SM2_TWO = {2, 0, 0, 0, 0, 0, 0, 0};
const TPRE_BN SM2_THREE = {3, 0, 0, 0, 0, 0, 0, 0}; const TPRE_BN SM2_THREE = {3, 0, 0, 0, 0, 0, 0, 0};
// 将Python中的multiply函数转换为C // 点乘
static Point multiply(Point a, int64_t n) static void multiply(TPRE_POINT r, const TPRE_POINT a, int64_t n)
{ {
Point result; Point result;
// ...实现乘法逻辑... // ...实现乘法逻辑...
return result; return result;
} }
// Python接口函数 // 点乘的Python接口函数
static PyObject *py_multiply(PyObject *self, PyObject *args) static PyObject *py_multiply(PyObject *self, PyObject *args)
{ {
Point a; Point a;
@ -138,16 +149,30 @@ static PyObject *py_multiply(PyObject *self, PyObject *args)
return Py_BuildValue("(ll)", result.x, result.y); return Py_BuildValue("(ll)", result.x, result.y);
} }
// 点加的Python接口函数
static PyObject *py_add(PyObject *self, PyObject *args)
{
return
}
// 求逆的Python接口函数
static PyObject *py_inv(PyObject *self, PyObject *args)
{
return
}
// 模块方法定义 // 模块方法定义
static PyMethodDef MyMethods[] = { static PyMethodDef MyMethods[] = {
{"multiply", py_multiply, METH_VARARGS, "Multiply a point on the curve by a scalar"}, {"multiply", py_multiply, METH_VARARGS, "Multiply a point on the sm2p256v1 curve"},
{"add", py_add, METH_VARARGS, "Add a point on thesm2p256v1 curve"},
{"inv", py_inv, METH_VARARGS, "Calculate an inv of a number"},
{NULL, NULL, 0, NULL} // 哨兵 {NULL, NULL, 0, NULL} // 哨兵
}; };
// 模块定义 // 模块定义
static struct PyModuleDef tpre = { static struct PyModuleDef tpreECC = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
"tpre", "tpreECC",
NULL, // 模块文档 NULL, // 模块文档
-1, -1,
MyMethods}; MyMethods};
@ -155,5 +180,5 @@ static struct PyModuleDef tpre = {
// 初始化模块 // 初始化模块
PyMODINIT_FUNC PyInit_tpre(void) PyMODINIT_FUNC PyInit_tpre(void)
{ {
return PyModule_Create(&tpre); return PyModule_Create(&tpreECC);
} }