Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequence of matrix-vector multiplications #55

Open
JanW92 opened this issue Jul 24, 2021 · 1 comment
Open

Sequence of matrix-vector multiplications #55

JanW92 opened this issue Jul 24, 2021 · 1 comment

Comments

@JanW92
Copy link

JanW92 commented Jul 24, 2021

Hello,
I am implementing a matrix-vector multiplication as following:
`
Ring ring;
SecretKey sk(ring);
Scheme scheme(sk, ring);
long logq = 800; ///< Ciphertext Modulus. Should be smaller than logQ in "src/Params.h" : 800
long logp = 40; ///Scaling factor.

//Should be smaller than logN in "src/Params.h" : 16 => Max Slots = 65536
long logn = 7; // -> 128
long n = 1<< logn;

for (int j = 0; j < circuitDepth; j ++)
{
	std::cout << "Multiplication " << j << std::endl;

	//Perform matrix-vector multiplication
	scheme.mult(c_multiplicationResult,c_matrix[0],c_fieldVec);

	//new cM: logq = logq², logp = logp - logq -> rescale, so that it can be added with c_addIdentity
	scheme.reScaleByAndEqual(c_multiplicationResult, logp);

	//No multiplication was performed on c_addIdentity, therefore logq still has the initial value. In order to add it with cM the
            //logp must match as well.
	scheme.modDownByAndEqual(c_addIdentity, logp);

	//At the end of the process c_fieldVec will get the value from c_result, which has the same logp and logq as cM now.
	//For the next multiplication process they have to match.
	scheme.modDownByAndEqual(c_matrix[0],logp);
	scheme.add(c_result, c_multiplicationResult, c_addIdentity);

	scheme.leftRotateFastAndEqual(c_fieldVec, r);

	for(int i = 1; i< n; i++)
	{
		scheme.mult(c_multiplicationResult,c_matrix[i],c_fieldVec);
		scheme.reScaleByAndEqual(c_multiplicationResult, logp);
		scheme.modDownByAndEqual(c_matrix[i],logp);
		scheme.addAndEqual(c_result, c_multiplicationResult);

		scheme.leftRotateFastAndEqual(c_fieldVec, r);

	}
   }

`
The first multiplication gives me the expected result. But the second result consists of values with 10^187, which I have seen occasionally when the parameter were not correct.
Even though the logp and logq of c_matrix[0] and c_fieldVec matches and I have plenty of bits left (740), the second multiplication does not bring the expected result.
Does someone have an idea what I am doing wrong? Any hint would be much appreciated.

@swanhong
Copy link
Member

Hi, I've tested your code, but no such error has occurred. In my experiment, I used random inputs for c_matrix, c_fieldVec, etc, so it would be better if you share your exact code or your input data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants