github.com/consensys/gnark-crypto@v0.14.0/internal/generator/kzg/template/marshal.go.tmpl (about)

     1  
     2  import (
     3  	"io"
     4  	"github.com/consensys/gnark-crypto/ecc/{{ .Name }}"
     5  
     6  	"github.com/consensys/gnark-crypto/utils/unsafe"
     7  )
     8  
     9  // WriteTo writes binary encoding of the ProvingKey
    10  func (pk *ProvingKey) WriteTo(w io.Writer) (int64, error) {
    11  	return pk.writeTo(w)
    12  }
    13  
    14  // WriteRawTo writes binary encoding of ProvingKey to w without point compression
    15  func (pk *ProvingKey) WriteRawTo(w io.Writer) (int64, error) {
    16  	return pk.writeTo(w, {{.CurvePackage}}.RawEncoding())
    17  }
    18  
    19  func (pk *ProvingKey) writeTo(w io.Writer, options ...func(*{{.CurvePackage}}.Encoder)) (int64, error) {
    20  	// encode the ProvingKey
    21  	enc := {{ .CurvePackage }}.NewEncoder(w, options...)
    22  	if err := enc.Encode(pk.G1); err != nil {
    23  		return enc.BytesWritten(), err
    24  	}
    25  	return enc.BytesWritten(), nil
    26  }
    27  
    28  // WriteRawTo writes binary encoding of VerifyingKey to w without point compression
    29  func (vk *VerifyingKey) WriteRawTo(w io.Writer) (int64, error) {
    30  	return vk.writeTo(w, {{.CurvePackage}}.RawEncoding())
    31  }
    32  
    33  // WriteTo writes binary encoding of the VerifyingKey
    34  func (vk *VerifyingKey) WriteTo(w io.Writer) (int64, error) {
    35  	return vk.writeTo(w)
    36  }
    37  
    38  func (vk *VerifyingKey) writeTo(w io.Writer, options ...func(*{{.CurvePackage}}.Encoder)) (int64, error) {
    39  	// encode the VerifyingKey
    40  	enc := {{ .CurvePackage }}.NewEncoder(w, options...)
    41  
    42      {{- if eq .Name "bw6-756"}}
    43          nLines := 190
    44      {{- else if eq .Name "bw6-761"}}
    45          nLines := 189
    46      {{- else if eq .Name "bw6-633"}}
    47          nLines := 158
    48      {{- else if eq .Name "bn254"}}
    49          nLines := 66
    50      {{- else if or (eq .Name "bls12-381") (eq .Name "bls12-377") (eq .Name "bls12-378")}}
    51          nLines := 63
    52      {{- else }}
    53          nLines := 32
    54      {{- end}}
    55  	toEncode := make([]interface{}, 0, 4*nLines+3)
    56  	toEncode = append(toEncode, &vk.G2[0])
    57  	toEncode = append(toEncode, &vk.G2[1])
    58  	toEncode = append(toEncode, &vk.G1)
    59  	for k := 0; k < 2; k++ {
    60  		for j := 0; j < 2; j++ {
    61  			for i := nLines-1; i >= 0; i-- {
    62  				toEncode = append(toEncode, &vk.Lines[k][j][i].R0)
    63  				toEncode = append(toEncode, &vk.Lines[k][j][i].R1)
    64  			}
    65  		}
    66  	}
    67  
    68  	for _, v := range toEncode {
    69  		if err := enc.Encode(v); err != nil {
    70  			return enc.BytesWritten(), err
    71  		}
    72  	}
    73  
    74  	return enc.BytesWritten(), nil
    75  }
    76  
    77  // WriteDump writes the binary encoding of the entire SRS memory representation
    78  // It is meant to be use to achieve fast serialization/deserialization and
    79  // is not compatible with WriteTo / ReadFrom. It does not do any validation
    80  // and doesn't encode points in a canonical form.
    81  // @unsafe: this is platform dependent and may not be compatible with other platforms
    82  // @unstable: the format may change in the future
    83  // If maxPkPoints is provided, the number of points in the ProvingKey will be limited to maxPkPoints
    84  func (srs *SRS) WriteDump(w io.Writer, maxPkPoints ...int) error {
    85  	maxG1 := len(srs.Pk.G1)
    86  	if len(maxPkPoints) > 0 && maxPkPoints[0] < maxG1 && maxPkPoints[0] > 0{
    87  		maxG1 = maxPkPoints[0]
    88  	}
    89  	// first we write the VerifyingKey; it is small so we re-use WriteTo
    90  
    91  	if _, err := srs.Vk.writeTo(w, {{.CurvePackage}}.RawEncoding()); err != nil {
    92  		return err
    93  	}
    94  
    95  
    96  	// write the marker
    97  	if err := unsafe.WriteMarker(w); err != nil {
    98  		return err
    99  	}
   100  
   101  	// write the slice
   102  	return unsafe.WriteSlice(w, srs.Pk.G1[:maxG1])
   103  }
   104  
   105  // ReadDump deserializes the SRS from a reader, as written by WriteDump
   106  func (srs *SRS) ReadDump(r io.Reader, maxPkPoints ...int) error {
   107  	// first we read the VerifyingKey; it is small so we re-use ReadFrom
   108  	_, err := srs.Vk.ReadFrom(r)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	// read the marker
   114  	if err := unsafe.ReadMarker(r); err != nil {
   115  		return err
   116  	}
   117  
   118  	// read the slice
   119  	srs.Pk.G1, _, err = unsafe.ReadSlice[[]{{.CurvePackage}}.G1Affine](r, maxPkPoints...)
   120  	return err 
   121  }
   122  
   123  // WriteTo writes binary encoding of the entire SRS
   124  func (srs *SRS) WriteTo(w io.Writer) (int64, error) {
   125  	// encode the SRS
   126  	var pn, vn int64
   127  	var err error
   128  	if pn, err = srs.Pk.WriteTo(w); err != nil {
   129  		return pn, err
   130  	}
   131  	vn, err = srs.Vk.WriteTo(w)
   132  	return pn + vn, err
   133  }
   134  
   135  // WriteRawTo writes binary encoding of the entire SRS without point compression
   136  func (srs *SRS) WriteRawTo(w io.Writer) (int64, error) {
   137  	// encode the SRS
   138  	var pn, vn int64
   139  	var err error
   140  	if pn, err = srs.Pk.WriteRawTo(w); err != nil {
   141  		return pn, err
   142  	}
   143  	vn, err = srs.Vk.WriteRawTo(w)
   144  	return pn + vn, err
   145  }
   146  
   147  // ReadFrom decodes ProvingKey data from reader.
   148  func (pk *ProvingKey) ReadFrom(r io.Reader) (int64, error) {
   149  	// decode the ProvingKey
   150  	dec := {{ .CurvePackage }}.NewDecoder(r)
   151  	if err := dec.Decode(&pk.G1); err != nil {
   152  		return dec.BytesRead(), err
   153  	}
   154  	return dec.BytesRead(), nil
   155  }
   156  
   157  // UnsafeReadFrom decodes ProvingKey data from reader without checking
   158  // that point are in the correct subgroup.
   159  func (pk *ProvingKey) UnsafeReadFrom(r io.Reader) (int64, error) {
   160  	// decode the ProvingKey
   161  	dec := {{ .CurvePackage }}.NewDecoder(r, {{.CurvePackage}}.NoSubgroupChecks())
   162  	if err := dec.Decode(&pk.G1); err != nil {
   163  		return dec.BytesRead(), err
   164  	}
   165  	return dec.BytesRead(), nil
   166  }
   167  
   168  // ReadFrom decodes VerifyingKey data from reader.
   169  func (vk *VerifyingKey) ReadFrom(r io.Reader) (int64, error) {
   170  	// decode the VerifyingKey
   171  	dec := {{ .CurvePackage }}.NewDecoder(r)
   172  
   173      {{- if eq .Name "bw6-756"}}
   174          nLines := 190
   175      {{- else if eq .Name "bw6-761"}}
   176          nLines := 189
   177      {{- else if eq .Name "bw6-633"}}
   178          nLines := 158
   179      {{- else if eq .Name "bn254"}}
   180          nLines := 66
   181      {{- else if or (eq .Name "bls12-381") (eq .Name "bls12-377") (eq .Name "bls12-378")}}
   182          nLines := 63
   183      {{- else }}
   184          nLines := 32
   185      {{- end}}
   186  	toDecode := make([]interface{}, 0, 4*nLines+3)
   187  	toDecode = append(toDecode, &vk.G2[0])
   188  	toDecode = append(toDecode, &vk.G2[1])
   189  	toDecode = append(toDecode, &vk.G1)
   190  	for k := 0; k < 2; k++ {
   191  		for j := 0; j < 2; j++ {
   192  			for i := nLines-1; i >= 0; i-- {
   193  				toDecode = append(toDecode, &vk.Lines[k][j][i].R0)
   194  				toDecode = append(toDecode, &vk.Lines[k][j][i].R1)
   195  			}
   196  		}
   197  	}
   198  
   199  	for _, v := range toDecode {
   200  		if err := dec.Decode(v); err != nil {
   201  			return dec.BytesRead(), err
   202  		}
   203  	}
   204  
   205  	return dec.BytesRead(), nil
   206  }
   207  
   208  // ReadFrom decodes SRS data from reader.
   209  func (srs *SRS) ReadFrom(r io.Reader) (int64, error) {
   210  	// decode the VerifyingKey
   211  	var pn, vn int64
   212  	var err error
   213  	if pn, err = srs.Pk.ReadFrom(r); err != nil {
   214  		return pn, err
   215  	}
   216  	vn, err = srs.Vk.ReadFrom(r)
   217  	return pn+vn, err
   218  }
   219  
   220  // UnsafeReadFrom decodes SRS data from reader without sub group checks
   221  func (srs *SRS) UnsafeReadFrom(r io.Reader) (int64, error) {
   222  	// decode the VerifyingKey
   223  	var pn, vn int64
   224  	var err error
   225  	if pn, err = srs.Pk.UnsafeReadFrom(r); err != nil {
   226  		return pn, err
   227  	}
   228  	vn, err = srs.Vk.ReadFrom(r)
   229  	return pn+vn, err
   230  }
   231  
   232  
   233  
   234  // WriteTo writes binary encoding of a OpeningProof
   235  func (proof *OpeningProof) WriteTo(w io.Writer) (int64, error) {
   236  	enc := {{ .CurvePackage }}.NewEncoder(w)
   237  
   238  	toEncode := []interface{}{
   239  		&proof.H,
   240  		&proof.ClaimedValue,
   241  	}
   242  
   243  	for _, v := range toEncode {
   244  		if err := enc.Encode(v); err != nil {
   245  			return enc.BytesWritten(), err
   246  		}
   247  	}
   248  
   249  	return enc.BytesWritten(), nil
   250  }
   251  
   252  // ReadFrom decodes OpeningProof data from reader.
   253  func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) {
   254  	dec := {{ .CurvePackage }}.NewDecoder(r)
   255  
   256  	toDecode := []interface{}{
   257  		&proof.H,
   258  		&proof.ClaimedValue,
   259  	}
   260  
   261  	for _, v := range toDecode {
   262  		if err := dec.Decode(v); err != nil {
   263  			return dec.BytesRead(), err
   264  		}
   265  	}
   266  
   267  	return dec.BytesRead(), nil
   268  }
   269  
   270  // WriteTo writes binary encoding of a BatchOpeningProof
   271  func (proof *BatchOpeningProof) WriteTo(w io.Writer) (int64, error) {
   272  	enc := {{ .CurvePackage }}.NewEncoder(w)
   273  
   274  	toEncode := []interface{}{
   275  		&proof.H,
   276  		proof.ClaimedValues,
   277  	}
   278  
   279  	for _, v := range toEncode {
   280  		if err := enc.Encode(v); err != nil {
   281  			return enc.BytesWritten(), err
   282  		}
   283  	}
   284  
   285  	return enc.BytesWritten(), nil
   286  }
   287  
   288  // ReadFrom decodes BatchOpeningProof data from reader.
   289  func (proof *BatchOpeningProof) ReadFrom(r io.Reader) (int64, error) {
   290  	dec := {{ .CurvePackage }}.NewDecoder(r)
   291  	toDecode := []interface{}{
   292  		&proof.H,
   293  		&proof.ClaimedValues,
   294  	}
   295  
   296  	for _, v := range toDecode {
   297  		if err := dec.Decode(v); err != nil {
   298  			return dec.BytesRead(), err
   299  		}
   300  	}
   301  
   302  	return dec.BytesRead(), nil
   303  }