Skip to content

Commit

Permalink
Golint changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Jul 15, 2020
1 parent c008db7 commit 7435055
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 37 deletions.
7 changes: 7 additions & 0 deletions ot/mpint/mpint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,29 @@ import (
"math/big"
)

// FromBytes creates a big.Int from the data.
func FromBytes(data []byte) *big.Int {
return big.NewInt(0).SetBytes(data)
}

// Add adds two big.Int numbers and returns the result as a new
// big.Int.
func Add(a, b *big.Int) *big.Int {
return big.NewInt(0).Add(a, b)
}

// Sub subtracts two big.Int numbers and returns the result as a new
// big.Int.
func Sub(a, b *big.Int) *big.Int {
return big.NewInt(0).Sub(a, b)
}

// Exp computes x^y MOD m and returns the result as a new big.Int.
func Exp(x, y, m *big.Int) *big.Int {
return big.NewInt(0).Exp(x, y, m)
}

// Mod computes x%y and returns the result as a new big.Int.
func Mod(x, y *big.Int) *big.Int {
return big.NewInt(0).Mod(x, y)
}
44 changes: 43 additions & 1 deletion ot/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ import (
)

const (
PRFBlockSize = 16
// PRFBlockSize specifies the cipher block size for the PRF label
// generation algorithm.
PRFBlockSize = 16

// PRFBlockCount specifies the block count for the PRF label
// generation algorithm.
PRFBlockCount = 256
)

// LabelType specifies label generation algorithms.
type LabelType int

// Label generation algorithms.
const (
LabelRandom = iota
LabelPRF
Expand Down Expand Up @@ -83,6 +90,7 @@ func init() {
prfBlock = PRFBlockCount
}

// RandomData creates size bytes of random data.
func RandomData(size int) ([]byte, error) {
m := make([]byte, size)
_, err := rand.Read(m)
Expand All @@ -92,21 +100,25 @@ func RandomData(size int) ([]byte, error) {
return m, nil
}

// Label implements a 128 bit wire label.
type Label struct {
d0 uint64
d1 uint64
}

// LabelData contains lable data as byte array.
type LabelData [16]byte

func (l Label) String() string {
return fmt.Sprintf("%016x%016x", l.d0, l.d1)
}

// Equal test if the labels are equal.
func (l Label) Equal(o Label) bool {
return l.d0 == o.d0 && l.d1 == o.d1
}

// NewLabel creates a new random label.
func NewLabel(rand io.Reader) (Label, error) {
switch labelGenerator {
case LabelRandom:
Expand All @@ -131,16 +143,19 @@ func NewLabel(rand io.Reader) (Label, error) {
}
}

// NewTweak creates a new label from the tweak value.
func NewTweak(tweak uint32) Label {
return Label{
d1: uint64(tweak),
}
}

// S tests the label's S bit.
func (l Label) S() bool {
return (l.d0 & 0x8000000000000000) != 0
}

// SetS sets the label's S bit.
func (l *Label) SetS(set bool) {
if set {
l.d0 |= 0x8000000000000000
Expand All @@ -149,53 +164,63 @@ func (l *Label) SetS(set bool) {
}
}

// Mul2 multiplies the label by 2.
func (l *Label) Mul2() {
l.d0 <<= 1
l.d0 |= (l.d1 >> 63)
l.d1 <<= 1
}

// Mul4 multiplies the label by 4.
func (l *Label) Mul4() {
l.d0 <<= 2
l.d0 |= (l.d1 >> 62)
l.d1 <<= 2
}

// Xor xors the label with the argument label.
func (l *Label) Xor(o Label) {
l.d0 ^= o.d0
l.d1 ^= o.d1
}

// GetData gets the labels as label data.
func (l Label) GetData(buf *LabelData) {
binary.BigEndian.PutUint64((*buf)[0:8], l.d0)
binary.BigEndian.PutUint64((*buf)[8:16], l.d1)
}

// SetData sets the labels from label data.
func (l *Label) SetData(data *LabelData) {
l.d0 = binary.BigEndian.Uint64((*data)[0:8])
l.d1 = binary.BigEndian.Uint64((*data)[8:16])
}

// Bytes returns the label data as bytes.
func (l Label) Bytes() []byte {
var buf LabelData
l.GetData(&buf)
return buf[:]
}

// SetBytes sets the label data from bytes.
func (l *Label) SetBytes(data []byte) {
l.d0 = binary.BigEndian.Uint64(data[0:8])
l.d1 = binary.BigEndian.Uint64(data[8:16])
}

// Wire implements a wire with 0 and 1 labels.
type Wire struct {
L0 Label
L1 Label
}

// Sender implements OT sender.
type Sender struct {
key *rsa.PrivateKey
}

// NewSender creates a new OT sender for the bit.
func NewSender(keyBits int) (*Sender, error) {
key, err := rsa.GenerateKey(rand.Reader, keyBits)
if err != nil {
Expand All @@ -207,14 +232,17 @@ func NewSender(keyBits int) (*Sender, error) {
}, nil
}

// MessageSize returns the maximum OT message size.
func (s *Sender) MessageSize() int {
return s.key.PublicKey.Size()
}

// PublicKey returns the sender's public key.
func (s *Sender) PublicKey() *rsa.PublicKey {
return &s.key.PublicKey
}

// NewTransfer creates a new OT sender data transfer.
func (s *Sender) NewTransfer(m0, m1 []byte) (*SenderXfer, error) {
x0, err := RandomData(s.MessageSize())
if err != nil {
Expand All @@ -234,6 +262,7 @@ func (s *Sender) NewTransfer(m0, m1 []byte) (*SenderXfer, error) {
}, nil
}

// SenderXfer implements the OT sender data transfer.
type SenderXfer struct {
sender *Sender
m0 []byte
Expand All @@ -244,14 +273,17 @@ type SenderXfer struct {
k1 *big.Int
}

// MessageSize returns the maximum OT message size.
func (s *SenderXfer) MessageSize() int {
return s.sender.MessageSize()
}

// RandomMessages creates random messages.
func (s *SenderXfer) RandomMessages() ([]byte, []byte) {
return s.x0, s.x1
}

// ReceiveV receives the V.
func (s *SenderXfer) ReceiveV(data []byte) {
v := mpint.FromBytes(data)
x0 := mpint.FromBytes(s.x0)
Expand All @@ -261,6 +293,7 @@ func (s *SenderXfer) ReceiveV(data []byte) {
s.k1 = mpint.Exp(mpint.Sub(v, x1), s.sender.key.D, s.sender.key.PublicKey.N)
}

// Messages creates the transfer messages.
func (s *SenderXfer) Messages() ([]byte, []byte, error) {
m0, err := pkcs1.NewEncryptionBlock(pkcs1.BT1, s.MessageSize(), s.m0)
if err != nil {
Expand All @@ -277,27 +310,32 @@ func (s *SenderXfer) Messages() ([]byte, []byte, error) {
return m0p.Bytes(), m1p.Bytes(), nil
}

// Receiver implements OT receivers.
type Receiver struct {
pub *rsa.PublicKey
}

// NewReceiver creates a new OT receiver.
func NewReceiver(pub *rsa.PublicKey) (*Receiver, error) {
return &Receiver{
pub: pub,
}, nil
}

// MessageSize returns the maximum OT message size.
func (r *Receiver) MessageSize() int {
return r.pub.Size()
}

// NewTransfer creates a new OT receiver data transfer for the bit.
func (r *Receiver) NewTransfer(bit uint) (*ReceiverXfer, error) {
return &ReceiverXfer{
receiver: r,
bit: bit,
}, nil
}

// ReceiverXfer implements the OT receiver data transfer.
type ReceiverXfer struct {
receiver *Receiver
bit uint
Expand All @@ -306,6 +344,7 @@ type ReceiverXfer struct {
mb []byte
}

// ReceiveRandomMessages receives the random messages x0 and x1.
func (r *ReceiverXfer) ReceiveRandomMessages(x0, x1 []byte) error {
k, err := rand.Int(rand.Reader, r.receiver.pub.N)
if err != nil {
Expand All @@ -327,10 +366,12 @@ func (r *ReceiverXfer) ReceiveRandomMessages(x0, x1 []byte) error {
return nil
}

// V returns the V of the exchange.
func (r *ReceiverXfer) V() []byte {
return r.v.Bytes()
}

// ReceiveMessages processes the received m0p and m1p messages.
func (r *ReceiverXfer) ReceiveMessages(m0p, m1p []byte, err error) error {
if err != nil {
return err
Expand All @@ -355,6 +396,7 @@ func (r *ReceiverXfer) ReceiveMessages(m0p, m1p []byte, err error) error {
return nil
}

// Message returns the message and bit from the exchange.
func (r *ReceiverXfer) Message() (m []byte, bit uint) {
return r.mb, r.bit
}
Loading

0 comments on commit 7435055

Please sign in to comment.