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

package org.bouncycastle.pqc.crypto.lms;

import org.bouncycastle.crypto.Digest;
import java.io.IOException;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Objects;
import org.bouncycastle.util.io.Streams;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import org.bouncycastle.util.Encodable;

class LMOtsPublicKey implements Encodable
{
    private final LMOtsParameters parameter;
    private final byte[] I;
    private final int q;
    private final byte[] K;
    
    LMOtsPublicKey(final LMOtsParameters parameter, final byte[] i, final int q, final byte[] k) {
        this.parameter = parameter;
        this.I = i;
        this.q = q;
        this.K = k;
    }
    
    public static LMOtsPublicKey getInstance(final Object obj) throws Exception {
        if (obj instanceof LMOtsPublicKey) {
            return (LMOtsPublicKey)obj;
        }
        if (obj instanceof DataInputStream) {
            final LMOtsParameters parametersForType = LMOtsParameters.getParametersForType(((DataInputStream)obj).readInt());
            final byte[] b = new byte[16];
            ((DataInputStream)obj).readFully(b);
            final int int1 = ((DataInputStream)obj).readInt();
            final byte[] b2 = new byte[parametersForType.getN()];
            ((DataInputStream)obj).readFully(b2);
            return new LMOtsPublicKey(parametersForType, b, int1, b2);
        }
        if (obj instanceof byte[]) {
            Object o = null;
            try {
                o = new DataInputStream(new ByteArrayInputStream((byte[])obj));
                return getInstance(o);
            }
            finally {
                if (o != null) {
                    ((InputStream)o).close();
                }
            }
        }
        if (obj instanceof InputStream) {
            return getInstance(Streams.readAll((InputStream)obj));
        }
        throw new IllegalArgumentException("cannot parse " + obj);
    }
    
    public LMOtsParameters getParameter() {
        return this.parameter;
    }
    
    public byte[] getI() {
        return this.I;
    }
    
    public int getQ() {
        return this.q;
    }
    
    public byte[] getK() {
        return this.K;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        final LMOtsPublicKey lmOtsPublicKey = (LMOtsPublicKey)o;
        return this.q == lmOtsPublicKey.q && Objects.areEqual(this.parameter, lmOtsPublicKey.parameter) && Arrays.areEqual(this.I, lmOtsPublicKey.I) && Arrays.areEqual(this.K, lmOtsPublicKey.K);
    }
    
    @Override
    public int hashCode() {
        return 31 * (31 * (31 * this.q + Objects.hashCode(this.parameter)) + Arrays.hashCode(this.I)) + Arrays.hashCode(this.K);
    }
    
    @Override
    public byte[] getEncoded() throws IOException {
        return Composer.compose().u32str(this.parameter.getType()).bytes(this.I).u32str(this.q).bytes(this.K).build();
    }
    
    LMSContext createOtsContext(final LMOtsSignature lmOtsSignature) {
        final Digest digest = DigestUtil.getDigest(this.parameter);
        LmsUtils.byteArray(this.I, digest);
        LmsUtils.u32str(this.q, digest);
        LmsUtils.u16str((short)(-32383), digest);
        LmsUtils.byteArray(lmOtsSignature.getC(), digest);
        return new LMSContext(this, lmOtsSignature, digest);
    }
    
    LMSContext createOtsContext(final LMSSignature lmsSignature) {
        final Digest digest = DigestUtil.getDigest(this.parameter);
        LmsUtils.byteArray(this.I, digest);
        LmsUtils.u32str(this.q, digest);
        LmsUtils.u16str((short)(-32383), digest);
        LmsUtils.byteArray(lmsSignature.getOtsSignature().getC(), digest);
        return new LMSContext(this, lmsSignature, digest);
    }
}
