summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/pqc/jcajce/provider/test/McElieceCCA2PrimitivesTest.java
blob: 39e16ad25c0c5d316d93c924aa9909cf32ccb5bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.bouncycastle.pqc.jcajce.provider.test;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

import org.bouncycastle.pqc.jcajce.provider.mceliece.BCMcElieceCCA2PrivateKey;
import org.bouncycastle.pqc.jcajce.provider.mceliece.BCMcElieceCCA2PublicKey;
import org.bouncycastle.pqc.jcajce.provider.mceliece.McElieceCCA2Primitives;
import org.bouncycastle.pqc.jcajce.spec.ECCKeyGenParameterSpec;
import org.bouncycastle.pqc.math.linearalgebra.GF2Vector;


public class McElieceCCA2PrimitivesTest
    extends FlexiTest
{

    KeyPairGenerator kpg;

    protected void setUp()
    {
        super.setUp();
        try
        {
            kpg = KeyPairGenerator.getInstance("McElieceKobaraImai");
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
    }

    public void testPrimitives()
        throws Exception
    {
        int m = 11;
        int t = 50;
        initKPG(m, t);
        int n = 1 << m;

        KeyPair pair = kpg.genKeyPair();
        BCMcElieceCCA2PublicKey pubKey = (BCMcElieceCCA2PublicKey)pair.getPublic();
        BCMcElieceCCA2PrivateKey privKey = (BCMcElieceCCA2PrivateKey)pair
            .getPrivate();

        GF2Vector plaintext = new GF2Vector(pubKey.getK(), sr);
        GF2Vector errors = new GF2Vector(n, t, sr);

        GF2Vector ciphertext = McElieceCCA2Primitives.encryptionPrimitive(
            pubKey, plaintext, errors);

        GF2Vector[] dec = McElieceCCA2Primitives.decryptionPrimitive(privKey,
            ciphertext);
        GF2Vector plaintextAgain = dec[0];
        GF2Vector errorsAgain = dec[1];

        assertEquals(plaintext, plaintextAgain);
        assertEquals(errors, errorsAgain);
    }

    /**
     * Initialize the key pair generator with the given parameters.
     */
    private void initKPG(int m, int t)
        throws Exception
    {
        ECCKeyGenParameterSpec params = new ECCKeyGenParameterSpec(m, t);
        kpg.initialize(params);
    }

}