mirror of
https://github.com/onsonr/sonr.git
synced 2025-03-10 21:09:11 +00:00
- **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**
382 lines
11 KiB
Go
Executable File
382 lines
11 KiB
Go
Executable File
//
|
|
// Copyright Coinbase, Inc. All Rights Reserved.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package gennaro
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/onsonr/sonr/crypto/core/curves"
|
|
v1 "github.com/onsonr/sonr/crypto/sharing/v1"
|
|
)
|
|
|
|
func TestParticipantRound1Works(t *testing.T) {
|
|
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
|
|
require.NoError(t, err)
|
|
bcast, p2psend, err := p1.Round1(nil)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, bcast)
|
|
require.NotNil(t, p2psend)
|
|
require.Equal(t, len(p2psend), 1)
|
|
require.Equal(t, len(bcast), 2)
|
|
require.NotNil(t, p1.pedersenResult)
|
|
require.Equal(t, p1.round, 2)
|
|
_, ok := p2psend[2]
|
|
require.True(t, ok)
|
|
}
|
|
|
|
func TestParticipantRound1RepeatCall(t *testing.T) {
|
|
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
|
|
require.NoError(t, err)
|
|
_, _, err = p1.Round1(nil)
|
|
require.NoError(t, err)
|
|
_, _, err = p1.Round1(nil)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestParticipantRound1BadSecret(t *testing.T) {
|
|
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 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) (*Participant, *Participant, Round1Bcast, Round1Bcast, Round1P2PSend) {
|
|
// Prepare round 1 output of 2 participants
|
|
p1, err := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
|
|
require.NoError(t, err)
|
|
require.Equal(t, p1.otherParticipantShares[2].Id, uint32(2))
|
|
p2, err := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
|
|
require.NoError(t, err)
|
|
require.Equal(t, p2.otherParticipantShares[1].Id, uint32(1))
|
|
bcast1, _, _ := p1.Round1(nil)
|
|
bcast2, p2psend2, _ := p2.Round1(nil)
|
|
return p1, p2, bcast1, bcast2, p2psend2
|
|
}
|
|
|
|
// Test Gennaro DKG round2 works
|
|
func TestParticipantRound2Works(t *testing.T) {
|
|
// Prepare Dkg 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]*Round1P2PSendPacket)
|
|
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.Equal(t, len(round2Out), 2)
|
|
require.NotNil(t, p1.skShare)
|
|
require.Equal(t, p1.round, 3)
|
|
require.NotNil(t, p1.otherParticipantShares)
|
|
}
|
|
|
|
// Test Gennaro DKG round 2 repeat call
|
|
func TestParticipantRound2RepeatCall(t *testing.T) {
|
|
// Prepare Dkg 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]*Round1P2PSendPacket)
|
|
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 Gennaro Dkg Round 2 Bad Input
|
|
func TestParticipantRound2BadInput(t *testing.T) {
|
|
// Prepare Dkg Round 1 output
|
|
p1, _, _, _, _ := PrepareRound2Input(t)
|
|
bcast := make(map[uint32]Round1Bcast)
|
|
p2p := make(map[uint32]*Round1P2PSendPacket)
|
|
|
|
// 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]*Round1P2PSendPacket)
|
|
|
|
// Tamper bcast1 and p2psend2 by doubling their value
|
|
bcast1[1].Y = bcast1[1].Y.Add(bcast1[1].Y, bcast1[1].Y)
|
|
p2psend2[1].SecretShare.Value = p2psend2[1].SecretShare.Value.Add(p2psend2[1].SecretShare.Value)
|
|
bcast[1] = bcast1
|
|
bcast[2] = bcast2
|
|
p2p[2] = p2psend2[1]
|
|
_, err = p1.Round2(bcast, p2p)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func PrepareRound3Input(t *testing.T) (*Participant, *Participant, map[uint32]Round2Bcast) {
|
|
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
|
|
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
|
|
bcast1, p2psend1, _ := p1.Round1(nil)
|
|
bcast2, p2psend2, _ := p2.Round1(nil)
|
|
bcast := make(map[uint32]Round1Bcast)
|
|
p2p1 := make(map[uint32]*Round1P2PSendPacket)
|
|
p2p2 := make(map[uint32]*Round1P2PSendPacket)
|
|
bcast[1] = bcast1
|
|
bcast[2] = bcast2
|
|
p2p1[2] = p2psend2[1]
|
|
p2p2[1] = p2psend1[2]
|
|
round2Out1, _ := p1.Round2(bcast, p2p1)
|
|
round2Out2, _ := p2.Round2(bcast, p2p2)
|
|
round3Input := make(map[uint32]Round2Bcast)
|
|
round3Input[1] = round2Out1
|
|
round3Input[2] = round2Out2
|
|
return p1, p2, round3Input
|
|
}
|
|
|
|
// Test Gennaro Dkg Round 3 Works
|
|
func TestParticipantRound3Works(t *testing.T) {
|
|
// Prepare Gennaro Dkg Round 3 Input
|
|
p1, p2, round3Input := PrepareRound3Input(t)
|
|
|
|
// Actual Test
|
|
round3Out1, _, err := p1.Round3(round3Input)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, round3Out1)
|
|
round3Out2, _, err := p2.Round3(round3Input)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, round3Out2)
|
|
require.Equal(t, p1.round, 4)
|
|
require.Equal(t, p2.round, 4)
|
|
require.Equal(t, p1.verificationKey, p2.verificationKey)
|
|
|
|
// Test if shares recombine properly
|
|
s, _ := v1.NewShamir(2, 2, curves.NewField(btcec.S256().N))
|
|
sk, err := s.Combine(&v1.ShamirShare{Identifier: p1.id, Value: p1.skShare},
|
|
&v1.ShamirShare{Identifier: p2.id, Value: p2.skShare})
|
|
require.NoError(t, err)
|
|
|
|
// Test verification keys are G * sk
|
|
x, y := btcec.S256().ScalarBaseMult(sk)
|
|
tmp := &curves.EcPoint{
|
|
Curve: btcec.S256(),
|
|
X: x,
|
|
Y: y,
|
|
}
|
|
require.True(t, tmp.Equals(p1.verificationKey))
|
|
require.True(t, tmp.Equals(p2.verificationKey))
|
|
}
|
|
|
|
// Test Gennaro Dkg Round3 Repeat Call
|
|
func TestParticipantRound3RepeatCall(t *testing.T) {
|
|
// Prepare Round 3 Input
|
|
p1, _, round3Input := PrepareRound3Input(t)
|
|
|
|
// Actual Test
|
|
_, _, err := p1.Round3(round3Input)
|
|
require.NoError(t, err)
|
|
_, _, err = p1.Round3(round3Input)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
// Test Gennaro DKG Round 3 Bad Input
|
|
func TestParticipantRound3BadInput(t *testing.T) {
|
|
// Test empty round 3 input
|
|
p1, _, _ := PrepareRound3Input(t)
|
|
emptyInput := make(map[uint32]Round2Bcast)
|
|
_, _, err := p1.Round3(emptyInput)
|
|
require.Error(t, err)
|
|
|
|
// Test nil round 3 input
|
|
p1, _, _ = PrepareRound3Input(t)
|
|
_, _, err = p1.Round3(nil)
|
|
require.Error(t, err)
|
|
|
|
// Test tampered round 3 input
|
|
p1, _, round3Input := PrepareRound3Input(t)
|
|
// Tamper participant2's broadcast
|
|
round3Input[2][0], _ = round3Input[2][0].Add(round3Input[2][1])
|
|
_, _, err = p1.Round3(round3Input)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
// Test Gennaro Dkg Round 4 Works
|
|
func TestParticipantRound4Works(t *testing.T) {
|
|
// Prepare Gennaro Dkg Round 3 Input
|
|
p1, p2, round3Input := PrepareRound3Input(t)
|
|
round3Out1, _, err := p1.Round3(round3Input)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, round3Out1)
|
|
round3Out2, _, err := p2.Round3(round3Input)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, round3Out2)
|
|
|
|
// Actual test
|
|
publicShares1, err := p1.Round4()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, publicShares1)
|
|
publicShares2, err := p2.Round4()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, publicShares2)
|
|
|
|
require.Equal(t, publicShares1, publicShares2)
|
|
}
|
|
|
|
// Test Gennaro Dkg Round 4 Works
|
|
func TestParticipantRound4RepeatCall(t *testing.T) {
|
|
// Prepare Gennaro Dkg Round 3 Input
|
|
p1, p2, round3Input := PrepareRound3Input(t)
|
|
round3Out1, _, err := p1.Round3(round3Input)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, round3Out1)
|
|
round3Out2, _, err := p2.Round3(round3Input)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, round3Out2)
|
|
|
|
// Actual test
|
|
publicShares1, err := p1.Round4()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, publicShares1)
|
|
publicShares2, err := p1.Round4()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, publicShares2)
|
|
|
|
require.Equal(t, publicShares1, publicShares2)
|
|
}
|
|
|
|
// Test all Gennaro DKG rounds
|
|
func TestAllGennaroDkgRounds(t *testing.T) {
|
|
// Initiate two participants
|
|
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
|
|
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
|
|
|
|
// Running round 1
|
|
bcast1, p2psend1, _ := p1.Round1(nil)
|
|
bcast2, p2psend2, _ := p2.Round1(nil)
|
|
bcast := make(map[uint32]Round1Bcast)
|
|
p2p1 := make(map[uint32]*Round1P2PSendPacket)
|
|
p2p2 := make(map[uint32]*Round1P2PSendPacket)
|
|
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)
|
|
round3Input := make(map[uint32]Round2Bcast)
|
|
round3Input[1] = round2Out1
|
|
round3Input[2] = round2Out2
|
|
|
|
// Running round 3
|
|
round3Out1, _, _ := p1.Round3(round3Input)
|
|
round3Out2, _, _ := p2.Round3(round3Input)
|
|
require.NotNil(t, round3Out1)
|
|
require.NotNil(t, round3Out2)
|
|
|
|
// Running round 4
|
|
publicShares1, _ := p1.Round4()
|
|
publicShares2, _ := p2.Round4()
|
|
|
|
// Test output of all rounds
|
|
require.Equal(t, publicShares1, publicShares2)
|
|
s, _ := v1.NewShamir(2, 2, curves.NewField(btcec.S256().N))
|
|
sk, err := s.Combine(&v1.ShamirShare{Identifier: p1.id, Value: p1.skShare},
|
|
&v1.ShamirShare{Identifier: p2.id, Value: p2.skShare})
|
|
require.NoError(t, err)
|
|
|
|
x, y := btcec.S256().ScalarBaseMult(sk)
|
|
tmp := &curves.EcPoint{
|
|
Curve: btcec.S256(),
|
|
X: x,
|
|
Y: y,
|
|
}
|
|
require.True(t, tmp.Equals(p1.verificationKey))
|
|
require.True(t, tmp.Equals(p2.verificationKey))
|
|
}
|
|
|
|
// Ensure correct functioning when input is missing
|
|
func TestParticipant2BadInput(t *testing.T) {
|
|
//
|
|
// Setup
|
|
//
|
|
p1, _ := NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 2)
|
|
p2, _ := NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 1)
|
|
bcast1, _, _ := p1.Round1(nil)
|
|
bcast2, p2psend2, _ := p2.Round1(nil)
|
|
bcast := make(map[uint32]Round1Bcast)
|
|
p2p1 := make(map[uint32]*Round1P2PSendPacket)
|
|
bcast[1] = bcast1
|
|
bcast[2] = bcast2
|
|
// Exclude p2p 2>1
|
|
p2p1[4] = p2psend2[1]
|
|
|
|
// Run round 2
|
|
_, err := p1.Round2(bcast, p2p1)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestValidIDs(t *testing.T) {
|
|
err := fmt.Errorf("")
|
|
tests := []struct {
|
|
name string
|
|
in []uint32
|
|
expected error
|
|
}{
|
|
{"positive-1,2", []uint32{1, 2}, nil},
|
|
{"positive-2,1", []uint32{2, 1}, nil},
|
|
{"positive-1-10", []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, nil},
|
|
{"positive-1-10-random", []uint32{10, 9, 6, 5, 3, 2, 8, 7, 1, 4}, nil},
|
|
{"negative-1,3", []uint32{1, 3}, err},
|
|
{"negative-1-10-missing-5", []uint32{10, 9, 6, 3, 2, 8, 7, 1, 4}, err},
|
|
}
|
|
// Run all the tests!
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
err := validIds(test.in)
|
|
if test.expected == nil {
|
|
require.NoError(t, err)
|
|
} else {
|
|
require.Error(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Test newParticipant with arbitrary IDs
|
|
func TestParticipantArbitraryIds(t *testing.T) {
|
|
_, err := NewParticipant(3, 2, testGenerator, curves.NewK256Scalar(), 4)
|
|
require.Error(t, err)
|
|
_, err = NewParticipant(0, 2, testGenerator, curves.NewK256Scalar(), 1)
|
|
require.Error(t, err)
|
|
_, err = NewParticipant(2, 2, testGenerator, curves.NewK256Scalar(), 2, 3, 5)
|
|
require.Error(t, err)
|
|
_, err = NewParticipant(1, 2, testGenerator, curves.NewK256Scalar(), 4)
|
|
require.Error(t, err)
|
|
}
|