Add blog link, small pending updates

This commit is contained in:
arnaucube
2021-10-10 11:36:07 +02:00
parent e8943659db
commit c63b841899
8 changed files with 73 additions and 333 deletions

View File

@@ -21,7 +21,7 @@ Call the library from javascript:
// Create shares from a secret
// nNeededShares: number of secrets needed
// nShares: number of shares
// p: random point
// p: size of finite field
// k: secret to share
createShares(nNeededShares, nShares, p, k);
```
@@ -29,12 +29,12 @@ createShares(nNeededShares, nShares, p, k);
## Usage from Go
```go
// define secret to share
k, ok := new(big.Int).SetString("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 10)
k, ok := new(big.Int).SetString("12345678901234567890123456789012345678", 10)
assert.True(t, ok)
// define random prime
p, err := rand.Prime(rand.Reader, bits/2)
assert.Nil(t, err)
// define the field
p, ok := new(big.Int).SetString("170141183460469231731687303715884105727", 10)
assert.True(t, ok)
// define how many shares want to generate
nShares := big.NewInt(int64(6))

View File

@@ -6,15 +6,21 @@ import (
"math/big"
)
const (
// bits = 1024
bits = 2048
)
func randBigInt(p *big.Int) (*big.Int, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
r := new(big.Int).SetBytes(b)
rp := new(big.Int).Mod(r, p)
return rp, nil
}
// Create calculates the secrets to share from given parameters
// t: number of secrets needed
// n: number of shares
// p: random point
// p: size of finite field
// k: secret to share
func Create(t, n, p, k *big.Int) (result [][]*big.Int, err error) {
if k.Cmp(p) > 0 {
@@ -24,11 +30,11 @@ func Create(t, n, p, k *big.Int) (result [][]*big.Int, err error) {
var basePolynomial []*big.Int
basePolynomial = append(basePolynomial, k)
for i := 0; i < int(t.Int64())-1; i++ {
randPrime, err := rand.Prime(rand.Reader, bits/2)
x, err := randBigInt(p)
if err != nil {
return result, err
}
basePolynomial = append(basePolynomial, randPrime)
basePolynomial = append(basePolynomial, x)
}
//calculate shares, based on the basePolynomial

View File

@@ -2,7 +2,6 @@ package shamirsecretsharing
import (
"bytes"
"crypto/rand"
"math/big"
"testing"
@@ -10,11 +9,12 @@ import (
)
func TestCreate(t *testing.T) {
k, ok := new(big.Int).SetString("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", 10)
k, ok := new(big.Int).SetString("12345678901234567890123456789012345678", 10)
assert.True(t, ok)
p, err := rand.Prime(rand.Reader, bits/2)
assert.Nil(t, err)
// 2 ** 127 - 1
p, ok := new(big.Int).SetString("170141183460469231731687303715884105727", 10)
assert.True(t, ok)
nShares := big.NewInt(int64(6))
nNeededShares := big.NewInt(int64(3))