All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.pqc.math.ntru.test.HRSSPolynomialTest Maven / Gradle / Ivy

Go to download

The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.

There is a newer version: 1.79
Show newest version
package org.bouncycastle.pqc.math.ntru.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;
import org.bouncycastle.pqc.math.ntru.HRSSPolynomial;
import org.bouncycastle.pqc.math.ntru.Polynomial;
import org.bouncycastle.pqc.math.ntru.parameters.NTRUHRSS701;
import org.bouncycastle.pqc.math.ntru.parameters.NTRUHRSSParameterSet;
import org.bouncycastle.pqc.math.ntru.parameters.NTRUParameterSet;

public class HRSSPolynomialTest
    extends TestCase
{
    private final String KAT_ROOT = "/org/bouncycastle/pqc/crypto/test/ntru/";

    private static List>> getTestCases(InputStream src)
    {
        List>> testCases = new ArrayList>>();
        BufferedReader bin = new BufferedReader(new InputStreamReader(src));
        Map> buf = new HashMap>();
        try
        {
            for (String line = bin.readLine(); line != null; line = bin.readLine())
            {
                if (line.startsWith("#"))
                {
                    continue;
                }
                if (line.length() == 0)
                {
                    if (buf.size() > 0)
                    {
                        testCases.add(buf);
                    }
                    buf = new HashMap>(); // ensures that each test case starts with a fresh map
                    continue;
                }
                // append fields
                int a = line.indexOf("=");
                if (a > -1)
                {
                    List values = new ArrayList();
                    for (String str : line.substring(a + 1).trim().split(" "))
                    {
                        values.add(Integer.parseInt(str));
                    }
                    buf.put(line.substring(0, a).trim(), values);
                }
            }
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
        return testCases;
    }

    public void testLift()
    {
        NTRUParameterSet params = new NTRUHRSS701();
        InputStream src = PolynomialTest.class.getResourceAsStream(KAT_ROOT + "ntruhrss701/poly_lift.txt");
        List>> testCases = getTestCases(src);
        for (Map> testCase : testCases)
        {
            Polynomial a = new HRSSPolynomial((NTRUHRSSParameterSet)params);
            int i = 0;
            for (Integer value : testCase.get("a"))
            {
                a.coeffs[i++] = value.shortValue();
            }

            Polynomial r = new HRSSPolynomial((NTRUHRSSParameterSet)params);
            r.lift(a);
            i = 0;
            for (Integer value : testCase.get("r"))
            {
                assertEquals(String.format("i = %d", i), value.shortValue(), r.coeffs[i++]);
            }
        }
    }

    public void testR2Inv()
    {
        NTRUParameterSet params = new NTRUHRSS701();
        InputStream src = PolynomialTest.class.getResourceAsStream(KAT_ROOT + "ntruhrss701/poly_R2_inv.txt");
        List>> testCases = getTestCases(src);
        for (Map> testCase : testCases)
        {
            Polynomial a = new HRSSPolynomial((NTRUHRSSParameterSet)params);
            int i = 0;
            for (Integer value : testCase.get("a"))
            {
                a.coeffs[i++] = value.shortValue();
            }

            Polynomial r = new HRSSPolynomial((NTRUHRSSParameterSet)params);
            r.r2Inv(a);
            i = 0;
            for (Integer value : testCase.get("r"))
            {
                assertEquals(String.format("i = %d", i), value.shortValue(), r.coeffs[i++]);
            }
        }
    }

    public void testRqInv()
    {
        NTRUParameterSet params = new NTRUHRSS701();
        InputStream src = PolynomialTest.class.getResourceAsStream(KAT_ROOT + "ntruhrss701/poly_Rq_inv.txt");
        List>> testCases = getTestCases(src);
        for (Map> testCase : testCases)
        {
            Polynomial a = new HRSSPolynomial((NTRUHRSSParameterSet)params);
            int i = 0;
            for (Integer value : testCase.get("a"))
            {
                a.coeffs[i++] = value.shortValue();
            }

            Polynomial r = new HRSSPolynomial((NTRUHRSSParameterSet)params);
            r.rqInv(a);
            i = 0;
            for (Integer value : testCase.get("r"))
            {
                assertEquals(String.format("i = %d", i), value.shortValue(), r.coeffs[i++]);
            }
        }
    }

    public void testS3Inv()
    {
        NTRUParameterSet params = new NTRUHRSS701();
        InputStream src = PolynomialTest.class.getResourceAsStream(KAT_ROOT + "ntruhrss701/poly_S3_inv.txt");
        List>> testCases = getTestCases(src);
        for (Map> testCase : testCases)
        {
            Polynomial a = new HRSSPolynomial((NTRUHRSSParameterSet)params);
            int i = 0;
            for (Integer value : testCase.get("a"))
            {
                a.coeffs[i++] = value.shortValue();
            }

            Polynomial r = new HRSSPolynomial((NTRUHRSSParameterSet)params);
            r.s3Inv(a);
            i = 0;
            for (Integer value : testCase.get("r"))
            {
                assertEquals(String.format("i = %d", i), value.shortValue(), r.coeffs[i++]);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy