Exemplo n.º 1
0
ModulusPoly
ModulusPoly::add(const ModulusPoly& other) const
{
	if (_field != other._field) {
		throw std::invalid_argument("ModulusPolys do not have same ModulusGF field");
	}
	if (isZero()) {
		return other;
	}
	if (other.isZero()) {
		return *this;
	}

	auto smallerCoefficients = &_coefficients;
	auto largerCoefficients = &other._coefficients;
	if (smallerCoefficients->size() > largerCoefficients->size()) {
		std::swap(smallerCoefficients, largerCoefficients);
	}
	std::vector<int> sumDiff(largerCoefficients->size());
	size_t lengthDiff = largerCoefficients->size() - smallerCoefficients->size();

	// Copy high-order terms only found in higher-degree polynomial's coefficients
	std::copy_n(largerCoefficients->begin(), lengthDiff, sumDiff.begin());
	for (size_t i = lengthDiff; i < largerCoefficients->size(); i++) {
		sumDiff[i] = _field->add((*smallerCoefficients)[i - lengthDiff], (*largerCoefficients)[i]);
	}
	return ModulusPoly(*_field, sumDiff);
}
Exemplo n.º 2
0
GF256Poly *GF256Poly::addOrSubtract(GF256Poly *b) {
    if (&field != &b->field) {
        throw IllegalArgumentException("Fields must be the same");
    }
    if (isZero()) {
        return b;
    }
    if (b->isZero()) {
        return this;
    }

    ArrayRef<int> largerCoefficients = coefficients;
    ArrayRef<int> smallerCoefficients = b->coefficients;
    if (smallerCoefficients.size() > largerCoefficients.size()) {
        ArrayRef<int> tmp(smallerCoefficients);
        smallerCoefficients = largerCoefficients;
        largerCoefficients = tmp;
    }

    ArrayRef<int> sumDiff(new Array<int>(largerCoefficients.size()));

    unsigned lengthDiff = largerCoefficients.size() - smallerCoefficients.size();
    for (unsigned i = 0; i < lengthDiff; i++) {
        sumDiff[i] = largerCoefficients[i];
    }
    for (unsigned i = lengthDiff; i < largerCoefficients.size(); i++) {
        sumDiff[i] = GF256::addOrSubtract(smallerCoefficients[i - lengthDiff],
                                          largerCoefficients[i]);
    }
    return new GF256Poly(field, sumDiff);
}