github.com/consensys/gnark-crypto@v0.14.0/ecc/bls24-315/fr/fft/bitreverse_test.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 fft
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/consensys/gnark-crypto/ecc/bls24-315/fr"
    24  )
    25  
    26  type bitReverseVariant struct {
    27  	name string
    28  	buf  []fr.Element
    29  	fn   func([]fr.Element)
    30  }
    31  
    32  const maxSizeBitReverse = 1 << 23
    33  
    34  var bitReverse = []bitReverseVariant{
    35  	{name: "bitReverseNaive", buf: make([]fr.Element, maxSizeBitReverse), fn: bitReverseNaive},
    36  	{name: "BitReverse", buf: make([]fr.Element, maxSizeBitReverse), fn: BitReverse},
    37  	{name: "bitReverseCobraInPlace", buf: make([]fr.Element, maxSizeBitReverse), fn: bitReverseCobraInPlace},
    38  }
    39  
    40  func TestBitReverse(t *testing.T) {
    41  
    42  	// generate a random []fr.Element array of size 2**20
    43  	pol := make([]fr.Element, maxSizeBitReverse)
    44  	one := fr.One()
    45  	pol[0].SetRandom()
    46  	for i := 1; i < maxSizeBitReverse; i++ {
    47  		pol[i].Add(&pol[i-1], &one)
    48  	}
    49  
    50  	// for each size, check that all the bitReverse functions fn compute the same result.
    51  	for size := 2; size <= maxSizeBitReverse; size <<= 1 {
    52  
    53  		// copy pol into the buffers
    54  		for _, data := range bitReverse {
    55  			copy(data.buf, pol[:size])
    56  		}
    57  
    58  		// compute bit reverse shuffling
    59  		for _, data := range bitReverse {
    60  			data.fn(data.buf[:size])
    61  		}
    62  
    63  		// all bitReverse.buf should hold the same result
    64  		for i := 0; i < size; i++ {
    65  			for j := 1; j < len(bitReverse); j++ {
    66  				if !bitReverse[0].buf[i].Equal(&bitReverse[j].buf[i]) {
    67  					t.Fatalf("bitReverse %s and %s do not compute the same result", bitReverse[0].name, bitReverse[j].name)
    68  				}
    69  			}
    70  		}
    71  
    72  		// bitReverse back should be identity
    73  		for _, data := range bitReverse {
    74  			data.fn(data.buf[:size])
    75  		}
    76  
    77  		for i := 0; i < size; i++ {
    78  			for j := 1; j < len(bitReverse); j++ {
    79  				if !bitReverse[0].buf[i].Equal(&bitReverse[j].buf[i]) {
    80  					t.Fatalf("(fn-1) bitReverse %s and %s do not compute the same result", bitReverse[0].name, bitReverse[j].name)
    81  				}
    82  			}
    83  		}
    84  	}
    85  
    86  }
    87  
    88  func BenchmarkBitReverse(b *testing.B) {
    89  	// generate a random []fr.Element array of size 2**22
    90  	pol := make([]fr.Element, maxSizeBitReverse)
    91  	one := fr.One()
    92  	pol[0].SetRandom()
    93  	for i := 1; i < maxSizeBitReverse; i++ {
    94  		pol[i].Add(&pol[i-1], &one)
    95  	}
    96  
    97  	// copy pol into the buffers
    98  	for _, data := range bitReverse {
    99  		copy(data.buf, pol[:maxSizeBitReverse])
   100  	}
   101  
   102  	// benchmark for each size, each bitReverse function
   103  	for size := 1 << 18; size <= maxSizeBitReverse; size <<= 1 {
   104  		for _, data := range bitReverse {
   105  			b.Run(fmt.Sprintf("name=%s/size=%d", data.name, size), func(b *testing.B) {
   106  				b.ResetTimer()
   107  				for j := 0; j < b.N; j++ {
   108  					data.fn(data.buf[:size])
   109  				}
   110  			})
   111  		}
   112  	}
   113  }