英文:
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)
问题是:
> 无法将类型为[]byte
的r
用作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.Read
和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("...")
}
英文:
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("...")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论