From ba9ceb4d353fe99d91f07f9f03079ba318a452b1 Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Tue, 23 Jan 2024 23:38:12 -0500 Subject: [PATCH 1/8] implement --- go.mod | 1 + go.sum | 2 ++ precompiles/ArbMathStats.go | 28 +++++++++++++++++++++++++ precompiles/ArbMathStats_test.go | 35 ++++++++++++++++++++++++++++++++ precompiles/precompile.go | 1 + 5 files changed, 67 insertions(+) create mode 100644 precompiles/ArbMathStats.go create mode 100644 precompiles/ArbMathStats_test.go diff --git a/go.mod b/go.mod index 5c3970ee17..29a4649625 100644 --- a/go.mod +++ b/go.mod @@ -268,6 +268,7 @@ require ( golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect golang.org/x/mod v0.10.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect + gonum.org/v1/gonum v0.14.0 // indirect google.golang.org/genproto v0.0.0-20231016165738-49dd2c1f3d0b // indirect google.golang.org/genproto/googleapis/api v0.0.0-20231012201019-e917dd12ba7a // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect diff --git a/go.sum b/go.sum index 424e3508c3..8d12fe6324 100644 --- a/go.sum +++ b/go.sum @@ -2077,6 +2077,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= +gonum.org/v1/gonum v0.14.0 h1:2NiG67LD1tEH0D7kM+ps2V+fXmsAnpUeec7n8tcr4S0= +gonum.org/v1/gonum v0.14.0/go.mod h1:AoWeoz0becf9QMWtE8iWXNXc27fK4fNeHNf/oMejGfU= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.1.0/go.mod h1:UGEZY7KEX120AnNLIHFMKIo4obdJhkp2tPbaPlQx13Y= diff --git a/precompiles/ArbMathStats.go b/precompiles/ArbMathStats.go new file mode 100644 index 0000000000..0a2bbca5ee --- /dev/null +++ b/precompiles/ArbMathStats.go @@ -0,0 +1,28 @@ +package precompiles + +import ( + "math" + "math/big" + + "gonum.org/v1/gonum/stat" +) + +// ArbMathStats provides methods for computing statistics +type ArbMathStats struct { + Address addr // 0x11b +} + +func (con *ArbMathStats) stdDev(c ctx, evm mech, input []int32, decimals uint32) (huge, error) { + floatInput := make([]float64, len(input)) + for i, num := range input { + floatInput[i] = float64(num) + } + + stdev := big.NewFloat(stat.StdDev(floatInput, nil)) + stdev.Mul(stdev, big.NewFloat(math.Pow10(int(decimals)))) + + truncatedStdev := new(big.Int) + stdev.Int(truncatedStdev) + + return truncatedStdev, nil +} diff --git a/precompiles/ArbMathStats_test.go b/precompiles/ArbMathStats_test.go new file mode 100644 index 0000000000..9e3286f7e5 --- /dev/null +++ b/precompiles/ArbMathStats_test.go @@ -0,0 +1,35 @@ +package precompiles + +import ( + "math/big" + "testing" +) + +func TestArbMathStatsStandardDeviationIdenticalElements(t *testing.T) { + mathStats := ArbMathStats{} + + data := []int32{1, 1, 1, 1, 1, 1} + stdev, _ := mathStats.stdDev(nil, nil, data, 0) + + if stdev.Cmp(big.NewInt(0)) != 0 { + t.Errorf("Expected stdev of 0 for identical elements, got: %s", stdev) + } +} + +func TestArbMathStatsStandardDeviation(t *testing.T) { + mathStats := ArbMathStats{} + + data := []int32{1, 5, 10, 100} + + stdev, _ := mathStats.stdDev(nil, nil, data, 4) + expected := big.NewInt(474763) + if stdev.Cmp(expected) != 0 { + t.Errorf("Incorrect stddev, expected %s, got %s", expected, stdev) + } + + stdev, _ = mathStats.stdDev(nil, nil, data, 2) + expected = big.NewInt(4747) + if stdev.Cmp(expected) != 0 { + t.Errorf("Incorrect stddev, expected %s, got %s", expected, stdev) + } +} diff --git a/precompiles/precompile.go b/precompiles/precompile.go index 031dbd6e21..3419c14719 100644 --- a/precompiles/precompile.go +++ b/precompiles/precompile.go @@ -541,6 +541,7 @@ func Precompiles() map[addr]ArbosPrecompile { insert(MakePrecompile(templates.ArbAggregatorMetaData, &ArbAggregator{Address: hex("6d")})) insert(MakePrecompile(templates.ArbStatisticsMetaData, &ArbStatistics{Address: hex("6f")})) insert(MakePrecompile(templates.ArbInferenceMetaData, &ArbInference{Address: hex("11a")})) + insert(MakePrecompile(templates.ArbMathStatsMetaData, &ArbMathStats{Address: hex("11b")})) eventCtx := func(gasLimit uint64, err error) *Context { if err != nil { From bd959149525783422099aed886b6707457e88289 Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Tue, 23 Jan 2024 23:51:18 -0500 Subject: [PATCH 2/8] switch to own contracts repo --- .gitmodules | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index fea7abff6d..f44b7b2081 100644 --- a/.gitmodules +++ b/.gitmodules @@ -9,8 +9,8 @@ url = https://github.com/google/brotli.git [submodule "contracts"] path = contracts - url = https://github.com/VannaLabs/nitro-contracts.git - branch = develop + url = https://github.com/adambalogh/nitro-contracts.git + branch = main [submodule "arbitrator/wasm-testsuite/testsuite"] path = arbitrator/wasm-testsuite/testsuite url = https://github.com/WebAssembly/testsuite.git From 6e403791ef7df764b3e89abf991078952cbfe0c6 Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Wed, 24 Jan 2024 00:02:39 -0500 Subject: [PATCH 3/8] fix type --- contracts | 2 +- precompiles/ArbInference.go | 3 +++ precompiles/ArbMathStats.go | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/contracts b/contracts index 6125e9874b..b79b69eccb 160000 --- a/contracts +++ b/contracts @@ -1 +1 @@ -Subproject commit 6125e9874b4a91bdd8e7b4bcfcda237a81d18645 +Subproject commit b79b69eccb156c12de8f1d92b252395d2a5788ad diff --git a/precompiles/ArbInference.go b/precompiles/ArbInference.go index b13e94ecda..8e01ac5b8d 100644 --- a/precompiles/ArbInference.go +++ b/precompiles/ArbInference.go @@ -1,6 +1,7 @@ package precompiles import ( + // #nosec G501 "crypto/md5" "encoding/hex" "strconv" @@ -127,6 +128,8 @@ func HashInferenceTX(arr []string, separator string) string { for i := 0; i < len(arr); i++ { hashString += arr[i] + separator } + + // #nosec G401 hasher := md5.New() hasher.Write([]byte(hashString)) return hex.EncodeToString(hasher.Sum(nil)) diff --git a/precompiles/ArbMathStats.go b/precompiles/ArbMathStats.go index 0a2bbca5ee..82c2186c2f 100644 --- a/precompiles/ArbMathStats.go +++ b/precompiles/ArbMathStats.go @@ -12,7 +12,7 @@ type ArbMathStats struct { Address addr // 0x11b } -func (con *ArbMathStats) stdDev(c ctx, evm mech, input []int32, decimals uint32) (huge, error) { +func (con *ArbMathStats) stdDev(c ctx, evm mech, input []int32, decimals uint8) (huge, error) { floatInput := make([]float64, len(input)) for i, num := range input { floatInput[i] = float64(num) From e6e8e60099205e208ebbfabf11b81a32a3671f78 Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Wed, 24 Jan 2024 00:11:37 -0500 Subject: [PATCH 4/8] fix name --- precompiles/ArbMathStats.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/precompiles/ArbMathStats.go b/precompiles/ArbMathStats.go index 82c2186c2f..a8b93eb15d 100644 --- a/precompiles/ArbMathStats.go +++ b/precompiles/ArbMathStats.go @@ -12,7 +12,7 @@ type ArbMathStats struct { Address addr // 0x11b } -func (con *ArbMathStats) stdDev(c ctx, evm mech, input []int32, decimals uint8) (huge, error) { +func (con *ArbMathStats) StdDev(c ctx, evm mech, input []int32, decimals uint8) (huge, error) { floatInput := make([]float64, len(input)) for i, num := range input { floatInput[i] = float64(num) From d85f93279575985ace93c8488a419ba61eeb7d38 Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Wed, 24 Jan 2024 09:09:02 -0500 Subject: [PATCH 5/8] fix test --- precompiles/ArbMathStats_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/precompiles/ArbMathStats_test.go b/precompiles/ArbMathStats_test.go index 9e3286f7e5..c87c3d7267 100644 --- a/precompiles/ArbMathStats_test.go +++ b/precompiles/ArbMathStats_test.go @@ -9,7 +9,7 @@ func TestArbMathStatsStandardDeviationIdenticalElements(t *testing.T) { mathStats := ArbMathStats{} data := []int32{1, 1, 1, 1, 1, 1} - stdev, _ := mathStats.stdDev(nil, nil, data, 0) + stdev, _ := mathStats.StdDev(nil, nil, data, 0) if stdev.Cmp(big.NewInt(0)) != 0 { t.Errorf("Expected stdev of 0 for identical elements, got: %s", stdev) @@ -21,13 +21,13 @@ func TestArbMathStatsStandardDeviation(t *testing.T) { data := []int32{1, 5, 10, 100} - stdev, _ := mathStats.stdDev(nil, nil, data, 4) + stdev, _ := mathStats.StdDev(nil, nil, data, 4) expected := big.NewInt(474763) if stdev.Cmp(expected) != 0 { t.Errorf("Incorrect stddev, expected %s, got %s", expected, stdev) } - stdev, _ = mathStats.stdDev(nil, nil, data, 2) + stdev, _ = mathStats.StdDev(nil, nil, data, 2) expected = big.NewInt(4747) if stdev.Cmp(expected) != 0 { t.Errorf("Incorrect stddev, expected %s, got %s", expected, stdev) From ab4c064e473eab52c33defe162f121aaf4bf7403 Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Wed, 24 Jan 2024 09:11:59 -0500 Subject: [PATCH 6/8] make impl pure --- precompiles/ArbMathStats.go | 2 +- precompiles/ArbMathStats_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/precompiles/ArbMathStats.go b/precompiles/ArbMathStats.go index a8b93eb15d..572d828f2e 100644 --- a/precompiles/ArbMathStats.go +++ b/precompiles/ArbMathStats.go @@ -12,7 +12,7 @@ type ArbMathStats struct { Address addr // 0x11b } -func (con *ArbMathStats) StdDev(c ctx, evm mech, input []int32, decimals uint8) (huge, error) { +func (con *ArbMathStats) StdDev(c ctx, input []int32, decimals uint8) (huge, error) { floatInput := make([]float64, len(input)) for i, num := range input { floatInput[i] = float64(num) diff --git a/precompiles/ArbMathStats_test.go b/precompiles/ArbMathStats_test.go index c87c3d7267..0a04fc3b24 100644 --- a/precompiles/ArbMathStats_test.go +++ b/precompiles/ArbMathStats_test.go @@ -9,7 +9,7 @@ func TestArbMathStatsStandardDeviationIdenticalElements(t *testing.T) { mathStats := ArbMathStats{} data := []int32{1, 1, 1, 1, 1, 1} - stdev, _ := mathStats.StdDev(nil, nil, data, 0) + stdev, _ := mathStats.StdDev(nil, data, 0) if stdev.Cmp(big.NewInt(0)) != 0 { t.Errorf("Expected stdev of 0 for identical elements, got: %s", stdev) @@ -21,13 +21,13 @@ func TestArbMathStatsStandardDeviation(t *testing.T) { data := []int32{1, 5, 10, 100} - stdev, _ := mathStats.StdDev(nil, nil, data, 4) + stdev, _ := mathStats.StdDev(nil, data, 4) expected := big.NewInt(474763) if stdev.Cmp(expected) != 0 { t.Errorf("Incorrect stddev, expected %s, got %s", expected, stdev) } - stdev, _ = mathStats.StdDev(nil, nil, data, 2) + stdev, _ = mathStats.StdDev(nil, data, 2) expected = big.NewInt(4747) if stdev.Cmp(expected) != 0 { t.Errorf("Incorrect stddev, expected %s, got %s", expected, stdev) From f48b078d08c15bca3401e6dc2e815a4e88efec67 Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Wed, 24 Jan 2024 09:12:49 -0500 Subject: [PATCH 7/8] rm gosec suppression --- precompiles/ArbInference.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/precompiles/ArbInference.go b/precompiles/ArbInference.go index 8e01ac5b8d..e5f91bf128 100644 --- a/precompiles/ArbInference.go +++ b/precompiles/ArbInference.go @@ -1,7 +1,6 @@ package precompiles import ( - // #nosec G501 "crypto/md5" "encoding/hex" "strconv" @@ -129,7 +128,6 @@ func HashInferenceTX(arr []string, separator string) string { hashString += arr[i] + separator } - // #nosec G401 hasher := md5.New() hasher.Write([]byte(hashString)) return hex.EncodeToString(hasher.Sum(nil)) From 4b2e61285c950a6277bfc763065df1a9c5606dda Mon Sep 17 00:00:00 2001 From: Adam Balogh Date: Wed, 24 Jan 2024 09:13:24 -0500 Subject: [PATCH 8/8] rm nl --- precompiles/ArbInference.go | 1 - 1 file changed, 1 deletion(-) diff --git a/precompiles/ArbInference.go b/precompiles/ArbInference.go index e5f91bf128..b13e94ecda 100644 --- a/precompiles/ArbInference.go +++ b/precompiles/ArbInference.go @@ -127,7 +127,6 @@ func HashInferenceTX(arr []string, separator string) string { for i := 0; i < len(arr); i++ { hashString += arr[i] + separator } - hasher := md5.New() hasher.Write([]byte(hashString)) return hex.EncodeToString(hasher.Sum(nil))