math.big: stronger asserts to reduce risks of aliasing

This commit is contained in:
unplanned 2025-12-04 01:05:17 +01:00
parent c785964cd0
commit f13ee9a68c

View file

@ -3632,6 +3632,7 @@ fn llmulaccKaratsuba(
/// r = r (op) a. /// r = r (op) a.
/// The result is computed modulo `r.len`. /// The result is computed modulo `r.len`.
fn llaccum(comptime op: AccOp, r: []Limb, a: []const Limb) void { fn llaccum(comptime op: AccOp, r: []Limb, a: []const Limb) void {
assert(!slicesOverlap(r, a) or @intFromPtr(r.ptr) <= @intFromPtr(a.ptr));
if (op == .sub) { if (op == .sub) {
_ = llsubcarry(r, r, a); _ = llsubcarry(r, r, a);
return; return;
@ -3701,6 +3702,8 @@ fn llmulaccLong(comptime op: AccOp, r: []Limb, a: []const Limb, b: []const Limb)
/// The result is computed modulo `r.len`. /// The result is computed modulo `r.len`.
/// Returns whether the operation overflowed. /// Returns whether the operation overflowed.
fn llmulLimb(comptime op: AccOp, acc: []Limb, y: []const Limb, xi: Limb) bool { fn llmulLimb(comptime op: AccOp, acc: []Limb, y: []const Limb, xi: Limb) bool {
assert(!slicesOverlap(acc, y) or @intFromPtr(acc.ptr) <= @intFromPtr(y.ptr));
if (xi == 0) { if (xi == 0) {
return false; return false;
} }
@ -3763,6 +3766,8 @@ fn llsubcarry(r: []Limb, a: []const Limb, b: []const Limb) Limb {
assert(a.len != 0 and b.len != 0); assert(a.len != 0 and b.len != 0);
assert(a.len >= b.len); assert(a.len >= b.len);
assert(r.len >= a.len); assert(r.len >= a.len);
assert(!slicesOverlap(r, a) or @intFromPtr(r.ptr) <= @intFromPtr(a.ptr));
assert(!slicesOverlap(r, b) or @intFromPtr(r.ptr) <= @intFromPtr(b.ptr));
var i: usize = 0; var i: usize = 0;
var borrow: Limb = 0; var borrow: Limb = 0;
@ -3794,6 +3799,8 @@ fn lladdcarry(r: []Limb, a: []const Limb, b: []const Limb) Limb {
assert(a.len != 0 and b.len != 0); assert(a.len != 0 and b.len != 0);
assert(a.len >= b.len); assert(a.len >= b.len);
assert(r.len >= a.len); assert(r.len >= a.len);
assert(!slicesOverlap(r, a) or @intFromPtr(r.ptr) <= @intFromPtr(a.ptr));
assert(!slicesOverlap(r, b) or @intFromPtr(r.ptr) <= @intFromPtr(b.ptr));
var i: usize = 0; var i: usize = 0;
var carry: Limb = 0; var carry: Limb = 0;