sqrt Mod Prime - Java java.lang

Java examples for java.lang:Math Prime Number

Description

sqrt Mod Prime

Demo Code

/*//w  w  w .java  2 s. c om
 * UniCrypt
 *
 *  UniCrypt(tm) : Cryptographical framework allowing the implementation of cryptographic protocols e.g. e-voting
 *  Copyright (C) 2014 Bern University of Applied Sciences (BFH), Research Institute for
 *  Security in the Information Society (RISIS), E-Voting Group (EVG)
 *  Quellgasse 21, CH-2501 Biel, Switzerland
 *
 *  Licensed under Dual License consisting of:
 *  1. GNU Affero General Public License (AGPL) v3
 *  and
 *  2. Commercial license
 *
 *
 *  1. This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  2. Licensees holding valid commercial licenses for UniCrypt may use this file in
 *   accordance with the commercial license agreement provided with the
 *   Software or, alternatively, in accordance with the terms contained in
 *   a written agreement between you and Bern University of Applied Sciences (BFH), Research Institute for
 *   Security in the Information Society (RISIS), E-Voting Group (EVG)
 *   Quellgasse 21, CH-2501 Biel, Switzerland.
 *
 *
 *   For further information contact <e-mail: unicrypt@bfh.ch>
 *
 *
 * Redistributions of files must retain the above copyright notice.
 */
//package com.java2s;
import java.math.BigInteger;

public class Main {
    public static BigInteger sqrtModPrime(BigInteger rSquare, BigInteger p) {
        BigInteger two = new BigInteger("2");
        BigInteger z = two;

        //z which must be a quadratic non-residue mod p.
        while (hasSqrtModPrime(z, p)) {
            z = z.add(BigInteger.ONE);
        }

        if (!hasSqrtModPrime(rSquare, p)) {
            throw new UnknownError("r has no square root");
        } else {
            if (p.mod(new BigInteger("4")).equals(new BigInteger("3"))) {
                return rSquare.modPow(
                        p.add(BigInteger.ONE).divide(new BigInteger("4")),
                        p);
            } else {
                BigInteger pMin1 = p.subtract(BigInteger.ONE); //p-1
                BigInteger s = BigInteger.ONE;
                BigInteger q = pMin1.divide(two);

                //Finding Q
                while (q.mod(two).equals(BigInteger.ZERO)) {
                    q = q.divide(two);
                    s = s.add(BigInteger.ONE);
                }

                BigInteger c = z.modPow(q, p);
                BigInteger r = rSquare.modPow(
                        q.add(BigInteger.ONE).divide(two), p);
                BigInteger t = rSquare.modPow(q, p);
                BigInteger m = s;

                //Loop until t==1
                while (!t.equals(BigInteger.ONE)) {
                    BigInteger i = BigInteger.ZERO;
                    while (!BigInteger.ONE.equals(t.modPow(
                            two.modPow(i, p), p))) {
                        i = i.add(BigInteger.ONE);
                    }

                    BigInteger b = c.modPow(two.modPow(m.subtract(i)
                            .subtract(BigInteger.ONE), p), p);
                    r = r.multiply(b).mod(p);
                    t = t.multiply(b.pow(2)).mod(p);
                    c = b.modPow(two, p);
                    m = i;
                }

                if (r.modPow(two, p).equals(rSquare.mod(p))) {
                    return r;
                } else {
                    throw new IllegalArgumentException("Tonnelli fails...");
                }

            }
        }
    }

    public static boolean hasSqrtModPrime(BigInteger r, BigInteger p) {
        BigInteger two = new BigInteger("2");
        return r.modPow(p.subtract(BigInteger.ONE).divide(two), p).equals(
                BigInteger.ONE);
    }
}

Related Tutorials