github.com/consensys/gnark-crypto@v0.14.0/ecc/bn254/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 bn254
    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 bucketg1JacExtendedC6 [32]g1JacExtended
    81  type bucketg1JacExtendedC7 [64]g1JacExtended
    82  type bucketg1JacExtendedC8 [128]g1JacExtended
    83  type bucketg1JacExtendedC9 [256]g1JacExtended
    84  type bucketg1JacExtendedC10 [512]g1JacExtended
    85  type bucketg1JacExtendedC11 [1024]g1JacExtended
    86  type bucketg1JacExtendedC12 [2048]g1JacExtended
    87  type bucketg1JacExtendedC13 [4096]g1JacExtended
    88  type bucketg1JacExtendedC14 [8192]g1JacExtended
    89  type bucketg1JacExtendedC15 [16384]g1JacExtended
    90  type bucketg1JacExtendedC16 [32768]g1JacExtended
    91  
    92  type ibg1JacExtended interface {
    93  	bucketg1JacExtendedC2 |
    94  		bucketg1JacExtendedC3 |
    95  		bucketg1JacExtendedC4 |
    96  		bucketg1JacExtendedC5 |
    97  		bucketg1JacExtendedC6 |
    98  		bucketg1JacExtendedC7 |
    99  		bucketg1JacExtendedC8 |
   100  		bucketg1JacExtendedC9 |
   101  		bucketg1JacExtendedC10 |
   102  		bucketg1JacExtendedC11 |
   103  		bucketg1JacExtendedC12 |
   104  		bucketg1JacExtendedC13 |
   105  		bucketg1JacExtendedC14 |
   106  		bucketg1JacExtendedC15 |
   107  		bucketg1JacExtendedC16
   108  }
   109  
   110  func processChunkG2Jacobian[B ibg2JacExtended](chunk uint64,
   111  	chRes chan<- g2JacExtended,
   112  	c uint64,
   113  	points []G2Affine,
   114  	digits []uint16,
   115  	sem chan struct{}) {
   116  
   117  	if sem != nil {
   118  		// if we are limited, wait for a token in the semaphore
   119  		<-sem
   120  	}
   121  
   122  	var buckets B
   123  	for i := 0; i < len(buckets); i++ {
   124  		buckets[i].setInfinity()
   125  	}
   126  
   127  	// for each scalars, get the digit corresponding to the chunk we're processing.
   128  	for i, digit := range digits {
   129  		if digit == 0 {
   130  			continue
   131  		}
   132  
   133  		// if msbWindow bit is set, we need to subtract
   134  		if digit&1 == 0 {
   135  			// add
   136  			buckets[(digit>>1)-1].addMixed(&points[i])
   137  		} else {
   138  			// sub
   139  			buckets[(digit >> 1)].subMixed(&points[i])
   140  		}
   141  	}
   142  
   143  	// reduce buckets into total
   144  	// total =  bucket[0] + 2*bucket[1] + 3*bucket[2] ... + n*bucket[n-1]
   145  
   146  	var runningSum, total g2JacExtended
   147  	runningSum.setInfinity()
   148  	total.setInfinity()
   149  	for k := len(buckets) - 1; k >= 0; k-- {
   150  		if !buckets[k].IsInfinity() {
   151  			runningSum.add(&buckets[k])
   152  		}
   153  		total.add(&runningSum)
   154  	}
   155  
   156  	if sem != nil {
   157  		// release a token to the semaphore
   158  		// before sending to chRes
   159  		sem <- struct{}{}
   160  	}
   161  
   162  	chRes <- total
   163  }
   164  
   165  // we declare the buckets as fixed-size array types
   166  // this allow us to allocate the buckets on the stack
   167  type bucketg2JacExtendedC2 [2]g2JacExtended
   168  type bucketg2JacExtendedC3 [4]g2JacExtended
   169  type bucketg2JacExtendedC4 [8]g2JacExtended
   170  type bucketg2JacExtendedC5 [16]g2JacExtended
   171  type bucketg2JacExtendedC6 [32]g2JacExtended
   172  type bucketg2JacExtendedC7 [64]g2JacExtended
   173  type bucketg2JacExtendedC8 [128]g2JacExtended
   174  type bucketg2JacExtendedC9 [256]g2JacExtended
   175  type bucketg2JacExtendedC10 [512]g2JacExtended
   176  type bucketg2JacExtendedC11 [1024]g2JacExtended
   177  type bucketg2JacExtendedC12 [2048]g2JacExtended
   178  type bucketg2JacExtendedC13 [4096]g2JacExtended
   179  type bucketg2JacExtendedC14 [8192]g2JacExtended
   180  type bucketg2JacExtendedC15 [16384]g2JacExtended
   181  type bucketg2JacExtendedC16 [32768]g2JacExtended
   182  
   183  type ibg2JacExtended interface {
   184  	bucketg2JacExtendedC2 |
   185  		bucketg2JacExtendedC3 |
   186  		bucketg2JacExtendedC4 |
   187  		bucketg2JacExtendedC5 |
   188  		bucketg2JacExtendedC6 |
   189  		bucketg2JacExtendedC7 |
   190  		bucketg2JacExtendedC8 |
   191  		bucketg2JacExtendedC9 |
   192  		bucketg2JacExtendedC10 |
   193  		bucketg2JacExtendedC11 |
   194  		bucketg2JacExtendedC12 |
   195  		bucketg2JacExtendedC13 |
   196  		bucketg2JacExtendedC14 |
   197  		bucketg2JacExtendedC15 |
   198  		bucketg2JacExtendedC16
   199  }