Feige菲亚特Shamir实施

时间:2015-04-22 21:16:50

标签: java algorithm math cryptography

我在Java中实施了Fiege Fiat Shamir识别方案,我非常确定它在数学方面很好。 (我已多次检查过)但它永远不会起作用(当调用check时,它几乎总是假的,即使用数字调用也应该有效)。我之前没有序列,(k值为1),它已经让它工作,但现在它不起作用。救命啊!

public class ZKPTimeTrials {

public static int gcd(int p, int q) {
    if (q == 0) return p;
    else return gcd(q, p % q);
}

public static int randomR(int min, int max) {
    Random randgen = new Random();
    return randgen.nextInt((max - min) + 1) + min;
}

public static int getRandomCoprime(int n) {
    int result = n;
    while (gcd(n, result) != 1) {
        result = randomR(2, n-1);
    }
    return result;
}

public static int[] makeSi(int k, int n) {
    int[] result = new int[k];
    for(int i = 0; i < result.length; i++) {
        result[i] = getRandomCoprime(n);
    } 
    return result;
}


public static int[] makeVi(int[] si, int n) {
    int[] result = new int[si.length];
    for(int i = 0; i < result.length; i++) {
        result[i] = (si[i] * si[i]) % n;
    } 
    return result;
}

public static int[] makeEi(int k) {
    int[] result = new int[k];
    for(int i = 0; i < k; i++) {
        result[i] = randomR(0, 1);
    }
    return result;
}

public static int makeY(int r, int[] ei, int[] si, int n) {
    int result = r;
    for(int i = 0; i < si.length; i++) {
        result *= (int) Math.pow(si[i], ei[i]);
    }
    return result % n;
}

public static boolean check(int n, int x, int y, int[] ei, int[] vi) {
    int signBit = ZKPTimeTrials.randomR(0, 1);
    if(signBit == 0) {
        signBit = -1;
    }
    int shouldY = x * signBit;
    for(int i = 0; i < vi.length; i++) {
        shouldY *= (int) Math.pow(vi[i], ei[i]);
    }
    return ((y * y) % n) == shouldY % n;
}

public static void main(String args[]) {
    int n = 71 * 7;
    int t = 50;
    int k = 10;
    int[] si = makeSi(k, n);
    int[] vi = makeVi(si, n);

    int r = randomR(2, n-1);
    int ei[] = makeEi(k);
    int s = randomR(0, 1);
    if(s == 0) {
        s = -1;
    }
    int x = (s * r * r) % n;
    int y = makeY(r, ei, si, n);
    for(int i = 0; i < si.length; i++) System.out.print(ei[i] + " ");
    System.out.println();
    for(int i = 0; i < si.length; i++) System.out.print(si[i] + " ");
    System.out.println(check(n, x, y, ei, vi));
}

}

1 个答案:

答案 0 :(得分:0)

第一个问题是makeY中的整数溢出并检查:在两个函数&#39; result&#39;很可能会溢出,因为您先构建产品并在之后减少模数。尝试在每次乘法后减少mod n以保持&#39;结果&#39;小。

例如在makeY中,写:

int result = r % n;
for (int i = 0; i < si.length; i++) {
    if (ei[i] == 1)
        result = (result * si[i]) % n;
}
return result;

(我还删除了Math.pow()以使其更具可读性和效率,但这不是错误。)

第二个问题是检查功能的逻辑:不需要signBit变量,而是应该检查y * y是否等于shouldY -shouldY。

public static boolean check(int n, int x, int y, int[] ei, int[] vi) {
    int shouldY = x % n;
    for (int i = 0; i < vi.length; i++) {
        if (ei[i] == 1)
            shouldY = (shouldY * vi[i]) % n;
    }
    return (y*y - shouldY) % n == 0 || (y*y + shouldY) % n == 0;
}

通过这些小修正,我设法让你的代码运行。希望它有所帮助...