No commit suggestions generated

This commit is contained in:
Prad Nukala
2025-10-09 15:10:39 -04:00
commit a934caa7d3
323 changed files with 98121 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
---
aliases: [README]
tags: []
title: README
linter-yaml-title-alias: README
date created: Wednesday, April 17th 2024, 4:11:40 pm
date modified: Thursday, April 18th 2024, 8:19:25 am
---
## FROST: Flexible Round-Optimized Schnorr Threshold Signatures
This package is an implementation of the DKG part of
[FROST: Flexible Round-Optimized Schnorr Threshold Signatures](https://eprint.iacr.org/2020/852.pdf)
+153
View File
@@ -0,0 +1,153 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package frost
import (
"bytes"
crand "crypto/rand"
"encoding/gob"
"fmt"
"reflect"
"github.com/pkg/errors"
"github.com/sonr-io/sonr/crypto/core/curves"
"github.com/sonr-io/sonr/crypto/internal"
"github.com/sonr-io/sonr/crypto/sharing"
)
// Round1Bcast are values that are broadcast to all other participants
// after round1 completes
type Round1Bcast struct {
Verifiers *sharing.FeldmanVerifier
Wi, Ci curves.Scalar
}
type Round1Result struct {
Broadcast *Round1Bcast
P2P *sharing.ShamirShare
}
func (result *Round1Result) Encode() ([]byte, error) {
gob.Register(result.Broadcast.Verifiers.Commitments[0]) // just the point for now
gob.Register(result.Broadcast.Ci)
buf := &bytes.Buffer{}
enc := gob.NewEncoder(buf)
if err := enc.Encode(result); err != nil {
return nil, errors.Wrap(err, "couldn't encode round 1 broadcast")
}
return buf.Bytes(), nil
}
func (result *Round1Result) Decode(input []byte) error {
buf := bytes.NewBuffer(input)
dec := gob.NewDecoder(buf)
if err := dec.Decode(result); err != nil {
return errors.Wrap(err, "couldn't encode round 1 broadcast")
}
return nil
}
// Round1P2PSend are values that are P2PSend to all other participants
// after round1 completes
type Round1P2PSend = map[uint32]*sharing.ShamirShare
// Round1 implements dkg round 1 of FROST
func (dp *DkgParticipant) Round1(secret []byte) (*Round1Bcast, Round1P2PSend, error) {
// Make sure dkg participant is not empty
if dp == nil || dp.Curve == nil {
return nil, nil, internal.ErrNilArguments
}
// Make sure round number is correct
if dp.round != 1 {
return nil, nil, internal.ErrInvalidRound
}
// Check number of participants
if uint32(len(dp.otherParticipantShares)+1) > dp.feldman.Limit ||
uint32(len(dp.otherParticipantShares)+1) < dp.feldman.Threshold {
return nil, nil, fmt.Errorf(
"length of dp.otherParticipantShares + 1 should be equal to feldman limit",
)
}
// If secret is nil, sample a new one
// If not, check secret is valid
var s curves.Scalar
var err error
if secret == nil {
s = dp.Curve.Scalar.Random(crand.Reader)
} else {
s, err = dp.Curve.Scalar.SetBytes(secret)
if err != nil {
return nil, nil, err
}
if s.IsZero() {
return nil, nil, internal.ErrZeroValue
}
}
// Step 1 - (Aj0,...Ajt), (xi1,...,xin) <- FeldmanShare(s)
// We should validate types of Feldman curve scalar and participant's curve scalar.
if reflect.TypeOf(dp.feldman.Curve.Scalar) != reflect.TypeOf(dp.Curve.Scalar) {
return nil, nil, fmt.Errorf(
"feldman scalar should have the same type as the dkg participant scalar",
)
}
verifiers, shares, err := dp.feldman.Split(s, crand.Reader)
if err != nil {
return nil, nil, err
}
// Store Verifiers and shares
dp.verifiers = verifiers
dp.secretShares = shares
// Step 2 - Sample ki <- Z_q
ki := dp.Curve.Scalar.Random(crand.Reader)
// Step 3 - Compute Ri = ki*G
Ri := dp.Curve.ScalarBaseMult(ki)
// Step 4 - Compute Ci = H(i, CTX, g^{a_(i,0)}, R_i), where CTX is fixed context string
var msg []byte
// Append participant id
msg = append(msg, byte(dp.Id))
// Append CTX
msg = append(msg, dp.ctx)
// Append a_{i,0}*G
msg = append(msg, verifiers.Commitments[0].ToAffineCompressed()...)
// Append Ri
msg = append(msg, Ri.ToAffineCompressed()...)
// Hash the message and get Ci
ci := dp.Curve.Scalar.Hash(msg)
// Step 5 - Compute Wi = ki+a_{i,0}*c_i mod q. Note that a_{i,0} is the secret.
// Note: We have to compute scalar in the following way when using ed25519 curve, rather than scalar := dp.Scalar.Mul(s, Ci)
// there is an invalid encoding error when we compute scalar as above.
wi := s.MulAdd(ci, ki)
// Step 6 - Broadcast (Ci, Wi, Ci) to other participants
round1Bcast := &Round1Bcast{
verifiers,
wi,
ci,
}
// Step 7 - P2PSend f_i(j) to each participant Pj and keep (i, f_j(i)) for himself
p2pSend := make(Round1P2PSend, len(dp.otherParticipantShares))
for id := range dp.otherParticipantShares {
p2pSend[id] = shares[id-1]
}
// Update internal state
dp.round = 2
// return
return round1Bcast, p2pSend, nil
}
+162
View File
@@ -0,0 +1,162 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package frost
import (
"fmt"
"github.com/sonr-io/sonr/crypto/core/curves"
"github.com/sonr-io/sonr/crypto/internal"
"github.com/sonr-io/sonr/crypto/sharing"
)
// Round2Bcast are values that are broadcast to all other participants
// after round2 completes
type Round2Bcast struct {
VerificationKey curves.Point
VkShare curves.Point
}
// Round2 implements dkg round 2 of FROST
func (dp *DkgParticipant) Round2(
bcast map[uint32]*Round1Bcast,
p2psend map[uint32]*sharing.ShamirShare,
) (*Round2Bcast, error) {
// Make sure dkg participant is not empty
if dp == nil || dp.Curve == nil {
return nil, internal.ErrNilArguments
}
// Check dkg participant has the correct dkg round number
if dp.round != 2 {
return nil, internal.ErrInvalidRound
}
// Check the input is valid
if bcast == nil || p2psend == nil || len(p2psend) == 0 {
return nil, internal.ErrNilArguments
}
// Check length of bcast and p2psend
if uint32(len(bcast)) > dp.feldman.Limit || uint32(len(bcast)) < dp.feldman.Threshold-1 {
return nil, fmt.Errorf("invalid broadcast length")
}
if uint32(len(p2psend)) > dp.feldman.Limit-1 || uint32(len(p2psend)) < dp.feldman.Threshold-1 {
return nil, fmt.Errorf("invalid p2pSend length")
}
// We should validate Wi and Ci values in Round1Bcast
for id := range bcast {
// ci should be within the range 1 to q-1, q is the group order.
if bcast[id].Ci.IsZero() {
return nil, fmt.Errorf("ci should not be zero from participant %d", id)
}
}
// Validate each received commitment is on curve
for id := range bcast {
for _, com := range bcast[id].Verifiers.Commitments {
if !com.IsOnCurve() || com.IsIdentity() {
return nil, fmt.Errorf("some commitment is not on curve from participant %d", id)
}
}
}
var err error
// Step 2 - for j in 1,...,n
for id := range bcast {
// Step 3 - if j == i, continue
if id == dp.Id {
continue
}
// Step 4 - Check equation c_j = H(j, CTX, A_{j,0}, g^{w_j}*A_{j,0}^{-c_j}
// Get Aj0
Aj0 := bcast[id].Verifiers.Commitments[0]
// Compute g^{w_j}
prod1 := dp.Curve.ScalarBaseMult(bcast[id].Wi)
// Compute A_{j,0}^{-c_j}
prod2 := Aj0.Mul(bcast[id].Ci.Neg())
// We need to check Aj0 and prod2 are points on the same curve.
if !Aj0.IsOnCurve() || Aj0.IsIdentity() || !prod2.IsOnCurve() || prod2.IsIdentity() ||
Aj0.CurveName() != prod2.CurveName() {
return nil, fmt.Errorf("invalid Aj0 or prod2 which is not on the same curve")
}
if prod2 == nil {
return nil, fmt.Errorf("invalid should not be nil")
}
prod := prod1.Add(prod2)
var msg []byte
// Append participant id
msg = append(msg, byte(id))
// Append CTX
msg = append(msg, dp.ctx)
// Append Aj0
msg = append(msg, Aj0.ToAffineCompressed()...)
// Append prod
msg = append(msg, prod.ToAffineCompressed()...)
// Hash the message and get cj
cj := dp.Curve.Scalar.Hash(msg)
// Check equation
if cj.Cmp(bcast[id].Ci) != 0 {
return nil, fmt.Errorf("hash check fails for participant with id %d", id)
}
// Step 5 - FeldmanVerify
fji := p2psend[id]
if err = bcast[id].Verifiers.Verify(fji); err != nil {
return nil, fmt.Errorf("feldman verify fails for participant with id %d", id)
}
}
sk, err := dp.Curve.Scalar.SetBytes(dp.secretShares[dp.Id-1].Value)
if err != nil {
return nil, err
}
vk := dp.verifiers.Commitments[0]
// Step 6 - Compute signing key share ski = \sum_{j=1}^n xji
for id := range bcast {
if id == dp.Id {
continue
}
t2, err := dp.Curve.Scalar.SetBytes(p2psend[id].Value)
if err != nil {
return nil, err
}
sk = sk.Add(t2)
}
// Step 8 - Compute verification key vk = sum(A_{j,0}), j = 1,...,n
for id := range bcast {
if id == dp.Id {
continue
}
vk = vk.Add(bcast[id].Verifiers.Commitments[0])
}
// Store signing key share
dp.SkShare = sk
// Step 7 - Compute verification key share vki = ski*G and store
dp.VkShare = dp.Curve.ScalarBaseMult(sk)
// Store verification key
dp.VerificationKey = vk
// Update round number
dp.round = 3
// Broadcast
return &Round2Bcast{
vk,
dp.VkShare,
}, nil
}
+206
View File
@@ -0,0 +1,206 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package frost
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/sonr-io/sonr/crypto/core/curves"
"github.com/sonr-io/sonr/crypto/sharing"
)
var (
testCurve = curves.ED25519()
Ctx = "string to prevent replay attack"
)
// Test dkg round1 works for 2 participants
func TestDkgRound1Works(t *testing.T) {
p1, err := NewDkgParticipant(1, 2, Ctx, testCurve, 2)
require.NoError(t, err)
bcast, p2psend, err := p1.Round1(nil)
require.NoError(t, err)
require.NotNil(t, bcast)
require.NotNil(t, p2psend)
require.NotNil(t, p1.ctx)
require.Equal(t, len(p2psend), 1)
require.Equal(t, p1.round, 2)
_, ok := p2psend[2]
require.True(t, ok)
}
func TestDkgRound1RepeatCall(t *testing.T) {
p1, err := NewDkgParticipant(1, 2, Ctx, testCurve, 2)
require.NoError(t, err)
_, _, err = p1.Round1(nil)
require.NoError(t, err)
_, _, err = p1.Round1(nil)
require.Error(t, err)
}
func TestDkgRound1BadSecret(t *testing.T) {
p1, err := NewDkgParticipant(1, 2, Ctx, testCurve, 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,
) (*DkgParticipant, *DkgParticipant, *Round1Bcast, *Round1Bcast, Round1P2PSend, Round1P2PSend) {
// Prepare round 1 output of 2 participants
p1, err := NewDkgParticipant(1, 2, Ctx, testCurve, 2)
require.NoError(t, err)
require.Equal(t, p1.otherParticipantShares[2].Id, uint32(2))
p2, err := NewDkgParticipant(2, 2, Ctx, testCurve, 1)
require.NoError(t, err)
require.Equal(t, p2.otherParticipantShares[1].Id, uint32(1))
bcast1, p2psend1, _ := p1.Round1(nil)
bcast2, p2psend2, _ := p2.Round1(nil)
return p1, p2, bcast1, bcast2, p2psend1, p2psend2
}
// Test FROST DKG round 2 works
func TestDkgRound2Works(t *testing.T) {
// Prepare Dkg Round1 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]*sharing.ShamirShare)
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.NotNil(t, p1.SkShare)
require.NotNil(t, p1.VkShare)
require.NotNil(t, p1.VerificationKey)
require.NotNil(t, p1.otherParticipantShares)
}
// Test FROST DKG round 2 repeat call
func TestDkgRound2RepeatCall(t *testing.T) {
// Prepare 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]*sharing.ShamirShare)
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 FROST Dkg Round 2 Bad Input
func TestDkgRound2BadInput(t *testing.T) {
// Prepare Dkg Round 1 output
p1, _, _, _, _, _ := PrepareRound2Input(t)
bcast := make(map[uint32]*Round1Bcast)
p2p := make(map[uint32]*sharing.ShamirShare)
// 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]*sharing.ShamirShare)
// Tamper p2psend2 by doubling the value
tmp, _ := testCurve.Scalar.SetBytes(p2psend2[1].Value)
p2psend2[1].Value = tmp.Double().Bytes()
bcast[1] = bcast1
bcast[2] = bcast2
p2p[2] = p2psend2[1]
_, err = p1.Round2(bcast, p2p)
require.Error(t, err)
}
// Test full round works
func TestFullDkgRoundsWorks(t *testing.T) {
// Initiate two participants and running round 1
p1, p2, bcast1, bcast2, p2psend1, p2psend2 := PrepareRound2Input(t)
bcast := make(map[uint32]*Round1Bcast)
p2p1 := make(map[uint32]*sharing.ShamirShare)
p2p2 := make(map[uint32]*sharing.ShamirShare)
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)
require.Equal(t, round2Out1.VerificationKey, round2Out2.VerificationKey)
s, _ := sharing.NewShamir(2, 2, testCurve)
sk, err := s.Combine(&sharing.ShamirShare{Id: p1.Id, Value: p1.SkShare.Bytes()},
&sharing.ShamirShare{Id: p2.Id, Value: p2.SkShare.Bytes()})
require.NoError(t, err)
vk := testCurve.ScalarBaseMult(sk)
require.True(t, vk.Equal(p1.VerificationKey))
}
+70
View File
@@ -0,0 +1,70 @@
//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
// Package frost is an implementation of the DKG part of https://eprint.iacr.org/2020/852.pdf
package frost
import (
"strconv"
"github.com/sonr-io/sonr/crypto/core/curves"
"github.com/sonr-io/sonr/crypto/internal"
"github.com/sonr-io/sonr/crypto/sharing"
)
type DkgParticipant struct {
round int
Curve *curves.Curve
otherParticipantShares map[uint32]*dkgParticipantData
Id uint32
SkShare curves.Scalar
VerificationKey curves.Point
VkShare curves.Point
feldman *sharing.Feldman
verifiers *sharing.FeldmanVerifier
secretShares []*sharing.ShamirShare
ctx byte
}
type dkgParticipantData struct {
Id uint32
Share *sharing.ShamirShare
Verifiers *sharing.FeldmanVerifier
}
func NewDkgParticipant(
id, threshold uint32,
ctx string,
curve *curves.Curve,
otherParticipants ...uint32,
) (*DkgParticipant, error) {
if curve == nil || len(otherParticipants) == 0 {
return nil, internal.ErrNilArguments
}
limit := uint32(len(otherParticipants)) + 1
feldman, err := sharing.NewFeldman(threshold, limit, curve)
if err != nil {
return nil, err
}
otherParticipantShares := make(map[uint32]*dkgParticipantData, len(otherParticipants))
for _, id := range otherParticipants {
otherParticipantShares[id] = &dkgParticipantData{
Id: id,
}
}
// SetBigInt the common fixed string
ctxV, _ := strconv.Atoi(ctx)
return &DkgParticipant{
Id: id,
round: 1,
Curve: curve,
feldman: feldman,
otherParticipantShares: otherParticipantShares,
ctx: byte(ctxV),
}, nil
}