Computes the elegant pairing function for a given list of non-negative BigInteger values. - Java java.lang

Java examples for java.lang:Math Calculation

Description

Computes the elegant pairing function for a given list of non-negative BigInteger values.

Demo Code

/*/*  w ww .  j a va 2  s. co  m*/
 * 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.
 */
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;

public class Main{
    /**
     * Computes the elegant pairing function for a given list of non-negative BigInteger values. The size of the given
     * input list is taken as an additional input value.
     * <p>
     * @see <a href="http://szudzik.com/ElegantPairing.pdf">ElegantPairing.pdf</a>
     * @param values The given values
     * @return The result of applying the elegant pairing function
     * @throws IllegalArgumentException if {@literal values} is null
     * @throws IllegalArgumentException if {@literal values} contains null or negative value
     */
    public static BigInteger pairWithSize(BigInteger... values) {
        if (values == null) {
            throw new IllegalArgumentException();
        }
        return pair(pair(values), BigInteger.valueOf(values.length));
    }
    /**
     * Computes the elegant pairing function for two non-negative BigInteger values.
     * <p>
     * @see <a href="http://szudzik.com/ElegantPairing.pdf">ElegantPairing.pdf</a>
     * @param value1 The first value
     * @param value2 The second value
     * @return The result of applying the elegant pairing function
     * @throws IllegalArgumentException if {@literal value1} or {@literal value2} is null or negative
     */
    public static BigInteger pair(BigInteger value1, BigInteger value2) {
        if (value1 == null || value1.signum() < 0 || value2 == null
                || value2.signum() < 0) {
            throw new IllegalArgumentException();
        }
        if (value1.compareTo(value2) < 0) {
            return value2.multiply(value2).add(value1);
        }
        return value1.multiply(value1).add(value1).add(value2);
    }
    public static BigInteger pair(int... values) {
        if (values == null) {
            throw new IllegalArgumentException();
        }
        BigInteger[] bigIntegers = new BigInteger[values.length];
        for (int i = 0; i < values.length; i++) {
            bigIntegers[i] = BigInteger.valueOf(values[i]);
        }
        return MathUtil.pair(bigIntegers);
    }
    /**
     * Computes the elegant pairing function for a given list of non-negative BigInteger values. The order in which the
     * binary pairing function is applied is recursively from left to right.
     * <p>
     * @see <a href="http://szudzik.com/ElegantPairing.pdf">ElegantPairing.pdf</a>
     * @param values The given values
     * @return The result of applying the elegant pairing function
     * @throws IllegalArgumentException if {@literal values} is null
     * @throws IllegalArgumentException if {@literal values} contains null or negative value
     */
    public static BigInteger pair(BigInteger... values) {
        if (values == null) {
            throw new IllegalArgumentException();
        }
        int n = values.length;
        if (n == 0) {
            return BigInteger.ZERO;
        }
        if (n == 1) {
            return values[0];
        }
        BigInteger[] a = new BigInteger[n / 2 + n % 2];
        for (int i = 0; i < n / 2; i++) {
            a[i] = pair(values[2 * i], values[2 * i + 1]);
        }
        if (n % 2 == 1) {
            a[n / 2] = values[n - 1];
        }
        return pair(a);
    }
}

Related Tutorials