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

Posted higgerw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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生成成功!
");
    else
        printf("点G生成失败!
");
    if(epoint_x(Gx))
        printf("Gx坐标有效!
");
    else
        printf("Gx坐标无效!
");
    mirkill(a);
    mirkill(b);
    mirkill(p);
    mirkill(Gx);
    mirkill(Gy);
    epoint_free(G);
    mirexit();
    return 0;
}
View Code

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

技术分享图片


 

若修改一下Gx内容:

技术分享图片

再次执行:

技术分享图片


 

也可以只修改Gy的值:

技术分享图片

执行:

技术分享图片





















































































以上是关于miracl库下椭圆曲线方程常用函数使用入门的主要内容,如果未能解决你的问题,请参考以下文章

椭圆曲线密码学

椭圆曲线密码体制(ECC)简介

Python -- matplotlib 椭圆曲线

MT333二次曲线系方程

椭圆曲线算法:简单介绍

golang 椭圆曲线加密使用ecdsa