Skip to content

Commit

Permalink
Add unit tests to golang sdk
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <[email protected]>
  • Loading branch information
jimthematrix committed Aug 6, 2024
1 parent 3af2958 commit c707e89
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
54 changes: 54 additions & 0 deletions zkp/golang/internal/sparse-merkle-tree/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package node

import (
"errors"
"math/big"
"testing"

"github.com/hyperledger-labs/zeto/internal/sparse-merkle-tree/utils"
"github.com/hyperledger-labs/zeto/pkg/sparse-merkle-tree/core"
"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/stretchr/testify/assert"
)

Expand All @@ -45,3 +49,53 @@ func TestNodeIndex(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, v4.Cmp(idx5.BigInt()))
}

func TestNewEmptyNode(t *testing.T) {
node := NewEmptyNode()
assert.Equal(t, node.Type(), core.NodeTypeEmpty)
assert.Nil(t, node.Index())
assert.Nil(t, node.Ref())
assert.Nil(t, node.LeftChild())
assert.Nil(t, node.RightChild())
}

func TestNewLeafNode(t *testing.T) {
idx, _ := NewNodeIndexFromBigInt(big.NewInt(10))
i := utils.NewIndexOnly(idx)
node, err := NewLeafNode(i)
assert.NoError(t, err)
assert.Equal(t, node.Type(), core.NodeTypeLeaf)
assert.Equal(t, node.Index(), idx)
elements := []*big.Int{idx.BigInt(), idx.BigInt(), big.NewInt(1)}
hash, err := poseidon.Hash(elements)
assert.NoError(t, err)
assert.Equal(t, node.Ref().BigInt(), hash)
assert.Nil(t, node.LeftChild())
assert.Nil(t, node.RightChild())
}

func TestNewBranchNode(t *testing.T) {
idx0, _ := NewNodeIndexFromBigInt(big.NewInt(0))
idx1, _ := NewNodeIndexFromBigInt(big.NewInt(1))
node, err := NewBranchNode(idx0, idx1)
assert.NoError(t, err)
assert.Equal(t, node.Type(), core.NodeTypeBranch)
assert.Nil(t, node.Index())
elements := []*big.Int{idx0.BigInt(), idx1.BigInt()}
hash, err := poseidon.Hash(elements)
assert.NoError(t, err)
assert.Equal(t, node.Ref().BigInt(), hash)
assert.Equal(t, node.LeftChild(), idx0)
assert.Equal(t, node.RightChild(), idx1)
}

type badIndex struct{}

func (f *badIndex) CalculateIndex() (core.NodeIndex, error) {
return nil, errors.New("Bang!")
}

func TestNewLeafNodeFail(t *testing.T) {
_, err := NewLeafNode(&badIndex{})
assert.EqualError(t, err, "Bang!")
}
33 changes: 33 additions & 0 deletions zkp/golang/internal/sparse-merkle-tree/utxo/fungible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,36 @@ func TestFungibleUTXOs(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "26e3879b46b15a4ddbaca5d96af1bd2743f67f13f0bb85c40782950a2a700138", idx1.BigInt().Text(16))
}

func TestFungibleUTXOsWithNullifiers(t *testing.T) {
privateKey, _ := new(big.Int).SetString("df7ff8191db562f4ed9404e91f184502fe67f880cd1fe67c0f84d224a53ee55", 16)
salt, _ := new(big.Int).SetString("13de02d64a5736a56b2d35d2a83dd60397ba70aae6f8347629f0960d4fee5d58", 16)
utxo1 := NewFungibleNullifier(big.NewInt(10), privateKey, salt)

idx1, err := utxo1.CalculateIndex()
assert.NoError(t, err)
assert.Equal(t, "2e9d5efff5d38753195e9a0097a475c90e6bce0fe40b0408470fd81e53d145e6", idx1.BigInt().Text(16))
}

func TestFungibleUTXOsFail(t *testing.T) {
x, _ := new(big.Int).SetString("825008057393653951717923193550498143270634388542414913039278922773034837543", 10)
y, _ := new(big.Int).SetString("12283738323983038985315025769936102865095332379710901215078187917699827946177", 10)
alice := &babyjub.PublicKey{
X: x,
Y: y,
}
salt, _ := new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
utxo1 := NewFungible(big.NewInt(10), alice, salt)

_, err := utxo1.CalculateIndex()
assert.EqualError(t, err, "inputs values not inside Finite Field")
}

func TestFungibleUTXOsWithNullifiersFail(t *testing.T) {
privateKey, _ := new(big.Int).SetString("df7ff8191db562f4ed9404e91f184502fe67f880cd1fe67c0f84d224a53ee55", 16)
salt, _ := new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
utxo1 := NewFungibleNullifier(big.NewInt(10), privateKey, salt)

_, err := utxo1.CalculateIndex()
assert.EqualError(t, err, "inputs values not inside Finite Field")
}
37 changes: 37 additions & 0 deletions zkp/golang/internal/sparse-merkle-tree/utxo/nonfungible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,40 @@ func TestNonFungibleUTXOs(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "14387265223978393188138799516680159264477343701050531343910120918831961837958", idx1.BigInt().String())
}

func TestNonFungibleUTXOsWithNullifiers(t *testing.T) {
privateKey, _ := new(big.Int).SetString("df7ff8191db562f4ed9404e91f184502fe67f880cd1fe67c0f84d224a53ee55", 16)
salt, _ := new(big.Int).SetString("14366367216420666010683918465570547601749064763665615379119566396413295472937", 10)
uri, err := HashTokenUri("http://ipfs.io/file-hash-1")
assert.NoError(t, err)
utxo1 := NewNonFungibleNullifier(big.NewInt(1001), uri, privateKey, salt)

idx1, err := utxo1.CalculateIndex()
assert.NoError(t, err)
assert.Equal(t, "187a5bbc943a0fab725eaacf81872c4e023bb0deec1858ab2a43867f6646c0d8", idx1.BigInt().Text(16))
}

func TestNonFungibleUTXOsFail(t *testing.T) {
x, _ := new(big.Int).SetString("14071052441699386420964762094868612757480677741190253249248703784837194954083", 10)
y, _ := new(big.Int).SetString("10144222387217718469257170015212761087909907436961262709512987264580109747792", 10)
alice := &babyjub.PublicKey{
X: x,
Y: y,
}
salt, _ := new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
uri, err := HashTokenUri("http://ipfs.io/file-hash-1")
assert.NoError(t, err)
utxo1 := NewNonFungible(big.NewInt(1001), uri, alice, salt)
_, err = utxo1.CalculateIndex()
assert.EqualError(t, err, "inputs values not inside Finite Field")
}

func TestNonFungibleUTXOsWithNullifiersFail(t *testing.T) {
privateKey, _ := new(big.Int).SetString("df7ff8191db562f4ed9404e91f184502fe67f880cd1fe67c0f84d224a53ee55", 16)
salt, _ := new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
uri, err := HashTokenUri("http://ipfs.io/file-hash-1")
assert.NoError(t, err)
utxo1 := NewNonFungibleNullifier(big.NewInt(1001), uri, privateKey, salt)
_, err = utxo1.CalculateIndex()
assert.EqualError(t, err, "inputs values not inside Finite Field")
}

0 comments on commit c707e89

Please sign in to comment.