2024-11-23 01:28:58 -05:00
|
|
|
package mpc
|
|
|
|
|
|
|
|
import (
|
2024-12-13 15:10:27 -05:00
|
|
|
"github.com/ipfs/kubo/client/rpc"
|
2024-12-02 14:27:18 -05:00
|
|
|
"github.com/onsonr/sonr/crypto/core/curves"
|
|
|
|
"github.com/onsonr/sonr/crypto/core/protocol"
|
|
|
|
"github.com/onsonr/sonr/crypto/tecdsa/dklsv1"
|
2024-11-23 01:28:58 -05:00
|
|
|
)
|
|
|
|
|
2024-12-13 15:10:27 -05:00
|
|
|
// GenEnclave generates a new MPC keyshare
|
|
|
|
func GenEnclave() (Enclave, error) {
|
2024-11-23 01:28:58 -05:00
|
|
|
curve := curves.K256()
|
|
|
|
valKs := dklsv1.NewAliceDkg(curve, protocol.Version1)
|
|
|
|
userKs := dklsv1.NewBobDkg(curve, protocol.Version1)
|
2024-12-05 20:36:58 -05:00
|
|
|
aErr, bErr := RunProtocol(userKs, valKs)
|
2024-11-23 01:28:58 -05:00
|
|
|
if err := checkIteratedErrors(aErr, bErr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
valRes, err := valKs.Result(protocol.Version1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
userRes, err := userKs.Result(protocol.Version1)
|
2024-11-23 01:28:58 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
return initKeyEnclave(valRes, userRes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenEnclaveIPFS generates a new MPC keyshare
|
|
|
|
func GenEnclaveIPFS(ipc *rpc.HttpApi) (Enclave, error) {
|
|
|
|
curve := curves.K256()
|
|
|
|
valKs := dklsv1.NewAliceDkg(curve, protocol.Version1)
|
|
|
|
userKs := dklsv1.NewBobDkg(curve, protocol.Version1)
|
|
|
|
aErr, bErr := RunProtocol(userKs, valKs)
|
|
|
|
if err := checkIteratedErrors(aErr, bErr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
valRes, err := valKs.Result(protocol.Version1)
|
2024-11-23 01:28:58 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
userRes, err := userKs.Result(protocol.Version1)
|
2024-11-23 01:28:58 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
e, err := initKeyEnclave(valRes, userRes)
|
2024-12-06 21:31:20 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
return addEnclaveIPFS(e, ipc)
|
2024-11-23 01:28:58 -05:00
|
|
|
}
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
// ExecuteSigning runs the MPC signing protocol
|
2024-12-13 15:10:27 -05:00
|
|
|
func ExecuteSigning(signFuncVal SignFunc, signFuncUser SignFunc) ([]byte, error) {
|
2024-12-05 20:36:58 -05:00
|
|
|
aErr, bErr := RunProtocol(signFuncVal, signFuncUser)
|
2024-11-23 01:28:58 -05:00
|
|
|
if err := checkIteratedErrors(aErr, bErr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out, err := signFuncUser.Result(protocol.Version1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
s, err := dklsv1.DecodeSignature(out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sig, err := serializeSignature(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sig, nil
|
2024-11-23 01:28:58 -05:00
|
|
|
}
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
// ExecuteRefresh runs the MPC refresh protocol
|
2024-12-13 15:10:27 -05:00
|
|
|
func ExecuteRefresh(refreshFuncVal RefreshFunc, refreshFuncUser RefreshFunc) (*KeyEnclave, error) {
|
2024-12-05 20:36:58 -05:00
|
|
|
aErr, bErr := RunProtocol(refreshFuncVal, refreshFuncUser)
|
2024-11-23 01:28:58 -05:00
|
|
|
if err := checkIteratedErrors(aErr, bErr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
valRefreshResult, err := refreshFuncVal.Result(protocol.Version1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userRefreshResult, err := refreshFuncUser.Result(protocol.Version1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
return initKeyEnclave(valRefreshResult, userRefreshResult)
|
2024-11-23 01:28:58 -05:00
|
|
|
}
|
2024-12-09 11:49:20 -05:00
|
|
|
|
|
|
|
// For DKG bob starts first. For refresh and sign, Alice starts first.
|
|
|
|
func RunProtocol(firstParty protocol.Iterator, secondParty protocol.Iterator) (error, error) {
|
|
|
|
var (
|
|
|
|
message *protocol.Message
|
|
|
|
aErr error
|
|
|
|
bErr error
|
|
|
|
)
|
|
|
|
|
|
|
|
for aErr != protocol.ErrProtocolFinished || bErr != protocol.ErrProtocolFinished {
|
|
|
|
// Crank each protocol forward one iteration
|
|
|
|
message, bErr = firstParty.Next(message)
|
|
|
|
if bErr != nil && bErr != protocol.ErrProtocolFinished {
|
|
|
|
return nil, bErr
|
|
|
|
}
|
|
|
|
|
|
|
|
message, aErr = secondParty.Next(message)
|
|
|
|
if aErr != nil && aErr != protocol.ErrProtocolFinished {
|
|
|
|
return aErr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aErr, bErr
|
|
|
|
}
|