use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix 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 };
}
use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix 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);
}
use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix in project LinLong-Java by zhenwei1108.
the class McElieceCCA2Primitives method encryptionPrimitive.
/**
* The McEliece encryption primitive.
*
* @param pubKey the public key
* @param m the message vector
* @param z the error vector
* @return <tt>m*G + z</tt>
*/
public static GF2Vector encryptionPrimitive(McElieceCCA2PublicKeyParameters pubKey, GF2Vector m, GF2Vector z) {
GF2Matrix matrixG = pubKey.getG();
Vector mG = matrixG.leftMultiplyLeftCompactForm(m);
return (GF2Vector) mG.add(z);
}
use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix in project LinLong-Java by zhenwei1108.
the class McElieceCipher method messageEncrypt.
/**
* Encrypt a plain text.
*
* @param input the plain text
* @return the cipher text
*/
public byte[] messageEncrypt(byte[] input) {
if (!forEncryption) {
throw new IllegalStateException("cipher initialised for decryption");
}
GF2Vector m = computeMessageRepresentative(input);
GF2Vector z = new GF2Vector(n, t, sr);
GF2Matrix g = ((McEliecePublicKeyParameters) key).getG();
Vector mG = g.leftMultiply(m);
GF2Vector mGZ = (GF2Vector) mG.add(z);
return mGZ.getEncoded();
}
use of com.github.zhenwei.core.pqc.math.linearalgebra.GF2Matrix in project LinLong-Java by zhenwei1108.
the class McElieceCCA2Primitives method decryptionPrimitive.
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 };
}
Aggregations