How to Convert type byte to Kyber.Scalar in Go

143 views Asked by At

I am using the kyber.scalar method in Go. I would like to send my data(kyber.scalar) with socket programing and can read other program. When i read, i can't turn back into kyber.scalar type again.

This my code for sending

r := suite.Scalar()
r.Mul(x, c_scl).Sub(v, r)

r_by, err := r.MarshalBinary()

_, err = connection.Write(r_by)
defer connection.Close()

This my code for Reading

buffer5 := make([]byte, 1024)
mLen5, err := connection.Read(buffer5)
if err != nil {
    fmt.Println("Error reading:", err.Error())
}
r := buffer5[:mLen5]

rG := suite.Point().Mul(r, G_pt)

The problem.

cannot use r (type []byte) as type kyber.Scalar in argument to suite.Curve.Point().Mul: []byte does not implement kyber.Scalar (missing Add method)

How to fixed, or is there a recommended way to convert the bytes to kyber.scalar ?

1

There are 1 answers

9
Chandan On

If you are using go.dedis.ch/kyber then below are some ways to achieve what is expected

Using SetBytes

suite := suites.MustFind("Ed25519")
a := suite.Scalar().Pick(suite.RandomStream())
a_by, err := a.MarshalBinary()
if err != nil {
  log.Fatal("...")
}

// New Scalar
b := suite.Scalar()
b.SetBytes(a_by)

Using suite.Read and suite.Write

suite := suites.MustFind("Ed25519")
a := suite.Scalar().Pick(suite.RandomStream())
buf := bytes.Buffer{}
suite.Write(&buf, &a)

var c kyber.Scalar
bufBytes := buf.Bytes()
if err := suite.Read(bytes.NewBuffer(bufBytes), &c); err != nil {
  log.Fatal("...")
}