github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/secp256k1/z_init.go (about)

     1  // +build ignore
     2  
     3  /*
     4  This file is normally not used, as the data it would pre-compute is embedded
     5  into the source code, for fast library initialization.
     6  
     7  If you prefer to use this file (e.g. to have a smaller executabe), remove the
     8  "build ignore" directive above and delete the two source files:
     9    1) z_consts_5x52.go
    10    2) z_consts_10x26.go
    11  */
    12  
    13  package secp256k1
    14  
    15  import (
    16  	"os"
    17  	"fmt"
    18  	"time"
    19  )
    20  
    21  
    22  var (
    23  	pre_g, pre_g_128 []XY
    24  	prec [64][16]XY
    25  	fin XY
    26  )
    27  
    28  const SAVE = false
    29  
    30  func ecmult_start() {
    31  	sta := time.Now()
    32  
    33  	g := TheCurve.G
    34  
    35  	// calculate 2^128*generator
    36  	var g_128j XYZ
    37  	g_128j.SetXY(&g)
    38  
    39  	for i := 0; i < 128; i++ {
    40  		g_128j.Double(&g_128j)
    41  	}
    42  
    43  	var g_128 XY
    44  	g_128.SetXYZ(&g_128j)
    45  
    46      // precompute the tables with odd multiples
    47  	pre_g = g.precomp(WINDOW_G)
    48  	pre_g_128 = g_128.precomp(WINDOW_G)
    49  
    50  	// compute prec and fin
    51  	var gg XYZ
    52  	gg.SetXY(&g)
    53  	ad := g
    54  	var fn XYZ
    55  	fn.Infinity = true
    56  	for j:=0; j<64; j++ {
    57  		prec[j][0].SetXYZ(&gg)
    58  		fn.Add(&fn, &gg)
    59  		for i:=1; i<16; i++ {
    60  			gg.AddXY(&gg, &ad)
    61  			prec[j][i].SetXYZ(&gg)
    62  		}
    63  		ad = prec[j][15]
    64  	}
    65  	fin.SetXYZ(&fn)
    66  	fin.Neg(&fin)
    67  
    68  	if SAVE {
    69  		f, _ := os.Create("z_prec.go")
    70  		fmt.Fprintln(f, "package secp256k1\n\nvar prec = [64][16]XY {")
    71  		for j:=0; j<64; j++ {
    72  			fmt.Fprintln(f, " {")
    73  			for i:=0; i<16; i++ {
    74  				fmt.Fprintln(f, "{X:" + fe2str(&prec[j][i].X) + ", Y:" + fe2str(&prec[j][i].Y) + "},")
    75  			}
    76  			fmt.Fprintln(f, "},")
    77  		}
    78  		fmt.Fprintln(f, "}")
    79  		f.Close()
    80  	}
    81  
    82  	if SAVE {
    83  		f, _ := os.Create("z_pre_g.go")
    84  		fmt.Fprintln(f, "package secp256k1\n\nvar pre_g = []XY {")
    85  		for i := range pre_g {
    86  			fmt.Fprintln(f, "{X:" + fe2str(&pre_g[i].X) + ", Y:" + fe2str(&pre_g[i].Y) + "},")
    87  		}
    88  		fmt.Fprintln(f, "}")
    89  		f.Close()
    90  	}
    91  
    92  	if SAVE {
    93  		f, _ := os.Create("z_pre_g_128.go")
    94  		fmt.Fprintln(f, "package secp256k1\n\nvar pre_g_128 = []XY {")
    95  		for i := range pre_g_128 {
    96  			fmt.Fprintln(f, "{X:" + fe2str(&pre_g_128[i].X) + ", Y:" + fe2str(&pre_g_128[i].Y) + "},")
    97  		}
    98  		fmt.Fprintln(f, "}")
    99  		f.Close()
   100  	}
   101  
   102  	if SAVE {
   103  		f, _ := os.Create("z_fin.go")
   104  		fmt.Fprintln(f, "package secp256k1\n\nvar fin = XY {")
   105  		fmt.Fprintln(f, "X:" + fe2str(&fin.X) + ", Y:" + fe2str(&fin.Y) + ",")
   106  		fmt.Fprintln(f, "}")
   107  		f.Close()
   108  	}
   109  
   110  	println("start done in", time.Now().Sub(sta).String())
   111  }
   112  
   113  
   114  func fe2str_26(f *Field) (s string) {
   115  	s = fmt.Sprintf("Field{[10]uint32{0x%08x", f.n[0])
   116  	for i:=1; i<len(f.n); i++ {
   117  		s += fmt.Sprintf(", 0x%08x", f.n[i])
   118  	}
   119  	s += "}}"
   120  	return
   121  }
   122  
   123  func fe2str(f *Field) (s string) {
   124  	s = fmt.Sprintf("Field{[5]uint64{0x%08x", f.n[0])
   125  	for i:=1; i<len(f.n); i++ {
   126  		s += fmt.Sprintf(", 0x%08x", f.n[i])
   127  	}
   128  	s += "}}"
   129  	return
   130  }
   131  
   132  func init() {
   133  	ecmult_start()
   134  }