import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.junit.Test;
public class Cascade {
//密钥总长度
int n = 10000;
//误码率
double p = 0.01;
//块长参数
double alpha = 0.58;
//块长增长参数
double beta = 2.5;
//Alice的密钥
int[] aliceKeys;
//Bob的密钥
int[] bobKeys;
//第二轮置乱后的Alice密钥
int[] alieceKeysShuffled;
//第二轮置乱后Bob的密钥
int[] bobKeysShuffled;
//第一轮Alice的分块,如aliceBlock[0]表示第一个分块,也是个数组
int[][] aliceBlocks1;
//第一轮Bob的分块
int[][] bobBlocks1;
//第二轮Alice的分块
int[][] aliceBlocks2;
//第二轮Bob的分块
int[][] bobBlocks2;
//第一轮分块长度
int blockLength1 = (int) Math.rint(alpha/p);
//第二轮分块长度
int blockLength2 = (int) Math.rint(blockLength1 * beta);
//第三轮Alice的密钥
int[] aliceKeysR3;
//第三轮Bob的密钥
int[] bobKeysR3;
//置乱的规则
int[] mapRule;
//泄露的信息
int leakedMsg = 0;
//已纠正的错误数
int correctedErr = 0;
//错误分块的集合(都是第一轮的分块),即第几个分块还存在奇数个错误
List<Integer> errBlocks = new LinkedList<Integer>();
//用于产生随机数
Random r = new Random();
public static void main(String[] args) {
//设置循环次数
int num = 1000;
//协议成功次数
int suc = 0;
double eff = 0;
for (int i=0; i<num; i++){
Cascade c = new Cascade();
eff += c.cascade();
if (c.correctedErr == c.n * c.p) suc += 1;
}
eff = eff/num;
System.out.println("效率: "+eff);
System.out.println("成功概率:"+(double)suc/num);
}
//执行cascade流程,返回协商效率
public double cascade(){
//产生Alice和Bob的密钥
aliceKeys = aliceKeysGenerator();
//System.out.println(Arrays.toString(aliceKeys));
bobKeys = bobKeysGenerator();
//System.out.println(Arrays.toString(bobKeys));
//产生Alice和Bob的分块密钥
aliceBlocks1 = divIntoBlocks(aliceKeys, blockLength1);
bobBlocks1 = divIntoBlocks(bobKeys, blockLength1);
//printBlocks(aliceBlocks1,bobBlocks1);
//第一轮纠错
for (int i=0; i<aliceBlocks1.length; i++){
boolean parity = parityCheck(aliceBlocks1[i], bobBlocks1[i]);
//System.out.print(parity+" ");
if (parity == false){
int index = binary(aliceBlocks1[i], bobBlocks1[i]);
bobKeys[blockLength1*i + index] = (bobKeys[blockLength1*i &
cascade算法的java实现
最新推荐文章于 2023-12-23 11:39:28 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)