1. 程式人生 > >miracl庫下橢圓曲線方程常用函數使用入門

miracl庫下橢圓曲線方程常用函數使用入門

system gate val ets sse compress 推薦 crypt separate

下面列舉了橢圓曲線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;
}
View Code

執行查看點坐標G是否合法、存在

技術分享圖片


若修改一下Gx內容:

技術分享圖片

再次執行:

技術分享圖片


也可以只修改Gy的值:

技術分享圖片

執行:

技術分享圖片

miracl庫下橢圓曲線方程常用函數使用入門