Search in sources :

Example 1 with GF2mField

use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField in project LinLong-Java by zhenwei1108.

the class McElieceCCA2Primitives method decryptionPrimitive.

/**
 * The McEliece decryption primitive.
 *
 * @param privKey the private key
 * @param c       the ciphertext vector <tt>c = m*G + z</tt>
 * @return the message vector <tt>m</tt> and the error vector <tt>z</tt>
 */
public static GF2Vector[] decryptionPrimitive(McElieceCCA2PrivateKeyParameters privKey, GF2Vector c) {
    // obtain values from private key
    int k = privKey.getK();
    Permutation p = privKey.getP();
    GF2mField field = privKey.getField();
    PolynomialGF2mSmallM gp = privKey.getGoppaPoly();
    GF2Matrix h = privKey.getH();
    PolynomialGF2mSmallM[] q = privKey.getQInv();
    // compute inverse permutation P^-1
    Permutation pInv = p.computeInverse();
    // multiply c with permutation P^-1
    GF2Vector cPInv = (GF2Vector) c.multiply(pInv);
    // compute syndrome of cP^-1
    GF2Vector syndVec = (GF2Vector) h.rightMultiply(cPInv);
    // decode syndrome
    GF2Vector errors = GoppaCode.syndromeDecode(syndVec, field, gp, q);
    GF2Vector mG = (GF2Vector) cPInv.add(errors);
    // multiply codeword and error vector with P
    mG = (GF2Vector) mG.multiply(p);
    errors = (GF2Vector) errors.multiply(p);
    // extract plaintext vector (last k columns of mG)
    GF2Vector m = mG.extractRightVector(k);
    // return vectors
    return new GF2Vector[] { m, errors };
}
Also used : GF2mField(com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField) PolynomialGF2mSmallM(com.github.zhenwei.core.pqc.math.linearalgebra.PolynomialGF2mSmallM) GF2Matrix(com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix) Permutation(com.github.zhenwei.core.pqc.math.linearalgebra.Permutation) GF2Vector(com.github.zhenwei.core.pqc.math.linearalgebra.GF2Vector)

Example 2 with GF2mField

use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField in project LinLong-Java by zhenwei1108.

the class McElieceCipher method messageDecrypt.

/**
 * Decrypt a cipher text.
 *
 * @param input the cipher text
 * @return the plain text
 * @throws InvalidCipherTextException if the cipher text is invalid.
 */
public byte[] messageDecrypt(byte[] input) throws InvalidCipherTextException {
    if (forEncryption) {
        throw new IllegalStateException("cipher initialised for decryption");
    }
    GF2Vector vec = GF2Vector.OS2VP(n, input);
    McEliecePrivateKeyParameters privKey = (McEliecePrivateKeyParameters) key;
    GF2mField field = privKey.getField();
    PolynomialGF2mSmallM gp = privKey.getGoppaPoly();
    GF2Matrix sInv = privKey.getSInv();
    Permutation p1 = privKey.getP1();
    Permutation p2 = privKey.getP2();
    GF2Matrix h = privKey.getH();
    PolynomialGF2mSmallM[] qInv = privKey.getQInv();
    // compute permutation P = P1 * P2
    Permutation p = p1.rightMultiply(p2);
    // compute P^-1
    Permutation pInv = p.computeInverse();
    // compute c P^-1
    GF2Vector cPInv = (GF2Vector) vec.multiply(pInv);
    // compute syndrome of c P^-1
    GF2Vector syndrome = (GF2Vector) h.rightMultiply(cPInv);
    // decode syndrome
    GF2Vector z = GoppaCode.syndromeDecode(syndrome, field, gp, qInv);
    GF2Vector mSG = (GF2Vector) cPInv.add(z);
    // multiply codeword with P1 and error vector with P
    mSG = (GF2Vector) mSG.multiply(p1);
    z = (GF2Vector) z.multiply(p);
    // extract mS (last k columns of mSG)
    GF2Vector mS = mSG.extractRightVector(k);
    // compute plaintext vector
    GF2Vector mVec = (GF2Vector) sInv.leftMultiply(mS);
    // compute and return plaintext
    return computeMessage(mVec);
}
Also used : GF2mField(com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField) PolynomialGF2mSmallM(com.github.zhenwei.core.pqc.math.linearalgebra.PolynomialGF2mSmallM) GF2Matrix(com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix) GF2Vector(com.github.zhenwei.core.pqc.math.linearalgebra.GF2Vector) Permutation(com.github.zhenwei.core.pqc.math.linearalgebra.Permutation)

Example 3 with GF2mField

use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField in project LinLong-Java by zhenwei1108.

the class McElieceKeyPairGenerator method genKeyPair.

private AsymmetricCipherKeyPair genKeyPair() {
    if (!initialized) {
        initializeDefault();
    }
    // finite field GF(2^m)
    GF2mField field = new GF2mField(m, fieldPoly);
    // irreducible Goppa polynomial
    PolynomialGF2mSmallM gp = new PolynomialGF2mSmallM(field, t, PolynomialGF2mSmallM.RANDOM_IRREDUCIBLE_POLYNOMIAL, random);
    PolynomialRingGF2m ring = new PolynomialRingGF2m(field, gp);
    // matrix used to compute square roots in (GF(2^m))^t
    PolynomialGF2mSmallM[] sqRootMatrix = ring.getSquareRootMatrix();
    // generate canonical check matrix
    GF2Matrix h = GoppaCode.createCanonicalCheckMatrix(field, gp);
    // compute short systematic form of check matrix
    MaMaPe mmp = GoppaCode.computeSystematicForm(h, random);
    GF2Matrix shortH = mmp.getSecondMatrix();
    Permutation p1 = mmp.getPermutation();
    // compute short systematic form of generator matrix
    GF2Matrix shortG = (GF2Matrix) shortH.computeTranspose();
    // extend to full systematic form
    GF2Matrix gPrime = shortG.extendLeftCompactForm();
    // obtain number of rows of G (= dimension of the code)
    int k = shortG.getNumRows();
    // generate random invertible (k x k)-matrix S and its inverse S^-1
    GF2Matrix[] matrixSandInverse = GF2Matrix.createRandomRegularMatrixAndItsInverse(k, random);
    // generate random permutation P2
    Permutation p2 = new Permutation(n, random);
    // compute public matrix G=S*G'*P2
    GF2Matrix g = (GF2Matrix) matrixSandInverse[0].rightMultiply(gPrime);
    g = (GF2Matrix) g.rightMultiply(p2);
    // generate keys
    McEliecePublicKeyParameters pubKey = new McEliecePublicKeyParameters(n, t, g);
    McEliecePrivateKeyParameters privKey = new McEliecePrivateKeyParameters(n, k, field, gp, p1, p2, matrixSandInverse[1]);
    // return key pair
    return new AsymmetricCipherKeyPair(pubKey, privKey);
}
Also used : GF2mField(com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField) PolynomialGF2mSmallM(com.github.zhenwei.core.pqc.math.linearalgebra.PolynomialGF2mSmallM) PolynomialRingGF2m(com.github.zhenwei.core.pqc.math.linearalgebra.PolynomialRingGF2m) GF2Matrix(com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix) Permutation(com.github.zhenwei.core.pqc.math.linearalgebra.Permutation) MaMaPe(com.github.zhenwei.core.pqc.math.linearalgebra.GoppaCode.MaMaPe) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 4 with GF2mField

use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField in project LinLong-Java by zhenwei1108.

the class McElieceCCA2Primitives method decryptionPrimitive.

/**
 * The McEliece decryption primitive.
 *
 * @param privKey the private key
 * @param c       the ciphertext vector <tt>c = m*G + z</tt>
 * @return the message vector <tt>m</tt> and the error vector <tt>z</tt>
 */
public static GF2Vector[] decryptionPrimitive(BCMcElieceCCA2PrivateKey privKey, GF2Vector c) {
    // obtain values from private key
    int k = privKey.getK();
    Permutation p = privKey.getP();
    GF2mField field = privKey.getField();
    PolynomialGF2mSmallM gp = privKey.getGoppaPoly();
    GF2Matrix h = privKey.getH();
    PolynomialGF2mSmallM[] q = privKey.getQInv();
    // compute inverse permutation P^-1
    Permutation pInv = p.computeInverse();
    // multiply c with permutation P^-1
    GF2Vector cPInv = (GF2Vector) c.multiply(pInv);
    // compute syndrome of cP^-1
    GF2Vector syndVec = (GF2Vector) h.rightMultiply(cPInv);
    // decode syndrome
    GF2Vector errors = GoppaCode.syndromeDecode(syndVec, field, gp, q);
    GF2Vector mG = (GF2Vector) cPInv.add(errors);
    // multiply codeword and error vector with P
    mG = (GF2Vector) mG.multiply(p);
    errors = (GF2Vector) errors.multiply(p);
    // extract plaintext vector (last k columns of mG)
    GF2Vector m = mG.extractRightVector(k);
    // return vectors
    return new GF2Vector[] { m, errors };
}
Also used : GF2mField(com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField) PolynomialGF2mSmallM(com.github.zhenwei.core.pqc.math.linearalgebra.PolynomialGF2mSmallM) GF2Matrix(com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix) Permutation(com.github.zhenwei.core.pqc.math.linearalgebra.Permutation) GF2Vector(com.github.zhenwei.core.pqc.math.linearalgebra.GF2Vector)

Example 5 with GF2mField

use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField in project LinLong-Java by zhenwei1108.

the class McElieceCCA2KeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    if (!initialized) {
        initializeDefault();
    }
    // finite field GF(2^m)
    GF2mField field = new GF2mField(m, fieldPoly);
    // irreducible Goppa polynomial
    PolynomialGF2mSmallM gp = new PolynomialGF2mSmallM(field, t, PolynomialGF2mSmallM.RANDOM_IRREDUCIBLE_POLYNOMIAL, random);
    // generate canonical check matrix
    GF2Matrix h = GoppaCode.createCanonicalCheckMatrix(field, gp);
    // compute short systematic form of check matrix
    MaMaPe mmp = GoppaCode.computeSystematicForm(h, random);
    GF2Matrix shortH = mmp.getSecondMatrix();
    Permutation p = mmp.getPermutation();
    // compute short systematic form of generator matrix
    GF2Matrix shortG = (GF2Matrix) shortH.computeTranspose();
    // obtain number of rows of G (= dimension of the code)
    int k = shortG.getNumRows();
    // generate keys
    McElieceCCA2PublicKeyParameters pubKey = new McElieceCCA2PublicKeyParameters(n, t, shortG, mcElieceCCA2Params.getParameters().getDigest());
    McElieceCCA2PrivateKeyParameters privKey = new McElieceCCA2PrivateKeyParameters(n, k, field, gp, p, mcElieceCCA2Params.getParameters().getDigest());
    // return key pair
    return new AsymmetricCipherKeyPair(pubKey, privKey);
}
Also used : GF2mField(com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField) PolynomialGF2mSmallM(com.github.zhenwei.core.pqc.math.linearalgebra.PolynomialGF2mSmallM) GF2Matrix(com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix) Permutation(com.github.zhenwei.core.pqc.math.linearalgebra.Permutation) MaMaPe(com.github.zhenwei.core.pqc.math.linearalgebra.GoppaCode.MaMaPe) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Aggregations

GF2Matrix (com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix)6 GF2mField (com.github.zhenwei.core.pqc.math.linearalgebra.GF2mField)6 Permutation (com.github.zhenwei.core.pqc.math.linearalgebra.Permutation)6 PolynomialGF2mSmallM (com.github.zhenwei.core.pqc.math.linearalgebra.PolynomialGF2mSmallM)6 GF2Vector (com.github.zhenwei.core.pqc.math.linearalgebra.GF2Vector)4 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)2 MaMaPe (com.github.zhenwei.core.pqc.math.linearalgebra.GoppaCode.MaMaPe)2 PolynomialRingGF2m (com.github.zhenwei.core.pqc.math.linearalgebra.PolynomialRingGF2m)1