Skip to content

Commit

Permalink
Code cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Feb 17, 2023
1 parent 99e833f commit b7ae910
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 29 deletions.
24 changes: 24 additions & 0 deletions ot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Oblivious Transfer

This module implements the Oblivous Transfer with the following
algorithms:

- RSA: simple RSA encryption based OT. Each transfer requires one RSA
operation.
- Chou Orlandi OT: Diffie-Hellman - like fast OT algorithm.

## Performance

| Algorithm | ns/op | ops/s |
| :----------- | ---------: | ------: |
| RSA-512 | 252557 | 3960 |
| RSA-1024 | 1256961 | 796 |
| RSA-2048 | 7785958 | 128 |
| CO-batch-1 | 170791 | 5855 |
| CO-batch-2 | 269399 | 7424 |
| CO-batch-4 | 468161 | 8544 |
| CO-batch-8 | 877664 | 9115 |
| CO-batch-16 | 1706184 | 9378 |
| CO-batch-32 | 3273137 | 9777 |
| CO-batch-64 | 6480310 | 9876 |
| CO-batch-128 | 12845639 | 9964 |
43 changes: 15 additions & 28 deletions ot/co.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,10 @@ func (co *CO) Send(wires []Wire) error {
// A = G^a
Ax, Ay := co.curve.ScalarBaseMult(aBytes)

err = co.io.SendData(Ax.Bytes())
if err != nil {
if err := co.io.SendData(Ax.Bytes()); err != nil {
return err
}
err = co.io.SendData(Ay.Bytes())
if err != nil {
if err := co.io.SendData(Ay.Bytes()); err != nil {
return err
}

Expand All @@ -298,17 +296,14 @@ func (co *CO) Send(wires []Wire) error {
AaInvy := big.NewInt(0).Sub(curveParams.P, Aay)

for i := 0; i < len(wires); i++ {
data, err := co.io.ReceiveData()
Bx, err := ReceiveBigInt(co.io)
if err != nil {
return err
}
Bx := big.NewInt(0).SetBytes(data)

data, err = co.io.ReceiveData()
By, err := ReceiveBigInt(co.io)
if err != nil {
return err
}
By := big.NewInt(0).SetBytes(data)

Bx, By = co.curve.ScalarMult(Bx, By, aBytes)
Bax, Bay := co.curve.Add(Bx, By, AaInvx, AaInvy)
Expand All @@ -317,14 +312,12 @@ func (co *CO) Send(wires []Wire) error {

wires[i].L0.GetData(&labelData)
e0 := xor(kdf(co.hash, Bx, By, uint64(i), co.digest[:]), labelData[:])
err = co.io.SendData(e0)
if err != nil {
if err := co.io.SendData(e0); err != nil {
return err
}
wires[i].L1.GetData(&labelData)
e1 := xor(kdf(co.hash, Bax, Bay, uint64(i), co.digest[:]), labelData[:])
err = co.io.SendData(e1)
if err != nil {
if err := co.io.SendData(e1); err != nil {
return err
}
}
Expand All @@ -334,20 +327,16 @@ func (co *CO) Send(wires []Wire) error {
// Receive receives the wire labels with OT based on the flag values.
func (co *CO) Receive(flags []bool) ([]Label, error) {
curveParams := co.curve.Params()

result := make([]Label, len(flags))

data, err := co.io.ReceiveData()
Ax, err := ReceiveBigInt(co.io)
if err != nil {
return nil, err
}
Ax := big.NewInt(0).SetBytes(data)

data, err = co.io.ReceiveData()
Ay, err := ReceiveBigInt(co.io)
if err != nil {
return nil, err
}
Ay := big.NewInt(0).SetBytes(data)

for i := 0; i < len(flags); i++ {
// b <= Zp
Expand All @@ -361,28 +350,27 @@ func (co *CO) Receive(flags []bool) ([]Label, error) {
if flags[i] {
Bx, By = co.curve.Add(Bx, By, Ax, Ay)
}
err = co.io.SendData(Bx.Bytes())
if err != nil {
if err := co.io.SendData(Bx.Bytes()); err != nil {
return nil, err
}
err = co.io.SendData(By.Bytes())
if err != nil {
if err := co.io.SendData(By.Bytes()); err != nil {
return nil, err
}

Asx, Asy := co.curve.ScalarMult(Ax, Ay, bBytes)

// Receive E

// Receive E. Please, be careful when editing the code below
// since the co.digest will be used as data after kdf()
// call. Also, data received from co.io can be overridden by
// the next call so we do the xor() as soon as we received the
// data.
data := kdf(co.hash, Asx, Asy, uint64(i), co.digest[:])

var e []byte
if flags[i] {
_, err = co.io.ReceiveData()
if err != nil {
return nil, err
}

e, err := co.io.ReceiveData()
if err != nil {
return nil, err
Expand All @@ -394,7 +382,6 @@ func (co *CO) Receive(flags []bool) ([]Label, error) {
return nil, err
}
data = xor(data, e)

_, err := co.io.ReceiveData()
if err != nil {
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions ot/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

package ot

import (
"math/big"
)

// IO defines an I/O interface to communicate between peers.
type IO interface {
// SendData sends binary data.
Expand All @@ -24,3 +28,12 @@ type IO interface {
// ReceiveUint32 receives an uint32 value.
ReceiveUint32() (int, error)
}

// ReceiveBigInt receives a bit.Int from the connection.
func ReceiveBigInt(io IO) (*big.Int, error) {
data, err := io.ReceiveData()
if err != nil {
return nil, err
}
return big.NewInt(0).SetBytes(data), nil
}
18 changes: 17 additions & 1 deletion ot/ot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ func BenchmarkOTCO1(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 1, b)
}

func XBenchmarkOTCO2(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 2, b)
}

func XBenchmarkOTCO4(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 4, b)
}

func BenchmarkOTCO8(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 8, b)
}

func BenchmarkOTCO16(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 16, b)
}

func BenchmarkOTCO32(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 32, b)
}
Expand All @@ -182,6 +198,6 @@ func BenchmarkOTCO64(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 64, b)
}

func BenchmarkOTCO128(b *testing.B) {
func XBenchmarkOTCO128(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 128, b)
}

0 comments on commit b7ae910

Please sign in to comment.