sonr/crypto/dkg/gennaro/rounds_test.go

382 lines
11 KiB
Go
Raw Normal View History

feature/1114 implement account interface (#1167) - **refactor: move session-related code to middleware package** - **refactor: update PKL build process and adjust related configurations** - **feat: integrate base.cosmos.v1 Genesis module** - **refactor: pass session context to modal rendering functions** - **refactor: move nebula package to app directory and update templ version** - **refactor: Move home section video view to dedicated directory** - **refactor: remove unused views file** - **refactor: move styles and UI components to global scope** - **refactor: Rename images.go to cdn.go** - **feat: Add Empty State Illustrations** - **refactor: Consolidate Vault Index Logic** - **fix: References to App.wasm and remove Vault Directory embedded CDN files** - **refactor: Move CDN types to Models** - **fix: Correct line numbers in templ error messages for arch_templ.go** - **refactor: use common types for peer roles** - **refactor: move common types and ORM to a shared package** - **fix: Config import dwn** - **refactor: move nebula directory to app** - **feat: Rebuild nebula** - **fix: correct file paths in panels templates** - **feat: Remove duplicate types** - **refactor: Move dwn to pkg/core** - **refactor: Binary Structure** - **feat: Introduce Crypto Pkg** - **fix: Broken Process Start** - **feat: Update pkg/* structure** - **feat: Refactor PKL Structure** - **build: update pkl build process** - **chore: Remove Empty Files** - **refactor: remove unused macaroon package** - **feat: Add WebAwesome Components** - **refactor: consolidate build and generation tasks into a single taskfile, remove redundant makefile targets** - **refactor: refactor server and move components to pkg/core/dwn** - **build: update go modules** - **refactor: move gateway logic into dedicated hway command** - **feat: Add KSS (Krawczyk-Song-Song) MPC cryptography module** - **feat: Implement MPC-based JWT signing and UCAN token generation** - **feat: add support for MPC-based JWT signing** - **feat: Implement MPC-based UCAN capabilities for smart accounts** - **feat: add address field to keyshareSource** - **feat: Add comprehensive MPC test suite for keyshares, UCAN tokens, and token attenuations** - **refactor: improve MPC keyshare management and signing process** - **feat: enhance MPC capability hierarchy documentation** - **refactor: rename GenerateKeyshares function to NewKeyshareSource for clarity** - **refactor: remove unused Ethereum address computation** - **feat: Add HasHandle and IsAuthenticated methods to HTTPContext** - **refactor: Add context.Context support to session HTTPContext** - **refactor: Resolve context interface conflicts in HTTPContext** - **feat: Add session ID context key and helper functions** - **feat: Update WebApp Page Rendering** - **refactor: Simplify context management by using single HTTPContext key** - **refactor: Simplify HTTPContext creation and context management in session middleware** - **refactor: refactor session middleware to use a single data structure** - **refactor: Simplify HTTPContext implementation and session data handling** - **refactor: Improve session context handling and prevent nil pointer errors** - **refactor: Improve session context handling with nil safety and type support** - **refactor: improve session data injection** - **feat: add full-screen modal component and update registration flow** - **chore: add .air.toml to .gitignore** - **feat: add Air to devbox and update dependencies**
2024-11-23 01:28:58 -05:00
//
// 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"
feature/1114 implement account interface (#1167) - **refactor: move session-related code to middleware package** - **refactor: update PKL build process and adjust related configurations** - **feat: integrate base.cosmos.v1 Genesis module** - **refactor: pass session context to modal rendering functions** - **refactor: move nebula package to app directory and update templ version** - **refactor: Move home section video view to dedicated directory** - **refactor: remove unused views file** - **refactor: move styles and UI components to global scope** - **refactor: Rename images.go to cdn.go** - **feat: Add Empty State Illustrations** - **refactor: Consolidate Vault Index Logic** - **fix: References to App.wasm and remove Vault Directory embedded CDN files** - **refactor: Move CDN types to Models** - **fix: Correct line numbers in templ error messages for arch_templ.go** - **refactor: use common types for peer roles** - **refactor: move common types and ORM to a shared package** - **fix: Config import dwn** - **refactor: move nebula directory to app** - **feat: Rebuild nebula** - **fix: correct file paths in panels templates** - **feat: Remove duplicate types** - **refactor: Move dwn to pkg/core** - **refactor: Binary Structure** - **feat: Introduce Crypto Pkg** - **fix: Broken Process Start** - **feat: Update pkg/* structure** - **feat: Refactor PKL Structure** - **build: update pkl build process** - **chore: Remove Empty Files** - **refactor: remove unused macaroon package** - **feat: Add WebAwesome Components** - **refactor: consolidate build and generation tasks into a single taskfile, remove redundant makefile targets** - **refactor: refactor server and move components to pkg/core/dwn** - **build: update go modules** - **refactor: move gateway logic into dedicated hway command** - **feat: Add KSS (Krawczyk-Song-Song) MPC cryptography module** - **feat: Implement MPC-based JWT signing and UCAN token generation** - **feat: add support for MPC-based JWT signing** - **feat: Implement MPC-based UCAN capabilities for smart accounts** - **feat: add address field to keyshareSource** - **feat: Add comprehensive MPC test suite for keyshares, UCAN tokens, and token attenuations** - **refactor: improve MPC keyshare management and signing process** - **feat: enhance MPC capability hierarchy documentation** - **refactor: rename GenerateKeyshares function to NewKeyshareSource for clarity** - **refactor: remove unused Ethereum address computation** - **feat: Add HasHandle and IsAuthenticated methods to HTTPContext** - **refactor: Add context.Context support to session HTTPContext** - **refactor: Resolve context interface conflicts in HTTPContext** - **feat: Add session ID context key and helper functions** - **feat: Update WebApp Page Rendering** - **refactor: Simplify context management by using single HTTPContext key** - **refactor: Simplify HTTPContext creation and context management in session middleware** - **refactor: refactor session middleware to use a single data structure** - **refactor: Simplify HTTPContext implementation and session data handling** - **refactor: Improve session context handling and prevent nil pointer errors** - **refactor: Improve session context handling with nil safety and type support** - **refactor: improve session data injection** - **feat: add full-screen modal component and update registration flow** - **chore: add .air.toml to .gitignore** - **feat: add Air to devbox and update dependencies**
2024-11-23 01:28:58 -05:00
)
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)
}