From f08f759bd65b08153d5da4c4ad935f73f230cbc9 Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Sat, 21 Dec 2024 08:49:50 -0600 Subject: [PATCH] Feat/write point slice ptr (#585) Co-authored-by: Arya Tabaie <15056835+Tabaie@users.noreply.github.com> --- ecc/bls12-377/marshal.go | 10 +++++++++- ecc/bls12-381/marshal.go | 10 +++++++++- ecc/bls24-315/marshal.go | 10 +++++++++- ecc/bls24-317/marshal.go | 10 +++++++++- ecc/bn254/marshal.go | 10 +++++++++- ecc/bw6-633/marshal.go | 10 +++++++++- ecc/bw6-761/marshal.go | 10 +++++++++- ecc/utils.go | 3 ++- internal/generator/ecc/template/marshal.go.tmpl | 6 +++++- 9 files changed, 70 insertions(+), 9 deletions(-) diff --git a/ecc/bls12-377/marshal.go b/ecc/bls12-377/marshal.go index 560b22a37e..e6be30a2ee 100644 --- a/ecc/bls12-377/marshal.go +++ b/ecc/bls12-377/marshal.go @@ -435,7 +435,7 @@ func NewEncoder(w io.Writer, options ...func(*Encoder)) *Encoder { } // Encode writes the binary encoding of v to the stream -// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine or []G2Affine +// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine, []G2Affine, *[]G1Affine or *[]G2Affine func (enc *Encoder) Encode(v interface{}) (err error) { if enc.raw { return enc.encodeRaw(v) @@ -565,6 +565,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encode(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -584,6 +586,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encode(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -702,6 +706,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encodeRaw(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -721,6 +727,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encodeRaw(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) diff --git a/ecc/bls12-381/marshal.go b/ecc/bls12-381/marshal.go index f647957143..3abb837ba6 100644 --- a/ecc/bls12-381/marshal.go +++ b/ecc/bls12-381/marshal.go @@ -435,7 +435,7 @@ func NewEncoder(w io.Writer, options ...func(*Encoder)) *Encoder { } // Encode writes the binary encoding of v to the stream -// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine or []G2Affine +// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine, []G2Affine, *[]G1Affine or *[]G2Affine func (enc *Encoder) Encode(v interface{}) (err error) { if enc.raw { return enc.encodeRaw(v) @@ -565,6 +565,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encode(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -584,6 +586,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encode(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -702,6 +706,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encodeRaw(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -721,6 +727,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encodeRaw(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) diff --git a/ecc/bls24-315/marshal.go b/ecc/bls24-315/marshal.go index 3b6c69362f..45ed61b8aa 100644 --- a/ecc/bls24-315/marshal.go +++ b/ecc/bls24-315/marshal.go @@ -435,7 +435,7 @@ func NewEncoder(w io.Writer, options ...func(*Encoder)) *Encoder { } // Encode writes the binary encoding of v to the stream -// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine or []G2Affine +// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine, []G2Affine, *[]G1Affine or *[]G2Affine func (enc *Encoder) Encode(v interface{}) (err error) { if enc.raw { return enc.encodeRaw(v) @@ -565,6 +565,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encode(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -584,6 +586,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encode(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -702,6 +706,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encodeRaw(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -721,6 +727,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encodeRaw(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) diff --git a/ecc/bls24-317/marshal.go b/ecc/bls24-317/marshal.go index cc267843bc..763eb62c94 100644 --- a/ecc/bls24-317/marshal.go +++ b/ecc/bls24-317/marshal.go @@ -435,7 +435,7 @@ func NewEncoder(w io.Writer, options ...func(*Encoder)) *Encoder { } // Encode writes the binary encoding of v to the stream -// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine or []G2Affine +// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine, []G2Affine, *[]G1Affine or *[]G2Affine func (enc *Encoder) Encode(v interface{}) (err error) { if enc.raw { return enc.encodeRaw(v) @@ -565,6 +565,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encode(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -584,6 +586,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encode(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -702,6 +706,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encodeRaw(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -721,6 +727,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encodeRaw(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) diff --git a/ecc/bn254/marshal.go b/ecc/bn254/marshal.go index 4db3316aa7..ea09bfbef0 100644 --- a/ecc/bn254/marshal.go +++ b/ecc/bn254/marshal.go @@ -400,7 +400,7 @@ func NewEncoder(w io.Writer, options ...func(*Encoder)) *Encoder { } // Encode writes the binary encoding of v to the stream -// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine or []G2Affine +// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine, []G2Affine, *[]G1Affine or *[]G2Affine func (enc *Encoder) Encode(v interface{}) (err error) { if enc.raw { return enc.encodeRaw(v) @@ -530,6 +530,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encode(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -549,6 +551,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encode(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -667,6 +671,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encodeRaw(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -686,6 +692,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encodeRaw(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) diff --git a/ecc/bw6-633/marshal.go b/ecc/bw6-633/marshal.go index edc60f546a..2463435468 100644 --- a/ecc/bw6-633/marshal.go +++ b/ecc/bw6-633/marshal.go @@ -435,7 +435,7 @@ func NewEncoder(w io.Writer, options ...func(*Encoder)) *Encoder { } // Encode writes the binary encoding of v to the stream -// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine or []G2Affine +// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine, []G2Affine, *[]G1Affine or *[]G2Affine func (enc *Encoder) Encode(v interface{}) (err error) { if enc.raw { return enc.encodeRaw(v) @@ -565,6 +565,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encode(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -584,6 +586,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encode(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -702,6 +706,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encodeRaw(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -721,6 +727,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encodeRaw(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) diff --git a/ecc/bw6-761/marshal.go b/ecc/bw6-761/marshal.go index ee4d185e2b..a173dd35e2 100644 --- a/ecc/bw6-761/marshal.go +++ b/ecc/bw6-761/marshal.go @@ -435,7 +435,7 @@ func NewEncoder(w io.Writer, options ...func(*Encoder)) *Encoder { } // Encode writes the binary encoding of v to the stream -// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine or []G2Affine +// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine, []G2Affine, *[]G1Affine or *[]G2Affine func (enc *Encoder) Encode(v interface{}) (err error) { if enc.raw { return enc.encodeRaw(v) @@ -565,6 +565,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encode(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -584,6 +586,8 @@ func (enc *Encoder) encode(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encode(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -702,6 +706,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encodeRaw(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -721,6 +727,8 @@ func (enc *Encoder) encodeRaw(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encodeRaw(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) diff --git a/ecc/utils.go b/ecc/utils.go index 78126fc1cc..c86c6295eb 100644 --- a/ecc/utils.go +++ b/ecc/utils.go @@ -178,7 +178,8 @@ func getVector(l *Lattice, a, b *big.Int) [2]big.Int { return res } -// NextPowerOfTwo returns the next power of 2 of n +// NextPowerOfTwo(n) = 2^⌈log₂(n)⌉ +// or 2ᵏ where 2ᵏ⁻¹ < n ≤ 2ᵏ func NextPowerOfTwo(n uint64) uint64 { c := bits.OnesCount64(n) if c == 0 { diff --git a/internal/generator/ecc/template/marshal.go.tmpl b/internal/generator/ecc/template/marshal.go.tmpl index d26de5fa79..2f1bd7b914 100644 --- a/internal/generator/ecc/template/marshal.go.tmpl +++ b/internal/generator/ecc/template/marshal.go.tmpl @@ -464,7 +464,7 @@ func NewEncoder(w io.Writer, options ...func(*Encoder)) *Encoder { // Encode writes the binary encoding of v to the stream -// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine or []G2Affine +// type must be uint64, *fr.Element, *fp.Element, *G1Affine, *G2Affine, []G1Affine, []G2Affine, *[]G1Affine or *[]G2Affine func (enc *Encoder) Encode(v interface{}) (err error) { if enc.raw { return enc.encodeRaw(v) @@ -645,6 +645,8 @@ func (enc *Encoder) encode{{- $.Raw}}(v interface{}) (err error) { } } return + case *[]G1Affine: + return enc.encode{{- $.Raw}}(*t) case []G1Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t))) @@ -664,6 +666,8 @@ func (enc *Encoder) encode{{- $.Raw}}(v interface{}) (err error) { } } return nil + case *[]G2Affine: + return enc.encode{{- $.Raw}}(*t) case []G2Affine: // write slice length err = binary.Write(enc.w, binary.BigEndian, uint32(len(t)))