github.com/consensys/gnark@v0.11.0/constraint/commitment.go (about)

     1  package constraint
     2  
     3  import (
     4  	"math/big"
     5  )
     6  
     7  const CommitmentDst = "bsb22-commitment"
     8  
     9  type Groth16Commitment struct {
    10  	PublicAndCommitmentCommitted []int // PublicAndCommitmentCommitted sorted list of id's of public and commitment committed wires
    11  	PrivateCommitted             []int // PrivateCommitted sorted list of id's of private/internal committed wires
    12  	CommitmentIndex              int   // CommitmentIndex the wire index of the commitment
    13  	NbPublicCommitted            int
    14  }
    15  
    16  type PlonkCommitment struct {
    17  	Committed       []int // sorted list of id's of committed variables in groth16. in plonk, list of indexes of constraints defining committed values
    18  	CommitmentIndex int   // CommitmentIndex index of the constraint defining the commitment
    19  }
    20  
    21  type Commitment interface{}
    22  type Commitments interface{ CommitmentIndexes() []int }
    23  
    24  type Groth16Commitments []Groth16Commitment
    25  type PlonkCommitments []PlonkCommitment
    26  
    27  func (c Groth16Commitments) CommitmentIndexes() []int {
    28  	commitmentWires := make([]int, len(c))
    29  	for i := range c {
    30  		commitmentWires[i] = c[i].CommitmentIndex
    31  	}
    32  	return commitmentWires
    33  }
    34  
    35  func (c PlonkCommitments) CommitmentIndexes() []int {
    36  	commitmentWires := make([]int, len(c))
    37  	for i := range c {
    38  		commitmentWires[i] = c[i].CommitmentIndex
    39  	}
    40  	return commitmentWires
    41  }
    42  
    43  func (c Groth16Commitments) GetPrivateCommitted() [][]int {
    44  	res := make([][]int, len(c))
    45  	for i := range c {
    46  		res[i] = c[i].PrivateCommitted
    47  	}
    48  	return res
    49  }
    50  
    51  // GetPublicAndCommitmentCommitted returns the list of public and commitment committed wires
    52  // if committedTranslationList is not nil, commitment indexes are translated into their relative positions on the list plus the offset
    53  func (c Groth16Commitments) GetPublicAndCommitmentCommitted(committedTranslationList []int, offset int) [][]int {
    54  	res := make([][]int, len(c))
    55  	for i := range c {
    56  		res[i] = make([]int, len(c[i].PublicAndCommitmentCommitted))
    57  		copy(res[i], c[i].GetPublicCommitted())
    58  		translatedCommitmentCommitted := res[i][c[i].NbPublicCommitted:]
    59  		commitmentCommitted := c[i].GetCommitmentCommitted()
    60  		// convert commitment indexes to verifier understandable ones
    61  		if committedTranslationList == nil {
    62  			copy(translatedCommitmentCommitted, commitmentCommitted)
    63  		} else {
    64  			k := 0
    65  			for j := range translatedCommitmentCommitted {
    66  				for committedTranslationList[k] != commitmentCommitted[j] {
    67  					k++
    68  				} // find it in the translation list
    69  				translatedCommitmentCommitted[j] = k + offset
    70  			}
    71  		}
    72  	}
    73  	return res
    74  }
    75  
    76  func SerializeCommitment(privateCommitment []byte, publicCommitted []*big.Int, fieldByteLen int) []byte {
    77  
    78  	res := make([]byte, len(privateCommitment)+len(publicCommitted)*fieldByteLen)
    79  	copy(res, privateCommitment)
    80  
    81  	offset := len(privateCommitment)
    82  	for _, inJ := range publicCommitted {
    83  		inJ.FillBytes(res[offset : offset+fieldByteLen])
    84  		offset += fieldByteLen
    85  	}
    86  
    87  	return res
    88  }
    89  
    90  func NewCommitments(t SystemType) Commitments {
    91  	switch t {
    92  	case SystemR1CS:
    93  		return Groth16Commitments{}
    94  	case SystemSparseR1CS:
    95  		return PlonkCommitments{}
    96  	}
    97  	panic("unknown cs type")
    98  }
    99  
   100  func (c Groth16Commitment) GetPublicCommitted() []int {
   101  	return c.PublicAndCommitmentCommitted[:c.NbPublicCommitted]
   102  }
   103  
   104  func (c Groth16Commitment) GetCommitmentCommitted() []int {
   105  	return c.PublicAndCommitmentCommitted[c.NbPublicCommitted:]
   106  }