From c8672e469ac268c5537aa8721d200f9b8c2e9e4e Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Wed, 6 Dec 2023 17:27:06 +0800 Subject: [PATCH] feat: rewrite tpre lib in C --- .gitignore | 3 +- include/tpre.h | 32 ++++++++++++ src/setup.py | 11 ++++ src/tpre.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 include/tpre.h create mode 100644 src/setup.py create mode 100644 src/tpre.c diff --git a/.gitignore b/.gitignore index ff27424..7e9fb11 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ src/temp_message_file src/temp_key_file src/client.db src/server.db -build \ No newline at end of file +build +src/tpre.cpython-311-x86_64-linux-gnu.so diff --git a/include/tpre.h b/include/tpre.h new file mode 100644 index 0000000..dd4278d --- /dev/null +++ b/include/tpre.h @@ -0,0 +1,32 @@ +#ifndef tpre_SM2_H +#define tpre_SM2_H + +#include +#include +#include +#include + +// define TPRE Big Number +typedef uint64_t TPRE_BN[8] + + +// GF(p) +typedef TPRE_BN SM2_Fp; + +// GF(n) +typedef TPRE_BN SM2_Fn; + +// 定义一个结构体来表示雅各比坐标系的点 +typedef struct +{ + TPRE_BN X; + TPRE_BN Y; + TPRE_BN Z; +} JACOBIAN_POINT; + +// 定义一个结构体来表示点 +typedef struct +{ + uint8_t x[32]; + uint8_t y[32]; +} TPRE_POINT; \ No newline at end of file diff --git a/src/setup.py b/src/setup.py new file mode 100644 index 0000000..3d922fa --- /dev/null +++ b/src/setup.py @@ -0,0 +1,11 @@ +from setuptools import setup, Extension + +# 定义您的扩展 +ext = Extension("tpre", sources=["tpre.c"]) + +setup( + name="tpre", + version="1.0", + description="tpre written in C", + ext_modules=[ext], +) diff --git a/src/tpre.c b/src/tpre.c new file mode 100644 index 0000000..7535db7 --- /dev/null +++ b/src/tpre.c @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include "../include/tpre.h" + +const SM2_BN SM2_P = { + 0xffffffff, + 0xffffffff, + 0x00000000, + 0xffffffff, + 0xffffffff, + 0xffffffff, + 0xffffffff, + 0xfffffffe, +}; + +const SM2_BN SM2_B = { + 0x4d940e93, + 0xddbcbd41, + 0x15ab8f92, + 0xf39789f5, + 0xcf6509a7, + 0x4d5a9e4b, + 0x9d9f5e34, + 0x28e9fa9e, +}; + +const SM2_JACOBIAN_POINT _SM2_G = { + { + 0x334c74c7, + 0x715a4589, + 0xf2660be1, + 0x8fe30bbf, + 0x6a39c994, + 0x5f990446, + 0x1f198119, + 0x32c4ae2c, + }, + { + 0x2139f0a0, + 0x02df32e5, + 0xc62a4740, + 0xd0a9877c, + 0x6b692153, + 0x59bdcee3, + 0xf4f6779c, + 0xbc3736a2, + }, + { + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }, +}; +const SM2_JACOBIAN_POINT *SM2_G = &_SM2_G; + +const SM2_BN SM2_N = { + 0x39d54123, + 0x53bbf409, + 0x21c6052b, + 0x7203df6b, + 0xffffffff, + 0xffffffff, + 0xffffffff, + 0xfffffffe, +}; + +// u = (p - 1)/4, u + 1 = (p + 1)/4 +const SM2_BN SM2_U_PLUS_ONE = { + 0x00000000, + 0x40000000, + 0xc0000000, + 0xffffffff, + 0xffffffff, + 0xffffffff, + 0xbfffffff, + 0x3fffffff, +}; + +const SM2_BN SM2_ONE = {1, 0, 0, 0, 0, 0, 0, 0}; +const SM2_BN SM2_TWO = {2, 0, 0, 0, 0, 0, 0, 0}; +const SM2_BN SM2_THREE = {3, 0, 0, 0, 0, 0, 0, 0}; + + + + + +// 将Python中的multiply函数转换为C +static Point multiply(Point a, int64_t n) +{ + Point result; + // ...实现乘法逻辑... + return result; +} + +// Python接口函数 +static PyObject *py_multiply(PyObject *self, PyObject *args) +{ + Point a; + int64_t n; + + // 从Python参数解析值到C变量 + if (!PyArg_ParseTuple(args, "(ll)l", &a.x, &a.y, &n)) + { + return NULL; + } + + Point result = multiply(a, n); + + // 将C结构体的结果转换回Python对象 + return Py_BuildValue("(ll)", result.x, result.y); +} + +// 模块方法定义 +static PyMethodDef MyMethods[] = { + {"multiply", py_multiply, METH_VARARGS, "Multiply a point on the curve by a scalar"}, + {NULL, NULL, 0, NULL} // 哨兵 +}; + +// 模块定义 +static struct PyModuleDef tpre = { + PyModuleDef_HEAD_INIT, + "tpre", + NULL, // 模块文档 + -1, + MyMethods}; + +// 初始化模块 +PyMODINIT_FUNC PyInit_tpre(void) +{ + return PyModule_Create(&tpre); +}