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