From d61234cabc9ee6c11c424b9028589eec1e6902d1 Mon Sep 17 00:00:00 2001 From: Wizou Date: Fri, 20 Aug 2021 02:33:43 +0200 Subject: [PATCH] Make MillerRabinIterations configurable for slow devices --- src/Helpers.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Helpers.cs b/src/Helpers.cs index e98b2ad..31b5f53 100644 --- a/src/Helpers.cs +++ b/src/Helpers.cs @@ -146,8 +146,10 @@ namespace WTelegram } } + public static int MillerRabinIterations { get; set; } = 64; // 64 is OpenSSL default for 2048-bits numbers + // Miller–Rabin primality test - public static bool IsProbablePrime(this BigInteger n, int k = 64) + public static bool IsProbablePrime(this BigInteger n) { var n_minus_one = n - BigInteger.One; if (n_minus_one.Sign <= 0) return false; @@ -160,7 +162,9 @@ namespace WTelegram var randomBytes = new byte[bitLen / 8 + 1]; var lastByteMask = (byte)((1 << (int)(bitLen % 8)) - 1); BigInteger a; - for (int i = 0; i < k; i++) + if (MillerRabinIterations < 15) // 15 is the minimum recommended by Telegram + Log(3, $"MillerRabinIterations ({MillerRabinIterations}) is below the minimal level of safety (15)"); + for (int i = 0; i < MillerRabinIterations; i++) { do {