📄 quantum.cpp
字号:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<signal.h>
#include<dos.h>
#include<conio.h>
#define byte unsigned int
#define NULL 0
#define buffersize 200
#define Alice 121
#define Bob 234
#define ack '@'
#define end '&'
#define size 1000
int z1=0;
int r;
int bitseq[1000];
float negition=0;
typedef struct{
byte use;
int p1;
int p2;
byte full;
}Qchannel;
typedef struct{
byte dest;
byte source;
byte buffer[size];
byte full;
}Pchannel;
typedef struct{
byte bitseq[buffersize];/*随机数列*/
byte alphabet[buffersize];/*基*/
byte state[buffersize];/*零或一*/
}Per_buffer;
int *Bob_final_key;
int *Alice_final_key;
Qchannel *quantum;
Qchannel *Alice_quantum;
Qchannel *Eve_quantum;
Qchannel *Bob_quantum;
Pchannel *old_channel;
Pchannel *Alice_pool;
Pchannel *Bob_pool;
Per_buffer *Alice_per;
Per_buffer *Bob_per;
Per_buffer *Eve_per;
int counter;
void readbuffer(Per_buffer *bufferptr);
Per_buffer*Get_per_buffer(void);
Pchannel*Get_Pchannel(void);
Qchannel*Get_Qchannel(void);
void initrandom();
void readchannel();
void quantumtras();
void Onchannel();
void Eve_interupt();
void Eve_measure();
byte measure(Qchannel *ptr,Per_buffer *ftr);
void alphabet(byte qubit,byte alp,Qchannel *ptr);
byte prob();
byte condition(int p1,int p2,byte i);
void Bob_receieve();
void Qsetup();
void Bob_ack();
void Bob_measure();
void init();
void AliceToBob();
void Bob_get();
void BobToAlice();
void Alice_get();
void Alice_measure();
void Alice_send_quantum();
void convert(byte qubit);
void Bob_send_alphabet();
void Alice_compare(int index);
void Alice_send_raw();
void Bob_ext_raw(int index);
int Alice_detect(int n);
int calculate();
int Bob_send_neg(int neg_len);
void recycle();
void clear();
void logo();
void Get_key();
#define M 1500 /* 执行次数 */
#define N 200 /* 数组大小 */
void binsrch( int m[N],int k)
{
int low,high,mid;
low=0;high=N-1;
while (low<=high)
{
mid=(low+high)/2;
if (k>m[mid])
high=mid-1;
if (k<m[mid])
low=mid+1;
if (k==m[mid])
{
printf("找到此数",mid+1,k);
return;
}
}
return;
}
void main()
{
/*long start,end;
start=clock(); //时间开始函数*/
int a[N],t,s;
double start,finish; /* 开始时间,结束时间 */
start=(double)clock(); /* 我的time.h内没有CLOCKS_PER_SEC */
for (s=0;s<M;s++) { /* 执行M次 */
for (t=0;t<N;*(a+t++)=rand()%10); /* 每次重新赋值 */
binsrch(a,N-1); //单独使用二分法
}
finish=(double)clock();
printf("%.4fms",(finish-start)/M);
int raw_len;
int neg_len;
int result;
int i;
printf("Please write 1/0,1 means Eve is here!\n");
scanf("%d",&r);
logo();
init();
printf("\n");
printf("\n");
/*stage1:quantum comunication*/
printf("Then the system enter stage 1.This will take several minutes!\n");
for(counter=0;counter<buffersize;counter++)
{
Alice_send_quantum();
}
printf("\nAlice send bits: \n");
for( i=0;i<buffersize;i++)
printf("%d",Alice_per->bitseq[i]);
printf("\n");
printf("\nBob receive bits: \n");
for(i=0;i<buffersize;i++)
printf("%d",Bob_per->bitseq[i]);
if(r==1)
{
printf("\nEve receive bits: \n");
for(int k=0;k<buffersize;k++)
printf("%d",Eve_per->bitseq[k]);
}
printf("\nAlice use alphabet: \n");
for(i=0;i<buffersize;i++)
printf("%d",Alice_per->alphabet[i]);
printf("\nBob use alphabet: \n");
for(i=0;i<buffersize;i++)
printf("%d",Bob_per->alphabet[i]);
if(r==1)
{printf("\nEve use alphabet: \n");
for(i=0;i<buffersize;i++)
printf("%d",Eve_per->alphabet[i]);
}/*recycle();*/
Bob_send_alphabet();
Alice_send_raw();
/*stage2:detect Eve's presence*/
raw_len=calculate();
neg_len=raw_len/2;
result=Bob_send_neg(neg_len);
if(result==0)
{
printf("Secret key distribution success!\n");
Get_key();
exit(0);
}
else if(result/negition>0.25)
{ printf("The error fate is %f\n",result/negition);//计算出窃听时产生的误码率
printf("Eve is detected!Secret key distribution failed\n");
exit(0);
}
else
{
raw_len=calculate();
neg_len=raw_len/2;
result=Bob_send_neg(neg_len);
printf("The error fate is %f\n",result/negition);
printf("The error fate may be caused of environment'noise\n");
/////////////////////
}
/*Get back resource*/
clear();
//getchar();
//getch();
system("pause");
//return 0;
}
void readbuffer(Per_buffer*bufferptr)
{
int i;
for(i=0;i<buffersize;i++)
{
printf(" %d",bufferptr->bitseq[i]);
if((i-(i/10)*10)==9)printf("\n");
}
printf("\n");
for(i=0;i<buffersize;i++)
{
printf(" %d",bufferptr->alphabet[i]);
if((i-(i/10)*10)==9)printf("\n");
}
printf("\n");
for(i=0;i<buffersize;i++)
{
printf(" %d",bufferptr->state[i]);
if((i-(i/10)*10)==9)printf("\n");
}
printf("\n");
return;
}
Per_buffer*Get_per_buffer(void)
{
Per_buffer*ptr;
ptr=(Per_buffer*)malloc(sizeof(Per_buffer));
if(ptr==NULL)
{
printf("ERROR:not enough memory!\n");
exit(0);
}
return ptr;
}
Pchannel*Get_Pchannel(void)
{
Pchannel *ptr;
ptr=(Pchannel*)malloc(sizeof(Pchannel));
if(ptr==NULL)
{
printf("ERROR:not enough memory!\n");
exit(0);
}
return ptr;
}
Qchannel*Get_Qchannel(void)
{
Qchannel *ptr;
ptr=(Qchannel*)malloc(sizeof(Qchannel));
if(ptr==NULL)
{
printf("ERROR:not enough memory!\n");
exit(0);
}
return ptr;
}
void initrandom()
{
FILE*fp1,*fp2;
int count1,count2;
char ch1,ch2;
printf("Rand number A:\n");
if((fp1=fopen("F:\\量子密钥分配(二分法BB84协议)\\a.log","rt"))==NULL)
{
printf("cannot openfile A!press any key exit!\n");
}
ch1=fgetc(fp1);
count1=0;
while((ch1!=EOF)&&(count1<buffersize))
{
ch1=ch1-(ch1/2)*2;
Alice_per->bitseq[count1]=ch1;
printf("%d",ch1);
count1++;
ch1=fgetc(fp1);
}
fclose(fp1);
printf("\n");
printf("Rand number B:\n");
if((fp2=fopen("F:\\量子密钥分配(二分法BB84协议)\\b.log","rt"))==NULL)
{
printf("cannot openfile B!press any key exit!\n");
}
ch2=fgetc(fp2);
count2=0;
while((ch2!=EOF)&&(count2<size))
{
ch2=ch2-(ch2/2)*2;
bitseq[count2]=ch2;
printf("%d",ch2);
count2++;
ch2=fgetc(fp2);
}
fclose(fp2);
printf("\n");
}
void readchannel(Qchannel*ptr)
{
printf("use %d \n",ptr->use);
printf("p1 %d\n",ptr->p1);
printf("p2 %d\n",ptr->p2);
printf("full %d\n",ptr->full);
return;
}
void quantumtras()
{
quantum=Alice_quantum;
Onchannel();
return;
}
void Onchannel()
{
Eve_interupt();
Bob_receieve();
return;
}
void Eve_interupt()
{
if(r==1)
{
Eve_quantum=quantum;
Eve_measure();
quantum=Eve_quantum;
}
return;
}
void Eve_measure()
{
byte qubit;
if((Eve_quantum->use==1)&&(Eve_quantum->full==0))
{
printf("Eve:I know the channel is setup!\n");
return;
}
if((Eve_quantum->use==1)&&(Eve_quantum->full==1))
{
qubit=measure(Eve_quantum,Eve_per);
Eve_per->bitseq[counter]=qubit;
return;
}
}
byte measure(Qchannel *ptr,Per_buffer*ftr)
{
byte alp;
byte qubit;
alp=prob();
ftr->alphabet[counter]=alp;
qubit=condition(ptr->p1,ptr->p2,alp);
alphabet(qubit,alp,ptr);
return qubit;
}
void alphabet(byte qubit,byte alp,Qchannel *ptr)
/*typedef struct{
byte use;
int p1;
int p2;
byte full;
}Qchannel;*/
{
if(alp==0)
{
if(qubit==1)
{
ptr->p1=1;
ptr->p2=0;
}
else
{
ptr->p1=0;
ptr->p2=1;
}
}
else
{
if(qubit==1)
{
ptr->p1=2;
ptr->p2=2;
}
else
{
ptr->p1=2;
ptr->p2=-2;
}
}
ptr->full=1;
ptr->use=1;
return;
}
byte prob()/*<time.>*/
{
z1++;
return bitseq[z1];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -