-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
errors, new api replacing string input by []byte, algorithm to split …
…the message in chunks based on prime number provided, new tests
- Loading branch information
1 parent
1e3fb59
commit fe0ce08
Showing
13 changed files
with
599 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package gosss | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"math/big" | ||
) | ||
|
||
// shareToStr converts a big.Int to a string. It uses the bytes of the big.Int | ||
func shareToStr(index, share *big.Int) (string, error) { | ||
if index.Cmp(big.NewInt(255)) > 0 || index.Cmp(big.NewInt(0)) < 0 { | ||
return "", ErrShareIndex | ||
} | ||
bShare := share.Bytes() | ||
// encode the index in a byte and append it at the end of the share | ||
bIndex := index.Bytes() | ||
if len(bIndex) == 0 { | ||
bIndex = []byte{0} | ||
} | ||
fullShare := append(bShare, bIndex[0]) | ||
return hex.EncodeToString(fullShare), nil | ||
} | ||
|
||
// strToShare converts a string to a big.Int. It uses the bytes of the string | ||
func strToShare(s string) (*big.Int, *big.Int, error) { | ||
b, err := hex.DecodeString(s) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("%w: %w", ErrDecodeShare, err) | ||
} | ||
bIndex := b[len(b)-1] | ||
bShare := b[:len(b)-1] | ||
return new(big.Int).SetBytes([]byte{bIndex}), new(big.Int).SetBytes(bShare), nil | ||
} | ||
|
||
// encodeShares function converts the x and y coordinates of the shares to | ||
// strings. It returns the shares as strings. It uses the shareToStr function to | ||
// encode the shares. It returns an error if the shares cannot be encoded. | ||
func encodeShares(xs, ys []*big.Int) ([]string, error) { | ||
if len(xs) == 0 || len(ys) == 0 || len(xs) != len(ys) { | ||
return nil, ErrInvalidShares | ||
} | ||
// convert the shares to strings and append them to the result | ||
shares := []string{} | ||
for i := 0; i < len(xs); i++ { | ||
share, err := shareToStr(xs[i], ys[i]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
shares = append(shares, share) | ||
} | ||
return shares, nil | ||
} | ||
|
||
// decodeShares function converts the strings of the shares to x and y | ||
// coordinates of the shares. It uses the strToShare function to decode the | ||
// shares. It returns an error if the shares cannot be decoded. | ||
func decodeShares(shares []string) ([]*big.Int, []*big.Int, error) { | ||
xs := []*big.Int{} | ||
ys := []*big.Int{} | ||
for _, strShare := range shares { | ||
index, share, err := strToShare(strShare) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
xs = append(xs, index) | ||
ys = append(ys, share) | ||
} | ||
return xs, ys, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package gosss | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func Test_shareToStrStrToShare(t *testing.T) { | ||
// generate 10 random big.Int and convert them to string | ||
for i := 0; i < 10; i++ { | ||
idx := big.NewInt(int64(i)) | ||
rand, err := randBigInt() | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
shareStr, err := shareToStr(idx, rand) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
return | ||
} | ||
index, shareBack, err := strToShare(shareStr) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
return | ||
} | ||
if index.Cmp(idx) != 0 { | ||
t.Errorf("unexpected index: %d", index) | ||
} | ||
if rand.Cmp(shareBack) != 0 { | ||
t.Errorf("unexpected share: %s", shareStr) | ||
} | ||
} | ||
} | ||
|
||
func Test_encodeDecodeShares(t *testing.T) { | ||
xs := []*big.Int{ | ||
big.NewInt(1), | ||
big.NewInt(2), | ||
big.NewInt(3), | ||
big.NewInt(4), | ||
} | ||
ys := []*big.Int{ | ||
big.NewInt(100), | ||
big.NewInt(200), | ||
big.NewInt(300), | ||
big.NewInt(400), | ||
} | ||
encodedShares, err := encodeShares(xs, ys) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
return | ||
} | ||
decodedXs, decodedYs, err := decodeShares(encodedShares) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
return | ||
} | ||
if len(xs) != len(decodedXs) || len(ys) != len(decodedYs) { | ||
t.Errorf("unexpected shares length") | ||
return | ||
} | ||
for i := 0; i < len(xs); i++ { | ||
if xs[i].Cmp(decodedXs[i]) != 0 { | ||
t.Errorf("unexpected x coordinate") | ||
return | ||
} | ||
if ys[i].Cmp(decodedYs[i]) != 0 { | ||
t.Errorf("unexpected y coordinate") | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package gosss | ||
|
||
import "fmt" | ||
|
||
var ( | ||
// config | ||
ErrConfigShares = fmt.Errorf("wrong number of shares, it must be between 1 and the maximum number of shares") | ||
ErrConfigMin = fmt.Errorf("wrong minimum number of shares, it must be between 2 and the number of shares minus 1") | ||
ErrConfigOp = fmt.Errorf("unknown operation") | ||
// encode | ||
ErrShareIndex = fmt.Errorf("the index must fit in a byte (0-255)") | ||
ErrDecodeShare = fmt.Errorf("error decoding share") | ||
ErrInvalidShares = fmt.Errorf("invalid shares") | ||
// math | ||
ErrReadingRandom = fmt.Errorf("error reading random number") | ||
// sss | ||
ErrRequiredConfig = fmt.Errorf("configuration is required") | ||
ErrEncodeMessage = fmt.Errorf("error encoding message") | ||
) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.