/** * Return a new vector consisting of the elements of this vector with the * indices given by the set <tt>setJ</tt>. * * @param setJ the set of indices of elements to extract * @return the new {@link GF2Vector} * <tt>[this_setJ[0], this_setJ[1], ..., this_setJ[#setJ-1]]</tt> */ public GF2Vector extractVector(int[] setJ) { int k = setJ.length; if (setJ[k - 1] > length) { throw new ArithmeticException("invalid index set"); } GF2Vector result = new GF2Vector(k); for (int i = 0; i < k; i++) { int e = v[setJ[i] >> 5] & (1 << (setJ[i] & 0x1f)); if (e != 0) { result.v[i >> 5] |= 1 << (i & 0x1f); } } return result; }
/** * Return a new vector consisting of the elements of this vector with the * indices given by the set <tt>setJ</tt>. * * @param setJ the set of indices of elements to extract * @return the new {@link GF2Vector} * <tt>[this_setJ[0], this_setJ[1], ..., this_setJ[#setJ-1]]</tt> */ public GF2Vector extractVector(int[] setJ) { int k = setJ.length; if (setJ[k - 1] > length) { throw new ArithmeticException("invalid index set"); } GF2Vector result = new GF2Vector(k); for (int i = 0; i < k; i++) { int e = v[setJ[i] >> 5] & (1 << (setJ[i] & 0x1f)); if (e != 0) { result.v[i >> 5] |= 1 << (i & 0x1f); } } return result; }
/** * Return a new vector consisting of the first <tt>k</tt> elements of this * vector. * * @param k the number of elements to extract * @return a new {@link GF2Vector} consisting of the first <tt>k</tt> * elements of this vector */ public GF2Vector extractLeftVector(int k) { if (k > length) { throw new ArithmeticException("invalid length"); } if (k == length) { return new GF2Vector(this); } GF2Vector result = new GF2Vector(k); int q = k >> 5; int r = k & 0x1f; System.arraycopy(v, 0, result.v, 0, q); if (r != 0) { result.v[q] = v[q] & ((1 << r) - 1); } return result; }
/** * Return a new vector consisting of the first <tt>k</tt> elements of this * vector. * * @param k the number of elements to extract * @return a new {@link GF2Vector} consisting of the first <tt>k</tt> * elements of this vector */ public GF2Vector extractLeftVector(int k) { if (k > length) { throw new ArithmeticException("invalid length"); } if (k == length) { return new GF2Vector(this); } GF2Vector result = new GF2Vector(k); int q = k >> 5; int r = k & 0x1f; System.arraycopy(v, 0, result.v, 0, q); if (r != 0) { result.v[q] = v[q] & ((1 << r) - 1); } return result; }
/** * Construct a new GF2Vector with the given length out of the encoded * vector. * * @param length the length of the vector * @param encVec the encoded vector * @return the decoded vector */ public static GF2Vector OS2VP(int length, byte[] encVec) { if (length < 0) { throw new ArithmeticException("negative length"); } int byteLen = (length + 7) >> 3; if (encVec.length > byteLen) { throw new ArithmeticException("length mismatch"); } return new GF2Vector(length, LittleEndianConversions.toIntArray(encVec)); }
/** * Construct a new GF2Vector with the given length out of the encoded * vector. * * @param length the length of the vector * @param encVec the encoded vector * @return the decoded vector */ public static GF2Vector OS2VP(int length, byte[] encVec) { if (length < 0) { throw new ArithmeticException("negative length"); } int byteLen = (length + 7) >> 3; if (encVec.length > byteLen) { throw new ArithmeticException("length mismatch"); } return new GF2Vector(length, LittleEndianConversions.toIntArray(encVec)); }
/** * Adds another GF2Vector to this vector. * * @param other another GF2Vector * @return <tt>this + other</tt> * @throws ArithmeticException if the other vector is not a GF2Vector or has another * length. */ public Vector add(Vector other) { if (!(other instanceof GF2Vector)) { throw new ArithmeticException("vector is not defined over GF(2)"); } GF2Vector otherVec = (GF2Vector)other; if (length != otherVec.length) { throw new ArithmeticException("length mismatch"); } int[] vec = IntUtils.clone(((GF2Vector)other).v); for (int i = vec.length - 1; i >= 0; i--) { vec[i] ^= v[i]; } return new GF2Vector(length, vec); }
/** * Multiply this vector with a permutation. * * @param p the permutation * @return <tt>this*p = p*this</tt> */ public Vector multiply(Permutation p) { int[] pVec = p.getVector(); if (length != pVec.length) { throw new ArithmeticException("length mismatch"); } GF2Vector result = new GF2Vector(length); for (int i = 0; i < pVec.length; i++) { int e = v[pVec[i] >> 5] & (1 << (pVec[i] & 0x1f)); if (e != 0) { result.v[i >> 5] |= 1 << (i & 0x1f); } } return result; }
/** * Adds another GF2Vector to this vector. * * @param other another GF2Vector * @return <tt>this + other</tt> * @throws ArithmeticException if the other vector is not a GF2Vector or has another * length. */ public Vector add(Vector other) { if (!(other instanceof GF2Vector)) { throw new ArithmeticException("vector is not defined over GF(2)"); } GF2Vector otherVec = (GF2Vector)other; if (length != otherVec.length) { throw new ArithmeticException("length mismatch"); } int[] vec = IntUtils.clone(((GF2Vector)other).v); for (int i = vec.length - 1; i >= 0; i--) { vec[i] ^= v[i]; } return new GF2Vector(length, vec); }
/** * Multiply this vector with a permutation. * * @param p the permutation * @return <tt>this*p = p*this</tt> */ public Vector multiply(Permutation p) { int[] pVec = p.getVector(); if (length != pVec.length) { throw new ArithmeticException("length mismatch"); } GF2Vector result = new GF2Vector(length); for (int i = 0; i < pVec.length; i++) { int e = v[pVec[i] >> 5] & (1 << (pVec[i] & 0x1f)); if (e != 0) { result.v[i >> 5] |= 1 << (i & 0x1f); } } return result; }
return new GF2Vector(res, numRows);
return new GF2Vector(res, numRows);
GF2Vector result = new GF2Vector(n);
return new GF2Vector(numRows, res);
/** * 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(); }
/** * Encrypt a plain text. * * @param input the plain text * @return the cipher text */ public byte[] messageEncrypt(byte[] input) { 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(); }
GF2Vector r = new GF2Vector(k, sr);
GF2Vector rPrime = new GF2Vector(k, sr);
GF2Vector r = new GF2Vector(k, sr);