bling_yu

Recover the truncated ZUC's driving sequence

Mar 20th, 2022 (edited)
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.18 KB | None | 0 0
  1. // This code is used to recover the truncated ZUC's driving sequence.
  2. // Suppose that the modulus, the coefficients and the initial states are unknown, given the first N truncated digits which consist of the   // alpha high-order bits of the sequence, we need to recover the modulus, the coefficients and the initial states.
  3. // The library used is the NTL 11.4.3 library.
  4. // The whole experimental process consists of 4 steps. Searching linear relations, Recovering the modulus, the coefficients and the initial // states.
  5.  
  6. #include <NTL/vec_ZZ.h>
  7. #include <NTL/mat_ZZ.h>
  8. #include <NTL/mat_ZZ_p.h>
  9. #include <NTL/ZZ_pX.h>
  10. #include <NTL/ZZX.h>
  11. #include <NTL/LLL.h>
  12. #include <fstream>
  13. #include <ctime>
  14. #include <math.h>
  15. #include <fstream>
  16. NTL_CLIENT
  17. using namespace std;
  18.  
  19. // Search polynomials that annihilate the sequence a
  20. mat_ZZ Search_linear_relations(vec_ZZ y, int r, int t)
  21. {
  22.     clock_t start, end;
  23.     start = clock();
  24.     mat_ZZ L, C;
  25.     int i, j;
  26.    
  27.     L.SetDims(r, r + t);
  28.     C.SetDims(3, r);
  29.  
  30.     // Construct the lattice L
  31.     for (i = 0; i < r; i++)
  32.     {
  33.         for (j = 0; j < r + t; j++)
  34.         {
  35.             L[i][j] = 0;
  36.         }
  37.         L[i][t + i] = 1;
  38.         for (j = 0; j < t; j++)
  39.         {
  40.             L[i][j] = y[i + j];
  41.         }
  42.     }
  43.  
  44.     // Reduce the lattice L
  45.     G_BKZ_FP(L, 0.99, 20);
  46.  
  47.     // Choose 3 polynomials that annihilate the sequence a
  48.     for (i = 0; i < 3; i++)
  49.     {
  50.         for (j = 0; j < r; j++)
  51.         {
  52.             C[i][j] = L[i][j + t];
  53.         }
  54.     }
  55.  
  56.     end = clock();
  57.     double time = (double)(end - start) / CLK_TCK;
  58.     cout << "Step 1: time of searching polynomials that annihilate the sequence a: " << time << "s\n\n";
  59.  
  60.     return C;
  61. }
  62.  
  63. // Recover the modulus by the resultant
  64. ZZ Recover_the_modulus(mat_ZZ C, int r)
  65. {
  66.     clock_t start, end;
  67.     start = clock();
  68.     ZZ m, res1, res2, res3, m_power;
  69.     ZZX k1, k2, k3;
  70.     int i;
  71.  
  72.     k1.SetLength(r);
  73.     k2.SetLength(r);
  74.     k3.SetLength(r);
  75.  
  76.     // Transform the vectors into polynomials
  77.     for (i = 0; i < r; i++)
  78.     {
  79.         SetCoeff(k1, i, C[0][i]);
  80.         SetCoeff(k2, i, C[1][i]);
  81.         SetCoeff(k3, i, C[2][i]);
  82.     }
  83.  
  84.     // Compute the resultants of the polynomials
  85.     res1 = resultant(k1, k2);
  86.     res2 = resultant(k1, k3);
  87.     res3 = resultant(k2, k3);
  88.  
  89.     // Compute the greatest common factor of the resultants to obtain m to the power of n
  90.     m_power = GCD(GCD(res1, res2), res3);
  91.     m = SqrRoot(SqrRoot(SqrRoot(SqrRoot(m_power))));
  92.  
  93.     end = clock();
  94.     double time = (double)(end - start) / CLK_TCK;
  95.     cout << "Step 2: time of recovering the modulus: " << time << "s\n\n";
  96.  
  97.     return m;
  98. }
  99.  
  100. // Recover the coefficients by the Chinese Remainder Theorem
  101. ZZ_pX Recover_the_coefficients(mat_ZZ C, ZZ m, int r)
  102. {
  103.     clock_t start, end;
  104.     start = clock();
  105.     ZZ_pX f, g;
  106.     mat_ZZ_p C_m;
  107.     ZZ_p c;
  108.     int i, j, mark;
  109.     c.init(m);
  110.  
  111.     C_m.SetDims(2, r);
  112.  
  113.     ofstream fout("C.txt");
  114.     for (i = 0; i < 2; i++)
  115.     {
  116.         for (j = 0; j < r; j++)
  117.         {
  118.             fout << C[i][j] << " ";
  119.         }
  120.         fout << "\n";
  121.     }
  122.     fout.close();
  123.     freopen("C.txt", "r", stdin);
  124.    
  125.     // Make the polynomials monic
  126.     for (i = 0; i < 2; i++)
  127.     {
  128.         for (j = 0; j < r; j++)
  129.         {
  130.             cin >> c;
  131.             C_m[i][j] = c;
  132.         }
  133.         mark = r - 1;
  134.         while (C_m[i][mark] == 0)
  135.         {
  136.             mark = mark - 1;
  137.         }
  138.         for (j = 0; j < r; j++)
  139.         {
  140.             C_m[i][j] = C_m[i][j] * inv(C_m[i][mark]);
  141.         }
  142.     }
  143.  
  144.     f.SetLength(r);
  145.     g.SetLength(r);
  146.     for (i = 0; i < r; i++)
  147.     {
  148.         SetCoeff(f, i, C_m[0][i]);
  149.         SetCoeff(g, i, C_m[1][i]);
  150.     }
  151.  
  152.     end = clock();
  153.     double time = (double)(end - start) / CLK_TCK;
  154.     cout << "Step 3: time of recovering the coefficients: " << time << "s\n\n";
  155.  
  156.     return GCD(f, g);
  157. }
  158.  
  159. // Recover the initial state by Kannan's embedding technique
  160. vec_ZZ Recover_the_initial_state(vec_ZZ y, ZZ_pX f, ZZ m, int n, int beta)
  161. {
  162.     clock_t start, end;
  163.     start = clock();
  164.     vec_ZZ a, z;
  165.     mat_ZZ Q, Q_power, L;
  166.     ZZ b;
  167.     int i, j, l, d;
  168.     d = 30;
  169.  
  170.     a.SetLength(n);
  171.     z.SetLength(n);
  172.     Q.SetDims(n, n);
  173.     Q_power.SetDims(n, n);
  174.     L.SetDims(d + 1, d + 1);
  175.  
  176.     Q_power = ident_mat_ZZ(n);
  177.  
  178.     Q[0][n - 1] = (-rep(f[0])) % m;
  179.     for (i = 1; i < n; i++)
  180.     {
  181.         Q[i][i - 1] = 1;
  182.         Q[i][n - 1] = (-rep(f[i])) % m;
  183.     }
  184.  
  185.     // Construct the lattice L
  186.     for (i = 1; i < n; i++)
  187.     {
  188.         Q_power = Q_power * Q;
  189.         for (j = 0; j < n; j++)
  190.         {
  191.             for (l = 0; l < n; l++)
  192.             {
  193.                 Q_power[j][l] = Q_power[j][l] % m;
  194.             }
  195.         }
  196.     }
  197.  
  198.     for (i = n; i < d; i++)
  199.     {
  200.         Q_power = Q_power * Q;
  201.         for (j = 0; j < n; j++)
  202.         {
  203.             for (l = 0; l < n; l++)
  204.             {
  205.                 Q_power[j][l] = Q_power[j][l] % m;
  206.             }
  207.         }
  208.         b = 0;
  209.         for (j = 0; j < n; j++)
  210.         {
  211.             L[j + 1][i + 1] = Q_power[j][0];
  212.             b += Q_power[j][0] * y[j];
  213.         }
  214.         L[0][i + 1] = (pow(2, beta) * (y[i] - b)) % m + pow(2, beta - 1);
  215.     }
  216.  
  217.     L[0][0] = pow(2, beta - 1);
  218.     for (i = 1; i <= n; i++)
  219.     {
  220.         L[0][i] = pow(2, beta - 1);
  221.         L[i][i] = 1;
  222.     }
  223.     for (i = n + 1; i <= d; i++)
  224.     {
  225.         L[i][i] = m;
  226.     }
  227.  
  228.     // Reduce the lattice L
  229.     G_BKZ_FP(L, 0.99, 20);
  230.  
  231.     // Recover the initial state
  232.     if (L[0][0] == -pow(2, beta - 1))
  233.     {
  234.         for (i = 0; i < n; i++)
  235.         {
  236.             z[i] = L[0][i + 1] + pow(2, beta - 1);
  237.             a[i] = y[i] * pow(2, beta) + z[i];
  238.         }
  239.     }
  240.     if (L[0][0] == pow(2, beta - 1))
  241.     {
  242.         for (i = 0; i < n; i++)
  243.         {
  244.             z[i] = pow(2, beta - 1) - L[0][i + 1];
  245.             a[i] = y[i] * pow(2, beta) + z[i];
  246.         }
  247.     }
  248.  
  249.     end = clock();
  250.     double time = (double)(end - start) / CLK_TCK;
  251.     cout << "Step 4: time of recovering the initial state: " << time << "s\n\n";
  252.  
  253.     return a;
  254. }
  255.  
  256. void main()
  257. {
  258.     vec_ZZ y, a;
  259.     mat_ZZ C;
  260.     ZZ m;
  261.     ZZ_pX f;
  262.     int n, r, t, N, alpha, beta;
  263.     int i, j;
  264.  
  265.     n = 16;
  266.     r = 175;
  267.     t = 65;
  268.     N = r + t - 1;
  269.     alpha = 17;
  270.     beta = 14;
  271.  
  272.     y.SetLength(N);
  273.     a.SetLength(n);
  274.  
  275.     long Y[239] = { 312,6195,20905,36760,33275,128773,5120,82629,3434,13450,115969,78595,46080,21032,126472,116368,39147,118169,53608,16848,30654,130814,92427,66994,64940,95674,83420,12319,51686,121628,103101,4578,55561,114165,99105,66029,11183,33637,126822,45172,71211,75549,31249,100570,57872,40625,53029,98594,35382,114241,86331,100286,74451,71452,113487,22824,119007,42576,9420,18312,62834,3099,103626,124788,28318,119993,120470,1417,66987,121877,100551,36538,107257,95791,90747,11856,78683,27967,33439,112227,42492,76803,73986,127640,127988,113849,62073,121003,67257,62990,28618,14066,37196,88819,91188,119086,47509,14040,46365,117363,96302,61501,69537,19261,5302,29632,112053,60563,73212,123325,7485,52451,92665,108139,31895,6630,28136,97771,1874,10433,123318,98757,45897,34866,118234,75543,95739,41629,82990,25310,28330,21183,102442,39164,13744,125171,78275,42927,27280,113230,115192,108433,91178,101247,100622,63782,105110,40546,20787,97620,33627,97857,54439,74890,65682,117061,34213,28237,72077,40838,44105,41674,48232,10965,79577,106188,27672,79314,114402,64028,44159,130067,47639,20721,16188,1301,53216,29228,52467,45721,17559,28506,122718,84634,109576,34973,129964,10245,48392,100922,5358,128436,37415,74936,89446,101529,75414,39696,7480,20690,113561,65658,28894,48013,100734,80683,119101,50176,38679,50948,30958,67432,120501,62535,103920,69944,73629,7114,19565,10250,685,37711,17024,38274,53345,89835,93532,61993,35816,55362,11054,112389,34513,44560,123360,26732,69001,67919,67745 };
  276.     for (i = 0; i < N; i++)
  277.         y[i] = ZZ(Y[i]);
  278.    
  279.     C = Search_linear_relations(y, r, t);
  280.     m = Recover_the_modulus(C, r);
  281.     cout << "m = " << m << "\n\n\n";
  282.     f = Recover_the_coefficients(C, m, r);
  283.     cout << "f = " << f << "\n\n\n";
  284.     a = Recover_the_initial_state(y, f, m, n, beta);
  285.     cout << "a = " << a << "\n\n\n";
  286. }
Add Comment
Please, Sign In to add comment