From 127d50bbcb64ded26601e59fb1a2add61c260521 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 9 Mar 2023 16:05:15 +0900 Subject: [PATCH 1/4] feat: sha3.Sum256(), sha3.Sum512() --- stdlibs/crypto/sha3/sha3.gno | 18 +++++++++ stdlibs/crypto/sha3/sha3_test.gno | 27 ++++++++++++++ stdlibs/internal/crypto/sha3/sha3.gno | 1 + stdlibs/stdlibs.go | 54 +++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 stdlibs/crypto/sha3/sha3.gno create mode 100644 stdlibs/crypto/sha3/sha3_test.gno create mode 100644 stdlibs/internal/crypto/sha3/sha3.gno diff --git a/stdlibs/crypto/sha3/sha3.gno b/stdlibs/crypto/sha3/sha3.gno new file mode 100644 index 00000000000..4268aeb4978 --- /dev/null +++ b/stdlibs/crypto/sha3/sha3.gno @@ -0,0 +1,18 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package sha3 + +import ( + isha3 "internal/crypto/sha3" +) + + +func Sum256(data []byte) []byte { + return isha3.Sum256(data) +} + +func Sum512(data []byte) []byte { + return isha3.Sum512(data) +} \ No newline at end of file diff --git a/stdlibs/crypto/sha3/sha3_test.gno b/stdlibs/crypto/sha3/sha3_test.gno new file mode 100644 index 00000000000..d1500f71100 --- /dev/null +++ b/stdlibs/crypto/sha3/sha3_test.gno @@ -0,0 +1,27 @@ +package sha3 + +import ( + "std" + "testing" + + "crypto/sha3" + "encoding/hex" +) + +func TestSum256(t *testing.T) { + got := sha3.Sum256([]byte("welcome to gnoland")) + expected := "40b3c0de951082601cf10ebab6ec23ee2bd91f978a152f295451b9dcbed33ddb" + + if (hex.EncodeToString(got)) != expected { + t.Errorf("got %v(%T), expected %v(%T)", hex.EncodeToString(got), hex.EncodeToString(got), expected, expected) + } +} + +func TestSum512(t *testing.T) { + got := sha3.Sum512([]byte("hello gnomes")) + expected := "efcdb0b2a91166affe1ae1ac437f21edb9dc319808af9bdb59d52e56f68b83b671a3446412df6b931435f7a3e84713069b18e4d23bd8d230ea5bbfec6f59f417" + + if (hex.EncodeToString(got)) != expected { + t.Errorf("got %v(%T), expected %v(%T)", hex.EncodeToString(got), hex.EncodeToString(got), expected, expected) + } +} \ No newline at end of file diff --git a/stdlibs/internal/crypto/sha3/sha3.gno b/stdlibs/internal/crypto/sha3/sha3.gno new file mode 100644 index 00000000000..aa68577175b --- /dev/null +++ b/stdlibs/internal/crypto/sha3/sha3.gno @@ -0,0 +1 @@ +package sha3 \ No newline at end of file diff --git a/stdlibs/stdlibs.go b/stdlibs/stdlibs.go index b365e43a14e..814253fac19 100644 --- a/stdlibs/stdlibs.go +++ b/stdlibs/stdlibs.go @@ -1,11 +1,14 @@ package stdlibs import ( + "bytes" "math" "reflect" "strconv" "time" + "golang.org/x/crypto/sha3" + "github.com/gnolang/gno/pkgs/bech32" "github.com/gnolang/gno/pkgs/crypto" gno "github.com/gnolang/gno/pkgs/gnolang" @@ -20,6 +23,57 @@ func InjectNativeMappings(store gno.Store) { func InjectPackage(store gno.Store, pn *gno.PackageNode) { switch pn.PkgPath { + case "internal/crypto/sha3": + pn.DefineNative("Sum256", + gno.Flds( // params + "data", "[]byte", + ), + gno.Flds( // results + "bz", "[]byte", + ), + func(m *gno.Machine) { + arg0 := m.LastBlock().GetParams1().TV + bz := []byte(nil) + if arg0.V != nil { + slice := arg0.V.(*gno.SliceValue) + array := slice.GetBase(m.Store) + bz = array.GetReadonlyBytes() + } + bzNP := bytes.Trim(bz, "\x00") + hash := sha3.Sum256(bzNP) + res0 := gno.Go2GnoValue( + m.Alloc, + m.Store, + reflect.ValueOf(hash), + ) + m.PushValue(res0) + }, + ) + pn.DefineNative("Sum512", + gno.Flds( // params + "data", "[]byte", + ), + gno.Flds( // results + "bz", "[]byte", + ), + func(m *gno.Machine) { + arg0 := m.LastBlock().GetParams1().TV + bz := []byte(nil) + if arg0.V != nil { + slice := arg0.V.(*gno.SliceValue) + array := slice.GetBase(m.Store) + bz = array.GetReadonlyBytes() + } + bzNP := bytes.Trim(bz, "\x00") + hash := sha3.Sum512(bzNP) + res0 := gno.Go2GnoValue( + m.Alloc, + m.Store, + reflect.ValueOf(hash), + ) + m.PushValue(res0) + }, + ) case "internal/math": pn.DefineNative("Float32bits", gno.Flds( // params From 18474b795444ca993fb2128aaa7447c252c3e5fe Mon Sep 17 00:00:00 2001 From: n3wbie Date: Sat, 11 Mar 2023 10:40:39 +0900 Subject: [PATCH 2/4] refactory: read length of slice --- stdlibs/stdlibs.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlibs/stdlibs.go b/stdlibs/stdlibs.go index 814253fac19..5a87fd4469c 100644 --- a/stdlibs/stdlibs.go +++ b/stdlibs/stdlibs.go @@ -37,8 +37,9 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { if arg0.V != nil { slice := arg0.V.(*gno.SliceValue) array := slice.GetBase(m.Store) - bz = array.GetReadonlyBytes() + bz = array.GetReadonlyBytes()[:slice.Length] } + // remove padding bzNP := bytes.Trim(bz, "\x00") hash := sha3.Sum256(bzNP) res0 := gno.Go2GnoValue( @@ -62,7 +63,7 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { if arg0.V != nil { slice := arg0.V.(*gno.SliceValue) array := slice.GetBase(m.Store) - bz = array.GetReadonlyBytes() + bz = array.GetReadonlyBytes()[:slice.Length] } bzNP := bytes.Trim(bz, "\x00") hash := sha3.Sum512(bzNP) From 5d8e7c406201681e007548a6ad7792b190c79737 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 29 Mar 2023 10:17:55 +0900 Subject: [PATCH 3/4] refactor: remove padding & set fixed length --- stdlibs/stdlibs.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/stdlibs/stdlibs.go b/stdlibs/stdlibs.go index 31cd86d27af..055e8823451 100644 --- a/stdlibs/stdlibs.go +++ b/stdlibs/stdlibs.go @@ -1,7 +1,6 @@ package stdlibs import ( - "bytes" "crypto/sha256" "math" "reflect" @@ -30,7 +29,7 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { "data", "[]byte", ), gno.Flds( // results - "bz", "[]byte", + "bz", "[32]byte", ), func(m *gno.Machine) { arg0 := m.LastBlock().GetParams1().TV @@ -40,9 +39,8 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { array := slice.GetBase(m.Store) bz = array.GetReadonlyBytes()[:slice.Length] } - // remove padding - bzNP := bytes.Trim(bz, "\x00") - hash := sha3.Sum256(bzNP) + + hash := sha3.Sum256(bz) res0 := gno.Go2GnoValue( m.Alloc, m.Store, @@ -56,7 +54,7 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { "data", "[]byte", ), gno.Flds( // results - "bz", "[]byte", + "bz", "[64]byte", ), func(m *gno.Machine) { arg0 := m.LastBlock().GetParams1().TV @@ -66,9 +64,7 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { array := slice.GetBase(m.Store) bz = array.GetReadonlyBytes()[:slice.Length] } - // remove padding - bzNP := bytes.Trim(bz, "\x00") - hash := sha3.Sum512(bzNP) + hash := sha3.Sum512(bz) res0 := gno.Go2GnoValue( m.Alloc, m.Store, From 163dd7b34907c4046beead0cdf395b521b7f68de Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 29 Mar 2023 10:18:44 +0900 Subject: [PATCH 4/4] chore: wrap things up --- stdlibs/crypto/sha3/sha3.gno | 7 ++++--- stdlibs/crypto/sha3/sha3_test.gno | 4 ++-- stdlibs/internal/crypto/sha3/sha3.gno | 4 +++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/stdlibs/crypto/sha3/sha3.gno b/stdlibs/crypto/sha3/sha3.gno index 4268aeb4978..488f552d6b5 100644 --- a/stdlibs/crypto/sha3/sha3.gno +++ b/stdlibs/crypto/sha3/sha3.gno @@ -8,11 +8,12 @@ import ( isha3 "internal/crypto/sha3" ) - -func Sum256(data []byte) []byte { +// Sum256 returns the SHA3-256 checksum of the data. +func Sum256(data []byte) [32]byte { return isha3.Sum256(data) } -func Sum512(data []byte) []byte { +// Sum512 returns the SHA3-512 checksum of the data. +func Sum512(data []byte) [64]byte { return isha3.Sum512(data) } \ No newline at end of file diff --git a/stdlibs/crypto/sha3/sha3_test.gno b/stdlibs/crypto/sha3/sha3_test.gno index d1500f71100..7be0984b0a9 100644 --- a/stdlibs/crypto/sha3/sha3_test.gno +++ b/stdlibs/crypto/sha3/sha3_test.gno @@ -9,7 +9,7 @@ import ( ) func TestSum256(t *testing.T) { - got := sha3.Sum256([]byte("welcome to gnoland")) + got := sha3.Sum256([]byte("welcome to gnoland"))[:] expected := "40b3c0de951082601cf10ebab6ec23ee2bd91f978a152f295451b9dcbed33ddb" if (hex.EncodeToString(got)) != expected { @@ -18,7 +18,7 @@ func TestSum256(t *testing.T) { } func TestSum512(t *testing.T) { - got := sha3.Sum512([]byte("hello gnomes")) + got := sha3.Sum512([]byte("hello gnomes"))[:] expected := "efcdb0b2a91166affe1ae1ac437f21edb9dc319808af9bdb59d52e56f68b83b671a3446412df6b931435f7a3e84713069b18e4d23bd8d230ea5bbfec6f59f417" if (hex.EncodeToString(got)) != expected { diff --git a/stdlibs/internal/crypto/sha3/sha3.gno b/stdlibs/internal/crypto/sha3/sha3.gno index aa68577175b..fa5a7de83a8 100644 --- a/stdlibs/internal/crypto/sha3/sha3.gno +++ b/stdlibs/internal/crypto/sha3/sha3.gno @@ -1 +1,3 @@ -package sha3 \ No newline at end of file +package sha3 + +// XXX injected via stdlibs/stdlibs.go \ No newline at end of file