/// <summary> /// Computes the field polynomial for a ONB according to IEEE 1363 A.7.2 /// </summary> protected override void ComputeFieldPolynomial() { if (m_Type == 1) { FieldPoly = new GF2Polynomial(DegreeN + 1, "ALL"); } else if (m_Type == 2) { // 1. q = 1 GF2Polynomial q = new GF2Polynomial(DegreeN + 1, "ONE"); // 2. p = t+1 GF2Polynomial p = new GF2Polynomial(DegreeN + 1, "X"); p.AddToThis(q); GF2Polynomial r; int i; // 3. for i = 1 to (m-1) do for (i = 1; i < DegreeN; i++) { // r <- q r = q; // q <- p q = p; // p = tq+r p = q.ShiftLeft(); p.AddToThis(r); } FieldPoly = p; } }
/// <summary> /// Calculates the multiplicative inverse of <c>this</c> using the modified almost inverse algorithm and returns the result in a new GF2nPolynomialElement /// </summary> /// /// <returns>Returns <c>this</c>^(-1)</returns> public GF2nPolynomialElement InvertMAIA() { if (IsZero()) { throw new ArithmeticException(); } GF2Polynomial b = new GF2Polynomial(mDegree, "ONE"); GF2Polynomial c = new GF2Polynomial(mDegree); GF2Polynomial u = GetGF2Polynomial(); GF2Polynomial v = mField.FieldPolynomial; GF2Polynomial h; while (true) { while (!u.TestBit(0)) { // x|u (x divides u) u.ShiftRightThis(); // u = u / x if (!b.TestBit(0)) { b.ShiftRightThis(); } else { b.AddToThis(mField.FieldPolynomial); b.ShiftRightThis(); } } if (u.IsOne()) { return(new GF2nPolynomialElement((GF2nPolynomialField)mField, b)); } u.ReduceN(); v.ReduceN(); if (u.Length < v.Length) { h = u; u = v; v = h; h = b; b = c; c = h; } u.AddToThis(v); b.AddToThis(c); } }
/// <summary> /// Compute <c>this + addend</c> (overwrite <c>this</c>) /// </summary> /// /// <param name="Addend">The addend</param> public override void AddToThis(IGFElement Addend) { if (!(Addend is GF2nPolynomialElement)) { throw new Exception(); } if (!mField.Equals(((GF2nPolynomialElement)Addend).mField)) { throw new Exception(); } polynomial.AddToThis(((GF2nPolynomialElement)Addend).polynomial); }
/// <summary> /// Calculates the multiplicative inverse of <c>this</c> using the modified almost inverse algorithm and returns the result in a new GF2nPolynomialElement /// </summary> /// /// <returns>Returns <c>this</c>^(-1)</returns> public GF2nPolynomialElement InvertMAIA() { if (IsZero()) { throw new ArithmeticException(); } GF2Polynomial b = new GF2Polynomial(mDegree, "ONE"); GF2Polynomial c = new GF2Polynomial(mDegree); GF2Polynomial u = GetGF2Polynomial(); GF2Polynomial v = mField.FieldPolynomial; GF2Polynomial h; while (true) { while (!u.TestBit(0)) { // x|u (x divides u) u.ShiftRightThis(); // u = u / x if (!b.TestBit(0)) { b.ShiftRightThis(); } else { b.AddToThis(mField.FieldPolynomial); b.ShiftRightThis(); } } if (u.IsOne()) return new GF2nPolynomialElement((GF2nPolynomialField)mField, b); u.ReduceN(); v.ReduceN(); if (u.Length < v.Length) { h = u; u = v; v = h; h = b; b = c; c = h; } u.AddToThis(v); b.AddToThis(c); } }
/// <summary> /// Computes the field polynomial for a ONB according to IEEE 1363 A.7.2 /// </summary> protected override void ComputeFieldPolynomial() { if (_mType == 1) { FieldPoly = new GF2Polynomial(DegreeN + 1, "ALL"); } else if (_mType == 2) { // 1. q = 1 GF2Polynomial q = new GF2Polynomial(DegreeN + 1, "ONE"); // 2. p = t+1 GF2Polynomial p = new GF2Polynomial(DegreeN + 1, "X"); p.AddToThis(q); GF2Polynomial r; int i; // 3. for i = 1 to (m-1) do for (i = 1; i < DegreeN; i++) { // r <- q r = q; // q <- p q = p; // p = tq+r p = q.ShiftLeft(); p.AddToThis(r); } FieldPoly = p; } }