internal GF256Poly Multiply(GF256Poly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GF256Polys do not have same GF256 field"); } if (IsZero() || other.IsZero()) { return(field.GetZero()); } var aCoefficients = coefficients; var aLength = aCoefficients.Length; var bCoefficients = other.coefficients; var bLength = bCoefficients.Length; var product = new int[aLength + bLength - 1]; for (var i = 0; i < aLength; i++) { var aCoeff = aCoefficients[i]; for (var j = 0; j < bLength; j++) { product[i + j] = GF256.AddOrSubtract(product[i + j], field.Multiply(aCoeff, bCoefficients[j])); } } return(new GF256Poly(field, product)); }
/** * @param field the {@link GF256} instance representing the field to use * to perform computations * @param coefficients coefficients as ints representing elements of GF(256), arranged * from most significant (highest-power term) coefficient to least significant * @throws IllegalArgumentException if argument is null or empty, * or if leading coefficient is 0 and this is not a * constant polynomial (that is, it is not the monomial "0") */ internal GF256Poly(GF256 field, int[] coefficients) { if (coefficients == null || coefficients.Length == 0) { throw new ArgumentException(); } this.field = field; int coefficientsLength = coefficients.Length; if (coefficientsLength > 1 && coefficients[0] == 0) { // Leading term must be non-zero for anything except the constant polynomial "0" int firstNonZero = 1; while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0) { firstNonZero++; } if (firstNonZero == coefficientsLength) { this.coefficients = field.GetZero().coefficients; } else { this.coefficients = new int[coefficientsLength - firstNonZero]; System.Array.Copy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.Length); } } else { this.coefficients = coefficients; } }
/** * @param field the {@link GF256} instance representing the field to use * to perform computations * @param coefficients coefficients as ints representing elements of GF(256), arranged * from most significant (highest-power term) coefficient to least significant * @throws IllegalArgumentException if argument is null or empty, * or if leading coefficient is 0 and this is not a * constant polynomial (that is, it is not the monomial "0") */ internal GF256Poly(GF256 field, int[] coefficients) { if (coefficients == null || coefficients.Length == 0) { throw new ArgumentException(); } this.field = field; var coefficientsLength = coefficients.Length; if (coefficientsLength > 1 && coefficients[0] == 0) { // Leading term must be non-zero for anything except the constant polynomial "0" var firstNonZero = 1; while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0) { firstNonZero++; } if (firstNonZero == coefficientsLength) { this.coefficients = field.GetZero().coefficients; } else { this.coefficients = new int[coefficientsLength - firstNonZero]; System.Array.Copy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.Length); } } else { this.coefficients = coefficients; } }