ecn.h 和 ecn.cpp

定义ECn类,其中“ECn”表示“Arithmetic on an Elliptic Curve, mod n”。

如果没有定义ECN_H,则定义ECN_H,防止重复定义引发冲突。包含头文件cstringbig.h,其中<cstring>是C语言中<string.h>的C++版本,主要提供了一些操作C风格字符串(即以空字符 '\0' 结尾的字符数组)的函数和工具,如strcpystrlenstrcatstrcmp等函数都在这个头文件中声明,<cstring>更适合用在C语言库的兼容或性能优化;big.h详解参考big.h

#ifndef ECN_H
#define ECN_H

#include <cstring>
#include "big.h"

1. MR_INIT_ECN

如果定义了ZZNS,则定义MR_INIT_ECNmemset,否则定义为mem

#ifdef ZZNS
#define MR_INIT_ECN memset(mem,0,mr_ecp_reserve(1,ZZNS)); p=(epoint *)epoint_init_mem_variable(mem,0,ZZNS); 
#else
#define MR_INIT_ECN mem=(char *)ecp_memalloc(1); p=(epoint *)epoint_init_mem(mem,0); 
#endif
  1. 如果定义ZZNS,把mem指向的mr_ecp_reserve个字节空间初始化为0,memchar类型数组。 epoint_init_mem_variable用字符数组mem初始化一个epoint类型指针,也就是得到椭圆曲线的一个点。

  2. 如果没有定义ZZNSmem指向ecp_memalloc分配的内存, epoint_init_mem将字符串mem初始化为椭圆曲线上的点。

#ifdef ZZNS
    char mem[mr_ecp_reserve(1,ZZNS)];
#else
    char *mem;
#endif

2. ECn类

如果定义ZZNS,则设置mem的大小为mr_ecp_reserve的数组,否则mem作为char *类型指针。

定义ECn类的构造函数,其构造函数有4个,在构造函数中用MR_INIT_ECN完成对ECn初始化,MR_INIT_ECN就定义在这个文件中。

epoint_set在椭圆曲线上初始化一个点,epoint_copy实现椭圆曲线上两个点的复制。

get_point取出点p,get_status取出当前的状态。

ecurve_add实现椭圆曲线上的两个点相加,pa=pa+p

class ECn
{
    epoint *p;
#ifdef ZZNS
    char mem[mr_ecp_reserve(1,ZZNS)]; // 设置一个数组
#else
    char *mem;
#endif
public:
    ECn()                           {MR_INIT_ECN }

    ECn(const Big &x,const Big& y)  {MR_INIT_ECN 
                                   epoint_set(x.getbig(),y.getbig(),0,p); }

  // This next constructor restores a point on the curve from "compressed" 
  // data, that is the full x co-ordinate, and the LSB of y  (0 or 1)

#ifndef MR_SUPPORT_COMPRESSION
    ECn(const Big& x,int cb)             {MR_INIT_ECN
                                   epoint_set(x.getbig(),x.getbig(),cb,p); }
#endif

    ECn(const ECn &b)                   {MR_INIT_ECN epoint_copy(b.p,p);}

    epoint *get_point() const;
    int get_status() {return p->marker;}
    ECn& operator=(const ECn& b)  {epoint_copy(b.p,p);return *this;}

    ECn& operator+=(const ECn& b) {ecurve_add(b.p,p); return *this;}

    int add(const ECn&,big *,big *ex1=NULL,big *ex2=NULL) const; 
                                  // returns line slope as a big
    int sub(const ECn&,big *,big *ex1=NULL,big *ex2=NULL) const;         

    ECn& operator-=(const ECn& b) {ecurve_sub(b.p,p); return *this;}

  // Multiplication of a point by an integer. 

    ECn& operator*=(const Big& k) {ecurve_mult(k.getbig(),p,p); return *this;}

    void clear() {epoint_set(NULL,NULL,0,p);}
    BOOL set(const Big& x,const Big& y)    {return epoint_set(x.getbig(),y.getbig(),0,p);}
#ifndef MR_AFFINE_ONLY
// use with care if at all
    void setz(const Big& z) {nres(z.getbig(),p->Z); p->marker=MR_EPOINT_GENERAL;}
#endif
    BOOL iszero() const;
    int get(Big& x,Big& y) const;

  // This gets the point in compressed form. Return value is LSB of y-coordinate
    int get(Big& x) const;

  // get raw coordinates
    void getx(Big &x) const;
    void getxy(Big &x,Big &y) const;
    void getxyz(Big &x,Big &y,Big &z) const;

  // point compression

  // This sets the point from compressed form. cb is LSB of y coordinate 
#ifndef MR_SUPPORT_COMPRESSION
    BOOL set(const Big& x,int cb=0)  {return epoint_set(x.getbig(),x.getbig(),cb,p);}
#endif
    friend ECn operator-(const ECn&);
    friend void multi_add(int,ECn *,ECn *);
    friend void double_add(ECn&,ECn&,ECn&,ECn&,big&,big&);

    friend ECn mul(const Big&, const ECn&, const Big&, const ECn&);
    friend ECn mul(int, const Big *, ECn *);

    friend void normalise(ECn &e) {epoint_norm(e.p);}
    friend void multi_norm(int,ECn *);

    friend BOOL operator==(const ECn& a,const ECn& b)
                                  {return epoint_comp(a.p,b.p);}    
    friend BOOL operator!=(const ECn& a,const ECn& b)
                                  {return (!epoint_comp(a.p,b.p));}    

    friend ECn operator*(const Big &,const ECn&);

#ifndef MR_NO_STANDARD_IO

    friend ostream& operator<<(ostream&,const ECn&);

#endif

    ~ECn() {
#ifndef ZZNS
        mr_free(mem); 
#endif
 }

};

ECn实现的函数

add椭圆曲线上两点之和。

sub减法运算。

operator-

double_add

get

getx

getxy

getxyz

is_on_curve

iszero

operator<<

mul

multi_add

multi_norm

get_point

operator*

get_status

© phdlisl all right reserved,powered by GitbookUpdate in 2025-10-11

results matching ""

    No results matching ""