Converting some Java code to Matlab
4 views (last 30 days)
Show older comments
Hi,
I need some help in converting some java code to Matlab. I have absolutely no knowledge of java and hope anyone here with more knowledge in java than me can better interpret some of the functions used.
I am basically trying to perform euclidean distance calculation in the encrypted domain (Paillier encryption). Using the homomorphic properties of Paillier, the squared euclidean distance formula can be written as:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190946/image.jpeg)
I found someone facing the same problem trying to implement this, and he provided his code solution in java here.
If anyone with knowledge of java could please convert this to matlab, I would really appreciate it.
I have already implemented the "PallierEncrypt" function in matlab, it is the distance calculation below that is proving to be difficult.
Many thanks
public BigInteger secureEuDistanceMD(int[] p, int[] q){
// Server (Owner of q) - PART 1
BigInteger[] enc_Q = new BigInteger[q.length];
BigInteger sumsqrQ = BigInteger.ZERO;
BigInteger enc_sumsqrQ;
for (int i = 0; i < q.length; i++) {
BigInteger bigQ = new BigInteger("" + q[i]);
enc_Q[i] = paillier.Encryption(bigQ);
sumsqrQ = sumsqrQ.add(bigQ.multiply(bigQ));
}
enc_sumsqrQ = paillier.Encryption(sumsqrQ);
// Client (Owner of p) - PART 2
BigInteger sumsqrP = BigInteger.ZERO;
BigInteger enc_sumsqrP;
BigInteger enc_sumPQ = BigInteger.ONE;
for (int i = 0; i < p.length; i++) {
BigInteger bigP = new BigInteger("" + p[i]);
sumsqrP = sumsqrP.add(bigP.multiply(bigP));
enc_sumPQ = enc_sumPQ.multiply(enc_Q[i].modPow(new BigInteger("-2").multiply(bigP), paillier.nsquare));
}
enc_sumsqrP = paillier.Encryption(sumsqrP);
BigInteger enc_euDist = enc_sumsqrP.multiply(enc_sumsqrQ).multiply(enc_sumPQ);
// Server - PART 3 (Decryption)
return paillier.Decryption(enc_euDist);
}
2 Comments
Walter Roberson
on 31 May 2018
To be able to do BigInteger, you will need the Symbolic Toolbox, or you will need https://www.mathworks.com/matlabcentral/fileexchange/47497-big-integer-arithmetic or https://www.mathworks.com/matlabcentral/fileexchange/6446-multiple-precision-toolbox-for-matlab or https://www.mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic
Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!