sonr/crypto/ot/extension/kos/kos_test.go

142 lines
4.8 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
package kos
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/require"
"github.com/onsonr/sonr/crypto/core/curves"
"github.com/onsonr/sonr/crypto/ot/base/simplest"
"github.com/onsonr/sonr/crypto/ot/ottest"
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 TestBinaryMult(t *testing.T) {
for i := 0; i < 100; i++ {
temp := make([]byte, 32)
_, err := rand.Read(temp)
require.NoError(t, err)
expected := make([]byte, 32)
copy(expected, temp)
// this test is based on Fermat's little theorem.
// the multiplicative group of units of a finite field has order |F| - 1
// (in fact, it's necessarily cyclic; see e.g. https://math.stackexchange.com/a/59911, but this test doesn't rely on that fact)
// thus raising any element to the |F|th power should yield that element itself.
// this is a good test because it relies on subtle facts about the field structure, and will fail if anything goes wrong.
for j := 0; j < 256; j++ {
expected = binaryFieldMul(expected, expected)
}
require.Equal(t, temp, expected)
}
}
func TestCOTExtension(t *testing.T) {
curveInstances := []*curves.Curve{
curves.K256(),
curves.P256(),
}
for _, curve := range curveInstances {
uniqueSessionId := [simplest.DigestSize]byte{}
_, err := rand.Read(uniqueSessionId[:])
require.NoError(t, err)
baseOtSenderOutput, baseOtReceiverOutput, err := ottest.RunSimplestOT(curve, Kappa, uniqueSessionId)
require.NoError(t, err)
for i := 0; i < Kappa; i++ {
require.Equal(t, baseOtReceiverOutput.OneTimePadDecryptionKey[i], baseOtSenderOutput.OneTimePadEncryptionKeys[i][baseOtReceiverOutput.RandomChoiceBits[i]])
}
sender := NewCOtSender(baseOtReceiverOutput, curve)
receiver := NewCOtReceiver(baseOtSenderOutput, curve)
choice := [COtBlockSizeBytes]byte{} // receiver's input, namely choice vector. just random
_, err = rand.Read(choice[:])
require.NoError(t, err)
input := [L][OtWidth]curves.Scalar{} // sender's input, namely integer "sums" in case w_j == 1.
for i := 0; i < L; i++ {
for j := 0; j < OtWidth; j++ {
input[i][j] = curve.Scalar.Random(rand.Reader)
require.NoError(t, err)
}
}
firstMessage, err := receiver.Round1Initialize(uniqueSessionId, choice)
require.NoError(t, err)
responseTau, err := sender.Round2Transfer(uniqueSessionId, input, firstMessage)
require.NoError(t, err)
err = receiver.Round3Transfer(responseTau)
require.NoError(t, err)
for j := 0; j < L; j++ {
bit := simplest.ExtractBitFromByteVector(choice[:], j) == 1
for k := 0; k < OtWidth; k++ {
temp := sender.OutputAdditiveShares[j][k].Add(receiver.OutputAdditiveShares[j][k])
if bit {
require.Equal(t, temp, input[j][k])
} else {
require.Equal(t, temp, curve.Scalar.Zero())
}
}
}
}
}
func TestCOTExtensionStreaming(t *testing.T) {
curve := curves.K256()
hashKeySeed := [simplest.DigestSize]byte{}
_, err := rand.Read(hashKeySeed[:])
require.NoError(t, err)
baseOtReceiver, err := simplest.NewReceiver(curve, Kappa, hashKeySeed)
require.NoError(t, err)
sender := NewCOtSender(baseOtReceiver.Output, curve)
baseOtSender, err := simplest.NewSender(curve, Kappa, hashKeySeed)
require.NoError(t, err)
receiver := NewCOtReceiver(baseOtSender.Output, curve)
// first run the seed OT
senderPipe, receiverPipe := simplest.NewPipeWrappers()
errorsChannel := make(chan error, 2)
go func() {
errorsChannel <- simplest.SenderStreamOTRun(baseOtSender, senderPipe)
}()
go func() {
errorsChannel <- simplest.ReceiverStreamOTRun(baseOtReceiver, receiverPipe)
}()
for i := 0; i < 2; i++ {
require.Nil(t, <-errorsChannel)
}
for i := 0; i < Kappa; i++ {
require.Equal(t, baseOtReceiver.Output.OneTimePadDecryptionKey[i], baseOtSender.Output.OneTimePadEncryptionKeys[i][baseOtReceiver.Output.RandomChoiceBits[i]])
}
// begin test of cOT extension. first populate both parties' inputs randomly
choice := [COtBlockSizeBytes]byte{} // receiver's input, namely choice vector. just random
_, err = rand.Read(choice[:])
require.NoError(t, err)
input := [L][OtWidth]curves.Scalar{} // sender's input, namely integer "sums" in case w_j == 1. random for the test
for i := 0; i < L; i++ {
for j := 0; j < OtWidth; j++ {
input[i][j] = curve.Scalar.Random(rand.Reader)
require.NoError(t, err)
}
}
// now actually run it, stream-wise
go func() {
errorsChannel <- SenderStreamCOtRun(sender, hashKeySeed, input, receiverPipe)
}()
go func() {
errorsChannel <- ReceiverStreamCOtRun(receiver, hashKeySeed, choice, senderPipe)
}()
for i := 0; i < 2; i++ {
require.Nil(t, <-errorsChannel)
}
for j := 0; j < L; j++ {
bit := simplest.ExtractBitFromByteVector(choice[:], j) == 1
for k := 0; k < OtWidth; k++ {
temp := sender.OutputAdditiveShares[j][k].Add(receiver.OutputAdditiveShares[j][k])
if bit {
require.Equal(t, temp, input[j][k])
} else {
require.Equal(t, temp, curve.Scalar.Zero())
}
}
}
}