# Wrong values for multiplication and mod of DCRTPoly

**URL:** <https://openfhe.discourse.group/t/wrong-values-for-multiplication-and-mod-of-dcrtpoly/1793>\
**Category:** Library Questions\
**Created:** [December 19, 2024, 11:23pm UTC](https://openfhe.discourse.group/t/wrong-values-for-multiplication-and-mod-of-dcrtpoly/1793 "2024-12-19T23:23:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brals](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@Brals](https://openfhe.discourse.group/u/Brals)\
**Post date:** [December 19, 2024, 11:23pm UTC](https://openfhe.discourse.group/t/wrong-values-for-multiplication-and-mod-of-dcrtpoly/1793/1 "2024-12-19T23:23:46Z")

</div>

Hello, maybe I misunderstand how the library works. But this code gives me a non zero polynomial:

```auto
int pp = 17;
DCRTPoly gg(dgg, paramsPK, Format::EVALUATION);
gg.SetFormat(Format::COEFFICIENT);
auto ggg = gg.Times(pp);
auto gggg = ggg.ToNativePoly();
gggg.SwitchModulus(pp, 1, 0, 0);

```

It gives me this poly:

```auto
COEF: [7 7 0 0 7 0 7 7 0 0 0 0 0 0 7 0 0 7 7 0 7 7 0 7 0 0 7 7 0 7 7 7 0 0 7 0 ...] modulus: 17

```

Could someone please help me on how to make this multiplication and mod?

---

<div class="post-metadata">

**Author:** ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)\
**Post date:** [December 20, 2024, 5:30am UTC](https://openfhe.discourse.group/t/wrong-values-for-multiplication-and-mod-of-dcrtpoly/1793/2 "2024-12-20T05:30:10Z")

</div>

Please provide a complete example (minimum working example). It is not clear what `paramPK` is set to. If the starting modulus in paramsPK is too small (so that multiplying by 17 wraps around the modulus used there), then you can easily get non-zero values.

Another question, what are you actually trying to do?

---

<div class="post-metadata">

**Author:** ![Brals](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@Brals](https://openfhe.discourse.group/u/Brals)\
**Post date:** [December 20, 2024, 10:02am UTC](https://openfhe.discourse.group/t/wrong-values-for-multiplication-and-mod-of-dcrtpoly/1793/3 "2024-12-20T10:02:35Z")

</div>

Here is how I create parameters:

```auto
CCParams<CryptoContextBFVRNS> parameters;
parameters.SetPlaintextModulus(65537);
parameters.SetMultiplicativeDepth(2);
CryptoContext<DCRTPoly> cryptoContext = GenCryptoContext(parameters);
const auto cryptoParams = cryptoContext->GetCryptoParameters();
const std::shared_ptr<ParmType> paramsPK = cryptoParams->GetParamsPK();
std::cout << "ParamsPK:" << paramsPK->GetParams()[0]->GetModulus() << std::endl;
DugType dug(1);
int pp = 17;
DCRTPoly gg(dug, paramsPK, Format::EVALUATION);
gg.SetFormat(Format::COEFFICIENT);
auto ggg = gg.Times(pp);
auto gggg = ggg.ToNativePoly();
gggg.SwitchModulus(pp, 1, 0, 0);
std::cout << "gggg: " << gggg << std::endl;

```

I tried to change to use discrete uniform, but I guess I am missing on how is the DCRT polynomial representation, since it is a lot of big values. I am only trying to learn the library but got confused with both polynomial representations I guess.

This is the output trimmed:

```auto
ParamsPK:1152921504606830593
gggg: COEF: [5 11 1 3 1 6 7 4 10 8 4 0 15 14 7 12 13 14 3 11 15 5 8 9 1 6 6 5 15...] modulus: 17

```

How can I multiply the full polynomial by a value? I want to multiply a small poly (sampled by a gaussian) by a small number, I tried sampling with ternary as well and gives me wrong values. Only binary and other pp`s worked for now. For ternary, I don’t know why the values are 5, they should be 4 if the modulus operation is not balanced.

The main problem is that the sampler is centered on 0 but the operations (at least `Times`) are not using balanced modulo, do we have a workaround for this? If you have a polynomial (-x**2 + x%101)\*3, it should be (-3x**2 + 3x)%101 and not (98x\*\*2 + 3x)%101

---

<div class="post-metadata">

**Author:** ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)\
**Post date:** [December 23, 2024, 10:19pm UTC](https://openfhe.discourse.group/t/wrong-values-for-multiplication-and-mod-of-dcrtpoly/1793/5 "2024-12-23T22:19:02Z")

</div>

Here is a properly working example. There were a couple of errors. First, dug (random number mod) was used instead of dgg. Second, ToNativePoly() was called, which is used for a different purpose. Now the result is all 0’s.

```auto
#include "openfhe.h"
#include "openfhecore.h"

using namespace lbcrypto;

int main() {
	CCParams<CryptoContextBFVRNS> parameters;
	parameters.SetPlaintextModulus(65537);
	parameters.SetMultiplicativeDepth(2);
	CryptoContext<DCRTPoly> cryptoContext = GenCryptoContext(parameters);
	const auto cryptoParams = cryptoContext->GetCryptoParameters();
	const auto paramsPK = cryptoParams->GetParamsPK();
	std::cout << "ParamsPK:" << paramsPK->GetParams()[0]->GetModulus() << std::endl;
	// this code generates random numbers between 0 and modulus - 1
	// DCRTPoly::DugType dug;
	DCRTPoly::DggType dgg(3.19);
	int pp = 17;
	DCRTPoly gg(dgg, paramsPK, Format::EVALUATION);
	gg.SetFormat(Format::COEFFICIENT);
	std::cout << "gg: " << gg << std::endl;
	auto ggg = gg.Times(pp);
	std::cout << "ggg: " << ggg << std::endl;
	auto gggg = ggg.GetAllElements()[0];
	std::cout << "gggg: " << gggg << std::endl;
	gggg.SwitchModulus(pp, 1, 0, 0);
	std::cout << "gggg: " << gggg << std::endl;
    return 0;
}

```
