MRZ2342

Untitled

Apr 21st, 2023
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1.         public static void Run()
  2.         {
  3.             Console.Write("Ввести значения? (y/n): ");
  4.             if (Console.ReadLine() == "y")
  5.             {
  6.                 Console.Write("Введите x: ");
  7.                 var x = int.Parse(Console.ReadLine());
  8.                 Console.Write("Введите y: ");
  9.                 var y = int.Parse(Console.ReadLine());
  10.                 Console.Write("Введите a: ");
  11.                 var a = int.Parse(Console.ReadLine());
  12.                 Console.Write("Введите p: ");
  13.                 var p = int.Parse(Console.ReadLine());
  14.  
  15.                 Console.WriteLine();
  16.  
  17.                 var data = GetCryptoParams(x, y, a, p);
  18.                 if (data.has)
  19.                     Console.WriteLine($"\nУравнение эл. кривой: y^2 = x^3 + {data.a}x + {data.b}");
  20.  
  21.             }
  22.             else
  23.             {
  24.                 var data = GetCryptoParamsByRandom();
  25.                 Console.WriteLine($"\nУравнение эл. кривой: y^2 = x^3 + {data.a}x + {data.b}");
  26.             }
  27.  
  28.         }
  29.         static int GetRandomGaluaValue(int p)
  30.         {
  31.             return new Random().Next(4, (int)p - 1);
  32.         }
  33.  
  34.         public static (int a, int b, int p) GetCryptoParamsByRandom()
  35.         {
  36.             Console.WriteLine("\nВыбор случайных значений...\n");
  37.  
  38.             var s = new EratostheneSieve(100);
  39.  
  40.             var p = (int)s.GetRandomSimple();
  41.             if (p <= 5) p = (int)s.GetRandomSimple();
  42.  
  43.             while (true)
  44.             {
  45.                 var prms = GetCryptoParams(GetRandomGaluaValue(p), GetRandomGaluaValue(p), GetRandomGaluaValue(p), p);
  46.                 if (prms.has)
  47.                     return (prms.a, prms.b, prms.p);
  48.             }
  49.  
  50.         }
  51.  
  52.         public static (int a, int b, int p, bool has) GetCryptoParams(int x, int y, int a, int p)
  53.         {
  54.             Console.WriteLine($"p = {p}");
  55.             Console.WriteLine($"x = {x}");
  56.             Console.WriteLine($"y = {y}");
  57.             Console.WriteLine($"a = {a}");
  58.  
  59.             var b = NTMC.Functions.mod(y * y - (x * x * x + a * x), p);
  60.  
  61.             var check = NTMC.Functions.mod(4 * a * a * a + 27 * b * b, p);
  62.             if (check != 0)
  63.             {
  64.                 Console.WriteLine($"b = y^2 - (x^3 + ax) = {y * y} - ({x * x * x} + {a} * {x}) = {b}");
  65.                 Console.WriteLine($"\nПроверка выполнена т.к. 4a^3 + 27b^2 = 4*{a * a * a} + 27*{b * b} = {check} != 0");
  66.                 return (a, b, p, true);
  67.             }
  68.             else
  69.             {
  70.                 Console.WriteLine("\nПроверка не выполнена!\n");
  71.                 return (a, b, p, false);
  72.             }          
  73.         }
Advertisement
Add Comment
Please, Sign In to add comment