我试图测试循环组的任意生成器。我发现解释表明对于多重组Z * p,我们希望以下条件成立:
我试图使用以下测试:
返回号码
public static BigInteger getGenerator(BigInteger p, BigInteger q){
BigInteger generator = BigInteger.ZERO;
BigInteger oneModP = BigInteger.ONE.mod(p);
for(long i = 2; i < p.intValue()-1; i++)
{
generator = BigInteger.valueOf(i);
if(!generator.pow(2).equals(oneModP)) //if (g^2 = 1 mod p) then go to the next number
{
continue;
}
//TODO: make a method that works with BigInteger^BigInteger. This is hackey and limits us to 32 bits
if(!generator.pow(q.intValue()).equals(oneModP)) //if (g^q != 1modp) then go to the next number
{
continue; //next number!
}
//TODO: randomly generate then test. Sequential isn't really best here...just for testing purposes
else
{
return generator; //return the first one we find for simplicity's sake
}
}
return generator; }
但是,我得到了误报。例如,p = 11(Z * 11)它&#39;将9作为发电机返回。它不应该返回2吗?感谢您的帮助
答案 0 :(得分:1)
这里有几个问题。第一个已经在文本描述中:要获得一个2q阶的元素(或者你想要一个q阶元素?)测试必须是:
下一个问题是计算a ^ b == 1(mod p),这应该是
a.modPow(b, p).equals(BigInteger.ONE)
并且if语句与测试不对应,因为否定是错误的,例如使用
if (generator.modPow(BigInteger.valueOf(2), p).equals(BigInteger.ONE)) continue;