void exponentiate_uint_mod(const BigUInt &operand, const BigUInt &exponent, const BigUInt &modulus, BigUInt &destination, const MemoryPoolHandle &pool) { if (operand.significant_bit_count() > modulus.significant_bit_count()) { throw invalid_argument("operand is not reduced"); } if (operand.is_zero() && exponent == 0) { throw invalid_argument("undefined operation"); } if (!pool) { throw invalid_argument("pool is uninitialized"); } if (operand.is_zero()) { destination.set_zero(); return; } if (destination.bit_count() != modulus.significant_bit_count()) { destination.resize(modulus.significant_bit_count()); } ConstPointer operand_ptr = duplicate_uint_if_needed(operand, modulus.uint64_count(), false, pool); util::exponentiate_uint_mod(operand_ptr.get(), exponent.data(), exponent.uint64_count(), Modulus(modulus.data(), modulus.uint64_count(), pool), destination.data(), pool); }
ChooserPoly ChooserEvaluator::multiply_plain(const ChooserPoly &operand, int plain_max_coeff_count, const BigUInt &plain_max_abs_value) { if (operand.max_coeff_count_ <= 0 || operand.comp_ == nullptr) { throw invalid_argument("operand is not correctly initialized"); } if (plain_max_coeff_count <= 0) { throw invalid_argument("plain_max_coeff_count must be positive"); } if (plain_max_abs_value.is_zero()) { return ChooserPoly(1, 0, new MultiplyPlainComputation(*operand.comp_, plain_max_coeff_count, plain_max_abs_value)); } if (operand.max_abs_value_.is_zero()) { return ChooserPoly(1, 0, new MultiplyPlainComputation(*operand.comp_, plain_max_coeff_count, plain_max_abs_value)); } uint64_t growth_factor = min(operand.max_coeff_count_, plain_max_coeff_count); int prod_bit_count = operand.max_abs_value_.significant_bit_count() + plain_max_abs_value.significant_bit_count() + get_significant_bit_count(growth_factor) + 1; int prod_uint64_count = divide_round_up(prod_bit_count, bits_per_uint64); Pointer prod_max_abs_value(allocate_zero_uint(prod_uint64_count, pool_)); ConstPointer wide_operand_max_abs_value(duplicate_uint_if_needed(operand.max_abs_value_.pointer(), operand.max_abs_value_.uint64_count(), prod_uint64_count, false, pool_)); multiply_uint_uint(&growth_factor, 1, plain_max_abs_value.pointer(), plain_max_abs_value.uint64_count(), prod_uint64_count, prod_max_abs_value.get()); ConstPointer temp_pointer(duplicate_uint_if_needed(prod_max_abs_value.get(), prod_uint64_count, prod_uint64_count, true, pool_)); multiply_uint_uint(wide_operand_max_abs_value.get(), prod_uint64_count, temp_pointer.get(), prod_uint64_count, prod_uint64_count, prod_max_abs_value.get()); return ChooserPoly(operand.max_coeff_count_ + plain_max_coeff_count - 1, BigUInt(prod_bit_count, prod_max_abs_value.get()), new MultiplyPlainComputation(*operand.comp_, plain_max_coeff_count, plain_max_abs_value)); }
BigUInt poly_infty_norm_coeffmod(const BigPoly &poly, const BigUInt &modulus, const MemoryPoolHandle &pool) { if (modulus.is_zero()) { throw invalid_argument("modulus cannot be zero"); } if (!pool) { throw invalid_argument("pool is uninitialized"); } if (poly.is_zero()) { return BigUInt(); } int poly_coeff_count = poly.coeff_count(); int poly_coeff_bit_count = poly.coeff_bit_count(); int poly_coeff_uint64_count = divide_round_up(poly_coeff_bit_count, bits_per_uint64); Modulus mod(modulus.data(), modulus.uint64_count(), pool); BigUInt result(modulus.significant_bit_count()); util::poly_infty_norm_coeffmod(poly.data(), poly_coeff_count, poly_coeff_uint64_count, mod, result.data(), pool); return result; }
ChooserPoly::ChooserPoly(int max_coeff_count, const BigUInt &max_abs_value) : max_coeff_count_(max_coeff_count), max_abs_value_(max_abs_value), comp_(new FreshComputation()) { if (max_coeff_count <= 0) { throw invalid_argument("max_coeff_count must be strictly positive"); } if (max_abs_value.is_zero()) { max_coeff_count_ = 1; } }
BigUInt BigUInt::operator %(const BigUInt& operand2) const { if (operand2.is_zero()) { throw invalid_argument("operand2 must be positive"); } MemoryPool pool; Modulus modulus(operand2.pointer(), operand2.uint64_count(), pool); int result_bits = significant_bit_count(); BigUInt result(result_bits); result = *this; int uint64_count = divide_round_up(result_bits, bits_per_uint64); modulo_uint_inplace(result.pointer(), uint64_count, modulus, pool); return result; }
ChooserPoly ChooserEvaluator::sub_plain(const ChooserPoly &operand, int plain_max_coeff_count, const BigUInt &plain_max_abs_value) { if (operand.max_coeff_count_ <= 0 || operand.comp_ == nullptr) { throw invalid_argument("operand is not correctly initialized"); } if (plain_max_coeff_count <= 0) { throw invalid_argument("plain_max_coeff_count must be positive"); } if (plain_max_abs_value.is_zero()) { return ChooserPoly(operand.max_coeff_count_, operand.max_abs_value_, new SubPlainComputation(*operand.comp_)); } if (operand.max_abs_value_.is_zero()) { return ChooserPoly(plain_max_coeff_count, plain_max_abs_value, new SubPlainComputation(*operand.comp_)); } return ChooserPoly(max(operand.max_coeff_count_, plain_max_coeff_count), operand.max_abs_value_ + plain_max_abs_value, new SubPlainComputation(*operand.comp_)); }
void poly_eval_uint_mod(const BigPoly &poly_to_evaluate, const BigUInt &value, const BigUInt &modulus, BigUInt &destination, const MemoryPoolHandle &pool) { if (poly_to_evaluate.significant_coeff_bit_count() > modulus.significant_bit_count()) { throw invalid_argument("poly_to_evaluate is not reduced"); } if (value.significant_bit_count() > modulus.significant_bit_count()) { throw invalid_argument("value is not reduced"); } if (!pool) { throw invalid_argument("pool is uninitialized"); } int poly_to_eval_coeff_uint64_count = poly_to_evaluate.coeff_uint64_count(); int modulus_bit_count = modulus.significant_bit_count(); if (poly_to_evaluate.is_zero()) { destination.set_zero(); } if (value.is_zero()) { destination.resize(modulus_bit_count); modulo_uint(poly_to_evaluate.data(), poly_to_eval_coeff_uint64_count, Modulus(modulus.data(), modulus.uint64_count(), pool), destination.data(), pool); return; } ConstPointer value_ptr = duplicate_uint_if_needed(value, modulus.uint64_count(), false, pool); destination.resize(modulus_bit_count); util::poly_eval_uint_mod(poly_to_evaluate.data(), poly_to_evaluate.coeff_count(), value_ptr.get(), Modulus(modulus.data(), modulus.uint64_count(), pool), destination.data(), pool); }