// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.threshold;

import java.io.IOException;

public class ShamirSplitSecret implements SplitSecret
{
    private final ShamirSplitSecretShare[] secretShares;
    private final Polynomial poly;
    
    public ShamirSplitSecret(final ShamirSecretSplitter.Algorithm algorithm, final ShamirSecretSplitter.Mode mode, final ShamirSplitSecretShare[] secretShares) {
        this.secretShares = secretShares;
        this.poly = Polynomial.newInstance(algorithm, mode);
    }
    
    ShamirSplitSecret(final Polynomial poly, final ShamirSplitSecretShare[] secretShares) {
        this.secretShares = secretShares;
        this.poly = poly;
    }
    
    @Override
    public ShamirSplitSecretShare[] getSecretShares() {
        return this.secretShares;
    }
    
    public ShamirSplitSecret multiple(final int n) throws IOException {
        for (int i = 0; i < this.secretShares.length; ++i) {
            final byte[] encoded = this.secretShares[i].getEncoded();
            for (int j = 0; j < encoded.length; ++j) {
                encoded[j] = this.poly.gfMul(encoded[j] & 0xFF, n);
            }
            this.secretShares[i] = new ShamirSplitSecretShare(encoded, i + 1);
        }
        return this;
    }
    
    public ShamirSplitSecret divide(final int n) throws IOException {
        for (int i = 0; i < this.secretShares.length; ++i) {
            final byte[] encoded = this.secretShares[i].getEncoded();
            for (int j = 0; j < encoded.length; ++j) {
                encoded[j] = this.poly.gfDiv(encoded[j] & 0xFF, n);
            }
            this.secretShares[i] = new ShamirSplitSecretShare(encoded, i + 1);
        }
        return this;
    }
    
    @Override
    public byte[] getSecret() throws IOException {
        final int length = this.secretShares.length;
        final byte[] array = new byte[length];
        final byte[] array2 = new byte[length - 1];
        final byte[][] array3 = new byte[length][this.secretShares[0].getEncoded().length];
        for (int i = 0; i < length; ++i) {
            array3[i] = this.secretShares[i].getEncoded();
            int n = 0;
            for (int j = 0; j < length; ++j) {
                if (j != i) {
                    final byte[] array4 = array2;
                    final int n2 = n;
                    n = (byte)(n + 1);
                    array4[n2] = this.poly.gfDiv(this.secretShares[j].r, this.secretShares[i].r ^ this.secretShares[j].r);
                }
            }
            byte gfMul = 1;
            for (int k = 0; k != array2.length; ++k) {
                gfMul = this.poly.gfMul(gfMul & 0xFF, array2[k] & 0xFF);
            }
            array[i] = gfMul;
        }
        return this.poly.gfVecMul(array, array3);
    }
}
