It seems that BBSCheckInitialSeed() has an issue where it leaks GMP memory areas when checking the initial seed multiple times.
Currently, inside the attempt loop, it executes: mpz_init_set(z, rngctx->zSeed); ... mpz_init_set(zCycle, z); Since mpz_init_set() also initializes the mpz_t variable, on the second and subsequent attempts, mpz_init_set() is called again on z and zCycle, which are already initialized. At that time, it re-initializes them without calling mpz_clear() on the previously allocated memory areas, resulting in a memory leak. Since z and zCycle only need to be initialized once within the function, I changed it so that they are initialized before the loop as: mpz_init(z); mpz_init(zCycle); and inside the loop, we use: mpz_set(z, rngctx->zSeed); ... mpz_set(zCycle, z); This way, only the values are reset in each attempt, and the initialization corresponds to the existing final mpz_clear() just once. The actual logic of the BBS seed check itself remains unchanged. Best regards, -- TAKAHASHI Kaoru
>From bbb366d1fa9afd98389cea318fb2033fbfbf2620 Mon Sep 17 00:00:00 2001 From: TAKAHASHI Kaoru <[email protected]> Date: Sun, 23 Aug 2026 21:45:00 +0900 Subject: [PATCH] Fix memory leak in BBSCheckInitialSeed() --- dice.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dice.c b/dice.c index 872e90ea..1f7f0614 100644 --- a/dice.c +++ b/dice.c @@ -308,13 +308,16 @@ BBSCheckInitialSeed(rngcontext * rngctx) if (mpz_sgn(rngctx->zSeed) < 1) return BBSInitialSeedFailure(rngctx); + mpz_init(z); + mpz_init(zCycle); + for (iAttempt = 0; iAttempt < 32; iAttempt++) { - mpz_init_set(z, rngctx->zSeed); + mpz_set(z, rngctx->zSeed); for (i = 0; i < 8; i++) mpz_powm_ui(z, z, 2, rngctx->zModulus); - mpz_init_set(zCycle, z); + mpz_set(zCycle, z); for (i = 0; i < 16; i++) { mpz_powm_ui(z, z, 2, rngctx->zModulus); -- 2.47.3
