function ElG_Dec($c, $pk, $sk)
{
    $group = $pk["group"];
    list($c1, $c2) = $c;
    if (_DEBUG >= 8) {
        echo "Decryption of ({$c1}, {$c2})...\n";
    }
    //Operation is $c2/($c1^$x) <=> $c2*($c1^{x.(q-2)}) because varpĥi(vraphi(p)) = varphi(2q) = q-1
    //Could have been done in precomputation
    $inv_c1 = modular_inverse($c1, $group["modulo"]);
    if (_DEBUG >= 8) {
        echo "\t c1 = {$c1}, inv_c1 = {$inv_c1}\n";
    }
    if (_DEBUG >= 8) {
        echo "\t c1*inv_c1 = ", modular_mult($c1, $inv_c1, $group["modulo"]), "\n";
    }
    //Actual decryption
    $m = modular_mult($c2, modular_exp($inv_c1, $sk, $group["modulo"]), $group["modulo"]);
    if (_DEBUG >= 6) {
        echo "Decryption of ({$c1}, {$c2}) is {$m}\n";
    }
    return $m;
}
Exemplo n.º 2
0
     do {
         $r_tmp = $group["G"][array_rand($group["G"])];
     } while (gcd($r_tmp, $group["modulo"] - 1) != 1);
     $trap = $r_tmp * $dst_D;
     //Done by V (key generation is considered as done offline, prior to network setup)
     $keys_V = ElG_KeyGen_alt($r, $q);
     if ($keys_V == false) {
         echo "Error: Key Generation failed for V with lambda = {$lambda} and r = {$r}...\n";
         exit;
     }
     list($pk_V, $sk_V) = array_values($keys_V);
     $time_aux = microtime(true);
     $gpowdstDrtmpsrcV = modular_exp($group["gen"], modular_mult($trap, $src_V, $group["modulo"] - 1), $group["modulo"]);
     $cgpowdstDrtmpsrcV = ElG_Enc($gpowdstDrtmpsrcV, $pk_V);
     //Done by S
     $inv_rtmp_pmin1 = modular_inverse($r_tmp, $group["modulo"] - 1);
     $cgpowdstDsrcV = ElG_ScalarExp($cgpowdstDrtmpsrcV, $inv_rtmp_pmin1, $pk_V);
     $cLocalID_DV = ElG_PlainMult($cgpowdstDsrcV, $ID_D, $pk_V);
     $cLocalID_DV = ElG_Rerand($cLocalID_DV, $pk_V);
     //Done by V
     $LocalID_DV = ElG_Dec($cLocalID_DV, $pk_V, $sk_V);
     $total_time_init_route += microtime(true) - $time_aux;
     //Because V = P, we should have that the LocalID_DV found be equal to LocalID_DP_theoretic from above
     if ($LocalID_DV != $LocalID_DP_theoretic) {
         echo "Error in route initialization: for q = {$q}, p = ", $group["modulo"], ", ID_D = {$ID_D}, s_D = {$dst_D}, k_V = {$src_V}, r_tmp = {$r_tmp}, we have {$LocalID_DV} != {$LocalID_DP_theoretic}\n";
         exit;
     } elseif ($echo_when_OK) {
         echo "Route initialization test #{$i} OK: for q = {$q}, p = ", $group["modulo"], ", ID_D = {$ID_D}, s_D = {$dst_D}, k_V = {$src_V}, r_tmp = {$r_tmp},we have {$LocalID_DV} = {$LocalID_DP_theoretic}\n";
     }
 }
 if ($echo_when_OK) {