默认已经成功安装TFHE库,如果没安装可以看前面的文章
本节使用TFHE中的示例代码
代码位置tfhe/src/test/test-lwe.cpp
,代码引入库时做了修改:
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <sys/time.h>
#include "tfhe/tfhe.h"
#include "tfhe/polynomials.h"
#include "tfhe/lwesamples.h"
#include "tfhe/lweparams.h"
using namespace std;
// **********************************************************************************
// ********************************* MAIN *******************************************
// **********************************************************************************
double approxEquals(Torus32 a, Torus32 b) { return abs(a - b) < 10; }
int32_t main(int32_t argc, char **argv) {
LweParams *params = new_LweParams(512, 0.2, 0.5); //Both Alphas are a bit random
int32_t n = params->n;
LweKey *key = new_LweKey(params);
LweSample *cipher = new_LweSample(params);
Torus32 mu = dtot32(0.5);
//Note that 1<<30 corresponds to message 0.25!! Ira: You're right!
double alpha = 0.0625;
Torus32 phi;
double message;
int32_t Msize = 2;
lweKeyGen(key);
lweSymEncrypt(cipher, mu, alpha, key);
cout << "a = [";
for (int32_t i = 0; i < n - 1; ++i) cout << t32tod(cipher->a[i]) << ", ";
cout << t32tod(cipher->a[n - 1]) << "]" << endl;
cout << "b = " << t32tod(cipher->b) << endl;
phi = lwePhase(cipher, key);
cout << "phi = " << t32tod(phi) << endl;
message = lweSymDecrypt(cipher, key, Msize);
cout << "message = " << t32tod(message) << endl;
//lwe crash test
int32_t failures = 0;
int32_t trials = 1000;
for (int32_t i = 0; i < trials; i++) {
Torus32 input = dtot32((i % 3) / 3.);
lweKeyGen(key);
lweSymEncrypt(cipher, input, 0.047,
key); // Ira: Pay attention to the noise level!!! Look (I don't think 0.06 is correct, 0.047 works very well)
phi = lwePhase(cipher, key);
Torus32 decrypted = lweSymDecrypt(cipher, key, 3);
if (!approxEquals(input, decrypted)) {
cerr << "WARNING: the msg " << t32tod(input) << " gave phase " << t32tod(phi)
<< " and was incorrectly decrypted to " << t32tod(decrypted) << endl;
failures++;
}
}
cout << "There were " << failures << " failures out of " << trials << " trials" << endl;
cout << "(it might be normal)" << endl;
delete_LweParams(params); //Both Alphas are a bit random
delete_LweKey(key);
delete_LweSample(cipher);
return 0;
}
运行命令:
g++ -o lwe_test test-lwe.cpp -l tfhe-spqlios-fma
./lwe_test
输出如下
标签:TFHE,示例,代码,tfhe,params,test,new,include,lwe From: https://blog.csdn.net/qq_41359358/article/details/140567503a = [0.360936, 0.0268472, 0.259587, 0.48859, 0.164528, 0.371637, -0.41431, 0.469863, -0.438076, 0.0800416, 0.0525924, 0.163614, 0.467621, 0.181042, 0.227536, 0.12022, -0.289211, 0.144883, -0.391807, -0.144218, -0.308589, -0.270119, -0.102324, 0.00161439, -0.167656, 0.0934124, -0.196999, -0.326254, -0.245171, -0.47929, -0.116467, -0.240348, -0.455504, 0.130889, -0.383696, 0.234749, 0.0171215, 0.0158194, -0.231736, 0.201791, -0.425805, 0.465957, 0.286403, -0.0273749, -0.328193, -0.451731, 0.0112025, 0.428438, 0.337262, -0.404008, -0.429645, 0.293008, -0.48683, 0.462752, 0.415628, 0.0807097, -0.234286, -0.225632, 0.333983, 0.285791, 0.220657, -0.248863, -0.238968, -0.48247, -0.44641, 0.275915, 0.305555, 0.243341, 0.348498, 0.243425, 0.355225, 0.138323, 0.140052, 0.298738, 0.115135, -0.0502394, -0.174243, -0.032032, -0.070617, -0.0237845, 0.0614749, 0.351236, 0.0538694, 0.172288, -0.121189, 0.165379, -0.213409, 0.101241, -0.0128261, 0.402456, -0.42021, -0.425374, 0.190304, 0.325647, -0.469929, 0.0157019, 0.405948, 0.469936, 0.289931, 0.0947743, 0.0449265, -0.291602, -0.162654, 0.406523, 0.448262, -0.311258, 0.343789, -0.461589, -0.26118, 0.48794, 0.340599, 0.0367555, 0.241936, 0.249829, -0.426902, -0.459839, -0.0184199, -0.401846, 0.28784, 0.292837, 0.184101, 0.300066, 0.0743937, -0.260111, 0.323248, 0.399517, 0.178836, 0.167153, -0.166572, -0.198875, -0.0395852, 0.258266, 0.132067, -0.0831759, -0.18269, -0.270977, 0.0779365, -0.10256, 0.144327, 0.160306, 0.359578, 0.18668, -0.463075, -0.216909, -0.321237, 0.231529, 0.472226, 0.0252408, 0.297964, -0.44771, 0.416689, 0.329297, -0.140103, 0.418541, 0.267438, 0.161714, 0.418188, 0.267978, 0.121614, -0.376513, -0.0820015, 0.311621, 0.435642, -0.161967, 0.227274, 0.0161347, 0.427541, 0.330713, 0.0299799, 0.08821, -0.0513776, -0.154534, 0.28467, 0.136221, -0.0192819, -0.108763, -0.0199726, 0.2867, -0.435479, 0.252529, 0.0591986, -0.306646, 0.415548, -0.438278, -0.0756523, -0.447219, -0.319516, 0.290485, 0.0785771, -0.415445, -0.164324, 0.443255, 0.170924, 0.34982, -0.41502, 0.0365919, -0.4177, 0.386601, -0.283036, -0.0353393, -0.366857, 0.39639, -0.393042, -0.309686, -0.0357362, 0.178167, -0.10443, 0.191206, -0.334354, -0.0428159, 0.471035, 0.17603, -0.214054, 0.408658, 0.217688, 0.174578, -0.0404407, 0.37229, -0.111791, -0.193971, 0.0629611, 0.0666467, -0.436985, -0.124738, -0.270543, -0.158027, -0.0728818, -0.439912, -0.469869, 0.299597, 0.0268634, 0.0159192, -0.0127665, -0.386119, -0.276093, 0.466503, -0.243251, 0.429755, -0.187997, 0.322195, -0.487, 0.280162, 0.0603454, 0.0688578, 0.2713, 0.4433, -0.318703, -0.350394, 0.346963, -0.299223, 0.258798, -0.0503663, 0.344567, -0.295361, -0.403677, 0.316469, 0.173216, 0.246104, 0.159275, 0.193456, 0.382766, -0.0190397, 0.271412, -0.399192, 0.25688, 0.0733357, -0.238108, -0.214187, 0.339355, -0.0776796, -0.127963, 0.201392, 0.277726, 0.307251, -0.480168, 0.171745, -0.414303, 0.211923, 0.365186, 0.182822, 0.0433029, 0.209083, -0.337369, 0.0550661, 0.0296735, 0.458989, 0.429256, -0.0768373, -0.167827, 0.486597, 0.0796733, -0.358334, 0.141116, 0.215412, 0.379598, -0.415759, -0.498091, -0.410924, -0.165863, -0.266516, -0.453178, -0.173977, 0.101957, -0.491963, -0.445458, 0.280567, -0.48498, -0.256476, 0.274453, 0.376668, -0.413771, 0.190213, -0.349305, 0.445226, 0.354239, 0.26315, 0.0457668, 0.391065, -0.346071, -0.121687, 0.322673, -0.422884, -0.2724, 0.081214, -0.416357, 0.492716, -0.333215, -0.322837, -0.338925, 0.320688, 0.0188414, 0.039107, 0.245561, -0.373641, 0.329307, -0.499168, -0.0222422, 0.0827032, -0.0195221, -0.372719, 0.121505, -0.225386, -0.000854963, -0.31248, -0.127239, 0.320833, 0.0512341, -0.0683196, 0.465488, 0.4877, 0.415335, -0.405189, 0.187718, -0.201526, -0.111508, -0.0582177, -0.00266943, -0.250113, 0.455426, -0.0487532, -0.225817, 0.131576, 0.167803, 0.226442, 0.333552, -0.0903975, 0.270898, 0.203375, -0.14886, -0.169411, 0.138197, 0.150731, -0.0128799, -0.295021, -0.101981, -0.198997, 0.0365192, 0.19169, 0.195466, 0.279554, 0.00557157, -0.306309, 0.134293, -0.41919, 0.404155, -0.404165, 0.324077, 0.0364274, 0.189478, -0.253373, -0.391877, 0.176563, 0.219575, -0.211681, 0.0716221, 0.249918, -0.413536, 0.187311, -0.417658, -0.225093, 0.175934, -0.0421563, 0.403177, 0.0217036, 0.0650495, 0.48461, 0.415342, -0.346969, 0.166434, 0.0411973, 0.102077, -0.418032, -0.401268, -0.376487, 0.433996, -0.201649, 0.210026, 0.147397, 0.23716, -0.267984, -0.170968, -0.218847, -0.470401, -0.130919, -0.263405, 0.236961, -0.246805, -0.301756, 0.318161, 0.371001, -0.106146, 0.420558, 0.472405, 0.472981, -0.0856476, -0.3857, -0.0692853, -0.0226744, 0.375052, -0.298146, -0.318971, -0.398037, 0.41179, 0.20755, 0.30168, 0.0650888, -0.381584, 0.340372, -0.427004, -0.133701, -0.392276, -0.08042, 0.135351, -0.188593, 0.228866, -0.137383, -0.293582, 0.0511287, 0.217886, -0.378761, -0.184079, 0.27232, -0.264835, 0.426899, 0.0909905, 0.265465, 0.362646, -0.381127, 0.433944, 0.0193299, -0.0466466, -0.204143, 0.105671, -0.208941, 0.0775293, -0.346237, 0.184304, -0.482515, -0.114703, 0.151436, 0.483627, 0.348913, 0.0772153, 0.28273, -0.493769, 0.402747, 0.287826, -0.244587, 0.237388, 0.292591, -0.137189, 0.172594, 0.0164785, 0.204571, 0.0812665, -0.0264847, -0.333681, 0.417517, 0.220695, 0.0295229, 0.322143, 0.104443, 0.18351, 0.0618616, 0.406617, -0.229722, -0.133556, 0.274841, 0.35341, 0.360474, 0.0921785, 0.15207]
b = 0.185321
phi = 0.490627
message = -0.5
There were 0 failures out of 1000 trials
(it might be normal)