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

package org.bouncycastle.pqc.crypto.xmss;

import java.io.IOException;
import org.bouncycastle.util.Pack;
import org.bouncycastle.util.Encodable;

public final class XMSSMTPublicKeyParameters extends XMSSMTKeyParameters implements XMSSStoreableObjectInterface, Encodable
{
    private final XMSSMTParameters params;
    private final int oid;
    private final byte[] root;
    private final byte[] publicSeed;
    
    private XMSSMTPublicKeyParameters(final Builder builder) {
        super(false, builder.params.getTreeDigest());
        this.params = builder.params;
        if (this.params == null) {
            throw new NullPointerException("params == null");
        }
        final int treeDigestSize = this.params.getTreeDigestSize();
        final byte[] access$100 = builder.publicKey;
        if (access$100 != null) {
            final int n = 4;
            final int n2 = treeDigestSize;
            final int n3 = treeDigestSize;
            final int n4 = 0;
            if (access$100.length == n2 + n3) {
                this.oid = 0;
                this.root = XMSSUtil.extractBytesAtOffset(access$100, n4, n2);
                this.publicSeed = XMSSUtil.extractBytesAtOffset(access$100, n4 + n2, n3);
            }
            else {
                if (access$100.length != n + n2 + n3) {
                    throw new IllegalArgumentException("public key has wrong size");
                }
                this.oid = Pack.bigEndianToInt(access$100, 0);
                final int n5 = n4 + n;
                this.root = XMSSUtil.extractBytesAtOffset(access$100, n5, n2);
                this.publicSeed = XMSSUtil.extractBytesAtOffset(access$100, n5 + n2, n3);
            }
        }
        else {
            if (this.params.getOid() != null) {
                this.oid = this.params.getOid().getOid();
            }
            else {
                this.oid = 0;
            }
            final byte[] access$101 = builder.root;
            if (access$101 != null) {
                if (access$101.length != treeDigestSize) {
                    throw new IllegalArgumentException("length of root must be equal to length of digest");
                }
                this.root = access$101;
            }
            else {
                this.root = new byte[treeDigestSize];
            }
            final byte[] access$102 = builder.publicSeed;
            if (access$102 != null) {
                if (access$102.length != treeDigestSize) {
                    throw new IllegalArgumentException("length of publicSeed must be equal to length of digest");
                }
                this.publicSeed = access$102;
            }
            else {
                this.publicSeed = new byte[treeDigestSize];
            }
        }
    }
    
    @Override
    public byte[] getEncoded() throws IOException {
        return this.toByteArray();
    }
    
    @Deprecated
    @Override
    public byte[] toByteArray() {
        final int treeDigestSize = this.params.getTreeDigestSize();
        final int n = 4;
        final int n2 = treeDigestSize;
        final int n3 = treeDigestSize;
        int n4 = 0;
        byte[] array;
        if (this.oid != 0) {
            array = new byte[n + n2 + n3];
            Pack.intToBigEndian(this.oid, array, n4);
            n4 += n;
        }
        else {
            array = new byte[n2 + n3];
        }
        XMSSUtil.copyBytesAtOffset(array, this.root, n4);
        XMSSUtil.copyBytesAtOffset(array, this.publicSeed, n4 + n2);
        return array;
    }
    
    public byte[] getRoot() {
        return XMSSUtil.cloneArray(this.root);
    }
    
    public byte[] getPublicSeed() {
        return XMSSUtil.cloneArray(this.publicSeed);
    }
    
    public XMSSMTParameters getParameters() {
        return this.params;
    }
    
    public static class Builder
    {
        private final XMSSMTParameters params;
        private byte[] root;
        private byte[] publicSeed;
        private byte[] publicKey;
        
        public Builder(final XMSSMTParameters params) {
            this.root = null;
            this.publicSeed = null;
            this.publicKey = null;
            this.params = params;
        }
        
        public Builder withRoot(final byte[] array) {
            this.root = XMSSUtil.cloneArray(array);
            return this;
        }
        
        public Builder withPublicSeed(final byte[] array) {
            this.publicSeed = XMSSUtil.cloneArray(array);
            return this;
        }
        
        public Builder withPublicKey(final byte[] array) {
            this.publicKey = XMSSUtil.cloneArray(array);
            return this;
        }
        
        public XMSSMTPublicKeyParameters build() {
            return new XMSSMTPublicKeyParameters(this, null);
        }
    }
}
