Skip to content

Commit

Permalink
improve readability of NewKeySharesFromBackup
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Aug 27, 2024
1 parent 564cdbe commit 6af7893
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions primitives/shamir/keyshares.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,52 @@ type KeyShares struct {
Integrity string
}

// decodeShare decodes the share from the backup format
func decodeShare(share string) (*big.Int, *big.Int, int, string, error) {
components := strings.Split(share, ".")
if len(components) != 4 {
err := fmt.Errorf("invalid share format. Expected format: \"x.y.t.i\" - received %s", share)
return nil, nil, 0, "", err
}
// base64 decode x and y
decodedX, err := base64.StdEncoding.DecodeString(components[0])
if err != nil {
return nil, nil, 0, "", err
}
decodedY, err := base64.StdEncoding.DecodeString(components[1])
if err != nil {
return nil, nil, 0, "", err
}
var x *big.Int = big.NewInt(0).SetBytes(decodedX)
var y *big.Int = big.NewInt(0).SetBytes(decodedY)

t := components[2]
if t == "" {
return nil, nil, 0, "", fmt.Errorf("threshold not found")
}
i := components[3]
if i == "" {
return nil, nil, 0, "", fmt.Errorf("integrity not found")
}
tInt, err := strconv.Atoi(t)
if err != nil {
return nil, nil, 0, "", err
}
return x, y, tInt, i, nil
}

// NewKeySharesFromBackupFormat creates a new KeyShares object from a backup
func NewKeySharesFromBackupFormat(shares []string) (keyShares *KeyShares, error error) {
var threshold int = 0
var integrity string = ""
points := make([]*PointInFiniteField, 0)
for idx, share := range shares {
shareParts := strings.Split(share, ".")
if len(shareParts) != 4 {
return nil, fmt.Errorf("invalid share format in share %d. Expected format: \"x.y.t.i\" - received %s", idx, share)
}
// convert parts to bigints
var x *big.Int = big.NewInt(0)
var y *big.Int = big.NewInt(0)
// base64 decode x and y
decodedX, err := base64.StdEncoding.DecodeString(shareParts[0])
if err != nil {
return nil, err
}

decodedY, err := base64.StdEncoding.DecodeString(shareParts[1])
x, y, tInt, i, err := decodeShare(share)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to decode share %d: %e", idx, err)

Check failure on line 60 in primitives/shamir/keyshares.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/go-sdk/go-sdk)

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

x.SetBytes(decodedX)
y.SetBytes(decodedY)

t := shareParts[2]
i := shareParts[3]
if t == "" {
return nil, fmt.Errorf("threshold not found in share %d", idx)
}
if i == "" {
return nil, fmt.Errorf("integrity not found in share %d", idx)
}
tInt, err := strconv.Atoi(t)
if err != nil {
return nil, err
}
if idx != 0 && threshold != tInt {
return nil, fmt.Errorf("threshold mismatch in share %d", idx)
}
Expand Down

0 comments on commit 6af7893

Please sign in to comment.