From 434bd2dcc6bc3d1ce5489dd5643ed70d47430efb Mon Sep 17 00:00:00 2001 From: Shramee Srivastav Date: Sun, 12 Apr 2026 19:06:19 +0530 Subject: [PATCH] feat(emulated): checkZero mulcheck in hint closes consensys/gnark#1748 --- std/math/emulated/field_mul.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/std/math/emulated/field_mul.go b/std/math/emulated/field_mul.go index a434090194..5557f3cb70 100644 --- a/std/math/emulated/field_mul.go +++ b/std/math/emulated/field_mul.go @@ -534,8 +534,12 @@ func (f *Field[T]) callMulHint(a, b *Element[T], isMulMod bool, customMod *Eleme if customMod != nil { modulusLimbs = customMod.Limbs } - hintInputs := make([]frontend.Variable, 0, 4+len(modulusLimbs)+len(a.Limbs)+len(b.Limbs)) - hintInputs = append(hintInputs, nbBits, nbLimbs, len(a.Limbs), nbQuoLimbs) + isCheckZero := 0 + if !isMulMod { + isCheckZero = 1 + } + hintInputs := make([]frontend.Variable, 0, 5+len(modulusLimbs)+len(a.Limbs)+len(b.Limbs)) + hintInputs = append(hintInputs, nbBits, nbLimbs, len(a.Limbs), nbQuoLimbs, isCheckZero) hintInputs = append(hintInputs, modulusLimbs...) hintInputs = append(hintInputs, a.Limbs...) hintInputs = append(hintInputs, b.Limbs...) @@ -566,8 +570,9 @@ func mulHint(field *big.Int, inputs, outputs []*big.Int) error { nbLimbs := int(inputs[1].Int64()) nbALen := int(inputs[2].Int64()) nbQuoLen := int(inputs[3].Int64()) - nbBLen := len(inputs) - 4 - nbLimbs - nbALen - ptr := 4 + isCheckZero := inputs[4].Int64() == 1 + nbBLen := len(inputs) - 5 - nbLimbs - nbALen + ptr := 5 plimbs := inputs[ptr : ptr+nbLimbs] ptr += nbLimbs alimbs := inputs[ptr : ptr+nbALen] @@ -611,6 +616,10 @@ func mulHint(field *big.Int, inputs, outputs []*big.Int) error { if p.Sign() != 0 { quo.QuoRem(ab, p, rem) } + if isCheckZero && rem.Sign() != 0 { + // we expect the remainder to be zero + return fmt.Errorf("unexpected non-zero remainder for checkZero") + } if err := limbs.Decompose(quo, uint(nbBits), quoLimbs); err != nil { return fmt.Errorf("decompose quo: %w", err) }