/// <summary> /// Enlarges the size of this PolynomialGF2n to <c>k</c> + 1 /// </summary> /// /// <param name="K">The new maximum degree</param> public void Enlarge(int K) { if (K <= _size) { return; } int i; GF2nElement[] res = new GF2nElement[K]; Array.Copy(_coeff, 0, res, 0, _size); GF2nField f = _coeff[0].GetField(); if (_coeff[0] is GF2nPolynomialElement) { for (i = _size; i < K; i++) { res[i] = GF2nPolynomialElement.Zero((GF2nPolynomialField)f); } } else if (_coeff[0] is GF2nONBElement) { for (i = _size; i < K; i++) { res[i] = GF2nONBElement.Zero((GF2nONBField)f); } } _size = K; _coeff = res; }
/// <summary> /// Shifts left <c>this</c> by <c>N</c> and stores the result in <c>this</c> PolynomialGF2n /// </summary> /// /// <param name="N">The amount the amount to shift the coefficients</param> public void ShiftThisLeft(int N) { if (N > 0) { int i; int oldSize = _size; GF2nField f = _coeff[0].GetField(); Enlarge(_size + N); for (i = oldSize - 1; i >= 0; i--) { _coeff[i + N] = _coeff[i]; } if (_coeff[0] is GF2nPolynomialElement) { for (i = N - 1; i >= 0; i--) { _coeff[i] = GF2nPolynomialElement.Zero((GF2nPolynomialField)f); } } else if (_coeff[0] is GF2nONBElement) { for (i = N - 1; i >= 0; i--) { _coeff[i] = GF2nONBElement.Zero((GF2nONBField)f); } } } }
/// <summary> /// Compute the square root of this element and return the result in a new GF2nElement /// </summary> /// /// <returns>Returns <c>this^1/2</c> (newly created)</returns> public override GF2nElement SquareRoot() { GF2nONBElement result = new GF2nONBElement(this); result.SquareRootThis(); return(result); }
/// <summary> /// Compute <c>this</c> element + 1 /// </summary> /// /// <returns>Returns <c>this</c> + 1</returns> public override GF2nElement Increase() { GF2nONBElement result = new GF2nONBElement(this); result.IncreaseThis(); return(result); }
/// <summary> /// Compute the product of this element and <c>factor</c> /// </summary> /// /// <param name="Factor">he factor</param> /// /// <returns>Returns <c>this * factor</c> </returns> public override IGFElement Multiply(IGFElement Factor) { GF2nONBElement result = new GF2nONBElement(this); result.MultiplyThisBy(Factor); return(result); }
/// <summary> /// Compute the sum of this element and <c>Addend</c>. /// </summary> /// /// <param name="Addend">The addend</param> /// /// <returns>Returns <c>this + other</c></returns> public override IGFElement Add(IGFElement Addend) { GF2nONBElement result = new GF2nONBElement(this); result.AddToThis(Addend); return(result); }
/// <summary> /// Compute the multiplicative inverse of this element /// </summary> /// /// <returns>Returns <c>this^-1</c> (newly created)</returns> public override IGFElement Invert() { GF2nONBElement result = new GF2nONBElement(this); result.InvertThis(); return(result); }
/// <summary> /// Copy the field values from another GF2nONBElement instance /// </summary> /// /// <param name="Gf2n">The GF2nONBElement to copy</param> public GF2nONBElement(GF2nONBElement Gf2n) { mField = Gf2n.mField; mDegree = mField.Degree; _mLength = ((GF2nONBField)mField).GetONBLength(); _mBit = ((GF2nONBField)mField).GetONBBit(); _mPol = new long[_mLength]; Assign(Gf2n.GetElement()); }
/// <summary> /// Computes a random root of the given polynomial /// </summary> /// /// <param name="P">A polynomial</param> /// /// <returns>A random root of the polynomial</returns> public override GF2nElement RandomRoot(GF2Polynomial P) { // We are in B1!!! GF2nPolynomial c; GF2nPolynomial ut; GF2nElement u; GF2nPolynomial h; int hDegree; // 1. Set g(t) <- f(t) GF2nPolynomial g = new GF2nPolynomial(P, this); int gDegree = g.Degree; int i; // 2. while deg(g) > 1 while (gDegree > 1) { do { // 2.1 choose random u (element of) GF(2^m) u = new GF2nONBElement(this, _secRand); ut = new GF2nPolynomial(2, GF2nONBElement.Zero(this)); // 2.2 Set c(t) <- ut ut.Set(1, u); c = new GF2nPolynomial(ut); // 2.3 For i from 1 to m-1 do for (i = 1; i <= DegreeN - 1; i++) { // 2.3.1 c(t) <- (c(t)^2 + ut) mod g(t) c = c.MultiplyAndReduce(c, g); c = c.Add(ut); } // 2.4 set h(t) <- GCD(c(t), g(t)) h = c.Gcd(g); // 2.5 if h(t) is constant or deg(g) = deg(h) then go to // step 2.1 hDegree = h.Degree; gDegree = g.Degree; }while ((hDegree == 0) || (hDegree == gDegree)); // 2.6 If 2deg(h) > deg(g) then set g(t) <- g(t)/h(t) ... if ((hDegree << 1) > gDegree) { g = g.Quotient(h); } else // ... else g(t) <- h(t) { g = new GF2nPolynomial(h); } gDegree = g.Degree; } // 3. Output g(0) return(g.At(0)); }
/// <summary> /// Compare this element with another object /// </summary> /// /// <param name="Obj">The object for comprison</param> /// /// <returns>Returns <c>true</c> if the two objects are equal, <c>false</c> otherwise</returns> public override bool Equals(Object other) { if (other == null || !(other is GF2nONBElement)) { return(false); } GF2nONBElement otherElem = (GF2nONBElement)other; for (int i = 0; i < _mLength; i++) { if (_mPol[i] != otherElem._mPol[i]) { return(false); } } return(true); }
/// <summary> /// Multiplicatively invert of this element (overwrite <c>this</c>) /// </summary> public void InvertThis() { if (IsZero()) { throw new ArithmeticException(); } int r = 31; // mDegree kann nur 31 Bits lang sein!!! // Bitlaenge von mDegree: for (bool found = false; !found && r >= 0; r--) { if (((mDegree - 1) & _mBitmask[r]) != 0) { found = true; } } r++; GF2nElement m = Zero((GF2nONBField)mField); GF2nElement n = new GF2nONBElement(this); int k = 1; for (int i = r - 1; i >= 0; i--) { m = (GF2nElement)n.Clone(); for (int j = 1; j <= k; j++) { m.SquareThis(); } n.MultiplyThisBy(m); k <<= 1; if (((mDegree - 1) & _mBitmask[i]) != 0) { n.SquareThis(); n.MultiplyThisBy(this); k++; } } n.SquareThis(); }
/// <summary> /// Creates a new PolynomialGF2n from the given Bitstring <c>G</c> over the GF2nField <c>B1</c> /// </summary> /// /// <param name="G">The Bitstring to use</param> /// <param name="B1">The field</param> public GF2nPolynomial(GF2Polynomial G, GF2nField B1) { _size = B1.Degree + 1; _coeff = new GF2nElement[_size]; int i; if (B1 is GF2nONBField) { for (i = 0; i < _size; i++) { if (G.TestBit(i)) { _coeff[i] = GF2nONBElement.One((GF2nONBField)B1); } else { _coeff[i] = GF2nONBElement.Zero((GF2nONBField)B1); } } } else if (B1 is GF2nPolynomialField) { for (i = 0; i < _size; i++) { if (G.TestBit(i)) { _coeff[i] = GF2nPolynomialElement.One((GF2nPolynomialField)B1); } else { _coeff[i] = GF2nPolynomialElement.Zero((GF2nPolynomialField)B1); } } } else { throw new ArgumentException("GF2nPolynomial: PolynomialGF2n(Bitstring, GF2nField): B1 must be an instance of GF2nONBField or GF2nPolynomialField!"); } }
/// <summary> /// Computes the change-of-basis matrix for basis conversion according to 1363. /// The result is stored in the lists fields and matrices. /// </summary> /// /// <param name="B1">The GF2nField to convert to</param> public override void ComputeCOBMatrix(GF2nField B1) { // we are in B0 here! if (DegreeN != B1.Degree) { throw new ArgumentException("GF2nPolynomialField.computeCOBMatrix: B1 has a different degree and thus cannot be coverted to!"); } if (B1 is GF2nONBField) { // speedup (calculation is done in PolynomialElements instead of ONB) B1.ComputeCOBMatrix(this); return; } int i, j; GF2nElement[] gamma; GF2nElement u; GF2Polynomial[] COBMatrix = new GF2Polynomial[DegreeN]; for (i = 0; i < DegreeN; i++) { COBMatrix[i] = new GF2Polynomial(DegreeN); } // find Random Root do { // u is in representation according to B1 u = B1.RandomRoot(FieldPoly); }while (u.IsZero()); // build gamma matrix by multiplying by u if (u is GF2nONBElement) { gamma = new GF2nONBElement[DegreeN]; gamma[DegreeN - 1] = GF2nONBElement.One((GF2nONBField)B1); } else { gamma = new GF2nPolynomialElement[DegreeN]; gamma[DegreeN - 1] = GF2nPolynomialElement.One((GF2nPolynomialField)B1); } gamma[DegreeN - 2] = u; for (i = DegreeN - 3; i >= 0; i--) { gamma[i] = (GF2nElement)gamma[i + 1].Multiply(u); } if (B1 is GF2nONBField) { // convert horizontal gamma matrix by vertical Bitstrings for (i = 0; i < DegreeN; i++) { for (j = 0; j < DegreeN; j++) { // TODO remember: ONB treats its Bits in reverse order !!! if (gamma[i].TestBit(DegreeN - j - 1)) { COBMatrix[DegreeN - j - 1].SetBit(DegreeN - i - 1); } } } } else { // convert horizontal gamma matrix by vertical Bitstrings for (i = 0; i < DegreeN; i++) { for (j = 0; j < DegreeN; j++) { if (gamma[i].TestBit(j)) { COBMatrix[DegreeN - j - 1].SetBit(DegreeN - i - 1); } } } } // store field and matrix for further use Fields.Add(B1); Matrices.Add(COBMatrix); // store field and inverse matrix for further use in B1 B1.Fields.Add(this); B1.Matrices.Add(InvertMatrix(COBMatrix)); }
/// <summary> /// Converts the given element in representation according to this field to a new element in /// representation according to B1 using the change-of-basis matrix calculated by computeCOBMatrix. /// </summary> /// /// <param name="Elem">The GF2nElement to convert</param> /// <param name="Basis">The basis to convert <c>Elem</c> to</param> /// /// <returns>Returns <c>Elem</c> converted to a new element representation according to <c>basis</c></returns> public GF2nElement Convert(GF2nElement Elem, GF2nField Basis) { if (Basis == this) { return((GF2nElement)Elem.Clone()); } if (FieldPoly.Equals(Basis.FieldPoly)) { return((GF2nElement)Elem.Clone()); } if (DegreeN != Basis.DegreeN) { throw new Exception("GF2nField.Convert: B1 has a different degree and thus cannot be coverted to!"); } int i; GF2Polynomial[] COBMatrix; i = Fields.IndexOf(Basis); if (i == -1) { ComputeCOBMatrix(Basis); i = Fields.IndexOf(Basis); } COBMatrix = (GF2Polynomial[])Matrices[i]; GF2nElement elemCopy = (GF2nElement)Elem.Clone(); if (elemCopy is GF2nONBElement) { ((GF2nONBElement)elemCopy).ReverseOrder(); } GF2Polynomial bs = new GF2Polynomial(DegreeN, elemCopy.ToFlexiBigInt()); bs.ExpandN(DegreeN); GF2Polynomial result = new GF2Polynomial(DegreeN); for (i = 0; i < DegreeN; i++) { if (bs.VectorMult(COBMatrix[i])) { result.SetBit(DegreeN - 1 - i); } } if (Basis is GF2nPolynomialField) { return(new GF2nPolynomialElement((GF2nPolynomialField)Basis, result)); } else if (Basis is GF2nONBField) { GF2nONBElement res = new GF2nONBElement((GF2nONBField)Basis, result.ToFlexiBigInt()); res.ReverseOrder(); return(res); } else { throw new Exception("GF2nField.convert: B1 must be an instance of GF2nPolynomialField or GF2nONBField!"); } }