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 3 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
18 changes: 18 additions & 0 deletions stdlibs/crypto/sha3/sha3.gno
Original file line number Diff line number Diff line change
@@ -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)
}
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)
}
}
1 change: 1 addition & 0 deletions stdlibs/internal/crypto/sha3/sha3.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package sha3
56 changes: 56 additions & 0 deletions stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package stdlibs

import (
"bytes"
"crypto/sha256"
"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"
Expand All @@ -21,6 +24,59 @@ 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()[:slice.Length]
}
// remove padding
bzNP := bytes.Trim(bz, "\x00")
thehowl marked this conversation as resolved.
Show resolved Hide resolved
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()[:slice.Length]
}
// remove padding
bzNP := bytes.Trim(bz, "\x00")
hash := sha3.Sum512(bzNP)
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