github.com/consensys/gnark-crypto@v0.14.0/ecc/secp256k1/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 secp256k1
    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  
    91  type ibg1JacExtended interface {
    92  	bucketg1JacExtendedC2 |
    93  		bucketg1JacExtendedC3 |
    94  		bucketg1JacExtendedC4 |
    95  		bucketg1JacExtendedC5 |
    96  		bucketg1JacExtendedC6 |
    97  		bucketg1JacExtendedC7 |
    98  		bucketg1JacExtendedC8 |
    99  		bucketg1JacExtendedC9 |
   100  		bucketg1JacExtendedC10 |
   101  		bucketg1JacExtendedC11 |
   102  		bucketg1JacExtendedC12 |
   103  		bucketg1JacExtendedC13 |
   104  		bucketg1JacExtendedC14 |
   105  		bucketg1JacExtendedC15
   106  }