Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlibs): sha3.Sum256(), sha3.Sum512() #589

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions stdlibs/crypto/sha3/sha3.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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"
)

// Sum256 returns the SHA3-256 checksum of the data.
func Sum256(data []byte) [32]byte {
return isha3.Sum256(data)
}

// Sum512 returns the SHA3-512 checksum of the data.
func Sum512(data []byte) [64]byte {
return isha3.Sum512(data)
}
27 changes: 27 additions & 0 deletions stdlibs/crypto/sha3/sha3_test.gno
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 3 additions & 0 deletions stdlibs/internal/crypto/sha3/sha3.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package sha3

// XXX injected via stdlibs/stdlibs.go
52 changes: 52 additions & 0 deletions stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"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"
Expand All @@ -21,6 +23,56 @@ 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", "[32]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()[:slice.Length]
}

hash := sha3.Sum256(bz)
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", "[64]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()[:slice.Length]
}
hash := sha3.Sum512(bz)
res0 := gno.Go2GnoValue(
m.Alloc,
m.Store,
reflect.ValueOf(hash),
)
m.PushValue(res0)
},
)
case "internal/crypto/sha256":
pn.DefineNative("Sum256",
gno.Flds( // params
Expand Down