sonr/crypto/dkg/frost/dkg_rounds_test.go
Prad Nukala 31bcc21c35
feature/1121 implement ucan validation (#1176)
- **refactor: remove unused auth components**
- **refactor: improve devbox configuration and deployment process**
- **refactor: improve devnet and testnet setup**
- **fix: update templ version to v0.2.778**
- **refactor: rename pkl/net.matrix to pkl/matrix.net**
- **refactor: migrate webapp components to nebula**
- **refactor: protobuf types**
- **chore: update dependencies for improved security and stability**
- **feat: implement landing page and vault gateway servers**
- **refactor: Migrate data models to new module structure and update
related files**
- **feature/1121-implement-ucan-validation**
- **refactor: Replace hardcoded constants with model types in attns.go**
- **feature/1121-implement-ucan-validation**
- **chore: add origin Host struct and update main function to handle
multiple hosts**
- **build: remove unused static files from dwn module**
- **build: remove unused static files from dwn module**
- **refactor: Move DWN models to common package**
- **refactor: move models to pkg/common**
- **refactor: move vault web app assets to embed module**
- **refactor: update session middleware import path**
- **chore: configure port labels and auto-forwarding behavior**
- **feat: enhance devcontainer configuration**
- **feat: Add UCAN middleware for Echo with flexible token validation**
- **feat: add JWT middleware for UCAN authentication**
- **refactor: update package URI and versioning in PklProject files**
- **fix: correct sonr.pkl import path**
- **refactor: move JWT related code to auth package**
- **feat: introduce vault configuration retrieval and management**
- **refactor: Move vault components to gateway module and update file
paths**
- **refactor: remove Dexie and SQLite database implementations**
- **feat: enhance frontend with PWA features and WASM integration**
- **feat: add Devbox features and streamline Dockerfile**
- **chore: update dependencies to include TigerBeetle**
- **chore(deps): update go version to 1.23**
- **feat: enhance devnet setup with PATH environment variable and
updated PWA manifest**
- **fix: upgrade tigerbeetle-go dependency and remove indirect
dependency**
- **feat: add PostgreSQL support to devnet and testnet deployments**
- **refactor: rename keyshare cookie to token cookie**
- **feat: upgrade Go version to 1.23.3 and update dependencies**
- **refactor: update devnet and testnet configurations**
- **feat: add IPFS configuration for devnet**
- **I'll help you update the ipfs.config.pkl to include all the peers
from the shell script. Here's the updated configuration:**
- **refactor: move mpc package to crypto directory**
- **feat: add BIP32 support for various cryptocurrencies**
- **feat: enhance ATN.pkl with additional capabilities**
- **refactor: simplify smart account and vault attenuation creation**
- **feat: add new capabilities to the Attenuation type**
- **refactor: Rename MPC files for clarity and consistency**
- **feat: add DIDKey support for cryptographic operations**
- **feat: add devnet and testnet deployment configurations**
- **fix: correct key derivation in bip32 package**
- **refactor: rename crypto/bip32 package to crypto/accaddr**
- **fix: remove duplicate indirect dependency**
- **refactor: move vault package to root directory**
- **refactor: update routes for gateway and vault**
- **refactor: remove obsolete web configuration file**
- **refactor: remove unused TigerBeetle imports and update host
configuration**
- **refactor: adjust styles directory path**
- **feat: add broadcastTx and simulateTx functions to gateway**
- **feat: add PinVault handler**
2024-12-02 14:27:18 -05:00

169 lines
4.9 KiB
Go
Executable File

//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package frost
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/onsonr/sonr/crypto/core/curves"
"github.com/onsonr/sonr/crypto/sharing"
)
var (
testCurve = curves.ED25519()
Ctx = "string to prevent replay attack"
)
// Test dkg round1 works for 2 participants
func TestDkgRound1Works(t *testing.T) {
p1, err := NewDkgParticipant(1, 2, Ctx, testCurve, 2)
require.NoError(t, err)
bcast, p2psend, err := p1.Round1(nil)
require.NoError(t, err)
require.NotNil(t, bcast)
require.NotNil(t, p2psend)
require.NotNil(t, p1.ctx)
require.Equal(t, len(p2psend), 1)
require.Equal(t, p1.round, 2)
_, ok := p2psend[2]
require.True(t, ok)
}
func TestDkgRound1RepeatCall(t *testing.T) {
p1, err := NewDkgParticipant(1, 2, Ctx, testCurve, 2)
require.NoError(t, err)
_, _, err = p1.Round1(nil)
require.NoError(t, err)
_, _, err = p1.Round1(nil)
require.Error(t, err)
}
func TestDkgRound1BadSecret(t *testing.T) {
p1, err := NewDkgParticipant(1, 2, Ctx, testCurve, 2)
require.NoError(t, err)
// secret == 0
secret := []byte{0}
_, _, err = p1.Round1(secret)
require.Error(t, err)
// secret too big
secret = []byte{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}
_, _, err = p1.Round1(secret)
require.Error(t, err)
}
func PrepareRound2Input(t *testing.T) (*DkgParticipant, *DkgParticipant, *Round1Bcast, *Round1Bcast, Round1P2PSend, Round1P2PSend) {
// Prepare round 1 output of 2 participants
p1, err := NewDkgParticipant(1, 2, Ctx, testCurve, 2)
require.NoError(t, err)
require.Equal(t, p1.otherParticipantShares[2].Id, uint32(2))
p2, err := NewDkgParticipant(2, 2, Ctx, testCurve, 1)
require.NoError(t, err)
require.Equal(t, p2.otherParticipantShares[1].Id, uint32(1))
bcast1, p2psend1, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
return p1, p2, bcast1, bcast2, p2psend1, p2psend2
}
// Test FROST DKG round 2 works
func TestDkgRound2Works(t *testing.T) {
// Prepare Dkg Round1 output
p1, _, bcast1, bcast2, _, p2psend2 := PrepareRound2Input(t)
// Actual Test
require.NotNil(t, bcast1)
require.NotNil(t, bcast2)
require.NotNil(t, p2psend2[1])
bcast := make(map[uint32]*Round1Bcast)
p2p := make(map[uint32]*sharing.ShamirShare)
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
round2Out, err := p1.Round2(bcast, p2p)
require.NoError(t, err)
require.NotNil(t, round2Out)
require.NotNil(t, p1.SkShare)
require.NotNil(t, p1.VkShare)
require.NotNil(t, p1.VerificationKey)
require.NotNil(t, p1.otherParticipantShares)
}
// Test FROST DKG round 2 repeat call
func TestDkgRound2RepeatCall(t *testing.T) {
// Prepare round 1 output
p1, _, bcast1, bcast2, _, p2psend2 := PrepareRound2Input(t)
// Actual Test
require.NotNil(t, bcast1)
require.NotNil(t, bcast2)
require.NotNil(t, p2psend2[1])
bcast := make(map[uint32]*Round1Bcast)
p2p := make(map[uint32]*sharing.ShamirShare)
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
_, err := p1.Round2(bcast, p2p)
require.NoError(t, err)
_, err = p1.Round2(bcast, p2p)
require.Error(t, err)
}
// Test FROST Dkg Round 2 Bad Input
func TestDkgRound2BadInput(t *testing.T) {
// Prepare Dkg Round 1 output
p1, _, _, _, _, _ := PrepareRound2Input(t)
bcast := make(map[uint32]*Round1Bcast)
p2p := make(map[uint32]*sharing.ShamirShare)
// Test empty bcast and p2p
_, err := p1.Round2(bcast, p2p)
require.Error(t, err)
// Test nil bcast and p2p
p1, _, _, _, _, _ = PrepareRound2Input(t)
_, err = p1.Round2(nil, nil)
require.Error(t, err)
// Test tampered input bcast and p2p
p1, _, bcast1, bcast2, _, p2psend2 := PrepareRound2Input(t)
bcast = make(map[uint32]*Round1Bcast)
p2p = make(map[uint32]*sharing.ShamirShare)
// Tamper p2psend2 by doubling the value
tmp, _ := testCurve.Scalar.SetBytes(p2psend2[1].Value)
p2psend2[1].Value = tmp.Double().Bytes()
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
_, err = p1.Round2(bcast, p2p)
require.Error(t, err)
}
// Test full round works
func TestFullDkgRoundsWorks(t *testing.T) {
// Initiate two participants and running round 1
p1, p2, bcast1, bcast2, p2psend1, p2psend2 := PrepareRound2Input(t)
bcast := make(map[uint32]*Round1Bcast)
p2p1 := make(map[uint32]*sharing.ShamirShare)
p2p2 := make(map[uint32]*sharing.ShamirShare)
bcast[1] = bcast1
bcast[2] = bcast2
p2p1[2] = p2psend2[1]
p2p2[1] = p2psend1[2]
// Running round 2
round2Out1, _ := p1.Round2(bcast, p2p1)
round2Out2, _ := p2.Round2(bcast, p2p2)
require.Equal(t, round2Out1.VerificationKey, round2Out2.VerificationKey)
s, _ := sharing.NewShamir(2, 2, testCurve)
sk, err := s.Combine(&sharing.ShamirShare{Id: p1.Id, Value: p1.SkShare.Bytes()},
&sharing.ShamirShare{Id: p2.Id, Value: p2.SkShare.Bytes()})
require.NoError(t, err)
vk := testCurve.ScalarBaseMult(sk)
require.True(t, vk.Equal(p1.VerificationKey))
}