github.com/consensys/gnark-crypto@v0.14.0/ecc/bn254/kzg/marshal.go (about)

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