Skip to content

Commit

Permalink
update to current MP-SPDZ version
Browse files Browse the repository at this point in the history
Co-authored-by: Petra Scherer <[email protected]>
Co-authored-by: Timo Klenk <[email protected]>
Signed-off-by: Johannes Graf <[email protected]>
Signed-off-by: Petra Scherer <[email protected]>
  • Loading branch information
kindlich and Lila84 committed Mar 21, 2022
1 parent f7fa7f3 commit 84b5220
Show file tree
Hide file tree
Showing 15 changed files with 487 additions and 62 deletions.
5 changes: 3 additions & 2 deletions cmd/discovery/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package main_test

import (
"context"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -58,7 +59,7 @@ var _ = Describe("Main", func() {
})
Context("all required parameters are specified", func() {
AfterEach(func() {
_, _, err := cmder.CallCMD([]string{fmt.Sprintf("rm %s", path)}, "./")
_, _, err := cmder.CallCMD(context.TODO(), []string{fmt.Sprintf("rm %s", path)}, "./")
Expect(err).NotTo(HaveOccurred())
})
It("succeeds", func() {
Expand All @@ -77,7 +78,7 @@ var _ = Describe("Main", func() {
Context("one of the required parameters is missing", func() {
Context("when no frontendURL is defined", func() {
AfterEach(func() {
_, _, err := cmder.CallCMD([]string{fmt.Sprintf("rm %s", path)}, "./")
_, _, err := cmder.CallCMD(context.TODO(), []string{fmt.Sprintf("rm %s", path)}, "./")
Expect(err).NotTo(HaveOccurred())
})
It("returns an error", func() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/ephemeral/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package main_test

import (
"context"
"fmt"
"io/ioutil"
"math/rand"
Expand Down Expand Up @@ -43,7 +44,7 @@ var _ = Describe("Main", func() {
path = fmt.Sprintf("/tmp/test-%d", random)
})
AfterEach(func() {
_, _, err := cmder.CallCMD([]string{fmt.Sprintf("rm %s", path)}, "./")
_, _, err := cmder.CallCMD(context.TODO(), []string{fmt.Sprintf("rm %s", path)}, "./")
Expect(err).NotTo(HaveOccurred())
})
Context("when it succeeds", func() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/ephemeral/fake_spdz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package ephemeral

import (
"context"
"errors"
"github.com/carbynestack/ephemeral/pkg/discovery/fsm"
pb "github.com/carbynestack/ephemeral/pkg/discovery/transport/proto"
Expand Down Expand Up @@ -93,14 +94,14 @@ func (f *FakePlayer) PublishEvent(name, topic string, event *pb.Event) {
type FakeExecutor struct {
}

func (f *FakeExecutor) CallCMD(cmd []string, dir string) ([]byte, []byte, error) {
func (f *FakeExecutor) CallCMD(theContext context.Context, cmd []string, dir string) ([]byte, []byte, error) {
return []byte{}, []byte{}, nil
}

type BrokenFakeExecutor struct {
}

func (f *BrokenFakeExecutor) CallCMD(cmd []string, dir string) ([]byte, []byte, error) {
func (f *BrokenFakeExecutor) CallCMD(theContext context.Context, cmd []string, dir string) ([]byte, []byte, error) {
return []byte{}, []byte{}, errors.New("some error")
}

Expand Down
72 changes: 65 additions & 7 deletions pkg/ephemeral/io/carrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ package io

import (
"context"
"encoding/binary"
"errors"
"fmt"
"github.com/carbynestack/ephemeral/pkg/amphora"
"io"
"io/ioutil"
"net"
)
Expand All @@ -21,18 +24,19 @@ type Result struct {

// AbstractCarrier is the carriers interface.
type AbstractCarrier interface {
Connect(context.Context, string, string) error
Connect(int32, context.Context, string, string) error
Close() error
Send([]amphora.SecretShare) error
Read(ResponseConverter, bool) (*Result, error)
}

// Carrier is a TCP client for TCP sockets.
type Carrier struct {
Dialer func(ctx context.Context, addr, port string) (net.Conn, error)
Conn net.Conn
Packer Packer
connected bool
Dialer func(ctx context.Context, addr, port string) (net.Conn, error)
TlsConnector func(conn net.Conn, playerID int32) (net.Conn, error)
Conn net.Conn
Packer Packer
connected bool
}

// Config contains TCP connection properties of Carrier.
Expand All @@ -42,16 +46,55 @@ type Config struct {
}

// Connect establishes a TCP connection to a socket on a given host and port.
func (c *Carrier) Connect(ctx context.Context, host, port string) error {
func (c *Carrier) Connect(playerID int32, ctx context.Context, host string, port string) error {
conn, err := c.Dialer(ctx, host, port)
if err != nil {
return err
}
c.Conn = conn

_, err = conn.Write(c.buildHeader(playerID))
if err != nil {
return err
}

c.Conn, err = c.TlsConnector(conn, playerID)
if err != nil {
return err
}

if playerID == 0 {
err = c.readSpec()
if err != nil {
return err
}
}

c.connected = true
return nil
}

func (c Carrier) readSpec() error {
const size = 4

readBytes := make([]byte, size)
_, err := io.LimitReader(c.Conn, size).Read(readBytes)
if err != nil {
return err
}

sizeOfHeader := binary.LittleEndian.Uint32(readBytes)

readBytes = make([]byte, sizeOfHeader)
_, err = io.LimitReader(c.Conn, int64(sizeOfHeader)).Read(readBytes)
if err != nil {
return err
}

//ToDo, compare read PRIME with prime number from config?

return nil
}

// Close closes the underlying TCP connection.
func (c *Carrier) Close() error {
if c.connected {
Expand All @@ -68,16 +111,31 @@ func (c *Carrier) Send(secret []amphora.SecretShare) error {
shares = append(shares, secret[i].Data)
}
err := c.Packer.Marshal(shares, &input)

if err != nil {
return err
}
_, err = c.Conn.Write(input)

if err != nil {
return err
}
return nil
}

// Returns a new Slice with the header appended
// The header consists of the clientId as string:
// - 1 Long (4 Byte) that contains the length of the string in bytes
// - Then come X Bytes for the String
func (c *Carrier) buildHeader(playerId int32) []byte {
playerIdString := []byte(fmt.Sprintf("%d", playerId))

lengthOfString := make([]byte, 4)
binary.LittleEndian.PutUint32(lengthOfString, uint32(len(playerIdString)))

return append(lengthOfString, playerIdString...)
}

// Read reads the response from the TCP connection and unmarshals it.
func (c *Carrier) Read(conv ResponseConverter, bulkObjects bool) (*Result, error) {
resp := []byte{}
Expand Down
Loading

0 comments on commit 84b5220

Please sign in to comment.