miracl库下椭圆曲线方程常用函数使用入门

下面列举了椭圆曲线GF(p)素数域常用函数:(持续更新)




1.椭圆曲线方程初始化
ecurve_init
Function: void ecurve_init(A,B,p,type)
big A,B,p;
int type;
Module: mrcurve.c
Description: Initialises the internal parameters of the current active GF(p) elliptic curve. The curve is assumed to be of the form y2 =x3 + Ax + B mod p, the so-called Weierstrass model. This routine can be called subsequently with the parameters of a different curve.
Parameters: Three big numbers A, B and p. The type parameter must be either MR_PROJECTIVE or MR_AFFINE, and specifies whether projective or affine co-ordinates should be used internally. Normally the former is faster. (投影坐标、仿射坐标)
Return value: None

2.点乘
ecurve_mult
Function: void ecurve_mult(k,p,pa)
big k;
epoint *p,*pa;
Module: mrcurve.c
Description: Multiplies a point on a GP(p) elliptic curve by an integer. Uses the addition/subtraction method.
Parameters: A big number k, and two points p and pa. On exit pa=k*p.
Return value: None
Restrictions: The point p must be on the active curve.

3.点乘加快速运算
ecurve_mult2
Function: void ecurve_mult2(k1,p1,k2,p2,pa)
big k1,k2;
epoint *p1,*p2,*pa;
Description: Calculates the point k1.p1+k2.p2 on a GF(p) elliptic curve. This is quicker than doing two separate multiplications and an addition. Useful for certain cryptosystems. (See ecsver.c for example)
Parameters: Two big integers k1 and k2, and three points p1, p2 and pa.
On exit pa = k1.p1+k2.p2
Return value: None

4.点的减法pa=pa-a
ecurve_sub
Function: void ecurve_sub(p,pa)
epoint *p,*pa;
Description: Subtracts two points on a GF(p) elliptic curve. Actually negates p and adds it to pa. Subtraction is quicker if p is normalised.
Parameters: Two points on the current active curve, pa and p. On exit pa = pa-p.
Return value: None
Restrictions: The input points must actually be on the current active curve.

5.比较椭圆曲线上两个点是否相同
epoint_comp
Function: BOOL epoint_comp(p1,p2)
epoint *p1,*p2;
Description: Compares two points on the current active GF(p) elliptic curve.
Parameters: Two points p1 and p2.
Return Value: TRUE if the points are the same, otherwise FALSE.

6.点的复制
epoint_copy
Function: void epoint_copy(p1,p2)
epoint *p1,*p2;
Module: mrcurve.c
Description: Copies one point to another on a GF(p) elliptic curve.
Parameters: Two points p1 and p2. On exit p2=p1.
Return value: None

7.初始化点 返回epoint类型点
epoint_init
Function: epoint* epoint_init()
Module: mrcore.c
Description: Assigns memory to a point on a GF(p) elliptic curve, and initialises it to the "point at infinity".(并将其初始化为“无穷远点”)
Parameters: None.
Return value: A point p (in fact a pointer to a structure allocated from the heap).Parameters: A point p.
C程序员有责任确保通过调用此函数初始化的所有椭圆曲线点最终通过调用epoint_free释放;如果没有,将导致内存泄漏。

8.释放点内存
epoint_free
Function: void epoint_free(p)
epoint *p;
Module: mrcore.c
Description: Frees memory associated with a point on a GF(p) elliptic curve.

9.点坐标设置
epoint_set
Function: BOOL epoint_set(x,y,lsb,p)
big x,y;
int lsb;
epoint *p;
Description: Sets a point on the current active GF(p) elliptic curve (if possible).
Parameters: The integer co-ordinates x and y of the point p. If x and y are not distinct variables then x only is passed to the function, and lsb is taken as the least significant bit of y. In this case the full value of y is reconstructed internally. This is known as “point decompression” (and is a bit time-consuming, requiring the extraction of a modular square root). On exit p=(x,y).
Return value: TRUE if the point exists on the current active point, otherwise FALSE.
Restrictions: None
Example: C=epoint_init();
epoint_set(x,x,1,C);
/* decompress C */

10.检验x坐标是否在椭圆曲线下存在点(合法)
epoint_x
Function: BOOL epoint_x(x)
big x;
Description: Tests to see if the parameter x is a valid co-ordinate of a point on the curve. It is faster to test an x co-ordinate first in this way, rather than trying to directly set it on the curve by calling epoint_set, as it avoids an expensive modular square root.
Parameters: The integer coordinate x.
Return value: TRUE if x is the coordinate of a curve point, otherwise FALSE



这里建立的曲线方程参数是SM2国密算法官方文档给的比较安全的推荐参数:

推荐使用素数域256位椭圆曲线
椭圆曲线方程: y2 = x3 + ax + b
曲线参数:
p=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 00000000 FFFFFFFF FFFFFFFF
a=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 00000000 FFFFFFFF FFFFFFFC
b=28E9FA9E 9D9F5E34 4D5A9E4B CF6509A7 F39789F5 15AB8F92 DDBCBD41 4D940E93
n=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF 7203DF6B 21C6052B 53BBF409 39D54123
Gx=32C4AE2C 1F198119 5F990446 6A39C994 8FE30BBF F2660BE1 715A4589 334C74C7
Gy=BC3736A2 F4F6779C 59BDCEE3 6B692153 D0A9877C C62A4740 02DF32E5 2139F0A0



下面给出一段代码实例(把参数a、b、p、Gx、Gy依次放到txt文档内)

#include <stdio.h>
#include "miracl.h"
int main(){
    big a,b,p,Gx,Gy;
    FILE *fp;
    epoint* G=NULL;
    miracl* mip=mirsys(1000,16);
    a=mirvar(0);
    b=mirvar(0);
    p=mirvar(0); //p 256 bits
    Gx=mirvar(0);
    Gy=mirvar(0);
    fp=fopen("abp.txt","r+");  //fp指向同目录下存放大数的文件
    if(fp==0)
    {
        printf("文件打开失败!");
        exit(1);
    }
    mip->IOBASE=16;
    cinnum(a,fp);
    cinnum(b,fp);
    cinnum(p,fp);
    cinnum(Gx,fp);
    cinnum(Gy,fp);
    fclose(fp);
    /*
    printf("a=");
    cotnum(a,stdout);
    printf("b=");
    cotnum(b,stdout);
    printf("p=");
    cotnum(p,stdout);*/
    ecurve_init(a,b,p,MR_PROJECTIVE);
    G=epoint_init();
    if(epoint_set(Gx,Gy,0,G))
        printf("点G生成成功!\n");
    else
        printf("点G生成失败!\n");
    if(epoint_x(Gx))
        printf("Gx坐标有效!\n");
    else
        printf("Gx坐标无效!\n");
    mirkill(a);
    mirkill(b);
    mirkill(p);
    mirkill(Gx);
    mirkill(Gy);
    epoint_free(G);
    mirexit();
    return 0;
}

执行查看点坐标G是否合法、存在



若修改一下Gx内容:

再次执行:



也可以只修改Gy的值:

执行:

原文地址:https://www.cnblogs.com/Higgerw/p/10164179.html

时间: 2024-10-19 15:46:57

miracl库下椭圆曲线方程常用函数使用入门的相关文章

postgis常用函数介绍(一)

概述: 在进行地理信息系统开发的过程中,常用的空间数据库有esri的sde,postgres的postgis以及mySQL的mysql gis等等,在本文,给大家介绍的是有关postgis的一些常用函数的意思以及使用. 说明: 本文中所使用postgres的版本为9.4.0,你可从我的百度网盘获取相关的安装包,安装包地址如下: postgres:http://pan.baidu.com/s/1o69WORK postgres空间扩展:http://pan.baidu.com/s/1c0fPfpe

总结(5)--- Numpy和Pandas库常用函数

二.常用库 1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数据进行快速运算的标准数学函数(无需编写循环). 用于读写磁盘数据的工具以及用于操作内存映射文件的工具. 线性代数.随机数生成以及傅里叶变换功能. 用于集成C.C++.Fortran等语言编写的代码的工具. 首先要导入numpy库:import numpy as np A NumPy函数和属性: 类型 类型代码 说明 i

2.2.2 ufunc函数&amp;2.2.3 常用函数库

①add对应元素相加 ②subtract对应元素相减 ③数组元素相乘 ④divide.floor_divide除法和向下取整(丢弃余数) ⑤power幂函数 ⑥maximum.fmax返回两个数组较大者组成的数组 ⑦mod取余 ⑧greater.greater_equal.less.less_equal.equal.not_equal:元素比较运算相当于>.>=.<.<=.=.≠ ⑨logical_and.logical_or.logical_xor元素真值运算相当于&.|

turtle库常用函数

turtle库常用函数 引入turtle模块 import turtle turtle的绘图窗体 #setup()设置窗口大小及位置 #setup()可省略 turtle.setup(width,height,startx,starty) turtle.setup(800,800,0,0) turtle.setup(800,800) turtle的RGB色彩模式 #默认采用小数值 可切换为整数值 #1.0:RGB小数值模式 #255:RGB整数值模式 turtle.colormode(mode)

php中mysqli函数库常用函数

在使用php5.6中的mysql函数库时,php会输出一条建议使用mysqli的提示,于是就学习了mysqli mysqli与mysql操作大致相同,少了选择数据库的函数,把这项功能放到的链接数据库的函数里. 常用函数: 参考:http://www.w3school.com.cn/php/php_ref_mysqli.asp

我自己的Javascript 库,封装了一些常用函数 Kingwell.js

我自己的Javascript 库,封装了一些常用函数 Kingwell.js 博客分类: Javascript javascript 库javascript库 现在Javascript库海量,流行的也多,比如jQuery,YUI等,虽然功能强大,但也是不万能的,功能不可能涉及方方面面,自己写一个的JS库是对这些的补充,很多也比较实用,把应用到项目中中去也比较方面,这也是对工作的一些积累,也加深对知识的理解. 2012-6-20更新,添加设置Cookie,获取Cookie,删除Cookie方法.很

numpy函数库中一些常用函数的记录

numpy函数库中一些常用函数的记录 最近才开始接触python,python中为我们提供了大量的库,不太熟悉,因此在<机器学习实战>的学习中,对遇到的一些函数的用法进行记录. (1)mat( ) numpy函数库中存在两种不同的数据类型(矩阵matrix和数组array),都可以用于处理行列表示的数字元素.虽然他们看起来很相似,但是在这两个数据类型上执行相同的数学运算可以得到不同的结果,其中numpy函数库中matrix与MATLAB中matrices等价. 调用mat( )函数可以将数组转

Linux下常用函数-字符串函数

inux下常用函数-字符串函数 atof(将字符串转换成浮点型数)  相关函数   atoi,atol,strtod,strtol,strtoul 表头文件   #include <stdlib.h> 定义函数   double atof(const char *nptr); 函数说明   atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数 字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换 ,并将结果返回.参数nptr字符串可包含正负号.小数点或E

使用prototype扩展的JavaScript常用函数库

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 /** * 检索数组元素(原型扩展或重载) * @param {o} 被检索的元素值 * @type int * @returns 元素索引 */ Array.prototype.contains = function(o) { var index = -1; for(var i=0;i<thi