From 34f0760ab8d507f5c845b8fbf85d91a098859e37 Mon Sep 17 00:00:00 2001 From: Aaron Chen Date: Sat, 11 May 2024 01:27:29 +0800 Subject: [PATCH] uint256: optimize AddMod (#165) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` AddMod/small/uint256-8 21.0ns ±44% 17.0ns ±43% ~ (p=0.052 n=10+10) AddMod/mod64/uint256-8 28.6ns ±36% 26.6ns ±44% ~ (p=0.481 n=10+10) AddMod/mod128/uint256-8 56.1ns ±43% 53.7ns ±49% ~ (p=0.810 n=10+10) AddMod/mod192/uint256-8 62.0ns ±44% 62.2ns ±44% ~ (p=0.971 n=10+10) AddMod/mod256/uint256-8 25.4ns ±25% 12.0ns ±44% -52.74% (p=0.000 n=10+9) ``` --- conversion_test.go | 2 +- uint256.go | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/conversion_test.go b/conversion_test.go index 0f8864e2..3a1cc718 100644 --- a/conversion_test.go +++ b/conversion_test.go @@ -401,7 +401,7 @@ func BenchmarkSetBytes(b *testing.B) { val.SetBytes(bytearr[:27]) val.SetBytes(bytearr[:28]) val.SetBytes(bytearr[:29]) - val.SetBytes(bytearr[:20]) + val.SetBytes(bytearr[:30]) val.SetBytes(bytearr[:31]) val.SetBytes(bytearr[:32]) } diff --git a/uint256.go b/uint256.go index 263b11e2..377e388c 100644 --- a/uint256.go +++ b/uint256.go @@ -244,12 +244,10 @@ func (z *Int) AddMod(x, y, m *Int) *Int { // final sub was unnecessary if c1 == 0 && c2 != 0 { - copy((*z)[:], res[:]) - return z + return z.Set(&res) } - copy((*z)[:], tmp[:]) - return z + return z.Set(&tmp) } if m.IsZero() {