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

package org.bouncycastle.math.ec.endo;

import org.bouncycastle.math.ec.ECConstants;
import org.bouncycastle.math.ec.PreCompInfo;
import org.bouncycastle.math.ec.PreCompCallback;
import org.bouncycastle.math.ec.ECPoint;
import java.math.BigInteger;

public abstract class EndoUtil
{
    public static final String PRECOMP_NAME = "bc_endo";
    
    public static BigInteger[] decomposeScalar(final ScalarSplitParameters scalarSplitParameters, final BigInteger bigInteger) {
        final int bits = scalarSplitParameters.getBits();
        final BigInteger calculateB = calculateB(bigInteger, scalarSplitParameters.getG1(), bits);
        final BigInteger calculateB2 = calculateB(bigInteger, scalarSplitParameters.getG2(), bits);
        return new BigInteger[] { bigInteger.subtract(calculateB.multiply(scalarSplitParameters.getV1A()).add(calculateB2.multiply(scalarSplitParameters.getV2A()))), calculateB.multiply(scalarSplitParameters.getV1B()).add(calculateB2.multiply(scalarSplitParameters.getV2B())).negate() };
    }
    
    public static ECPoint mapPoint(final ECEndomorphism ecEndomorphism, final ECPoint ecPoint) {
        return ((EndoPreCompInfo)ecPoint.getCurve().precompute(ecPoint, "bc_endo", new PreCompCallback() {
            @Override
            public PreCompInfo precompute(final PreCompInfo preCompInfo) {
                final EndoPreCompInfo endoPreCompInfo = (preCompInfo instanceof EndoPreCompInfo) ? ((EndoPreCompInfo)preCompInfo) : null;
                if (this.checkExisting(endoPreCompInfo, ecEndomorphism)) {
                    return endoPreCompInfo;
                }
                final ECPoint map = ecEndomorphism.getPointMap().map(ecPoint);
                final EndoPreCompInfo endoPreCompInfo2 = new EndoPreCompInfo();
                endoPreCompInfo2.setEndomorphism(ecEndomorphism);
                endoPreCompInfo2.setMappedPoint(map);
                return endoPreCompInfo2;
            }
            
            private boolean checkExisting(final EndoPreCompInfo endoPreCompInfo, final ECEndomorphism ecEndomorphism) {
                return null != endoPreCompInfo && endoPreCompInfo.getEndomorphism() == ecEndomorphism && endoPreCompInfo.getMappedPoint() != null;
            }
        })).getMappedPoint();
    }
    
    private static BigInteger calculateB(final BigInteger bigInteger, final BigInteger bigInteger2, final int n) {
        final boolean b = bigInteger2.signum() < 0;
        final BigInteger multiply = bigInteger.multiply(bigInteger2.abs());
        final boolean testBit = multiply.testBit(n - 1);
        BigInteger bigInteger3 = multiply.shiftRight(n);
        if (testBit) {
            bigInteger3 = bigInteger3.add(ECConstants.ONE);
        }
        return b ? bigInteger3.negate() : bigInteger3;
    }
}
