github.com/consensys/gnark-crypto@v0.14.0/ecc/bw6-633/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 bw6633
    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 bucketg1JacExtendedC4 [8]g1JacExtended
    77  type bucketg1JacExtendedC5 [16]g1JacExtended
    78  type bucketg1JacExtendedC6 [32]g1JacExtended
    79  type bucketg1JacExtendedC8 [128]g1JacExtended
    80  type bucketg1JacExtendedC12 [2048]g1JacExtended
    81  type bucketg1JacExtendedC16 [32768]g1JacExtended
    82  
    83  type ibg1JacExtended interface {
    84  	bucketg1JacExtendedC4 |
    85  		bucketg1JacExtendedC5 |
    86  		bucketg1JacExtendedC6 |
    87  		bucketg1JacExtendedC8 |
    88  		bucketg1JacExtendedC12 |
    89  		bucketg1JacExtendedC16
    90  }
    91  
    92  func processChunkG2Jacobian[B ibg2JacExtended](chunk uint64,
    93  	chRes chan<- g2JacExtended,
    94  	c uint64,
    95  	points []G2Affine,
    96  	digits []uint16,
    97  	sem chan struct{}) {
    98  
    99  	if sem != nil {
   100  		// if we are limited, wait for a token in the semaphore
   101  		<-sem
   102  	}
   103  
   104  	var buckets B
   105  	for i := 0; i < len(buckets); i++ {
   106  		buckets[i].setInfinity()
   107  	}
   108  
   109  	// for each scalars, get the digit corresponding to the chunk we're processing.
   110  	for i, digit := range digits {
   111  		if digit == 0 {
   112  			continue
   113  		}
   114  
   115  		// if msbWindow bit is set, we need to subtract
   116  		if digit&1 == 0 {
   117  			// add
   118  			buckets[(digit>>1)-1].addMixed(&points[i])
   119  		} else {
   120  			// sub
   121  			buckets[(digit >> 1)].subMixed(&points[i])
   122  		}
   123  	}
   124  
   125  	// reduce buckets into total
   126  	// total =  bucket[0] + 2*bucket[1] + 3*bucket[2] ... + n*bucket[n-1]
   127  
   128  	var runningSum, total g2JacExtended
   129  	runningSum.setInfinity()
   130  	total.setInfinity()
   131  	for k := len(buckets) - 1; k >= 0; k-- {
   132  		if !buckets[k].IsInfinity() {
   133  			runningSum.add(&buckets[k])
   134  		}
   135  		total.add(&runningSum)
   136  	}
   137  
   138  	if sem != nil {
   139  		// release a token to the semaphore
   140  		// before sending to chRes
   141  		sem <- struct{}{}
   142  	}
   143  
   144  	chRes <- total
   145  }
   146  
   147  // we declare the buckets as fixed-size array types
   148  // this allow us to allocate the buckets on the stack
   149  type bucketg2JacExtendedC4 [8]g2JacExtended
   150  type bucketg2JacExtendedC5 [16]g2JacExtended
   151  type bucketg2JacExtendedC6 [32]g2JacExtended
   152  type bucketg2JacExtendedC8 [128]g2JacExtended
   153  type bucketg2JacExtendedC12 [2048]g2JacExtended
   154  type bucketg2JacExtendedC16 [32768]g2JacExtended
   155  
   156  type ibg2JacExtended interface {
   157  	bucketg2JacExtendedC4 |
   158  		bucketg2JacExtendedC5 |
   159  		bucketg2JacExtendedC6 |
   160  		bucketg2JacExtendedC8 |
   161  		bucketg2JacExtendedC12 |
   162  		bucketg2JacExtendedC16
   163  }