Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <jim.zhang@kaleido.io>
  • Loading branch information
jimthematrix committed Sep 4, 2024
1 parent 2bd1bc2 commit 0f6eda0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/golang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
path: zeto
with:
fetch-depth: 0

- name: Checkout kaleido's fork of go-iden3-crypto
uses: actions/checkout@v3
path: go-iden3-crypto
with:
repository: kaleido-io/go-iden3-crypto
ref: multi-states
Expand All @@ -30,10 +32,10 @@ jobs:
go-version: 1.22

- name: Build and Test
working-directory: go-sdk
working-directory: zeto/go-sdk
run: make

- uses: codecov/codecov-action@v4
with:
codecov_yml_path: ./codecov.yml
codecov_yml_path: ./zeto/codecov.yml
token: ${{ secrets.CODECOV_TOKEN }}
24 changes: 24 additions & 0 deletions go-sdk/integration-test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,30 @@ func TestZeto_2_SuccessfulProving(t *testing.T) {
assert.Equal(t, 3, len(proof.Proof.B))
assert.Equal(t, 3, len(proof.Proof.C))
assert.Equal(t, 9, len(proof.PubSignals))

// the receiver would be able to get the encrypted values and salts
// from the transaction events
encryptedValues := make([]*big.Int, 4)
for i := 0; i < 4; i++ {
v, ok := new(big.Int).SetString(proof.PubSignals[i], 10)
assert.True(t, ok)
encryptedValues[i] = v
}

// the first two elements in the public signals are the encrypted value and salt
// for the first output. decrypt using the receiver's private key and compare with
// the UTXO hash
secret := utxo.GenerateECDHSharedSecret(receiver.PrivateKey, sender.PublicKey)
decrypted, err := utxo.PoseidonDecrypt(encryptedValues, []*big.Int{secret.X, secret.Y}, encryptionNonce, 2)
assert.NoError(t, err)
assert.Equal(t, outputValues[0].String(), decrypted[0].String())
assert.Equal(t, salt3.String(), decrypted[1].String())

// as the receiver, to check if the decryption was successful, we hash the decrypted
// value and salt and compare with the output commitment
calculatedHash, err := poseidon.Hash([]*big.Int{decrypted[0], decrypted[1], receiver.PublicKey.X, receiver.PublicKey.Y})
assert.NoError(t, err)
assert.Equal(t, output1.String(), calculatedHash.String())
}

func TestZeto_3_SuccessfulProving(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions go-sdk/pkg/utxo/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ func NewSalt() *big.Int {
func NewEncryptionNonce() *big.Int {
return utxo.NewEncryptionNonce()
}

func PoseidonEncrypt(msg []*big.Int, key []*big.Int, nonce *big.Int) ([]*big.Int, error) {
return utxo.PoseidonEncrypt(msg, key, nonce)
}

func PoseidonDecrypt(ciphertext []*big.Int, key []*big.Int, nonce *big.Int, length int) ([]*big.Int, error) {
return utxo.PoseidonDecrypt(ciphertext, key, nonce, length)
}

func GenerateECDHSharedSecret(privKey *babyjub.PrivateKey, pubKey *babyjub.PublicKey) *babyjub.Point {
return utxo.GenerateECDHSharedSecret(privKey, pubKey)
}

0 comments on commit 0f6eda0

Please sign in to comment.