如何在Go中将byte类型转换为Kyber.Scalar类型

huangapple go评论88阅读模式
英文:

How to Convert type byte to Kyber.Scalar in Go

问题

我正在使用Go中的kyber.scalar方法。我想通过套接字编程发送我的数据(kyber.scalar),并能够读取其他程序。但是当我读取时,我无法再将其转换回kyber.scalar类型。

这是我发送的代码:

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

r_by, err := r.MarshalBinary()

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

这是我读取的代码:

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)

问题是:
> 无法将类型为[]byter用作suite.Curve.Point().Mul的参数类型:
[]byte没有实现kyber.Scalar(缺少Add方法)

如何修复这个问题,或者是否有一种推荐的方法将字节转换为kyber.scalar

英文:

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

得分: 1

如果你正在使用go.dedis.ch/kyber,那么下面是一些实现预期效果的方法:

使用SetBytes

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

// 新建 Scalar
b := suite.Scalar()
b.SetBytes(a_by)

使用suite.Readsuite.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("...")
}
英文:

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("...")
}

huangapple
  • 本文由 发表于 2022年5月11日 17:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/72198678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定