Calculating the exponent of two BigIntegers in Java (implementing Socialist Millionaire)

StackOverflow https://stackoverflow.com/questions/19758692

  •  03-07-2022
  •  | 
  •  

문제

I'm attempting to port a Python file to Java, but I'm having some troubles(could be caused my my limited knowledge of Python). The python file is an example of implementing the Socialist Millionaire problem. I'm having some issues with how Java handles BitInteger operations.

Python:

def step1(self):
    self.x2 = createRandomExponent()
    self.x3 = createRandomExponent()

    self.g2 = pow(self.gen, self.x2, self.mod)
    self.g3 = pow(self.gen, self.x3, self.mod)

    (c1, d1) = self.createLogProof('1', self.x2)
    (c2, d2) = self.createLogProof('2', self.x3)

    # Send g2a, g3a, c1, d1, c2, d2
    return packList(self.g2, self.g3, c1, d1, c2, d2)

Java:

public BigInteger[] step1() {
    x2 = getRandomExponent();
    x3 = getRandomExponent();

    g2 = new BigInteger(gen + "").pow(x2.intValue()).pow(mod.intValue());
    g3 = new BigInteger(gen + "").pow(x3.intValue()).pow(mod.intValue());

    BigInteger[] logProof1 = createLogProof("1", g2);
    BigInteger[] logProof2 = createLogProof("2", g3);

    BigInteger c1 = logProof1[0];
    BigInteger d1 = logProof1[1];
    BigInteger c2 = logProof2[0];
    BigInteger d2 = logProof2[1];

    return new BigInteger[] { g2, g3, c1, d1, c2, d2 };
}

I'm getting the following error(line 24 is where g2 is calculated):

Exception in thread "main" java.lang.ArithmeticException: Negative exponent
    at java.math.BigInteger.pow(BigInteger.java:1395)
    at n/a.crypto.SMPCheck.step1(SMPCheck.java:42)
    at n/a.Testing.main(Testing.java:24)

Which is caused by the fact that BigInteger.intValue() is producing a negative number when called. Does anybody have a solution for calculating the exponent of two BigIntegers?

Python source: http://shanetully.com/2013/08/mitm-protection-via-the-socialist-millionaire-protocol-otr-style/

도움이 되었습니까?

해결책 2

You are using the wrong method to compute the power, then the mod of that. The pow method does not accept negative exponents, and as it seems like you want also to mod that, there is a perfect method for you: modPow. This does an exponentiation, then a mod operation, and it accepts negative exponents.

public BigInteger[] step1() {
    x2 = getRandomExponent();
    x3 = getRandomExponent();

    g2 = new BigInteger(gen + "").modPow(x2, mod);
    g3 = new BigInteger(gen + "").modPow(x3, mod);

    BigInteger[] logProof1 = createLogProof("1", g2);
    BigInteger[] logProof2 = createLogProof("2", g3);

    BigInteger c1 = logProof1[0];
    BigInteger d1 = logProof1[1];
    BigInteger c2 = logProof2[0];
    BigInteger d2 = logProof2[1];

    return new BigInteger[] { g2, g3, c1, d1, c2, d2 };
}

다른 팁

Using high powers and then performing a mod is very slow. For this reason, BigInteger provides a modPow(BigInteger, BigInteger) method which appears to do what you want.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top