sonr/crypto/sharing/shamir_test.go

187 lines
4.6 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 sharing
import (
"bytes"
crand "crypto/rand"
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
"github.com/onsonr/sonr/crypto/core/curves"
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 TestShamirSplitInvalidArgs(t *testing.T) {
curve := curves.ED25519()
_, err := NewShamir(0, 0, curve)
require.NotNil(t, err)
_, err = NewShamir(3, 2, curve)
require.NotNil(t, err)
_, err = NewShamir(1, 10, curve)
require.NotNil(t, err)
scheme, err := NewShamir(2, 3, curve)
require.Nil(t, err)
require.NotNil(t, scheme)
_, err = scheme.Split(curve.NewScalar(), crand.Reader)
require.NotNil(t, err)
}
func TestShamirCombineNoShares(t *testing.T) {
curve := curves.ED25519()
scheme, err := NewShamir(2, 3, curve)
require.Nil(t, err)
require.NotNil(t, scheme)
_, err = scheme.Combine()
require.NotNil(t, err)
}
func TestShamirCombineDuplicateShare(t *testing.T) {
curve := curves.ED25519()
scheme, err := NewShamir(2, 3, curve)
require.Nil(t, err)
require.NotNil(t, scheme)
_, err = scheme.Combine([]*ShamirShare{
{
Id: 1,
Value: curve.NewScalar().New(3).Bytes(),
},
{
Id: 1,
Value: curve.NewScalar().New(3).Bytes(),
},
}...)
require.NotNil(t, err)
}
func TestShamirCombineBadIdentifier(t *testing.T) {
curve := curves.ED25519()
scheme, err := NewShamir(2, 3, curve)
require.Nil(t, err)
require.NotNil(t, scheme)
shares := []*ShamirShare{
{
Id: 0,
Value: curve.NewScalar().New(3).Bytes(),
},
{
Id: 2,
Value: curve.NewScalar().New(3).Bytes(),
},
}
_, err = scheme.Combine(shares...)
require.NotNil(t, err)
shares[0] = &ShamirShare{
Id: 4,
Value: curve.NewScalar().New(3).Bytes(),
}
_, err = scheme.Combine(shares...)
require.NotNil(t, err)
}
func TestShamirCombineSingle(t *testing.T) {
curve := curves.ED25519()
scheme, err := NewShamir(2, 3, curve)
require.Nil(t, err)
require.NotNil(t, scheme)
shares, err := scheme.Split(curve.NewScalar().Hash([]byte("test")), crand.Reader)
require.Nil(t, err)
require.NotNil(t, shares)
secret, err := scheme.Combine(shares...)
require.Nil(t, err)
require.Equal(t, secret, curve.NewScalar().Hash([]byte("test")))
}
// Test ComputeL function to compute Lagrange coefficients.
func TestShamirComputeL(t *testing.T) {
curve := curves.ED25519()
scheme, err := NewShamir(2, 2, curve)
require.Nil(t, err)
require.NotNil(t, scheme)
secret := curve.Scalar.Hash([]byte("test"))
shares, err := scheme.Split(secret, crand.Reader)
require.Nil(t, err)
require.NotNil(t, shares)
identities := make([]uint32, 0)
for _, xi := range shares {
identities = append(identities, xi.Id)
}
lCoeffs, err := scheme.LagrangeCoeffs(identities)
require.Nil(t, err)
require.NotNil(t, lCoeffs)
// Checking we can reconstruct the same secret using Lagrange coefficients.
result := curve.NewScalar()
for _, r := range shares {
rc, _ := curve.Scalar.SetBytes(r.Value)
result = result.Add(rc.Mul(lCoeffs[r.Id]))
}
require.Equal(t, result.Bytes(), secret.Bytes())
}
func TestShamirAllCombinations(t *testing.T) {
curve := curves.ED25519()
scheme, err := NewShamir(3, 5, curve)
require.Nil(t, err)
require.NotNil(t, scheme)
secret := curve.Scalar.Hash([]byte("test"))
shares, err := scheme.Split(secret, crand.Reader)
require.Nil(t, err)
require.NotNil(t, shares)
// There are 5*4*3 possible combinations
for i := 0; i < 5; i++ {
for j := 0; j < 5; j++ {
if i == j {
continue
}
for k := 0; k < 5; k++ {
if i == k || j == k {
continue
}
rSecret, err := scheme.Combine(shares[i], shares[j], shares[k])
require.Nil(t, err)
require.NotNil(t, rSecret)
require.Equal(t, rSecret, secret)
}
}
}
}
// Ensures that ShamirShare's un/marshal successfully.
func TestMarshalJsonRoundTrip(t *testing.T) {
curve := curves.ED25519()
shares := []ShamirShare{
{0, curve.Scalar.New(300).Bytes()},
{2, curve.Scalar.New(300000).Bytes()},
{20, curve.Scalar.New(12812798).Bytes()},
{31, curve.Scalar.New(17).Bytes()},
{57, curve.Scalar.New(5066680).Bytes()},
{128, curve.Scalar.New(3005).Bytes()},
{19, curve.Scalar.New(317).Bytes()},
{7, curve.Scalar.New(323).Bytes()},
{222, curve.NewScalar().New(-1).Bytes()},
}
// Run all the tests!
for _, in := range shares {
input, err := json.Marshal(in)
require.NoError(t, err)
require.NotNil(t, input)
// Unmarshal and test
out := &ShamirShare{}
// out.Value = curve.NewScalar()
err = json.Unmarshal(input, &out)
require.NoError(t, err)
require.Equal(t, in.Id, out.Id)
require.Equal(t, bytes.Compare(in.Value, out.Value), 0)
}
}