Commit 77a27c0c
77a27c0c0213Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
bigint: import mruby correctness fixes (Barrett gate, Montgomery pre-reduce, trim canonical zero)
Three correctness/UB fixes from mruby-bigint that the standalone library (adapted in d9637369) had not yet picked up. All three are pure algorithm / memory-safety fixes with no mrb_state / GC dependency, so they port verbatim.
-
mpz_mod: gate the Barrett path on its precondition x < 2^(2*bits(m)). The old gate (x->sz >= y->sz + 2) let x reach ~25 limbs against a 4-limb modulus, where mpz_barrett_reduce silently truncates high limbs and returns garbage. Add
x->sz <= 2 * y->szso out-of-range inputs fall through to the general division path. (mruby 4507b4a6) -
mpz_powm_montgomery: pre-reduce base mod n before forming base * R^2. REDC requires its input T < R*N; an unreduced base (base >= n) overflows that bound and REDC truncates, giving a wrong result. (mruby 206b9f74)
-
trim: maintain the canonical invariant sz == 0 implies sn == 0. An inconsistent zero (sn != 0, sz == 0) can flow into mpz_sqr, miss the zero_p guard, and reach mpn_zero(NULL, 0) which is UB (memset nonnull arg). (mruby 6ac2c3dc, close #6849)
The Barrett/powm paths are reached by Integer#remainder / Integer#pow(b,e,m), not yet wired into spinel codegen, so these are latent until that lands; trim's canonical-zero fix is exercised today via loop-multiplication squaring. No spinel->mruby bigint changes are needed (mruby already has equal-or-better).
Gates: 950 tests pass, 57 benchmarks pass (bm_bigint_fib, bm_pidigits), reachable bigint paths match CRuby.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 77a27c0c0213
- Parent
- 003c30228b13
lib/sp_bigint.cmodifiedView file
while (x->sz && x->p[x->sz-1] == 0) { x->sz--; } /* Maintain invariant: sz == 0 implies sn == 0 (zero is canonical). */ if (x->sz == 0) x->sn = 0;}/* z = x + y, without regard for sign */ return; } /* Barrett reduction for moderate-sized moduli (>= 4 limbs where setup is worthwhile) */ if (y->sz >= 4 && y->sz <= 16 && x->sz >= y->sz + 2) { /* Barrett reduction for moderate-sized moduli (>= 4 limbs where setup is worthwhile). * Barrett's precondition is x < 2^(2*bits(m)); inputs beyond ~2*m.sz limbs * violate it and the algorithm silently truncates high limbs. Fall through * to general division for those. */ if (y->sz >= 4 && y->sz <= 16 && x->sz >= y->sz + 2 && x->sz <= 2 * y->sz) { mpz_t mu; mpz_init_temp(ctx, &mu, y->sz + 1); mpz_barrett_mu(ctx, &mu, y); mpz_init(ctx, &one_mont); mpz_montgomery_reduce(ctx, &one_mont, &R2, n, rho); /* Convert base to Montgomery form: base_mont = base * R mod n = REDC(base * R^2) */ mpz_t base_mont, temp; /* Convert base to Montgomery form: base_mont = base * R mod n = REDC(base * R^2). * REDC requires its input T to satisfy T < R*N. If `base` is not already * reduced (e.g. base >= n), `base * R^2` can exceed R*N and REDC produces * a wrong result. Pre-reduce base modulo n via mpz_mmod (the general * division path) -- both operands are non-negative here so this is * semantically equivalent to mpz_mod. */ mpz_t base_mont, base_reduced, temp; mpz_init(ctx, &base_mont); mpz_init(ctx, &base_reduced); mpz_init_temp(ctx, &temp, n->sz * 4); mpz_mul(ctx, &temp, (mpz_t*)base, &R2); mpz_mmod(ctx, &base_reduced, (mpz_t*)base, (mpz_t*)n); mpz_mul(ctx, &temp, &base_reduced, &R2); mpz_montgomery_reduce(ctx, &base_mont, &temp, n, rho); /* Initialize accumulator to 1 in Montgomery form */ mpz_clear(ctx, &R2); mpz_clear(ctx, &one_mont); mpz_clear(ctx, &base_mont); mpz_clear(ctx, &base_reduced); mpz_clear(ctx, &temp); mpz_clear(ctx, &acc); pool_restore(ctx, pool_state);