github.com/consensys/gnark-crypto@v0.14.0/ecc/bls12-381/multiexp_jacobian.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 bls12381
    18  
    19  func processChunkG1Jacobian[B ibg1JacExtended](chunk uint64,
    20  	chRes chan<- g1JacExtended,
    21  	c uint64,
    22  	points []G1Affine,
    23  	digits []uint16,
    24  	sem chan struct{}) {
    25  
    26  	if sem != nil {
    27  		// if we are limited, wait for a token in the semaphore
    28  		<-sem
    29  	}
    30  
    31  	var buckets B
    32  	for i := 0; i < len(buckets); i++ {
    33  		buckets[i].setInfinity()
    34  	}
    35  
    36  	// for each scalars, get the digit corresponding to the chunk we're processing.
    37  	for i, digit := range digits {
    38  		if digit == 0 {
    39  			continue
    40  		}
    41  
    42  		// if msbWindow bit is set, we need to subtract
    43  		if digit&1 == 0 {
    44  			// add
    45  			buckets[(digit>>1)-1].addMixed(&points[i])
    46  		} else {
    47  			// sub
    48  			buckets[(digit >> 1)].subMixed(&points[i])
    49  		}
    50  	}
    51  
    52  	// reduce buckets into total
    53  	// total =  bucket[0] + 2*bucket[1] + 3*bucket[2] ... + n*bucket[n-1]
    54  
    55  	var runningSum, total g1JacExtended
    56  	runningSum.setInfinity()
    57  	total.setInfinity()
    58  	for k := len(buckets) - 1; k >= 0; k-- {
    59  		if !buckets[k].IsInfinity() {
    60  			runningSum.add(&buckets[k])
    61  		}
    62  		total.add(&runningSum)
    63  	}
    64  
    65  	if sem != nil {
    66  		// release a token to the semaphore
    67  		// before sending to chRes
    68  		sem <- struct{}{}
    69  	}
    70  
    71  	chRes <- total
    72  }
    73  
    74  // we declare the buckets as fixed-size array types
    75  // this allow us to allocate the buckets on the stack
    76  type bucketg1JacExtendedC3 [4]g1JacExtended
    77  type bucketg1JacExtendedC4 [8]g1JacExtended
    78  type bucketg1JacExtendedC5 [16]g1JacExtended
    79  type bucketg1JacExtendedC6 [32]g1JacExtended
    80  type bucketg1JacExtendedC7 [64]g1JacExtended
    81  type bucketg1JacExtendedC8 [128]g1JacExtended
    82  type bucketg1JacExtendedC9 [256]g1JacExtended
    83  type bucketg1JacExtendedC10 [512]g1JacExtended
    84  type bucketg1JacExtendedC11 [1024]g1JacExtended
    85  type bucketg1JacExtendedC12 [2048]g1JacExtended
    86  type bucketg1JacExtendedC13 [4096]g1JacExtended
    87  type bucketg1JacExtendedC14 [8192]g1JacExtended
    88  type bucketg1JacExtendedC15 [16384]g1JacExtended
    89  type bucketg1JacExtendedC16 [32768]g1JacExtended
    90  
    91  type ibg1JacExtended interface {
    92  	bucketg1JacExtendedC3 |
    93  		bucketg1JacExtendedC4 |
    94  		bucketg1JacExtendedC5 |
    95  		bucketg1JacExtendedC6 |
    96  		bucketg1JacExtendedC7 |
    97  		bucketg1JacExtendedC8 |
    98  		bucketg1JacExtendedC9 |
    99  		bucketg1JacExtendedC10 |
   100  		bucketg1JacExtendedC11 |
   101  		bucketg1JacExtendedC12 |
   102  		bucketg1JacExtendedC13 |
   103  		bucketg1JacExtendedC14 |
   104  		bucketg1JacExtendedC15 |
   105  		bucketg1JacExtendedC16
   106  }
   107  
   108  func processChunkG2Jacobian[B ibg2JacExtended](chunk uint64,
   109  	chRes chan<- g2JacExtended,
   110  	c uint64,
   111  	points []G2Affine,
   112  	digits []uint16,
   113  	sem chan struct{}) {
   114  
   115  	if sem != nil {
   116  		// if we are limited, wait for a token in the semaphore
   117  		<-sem
   118  	}
   119  
   120  	var buckets B
   121  	for i := 0; i < len(buckets); i++ {
   122  		buckets[i].setInfinity()
   123  	}
   124  
   125  	// for each scalars, get the digit corresponding to the chunk we're processing.
   126  	for i, digit := range digits {
   127  		if digit == 0 {
   128  			continue
   129  		}
   130  
   131  		// if msbWindow bit is set, we need to subtract
   132  		if digit&1 == 0 {
   133  			// add
   134  			buckets[(digit>>1)-1].addMixed(&points[i])
   135  		} else {
   136  			// sub
   137  			buckets[(digit >> 1)].subMixed(&points[i])
   138  		}
   139  	}
   140  
   141  	// reduce buckets into total
   142  	// total =  bucket[0] + 2*bucket[1] + 3*bucket[2] ... + n*bucket[n-1]
   143  
   144  	var runningSum, total g2JacExtended
   145  	runningSum.setInfinity()
   146  	total.setInfinity()
   147  	for k := len(buckets) - 1; k >= 0; k-- {
   148  		if !buckets[k].IsInfinity() {
   149  			runningSum.add(&buckets[k])
   150  		}
   151  		total.add(&runningSum)
   152  	}
   153  
   154  	if sem != nil {
   155  		// release a token to the semaphore
   156  		// before sending to chRes
   157  		sem <- struct{}{}
   158  	}
   159  
   160  	chRes <- total
   161  }
   162  
   163  // we declare the buckets as fixed-size array types
   164  // this allow us to allocate the buckets on the stack
   165  type bucketg2JacExtendedC3 [4]g2JacExtended
   166  type bucketg2JacExtendedC4 [8]g2JacExtended
   167  type bucketg2JacExtendedC5 [16]g2JacExtended
   168  type bucketg2JacExtendedC6 [32]g2JacExtended
   169  type bucketg2JacExtendedC7 [64]g2JacExtended
   170  type bucketg2JacExtendedC8 [128]g2JacExtended
   171  type bucketg2JacExtendedC9 [256]g2JacExtended
   172  type bucketg2JacExtendedC10 [512]g2JacExtended
   173  type bucketg2JacExtendedC11 [1024]g2JacExtended
   174  type bucketg2JacExtendedC12 [2048]g2JacExtended
   175  type bucketg2JacExtendedC13 [4096]g2JacExtended
   176  type bucketg2JacExtendedC14 [8192]g2JacExtended
   177  type bucketg2JacExtendedC15 [16384]g2JacExtended
   178  type bucketg2JacExtendedC16 [32768]g2JacExtended
   179  
   180  type ibg2JacExtended interface {
   181  	bucketg2JacExtendedC3 |
   182  		bucketg2JacExtendedC4 |
   183  		bucketg2JacExtendedC5 |
   184  		bucketg2JacExtendedC6 |
   185  		bucketg2JacExtendedC7 |
   186  		bucketg2JacExtendedC8 |
   187  		bucketg2JacExtendedC9 |
   188  		bucketg2JacExtendedC10 |
   189  		bucketg2JacExtendedC11 |
   190  		bucketg2JacExtendedC12 |
   191  		bucketg2JacExtendedC13 |
   192  		bucketg2JacExtendedC14 |
   193  		bucketg2JacExtendedC15 |
   194  		bucketg2JacExtendedC16
   195  }