Public-Key

使用 Paillier 同態加密計算兩個向量之間的距離

  • June 8, 2018

我基本上是在嘗試在加密域中執行歐幾里德距離計算(Paillier 加密)。利用 Paillier 的同態性質,平方歐幾里德距離公式可以寫為 ( paper ):

$ \newcommand{\opn}{\operatorname} $

$$ \underbrace{\opn{Enc}\left(\sum^d_{i=1}p_i^2\right)}{\text{(I)}}\cdot \underbrace{\opn{Enc}\left(\sum^d{i=1}q_i^2\right)}{\text{(II)}}\cdot \underbrace{\prod^d{i=1}\opn{Enc}(q_i)^{-2p_i}}_{\text{(III)}} $$ 我嘗試使用兩個樣本向量在 matlab 中實現這一點,ab解密時我似乎得到了完全錯誤的距離。沒有加密的兩個向量的歐幾里得距離是100.5883但是當應用上面的公式然後解密我得到的結果時10118

%I'm using the variable precision integer toolbox (vpi) to handle large numbers in Matlab
a = [10, 58, 23, 59, 78, 11];
b = [87, 15, 12, 32, 41, 22];

p = 708481;
q = 708497;
r = 461563;

n = p * q;

% calculating (I) from above equation, i.e. a^2
encA =vpi(zeros(size(A))); %initialize vector for encrypted values
sumA = 0;

for i=1:length(A)
   encA(i) = PaillierEncrypt(A(i), p, q, random_r); %this will encrypt the value at index i in the vector
   sumA = sumA + (A(i))^2;
end

enc_sumA = PaillierEncrypt(sumA, p, q, random_r); %now encrypt the summed value



% below is for (II) and (III) in the equation i.e. b^2 -2ab
encB =vpi(zeros(size(B)));
sumB = 0;
enc_prodAB = vpi(1);

for i=1:length(B)
   encB(i) = PaillierEncrypt(B(i), p, q, random_r);
   sumB = sumB + (B(i))^2;

%the below block is to assist in determining the mod of a negative exponent, basically it allows for this to run without error: mod((encA)^(-2*vpi(B)), n*n);
   a2 = encA(i); %the encrypted base
   d2 = (-2)*vpi(B(i)); %the exponent that is a non encrypted scalar
   n3 = n*n;

   negpowermod = @(a,d,n2) minv(powermod(a,abs(d),n2),n2); 

   result = negpowermod(a2,d2,n3);

   enc_prodAB = enc_prodAB * result;
end

enc_sumB = PaillierEncrypt(sumB, p, q, random_r);

dist = enc_sumA * enc_sumB * enc_prodAB;

我已經盡可能地評論了上面的程式碼來解釋我在做什麼。我確定我以某種方式搞砸了實施。

一些指導將不勝感激。謝謝

我沒有看到你的問題。10118 確實是歐幾里得距離的平方~100.5883

引用自:https://crypto.stackexchange.com/questions/59863