shamir secret sharing: create secret sharing, and reconstruct secret from shares with Langrange Interpolation

This commit is contained in:
arnaucode
2018-07-22 14:46:43 +02:00
parent 6ce09111f9
commit 9aa4a2d1a0
9 changed files with 206 additions and 17 deletions

View File

@@ -12,24 +12,25 @@ const (
var bigOne = big.NewInt(int64(1))
// PublicKey stores the public key data
type PublicKey struct {
E *big.Int `json:"e"`
N *big.Int `json:"n"`
}
type PublicKeyString struct {
E string `json:"e"`
N string `json:"n"`
}
// PrivateKey stores the private key data
type PrivateKey struct {
D *big.Int `json:"d"`
N *big.Int `json:"n"`
}
// Key stores the public and private key data
type Key struct {
PubK PublicKey
PrivK PrivateKey
}
// GenerateKeyPair generates a random private and public key
func GenerateKeyPair() (key Key, err error) {
p, err := rand.Prime(rand.Reader, bits/2)
if err != nil {
@@ -41,9 +42,9 @@ func GenerateKeyPair() (key Key, err error) {
}
n := new(big.Int).Mul(p, q)
p_1 := new(big.Int).Sub(p, bigOne)
q_1 := new(big.Int).Sub(q, bigOne)
phi := new(big.Int).Mul(p_1, q_1)
p1 := new(big.Int).Sub(p, bigOne)
q1 := new(big.Int).Sub(q, bigOne)
phi := new(big.Int).Mul(p1, q1)
e := 65537
var pubK PublicKey
pubK.E = big.NewInt(int64(e))
@@ -60,15 +61,19 @@ func GenerateKeyPair() (key Key, err error) {
return key, nil
}
// Encrypt encrypts a message m with given PublicKey
func Encrypt(m *big.Int, pubK PublicKey) *big.Int {
c := new(big.Int).Exp(m, pubK.E, pubK.N)
return c
}
// Decrypt deencrypts a ciphertext c with given PrivateKey
func Decrypt(c *big.Int, privK PrivateKey) *big.Int {
m := new(big.Int).Exp(c, privK.D, privK.N)
return m
}
// Blind blinds a message
func Blind(m *big.Int, r *big.Int, pubK PublicKey) *big.Int {
rE := new(big.Int).Exp(r, pubK.E, nil)
mrE := new(big.Int).Mul(m, rE)
@@ -76,16 +81,21 @@ func Blind(m *big.Int, r *big.Int, pubK PublicKey) *big.Int {
return mBlinded
}
// BlindSign blind signs a message without knowing the content
func BlindSign(m *big.Int, privK PrivateKey) *big.Int {
sigma := new(big.Int).Exp(m, privK.D, privK.N)
return sigma
}
// Unblind unblinds the Blinded Signature
func Unblind(sigma *big.Int, r *big.Int, pubK PublicKey) *big.Int {
r1 := new(big.Int).ModInverse(r, pubK.N)
bsr := new(big.Int).Mul(sigma, r1)
sig := new(big.Int).Mod(bsr, pubK.N)
return sig
}
// Verify verifies the signature of a message given the PublicKey of the signer
func Verify(msg *big.Int, mSigned *big.Int, pubK PublicKey) bool {
//decrypt the mSigned with pubK
Cd := new(big.Int).Exp(mSigned, pubK.E, nil)
@@ -93,6 +103,7 @@ func Verify(msg *big.Int, mSigned *big.Int, pubK PublicKey) bool {
return bytes.Equal(msg.Bytes(), m.Bytes())
}
// HomomorphicMul calculates the multiplication of tow encrypted values given a PublicKey
func HomomorphicMul(c1 *big.Int, c2 *big.Int, pubK PublicKey) *big.Int {
c1c2 := new(big.Int).Mul(c1, c2)
n2 := new(big.Int).Mul(pubK.N, pubK.N)

View File

@@ -2,7 +2,6 @@ package rsa
import (
"bytes"
"fmt"
"math/big"
"testing"
)
@@ -12,7 +11,6 @@ func TestEncryptDecrypt(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
fmt.Println(key)
mBytes := []byte("Hi")
m := new(big.Int).SetBytes(mBytes)
c := Encrypt(m, key.PubK)