1

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 Answers1

1

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("...")
}
Chandan
  • 11,465
  • 1
  • 6
  • 25
  • Hi @Chandan thanks for the solution. When I run the first program(server) with **socket** programming it runs smoothly, but when I run the second program (client) there is a problem : 1. Error reading: read tcp 127.0.0.1:62762->127.0.0.1:9988: wsarecv: An existing connection was forcibly closed by the remote host. 2. Error reading: read tcp 127.0.0.1:62762->127.0.0.1:9988: wsarecv: An existing connection was forcibly closed by the remote host. 5.panic: runtime error: invalid memory address or nil pointer dereference 4. [signal 0xc0000005 code=0x0 addr=0x18 pc=0x42383c] any solution ? – Daniel Perdana Putra May 12 '22 at 09:11
  • You can check my [code](https://github.com/Chyuu/Fiat) please! – Daniel Perdana Putra May 12 '22 at 09:20
  • @DanielPerdanaPutra the problem seems to be related to reading more bytes then is sent from server and connection is also being closed multiple times before server or client sent or receive data. – Chandan May 12 '22 at 10:32
  • How to fixed, or is there a recommended way to fixed ? – Daniel Perdana Putra May 12 '22 at 10:42
  • if you just want to run for just for testing purpose then just change reading `1024` to `10` since only 10 bytes are being sent and close connection only when necessary if you want to make it production ready then you may need to add various checks. – Chandan May 12 '22 at 11:02
  • Can you give me for example various checks please ? I got problem again : 1. panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x18 pc=0xc2383c] – Daniel Perdana Putra May 12 '22 at 11:07
  • @DanielPerdanaPutra `var c kyber.Scalar buf := bytes.Buffer{} bufBytes := buf.Bytes() if err := suite.Read(bytes.NewBuffer(bufBytes), &c); err != nil { log.Fatal("...", err.Error()) }` in this snippet on server side you are trying to load empty buffer which is causing the server to crash so client is not being to read since connection got closed why are you reading empty buffer to Scalar why not creating empty Scalar using suite. – Chandan May 12 '22 at 11:59
  • What should i do ? :( – Daniel Perdana Putra May 12 '22 at 12:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244689/discussion-between-chandan-and-daniel-perdana-putra). – Chandan May 12 '22 at 12:34