Package com.securityinnovation.jNeo.math

Source Code of com.securityinnovation.jNeo.math.BPGM3TestCase

/******************************************************************************
* NTRU Cryptography Reference Source Code
* Copyright (c) 2009-2013, by Security Innovation, Inc. All rights reserved.
*
* Copyright (C) 2009-2013  Security Innovation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*********************************************************************************/

package com.securityinnovation.jNeo.math;

import org.junit.Test;
import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import com.securityinnovation.testvectors.NtruEncryptTestVector;
import com.securityinnovation.jNeo.ParamSetNotSupportedException;
import com.securityinnovation.jNeo.ntruencrypt.KeyParams;
import com.securityinnovation.jNeo.inputstream.IGF2;
import com.securityinnovation.jNeo.inputstream.MGF1;


public class BPGM3TestCase {

    // Test polynomial generation with a fixed input that will
    // produce no collisions.
    @Test public void test_oddeven()
    {
        byte igfSequence[] = {0,00,20,40,60,8,
                              0,10,30,50,70,9};
        ByteArrayInputStream is = new ByteArrayInputStream(igfSequence);
        IGF2 igf = new IGF2(0x7fff, 16, is);
       
        short polyCoeffs[] = {1, -1, 1, -1, 1, -1, 1, -1, 1, -1};
        FullPolynomial expected = new FullPolynomial(polyCoeffs);

        FullPolynomial out = BPGM3.genTrinomial(10, 5, 5, igf);

        assertTrue(out.equals(expected));
    }


    // Test polynomial generation with a fixed input that will
    // produce collisions.
    @Test public void test_collisions()
    {
        byte igfSequence[] = {0,00,20,40,6
                              0,00,20,40,60,8,
                              0,00,20,40,60,8,
                              0,10,30,50,70,9};
        ByteArrayInputStream is = new ByteArrayInputStream(igfSequence);
        IGF2 igf = new IGF2(0x7fff, 16, is);
       
        short polyCoeffs[] = {1, -1, 1, -1, 1, -1, 1, -1, 1, -1};
        FullPolynomial expected = new FullPolynomial(polyCoeffs);

        FullPolynomial out = BPGM3.genTrinomial(10, 5, 5, igf);

        assertTrue(out.equals(expected));
    }


    // See if we can reproduce the generation of r for each NtruEncrypt
    // test vector
    // r = BGPM3(MGF1(dr, seed=sData));
    @Test public void test_genr()
        throws ParamSetNotSupportedException
    {
        NtruEncryptTestVector tests[] = NtruEncryptTestVector.getTestVectors();
        for (int t=0; t<tests.length; t++)
        {
            KeyParams keyParams = KeyParams.getKeyParams(tests[t].oid);
            FullPolynomial expected = new FullPolynomial(tests[t].r);
            IGF2 igf = new IGF2(
                keyParams.N, keyParams.c, keyParams.igfHash, 1,
                tests[t].sData, 0, tests[t].sData.length);
            FullPolynomial out = BPGM3.genTrinomial(
                keyParams.N, keyParams.dr, keyParams.dr, igf);

            assertTrue(out.equals(expected));
        }
    }
}
TOP

Related Classes of com.securityinnovation.jNeo.math.BPGM3TestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.