From 8007ecf25979dcf7dbeed5e00352c7469625ce8e Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 24 May 2022 00:18:11 +0200 Subject: [PATCH] BigInt Allocation opimization (#20) --- bandersnatch/fr/element.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/bandersnatch/fr/element.go b/bandersnatch/fr/element.go index 0061421..f58f604 100644 --- a/bandersnatch/fr/element.go +++ b/bandersnatch/fr/element.go @@ -708,11 +708,24 @@ func (z *Element) SetBytes(e []byte) *Element { vv.SetBytes(e) // set big int - z.SetBigInt(vv) - + { //code below is almost like z.SetBigInt(vv), but makes do with one less big.Int + var zero big.Int + // fast path + if c := vv.Cmp(&_modulus); c == 0 { + // v == 0 + z.SetZero() + } else if c != 1 && vv.Cmp(&zero) != -1 { + // 0 < v < q + z.setBigInt(vv) + } else { + // modular reduction + vv.Mod(vv, &_modulus) + // set big int byte value + z.setBigInt(vv) + } + } // put temporary object back in pool bigIntPool.Put(vv) - return z } @@ -722,17 +735,7 @@ func (z *Element) SetBytesLE(e []byte) *Element { for i, j := 0, len(e)-1; i < j; i, j = i+1, j-1 { e[i], e[j] = e[j], e[i] } - // get a big int from our pool - vv := bigIntPool.Get().(*big.Int) - vv.SetBytes(e) - - // set big int - z.SetBigInt(vv) - - // put temporary object back in pool - bigIntPool.Put(vv) - - return z + return z.SetBytes(e) } // SetBigInt sets z to v (regular form) and returns z in Montgomery form