GordianCoreNTRUSpec.java

/*
 * GordianKnot: Security Suite
 * Copyright 2026. Tony Washer
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.github.tonywasher.joceanus.gordianknot.impl.core.spec.keypair;

import io.github.tonywasher.joceanus.gordianknot.api.keypair.spec.GordianNTRUSpec;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
import org.bouncycastle.pqc.crypto.ntru.NTRUParameters;
import org.bouncycastle.pqc.jcajce.spec.NTRUParameterSpec;

import java.util.EnumMap;
import java.util.Map;

/**
 * NTRU KeySpec.
 */
public final class GordianCoreNTRUSpec {
    /**
     * The specMap.
     */
    private static final Map<GordianNTRUSpec, GordianCoreNTRUSpec> SPECMAP = newSpecMap();

    /**
     * The specArray.
     */
    private static final GordianCoreNTRUSpec[] VALUES = SPECMAP.values().toArray(new GordianCoreNTRUSpec[0]);

    /**
     * The Spec.
     */
    private final GordianNTRUSpec theSpec;

    /**
     * Constructor.
     *
     * @param pSpec the spec
     */
    private GordianCoreNTRUSpec(final GordianNTRUSpec pSpec) {
        theSpec = pSpec;
    }

    /**
     * Obtain the spec.
     *
     * @return the spec
     */
    public GordianNTRUSpec getSpec() {
        return theSpec;
    }

    /**
     * Obtain NTRU Parameters.
     *
     * @return the parameters.
     */
    public NTRUParameters getParameters() {
        return switch (theSpec) {
            case HPS509 -> NTRUParameters.ntruhps2048509;
            case HPS677 -> NTRUParameters.ntruhps2048677;
            case HPS821 -> NTRUParameters.ntruhps4096821;
            case HPS1229 -> NTRUParameters.ntruhps40961229;
            case HRSS701 -> NTRUParameters.ntruhrss701;
            case HRSS1373 -> NTRUParameters.ntruhrss1373;
            default -> throw new IllegalArgumentException();
        };
    }

    /**
     * Obtain NTRU ParameterSpec.
     *
     * @return the parameters.
     */
    public NTRUParameterSpec getParameterSpec() {
        return switch (theSpec) {
            case HPS509 -> NTRUParameterSpec.ntruhps2048509;
            case HPS677 -> NTRUParameterSpec.ntruhps2048677;
            case HPS821 -> NTRUParameterSpec.ntruhps4096821;
            case HPS1229 -> NTRUParameterSpec.ntruhps40961229;
            case HRSS701 -> NTRUParameterSpec.ntruhrss701;
            case HRSS1373 -> NTRUParameterSpec.ntruhrss1373;
            default -> throw new IllegalArgumentException();
        };
    }

    /**
     * Obtain NTRU algorithm Identifier.
     *
     * @return the identifier.
     */
    public ASN1ObjectIdentifier getIdentifier() {
        return switch (theSpec) {
            case HPS509 -> BCObjectIdentifiers.ntruhps2048509;
            case HPS677 -> BCObjectIdentifiers.ntruhps2048677;
            case HPS821 -> BCObjectIdentifiers.ntruhps4096821;
            case HPS1229 -> BCObjectIdentifiers.ntruhps40961229;
            case HRSS701 -> BCObjectIdentifiers.ntruhrss701;
            case HRSS1373 -> BCObjectIdentifiers.ntruhrss1373;
            default -> throw new IllegalArgumentException();
        };
    }

    @Override
    public String toString() {
        return theSpec.toString();
    }

    @Override
    public boolean equals(final Object pThat) {
        /* Handle the trivial cases */
        if (this == pThat) {
            return true;
        }
        if (pThat == null) {
            return false;
        }

        /* Check subFields */
        return pThat instanceof GordianCoreNTRUSpec myThat
                && theSpec == myThat.getSpec();
    }

    @Override
    public int hashCode() {
        return theSpec.hashCode();
    }

    /**
     * Obtain the core spec.
     *
     * @param pSpec the base spec
     * @return the core spec
     */
    public static GordianCoreNTRUSpec mapCoreSpec(final Object pSpec) {
        return pSpec instanceof GordianNTRUSpec mySpec ? SPECMAP.get(mySpec) : null;
    }

    /**
     * Build the type map.
     *
     * @return the type map
     */
    private static Map<GordianNTRUSpec, GordianCoreNTRUSpec> newSpecMap() {
        final Map<GordianNTRUSpec, GordianCoreNTRUSpec> myMap = new EnumMap<>(GordianNTRUSpec.class);
        for (GordianNTRUSpec mySpec : GordianNTRUSpec.values()) {
            myMap.put(mySpec, new GordianCoreNTRUSpec(mySpec));
        }
        return myMap;
    }

    /**
     * Obtain the values.
     *
     * @return the values
     */
    public static GordianCoreNTRUSpec[] values() {
        return VALUES;
    }
}