go-rln/rln/serialize.go
Richard Ramos 7c7e6a9171
feat: cross compilation and API refactor (#1)
* chore: use cross instead of rustup for building rln

* chore: generate protobuffer and contract

* refactor: make API match nwaku

* feat: add WakuRLNRelay type

* test: rln

* chore: add more architectures

* refactor: use time.Duration instead of uint/float
2022-07-04 13:01:54 -04:00

39 lines
1.5 KiB
Go

package rln
import "encoding/binary"
// serialize converts a RateLimitProof and the data to a byte seq
// this conversion is used in the proofGen function
// the serialization is done as instructed in https://github.com/kilic/rln/blob/7ac74183f8b69b399e3bc96c1ae8ab61c026dc43/src/public.rs#L146
// [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
func serialize(idKey IDKey, memIndex MembershipIndex, epoch Epoch, msg []byte) []byte {
memIndexBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(memIndexBytes, uint64(memIndex))
lenPrefMsg := appendLength(msg)
output := append(idKey[:], memIndexBytes...)
output = append(output, epoch[:]...)
output = append(output, lenPrefMsg...)
return output
}
// serialize converts a RateLimitProof and data to a byte seq
// this conversion is used in the proof verification proc
// the order of serialization is based on https://github.com/kilic/rln/blob/7ac74183f8b69b399e3bc96c1ae8ab61c026dc43/src/public.rs#L205
// [ proof<256>| root<32>| epoch<32>| share_x<32>| share_y<32>| nullifier<32> | signal_len<8> | signal<var> ]
func (r RateLimitProof) serialize(data []byte) []byte {
lenPrefMsg := appendLength(data)
proofBytes := append(r.Proof[:], r.MerkleRoot[:]...)
proofBytes = append(proofBytes, r.Epoch[:]...)
proofBytes = append(proofBytes, r.ShareX[:]...)
proofBytes = append(proofBytes, r.ShareY[:]...)
proofBytes = append(proofBytes, r.Nullifier[:]...)
proofBytes = append(proofBytes, lenPrefMsg...)
return proofBytes
}