Skip to content

Commit

Permalink
test(bls): add test for hash-to-curve
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Aug 17, 2024
1 parent e773444 commit c5a3c8e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crypto/bls/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestDuplicatedAggregate(t *testing.T) {
// TestHashToCurve ensures that the hash-to-curve function in kilic/bls12-381
// works as intended and is compatible with the spec.
// test vectors can be found here:
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16#appendix-J.9.1
// https://datatracker.ietf.org/doc/html/rfc9380
func TestHashToCurve(t *testing.T) {
domain := []byte("QUUX-V01-CS02-with-BLS12381G1_XMD:SHA-256_SSWU_RO_")
tests := []struct {
Expand Down Expand Up @@ -173,6 +173,16 @@ func TestHashToCurve(t *testing.T) {
"15f68eaa693b95ccb85215dc65fa81038d69629f70aeee0d0f677cf22285e7bf58d7cb86eefe8f2e9bc3f8cb84fac488" +
"1807a1d50c29f430b8cafc4f8638dfeeadf51211e1602a5f184443076715f91bb90a48ba1e370edce6ae1062f5e6dd38",
},
{
"a512_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"082aabae8b7dedb0e78aeb619ad3bfd9277a2f77ba7fad20ef6aabdc6c31d19ba5a6d12283553294c1825c4b3ca2dcfe" +
"05b84ae5a942248eea39e1d91030458c40153f3b654ab7872d779ad1e942856a20c438e8d99bc8abfbf74729ce1f7ac8",
},
}

g1 := bls12381.NewG1()
Expand Down
29 changes: 29 additions & 0 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wallet_test

import (
"bytes"
"fmt"
"path"
"testing"

Expand All @@ -9,8 +11,11 @@ import (
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/state"
"github.com/pactus-project/pactus/types/account"
"github.com/pactus-project/pactus/types/amount"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/util/encoding"
"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/pactus-project/pactus/wallet"
Expand Down Expand Up @@ -589,3 +594,27 @@ func TestTotalBalance(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, totalBalance, acc1.Balance()+acc3.Balance())
}

func Test_sign(t *testing.T) {

Check failure on line 598 in wallet/wallet_test.go

View workflow job for this annotation

GitHub Actions / linting

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 598 in wallet/wallet_test.go

View workflow job for this annotation

GitHub Actions / build-linux

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
prv, _ := bls.PrivateKeyFromString("SECRET1PDRWTLP5PX0FAHDX39GXZJP7FKZFALML0D5U9TT9KVQHDUC99CMGQQJVK67")
sender, _ := crypto.AddressFromString("pc1z5x2a0lkt5nrrdqe0rkcv6r4pfkmdhrr3mawvua")
receiver, _ := crypto.AddressFromString("pc1zt6qcdymkk48c5ds0fzfsaf6puwu8w8djn3ffpn")
amt, _ := amount.NewAmount(1.0)
fee, _ := amount.NewAmount(0.001)
lock_time := uint32(0x123456)

Check failure on line 604 in wallet/wallet_test.go

View workflow job for this annotation

GitHub Actions / linting

var-naming: don't use underscores in Go names; var lock_time should be lockTime (revive)

Check failure on line 604 in wallet/wallet_test.go

View workflow job for this annotation

GitHub Actions / build-linux

var-naming: don't use underscores in Go names; var lock_time should be lockTime (revive)

trx := tx.NewTransferTx(lock_time, sender, receiver, amt, fee, tx.WithMemo("test"))
fmt.Printf("msg to sign: %x\n", trx.SignBytes())
sig := prv.Sign(trx.SignBytes())

trx.SetPublicKey(prv.PublicKey())
trx.SetSignature(sig)

var buf bytes.Buffer
encoding.WriteVarInt(&buf, uint64(fee.ToNanoPAC()))

Check failure on line 614 in wallet/wallet_test.go

View workflow job for this annotation

GitHub Actions / linting

Error return value of `encoding.WriteVarInt` is not checked (errcheck)

Check failure on line 614 in wallet/wallet_test.go

View workflow job for this annotation

GitHub Actions / build-linux

Error return value of `encoding.WriteVarInt` is not checked (errcheck)
fmt.Printf("%x\n", buf.Bytes())

bz, _ := trx.Bytes()
fmt.Printf("%x", bz)

}

Check failure on line 620 in wallet/wallet_test.go

View workflow job for this annotation

GitHub Actions / linting

unnecessary trailing newline (whitespace)

Check failure on line 620 in wallet/wallet_test.go

View workflow job for this annotation

GitHub Actions / build-linux

unnecessary trailing newline (whitespace)

0 comments on commit c5a3c8e

Please sign in to comment.