/// <summary> /// Tests all trinomials of degree (n+1) until a irreducible is found and stores the result in <c>field polynomial</c>. /// Returns false if no irreducible trinomial exists in GF(2^n). This can take very long for huge degrees. /// </summary> /// /// <returns>Returns true if an irreducible trinomial is found</returns> private bool TestTrinomials() { int i, l; bool done = false; l = 0; FieldPoly = new GF2Polynomial(DegreeN + 1); FieldPoly.SetBit(0); FieldPoly.SetBit(DegreeN); for (i = 1; (i < DegreeN) && !done; i++) { FieldPoly.SetBit(i); done = FieldPoly.IsIrreducible(); l++; if (done) { _isTrinomial = true; _tc = i; return(done); } FieldPoly.ResetBit(i); done = FieldPoly.IsIrreducible(); } return(done); }
/// <summary> /// Tests all pentanomials of degree (n+1) until a irreducible is found and stores the result in <c>field polynomial</c>. /// Returns false if no irreducible pentanomial exists in GF(2^n). /// This can take very long for huge degrees. /// </summary> /// /// <returns>Returns true if an irreducible pentanomial is found</returns> private bool TestPentanomials() { int i, j, k, l; bool done = false; l = 0; FieldPoly = new GF2Polynomial(DegreeN + 1); FieldPoly.SetBit(0); FieldPoly.SetBit(DegreeN); for (i = 1; (i <= (DegreeN - 3)) && !done; i++) { FieldPoly.SetBit(i); for (j = i + 1; (j <= (DegreeN - 2)) && !done; j++) { FieldPoly.SetBit(j); for (k = j + 1; (k <= (DegreeN - 1)) && !done; k++) { FieldPoly.SetBit(k); if (((DegreeN & 1) != 0) | ((i & 1) != 0) | ((j & 1) != 0) | ((k & 1) != 0)) { done = FieldPoly.IsIrreducible(); l++; if (done) { _isPentanomial = true; _pc[0] = i; _pc[1] = j; _pc[2] = k; return(done); } } FieldPoly.ResetBit(k); } FieldPoly.ResetBit(j); } FieldPoly.ResetBit(i); } return(done); }
/// <summary> /// Squares this GF2nPolynomialElement using GF2nFields squaring matrix. /// <para>This is supposed to be fast when using a polynomial (no tri- or pentanomial) as fieldpolynomial. /// Use SquarePreCalc when using a tri- or pentanomial as fieldpolynomial instead.</para> /// </summary> public void SquareThisMatrix() { GF2Polynomial result = new GF2Polynomial(mDegree); for (int i = 0; i < mDegree; i++) { if (polynomial.VectorMult(((GF2nPolynomialField)mField).SquaringMatrix[mDegree - i - 1])) { result.SetBit(i); } } polynomial = result; }
/// <summary> /// Tests random polynomials of degree (n+1) until an irreducible is found and stores the result in <c>field polynomial</c>. /// This can take very long for huge degrees. /// </summary> /// /// <returns>Returns true</returns> private bool TestRandom() { int l; bool done = false; FieldPoly = new GF2Polynomial(DegreeN + 1); l = 0; while (!done) { l++; FieldPoly.Randomize(); FieldPoly.SetBit(DegreeN); FieldPoly.SetBit(0); if (FieldPoly.IsIrreducible()) { done = true; return(done); } } return(done); }
/// <summary> /// Tests all trinomials of degree (n+1) until a irreducible is found and stores the result in <c>field polynomial</c>. /// Returns false if no irreducible trinomial exists in GF(2^n). This can take very long for huge degrees. /// </summary> /// /// <returns>Returns true if an irreducible trinomial is found</returns> private bool TestTrinomials() { int i, l; bool done = false; l = 0; FieldPoly = new GF2Polynomial(DegreeN + 1); FieldPoly.SetBit(0); FieldPoly.SetBit(DegreeN); for (i = 1; (i < DegreeN) && !done; i++) { FieldPoly.SetBit(i); done = FieldPoly.IsIrreducible(); l++; if (done) { _isTrinomial = true; _tc = i; return done; } FieldPoly.ResetBit(i); done = FieldPoly.IsIrreducible(); } return done; }
/// <summary> /// Tests random polynomials of degree (n+1) until an irreducible is found and stores the result in <c>field polynomial</c>. /// This can take very long for huge degrees. /// </summary> /// /// <returns>Returns true</returns> private bool TestRandom() { int l; bool done = false; FieldPoly = new GF2Polynomial(DegreeN + 1); l = 0; while (!done) { l++; FieldPoly.Randomize(); FieldPoly.SetBit(DegreeN); FieldPoly.SetBit(0); if (FieldPoly.IsIrreducible()) { done = true; return done; } } return done; }
/// <summary> /// Tests all pentanomials of degree (n+1) until a irreducible is found and stores the result in <c>field polynomial</c>. /// Returns false if no irreducible pentanomial exists in GF(2^n). /// This can take very long for huge degrees. /// </summary> /// /// <returns>Returns true if an irreducible pentanomial is found</returns> private bool TestPentanomials() { int i, j, k, l; bool done = false; l = 0; FieldPoly = new GF2Polynomial(DegreeN + 1); FieldPoly.SetBit(0); FieldPoly.SetBit(DegreeN); for (i = 1; (i <= (DegreeN - 3)) && !done; i++) { FieldPoly.SetBit(i); for (j = i + 1; (j <= (DegreeN - 2)) && !done; j++) { FieldPoly.SetBit(j); for (k = j + 1; (k <= (DegreeN - 1)) && !done; k++) { FieldPoly.SetBit(k); if (((DegreeN & 1) != 0) | ((i & 1) != 0) | ((j & 1) != 0) | ((k & 1) != 0)) { done = FieldPoly.IsIrreducible(); l++; if (done) { _isPentanomial = true; _pc[0] = i; _pc[1] = j; _pc[2] = k; return done; } } FieldPoly.ResetBit(k); } FieldPoly.ResetBit(j); } FieldPoly.ResetBit(i); } return done; }
/// <summary> /// Squares this GF2nPolynomialElement using GF2nFields squaring matrix. /// <para>This is supposed to be fast when using a polynomial (no tri- or pentanomial) as fieldpolynomial. /// Use SquarePreCalc when using a tri- or pentanomial as fieldpolynomial instead.</para> /// </summary> public void SquareThisMatrix() { GF2Polynomial result = new GF2Polynomial(mDegree); for (int i = 0; i < mDegree; i++) { if (polynomial.VectorMult(((GF2nPolynomialField)mField).SquaringMatrix[mDegree - i - 1])) result.SetBit(i); } polynomial = result; }
/// <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!"); } }
/// <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!"); } }