⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ibe_set.cpp

📁 椭圆曲线加密算法
💻 CPP
字号:
/*
   Boneh & Franklin's Identity Based Encryption
   
   Set-up phase

   After this program has run the file common.ibe contains

   <Size of prime modulus in bits>
   <Prime p>
   <Prime q (divides p+1) >
   <Point P - x coordinate>
   <Point P - y coordinate>
   <Point Ppub - x coordinate>
   <Point Ppub - y coordinate>
   <Cube root of unity in Fp2 - x component >
   <Cube root of unity in Fp2 - y component >//cube 环的底

   The file master.ibe contains

   <The master secret s>

   NOTE: define SIMPLE below to use a "simple" fixed group order q
         with the minimum number of 1's. Here we use q=2^159+2^17+1

   Requires: zzn2.cpp big.cpp zzn.cpp ecn.cpp

 */
/////用到big大数类,zzn,zzn2两个整数环,ecurve类,一些数学函数,需要加一个伪随机数产生器,将tate对改成weil对
#include <iostream>
#include <fstream>
#include <cstring>
#include "ecn.h"
#include "zzn.h"//整数环
#include "zzn2.h"//整数环

#define PBITS 512  //require1024bit
#define QBITS 160  //require1024bit
using namespace std;

// Set parameter sizes. For example change PBITS to 1024

Miracl precision(18,0);  // increase if PBITS increases. (36,0) for 1024 bit p



BOOL ibe_set(long seed)//Setup 这一步由PKG完成
{
    ofstream common("common.ibe");
    ofstream master("master.ibe");
    	
	ECn P,Ppub;
    ZZn2 cube;

    Big s,p,q,t,n,cof,x,y;
    // long seed;
    miracl *mip=&precision;

    //cout << "Enter 9 digit random number seed  = ";
    //cin >> seed;
    irand(seed);//irand()随机数产生器,没用


// SET-UP 
//如果使用默认q,则q=2^159+2^17+1 否则,重新生成p和q.//p=2mod3,p=6q-1,p是素数,q是大于3的素数
#ifdef SIMPLE

	q=pow((Big)2,159)+pow((Big)2,17)+1;//我们用的是默认的q

	//q=pow((Big)2,160)-pow((Big)2,76)-1;

#else

	//generate random q 
	forever
	{
		n=rand(QBITS-1,2);  // 159 bit number, base 2 //rand()随机数产生//需要改
		q=2*n+1;            // 160 bit
		while (!prime(q)) q+=2;
		if (bits(q)>QBITS) continue;
		break;
	}

#endif

	//cout << "q= " << q << endl;

	//generate p 
	t=(pow((Big)2,PBITS)-1)/(2*q);
	s=(pow((Big)2,PBITS-1)-1)/(2*q);
	forever 
	{
		n=rand(t);
		if (n<s) continue;
		p=2*n*q-1;
		//if (p%24!=5) continue;  // could be 2 mod 3, also 5 mod 8
		if (p%12!=11) continue;  // must be 2 mod 3, also 3 mod 4
		if (prime(p)) break;
	} 
	//cout << "p= " << p << endl;

    cof=2*n;


//曲线生成ecurve(a2,a4,p, )
    ecurve(0,1,p,MR_PROJECTIVE);    // elliptic curve y^2=x^3+1 mod p

// Find suitable cube root of unity (solution in Fp2 of x^3=1 mod p)
//由x计算求出对应的y
	forever
	{
	    //cube=pow(randn2(),(p+1)*(p-1)/3);
		cube=pow(randn2(),(p+1)/3);
		cube=pow(cube,p-1);
		if (!cube.isunity()) break;
	}
    
	//cout << "Cube root of unity= " << cube << endl;

	if (!(cube*cube*cube).isunity())
	{
		//cout << "sanity check failed" << endl;
		exit(0);
	}


// Choosing an arbitrary P ....
//选择一个点P
    forever
    {
        while (!P.set(randn())) ;
        P*=cof;
        if (!P.iszero()) break;
    }

    cout << "Point P= " << P << endl;


// Pick a random master key s     
    s=rand(q);
    Ppub=s*P;
    cout << "Secret s= " << s << endl;
    cout << "Point Ppub= " << Ppub << endl;

    common << PBITS << endl;
    mip->IOBASE=16;
    common << p << endl;
    common << q << endl;
    P.get(x,y);
    common << x << endl;
    common << y << endl;
    Ppub.get(x,y);
    common << x << endl;
    common << y << endl;
    cube.get(x,y);
    common << x << endl;
    common << y << endl;

    master << s << endl;    

    return 0;
}
/*
int main()
{
	long seed;
    cout << "Enter 9 digit random number seed  = ";
    cin >> seed;
	ibe_set(seed);
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -