// Path: src/main/java/com/tiemens/secretshare/exceptions/SecretShareException.java
// public class SecretShareException
//     extends RuntimeException
// {
//     // ==================================================
//     // class static data
//     // ==================================================
// 
//     private static final long serialVersionUID = 2193317873773384997L;
// 
//     // ==================================================
//     // class static methods
//     // ==================================================
// 
// 
// 
//     // ==================================================
//     // instance data
//     // ==================================================
// 
//     // ==================================================
//     // factories
//     // ==================================================
// 
//     // ==================================================
//     // constructors
//     // ==================================================
// 
// 
//     /** Constructs a new runtime exception with the specified detail message.
//      * The cause is not initialized, and may subsequently be initialized by a
//      * call to {@link #initCause}.
//      *
//      * @param   message   the detail message. The detail message is saved for
//      *          later retrieval by the {@link #getMessage()} method.
//      */
//     public SecretShareException(final String message)
//     {
//         super(message);
//     }
// 
//     /**
//      * Constructs a new runtime exception with the specified detail message and
//      * cause.  <p>Note that the detail message associated with
//      * <code>cause</code> is <i>not</i> automatically incorporated in
//      * this runtime exception's detail message.
//      *
//      * @param  message the detail message (which is saved for later retrieval
//      *         by the {@link #getMessage()} method).
//      * @param  cause the cause (which is saved for later retrieval by the
//      *         {@link #getCause()} method).  (A <code>null</code> value is
//      *         permitted, and indicates that the cause is nonexistent or
//      *         unknown.)
//      */
//     public SecretShareException(final String message,
//                                 final Throwable cause)
//     {
//         super(message, cause);
//     }
// 
//     // ==================================================
//     // public methods
//     // ==================================================
// 
//     // ==================================================
//     // non public methods
//     // ==================================================
// }


import java.io.PrintStream;
import java.math.BigInteger;
import com.tiemens.secretshare.exceptions.SecretShareException;
/*******************************************************************************
 * Copyright (c) 2009, 2014 Tim Tiemens.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *
 * 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 Lesser General Public License for more details.
 *
 *
 * Contributors:
 *     Tim Tiemens - initial API and implementation
 *******************************************************************************/
package com.tiemens.secretshare.math.equation;



/**
 * Polynomial equation implementation.
 *   8*x^3  + 5*x^2  + 13*x   + 432
 *   [term3]  [term2]  [term1]  [term0]
 *
 * Arguments to the constructor are in this order:
 *   432     + 13*x    + 5*x^2 + 8*x^3
 *   [term0]   [term1]  [term2]  [term3]
 *
 *
 *  Provides support ONLY for integer exponent values.
 *  Provides support ONLY for BigInteger values of "x".
 *  Provides support ONLY for BigInteger values of "coefficients"
 *
 *  It may be a "short distance" between BigInteger and BigDecimal,
 *  but that distance is still too big to justify making
 *  this implementation more complex.
 *
 * @author tiemens
 *
 */
public class PolyEquationImpl
{

    // ==================================================
    // class static data
    // ==================================================

    // ==================================================
    // class static methods
    // ==================================================

    // ==================================================
    // instance data
    // ==================================================

    // [0] is x^0, i.e. "*1" the "constant term"
    // [1] is x^1, i.e. "*x" the "x term"
    // [2] is x^2, i.e. the "*x^2 term"
    private BigInteger[] coefficients;

    // ==================================================
    // factories
    // ==================================================

    /**
     * Helper factory to create instances.
     * Accepts 'int', converts them to BigInteger, and calls the constructor.
     *
     * @param args as int values
     * @return instance
     */
    public static PolyEquationImpl create(final int... args)
    {
        BigInteger[] bigints = new BigInteger[args.length];
        for (int i = 0, n = args.length; i < n; i++)
        {
            bigints[i] = BigInteger.valueOf(args[i]);
        }
        return new PolyEquationImpl(bigints);
    }
    // ==================================================
    // constructors
    // ==================================================
    public PolyEquationImpl(final BigInteger[] inCoeffs)
    {
        if (inCoeffs.length <= 0)
        {
            throw new SecretShareException("Must have at least 1 coefficient");
        }
        coefficients = new BigInteger[inCoeffs.length];

        for (int i = 0, n = inCoeffs.length; i < n; i++)
        {
            if (inCoeffs[i] == null)
            {
// Path: src/main/java/com/tiemens/secretshare/exceptions/SecretShareException.java
// public class SecretShareException
//     extends RuntimeException
// {
//     // ==================================================
//     // class static data
//     // ==================================================
// 
//     private static final long serialVersionUID = 2193317873773384997L;
// 
//     // ==================================================
//     // class static methods
//     // ==================================================
// 
// 
// 
//     // ==================================================
//     // instance data
//     // ==================================================
// 
//     // ==================================================
//     // factories
//     // ==================================================
// 
//     // ==================================================
//     // constructors
//     // ==================================================
// 
// 
//     /** Constructs a new runtime exception with the specified detail message.
//      * The cause is not initialized, and may subsequently be initialized by a
//      * call to {@link #initCause}.
//      *
//      * @param   message   the detail message. The detail message is saved for
//      *          later retrieval by the {@link #getMessage()} method.
//      */
//     public SecretShareException(final String message)
//     {
//         super(message);
//     }
// 
//     /**
//      * Constructs a new runtime exception with the specified detail message and
//      * cause.  <p>Note that the detail message associated with
//      * <code>cause</code> is <i>not</i> automatically incorporated in
//      * this runtime exception's detail message.
//      *
//      * @param  message the detail message (which is saved for later retrieval
//      *         by the {@link #getMessage()} method).
//      * @param  cause the cause (which is saved for later retrieval by the
//      *         {@link #getCause()} method).  (A <code>null</code> value is
//      *         permitted, and indicates that the cause is nonexistent or
//      *         unknown.)
//      */
//     public SecretShareException(final String message,
//                                 final Throwable cause)
//     {
//         super(message, cause);
//     }
// 
//     // ==================================================
//     // public methods
//     // ==================================================
// 
//     // ==================================================
//     // non public methods
//     // ==================================================
// }


// Path: src/main/java/com/tiemens/secretshare/math/equation/PolyEquationImpl.java
import java.io.PrintStream;
import java.math.BigInteger;
import com.tiemens.secretshare.exceptions.SecretShareException;

/*******************************************************************************
 * Copyright (c) 2009, 2014 Tim Tiemens.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *
 * 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 Lesser General Public License for more details.
 *
 *
 * Contributors:
 *     Tim Tiemens - initial API and implementation
 *******************************************************************************/
package com.tiemens.secretshare.math.equation;



/**
 * Polynomial equation implementation.
 *   8*x^3  + 5*x^2  + 13*x   + 432
 *   [term3]  [term2]  [term1]  [term0]
 *
 * Arguments to the constructor are in this order:
 *   432     + 13*x    + 5*x^2 + 8*x^3
 *   [term0]   [term1]  [term2]  [term3]
 *
 *
 *  Provides support ONLY for integer exponent values.
 *  Provides support ONLY for BigInteger values of "x".
 *  Provides support ONLY for BigInteger values of "coefficients"
 *
 *  It may be a "short distance" between BigInteger and BigDecimal,
 *  but that distance is still too big to justify making
 *  this implementation more complex.
 *
 * @author tiemens
 *
 */
public class PolyEquationImpl
{

    // ==================================================
    // class static data
    // ==================================================

    // ==================================================
    // class static methods
    // ==================================================

    // ==================================================
    // instance data
    // ==================================================

    // [0] is x^0, i.e. "*1" the "constant term"
    // [1] is x^1, i.e. "*x" the "x term"
    // [2] is x^2, i.e. the "*x^2 term"
    private BigInteger[] coefficients;

    // ==================================================
    // factories
    // ==================================================

    /**
     * Helper factory to create instances.
     * Accepts 'int', converts them to BigInteger, and calls the constructor.
     *
     * @param args as int values
     * @return instance
     */
    public static PolyEquationImpl create(final int... args)
    {
        BigInteger[] bigints = new BigInteger[args.length];
        for (int i = 0, n = args.length; i < n; i++)
        {
            bigints[i] = BigInteger.valueOf(args[i]);
        }
        return new PolyEquationImpl(bigints);
    }
    // ==================================================
    // constructors
    // ==================================================
    public PolyEquationImpl(final BigInteger[] inCoeffs)
    {
        if (inCoeffs.length <= 0)
        {
            throw new SecretShareException("Must have at least 1 coefficient");
        }
        coefficients = new BigInteger[inCoeffs.length];

        for (int i = 0, n = inCoeffs.length; i < n; i++)
        {
            if (inCoeffs[i] == null)
            {
                throw new SecretShareException("Coefficient index=" + i +