genprecomps.go raw

   1  // Copyright 2015 The btcsuite developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  // This file is ignored during the regular build due to the following build tag.
   6  // It is called by go generate and used to automatically generate pre-computed
   7  // tables used to accelerate operations.
   8  
   9  package main
  10  
  11  import (
  12  	"bytes"
  13  	"compress/zlib"
  14  	"encoding/base64"
  15  	"fmt"
  16  	"log"
  17  	"os"
  18  
  19  	"github.com/p9c/p9/pkg/apputil"
  20  	"github.com/p9c/p9/pkg/ecc"
  21  )
  22  
  23  func main() {
  24  	ss := "../bytepoints/secp256k1.go"
  25  	apputil.EnsureDir(ss)
  26  	fi, err := os.Create(ss)
  27  	if err != nil {
  28  		log.Fatal(err)
  29  	}
  30  	defer fi.Close()
  31  
  32  	// Compress the serialized byte points.
  33  	serialized := ecc.S256().SerializedBytePoints()
  34  	var compressed bytes.Buffer
  35  	w := zlib.NewWriter(&compressed)
  36  	if _, err := w.Write(serialized); err != nil {
  37  		fmt.Println(err)
  38  		os.Exit(1)
  39  	}
  40  	w.Close()
  41  
  42  	// Encode the compressed byte points with base64.
  43  	encoded := make([]byte, base64.StdEncoding.EncodedLen(compressed.Len()))
  44  	base64.StdEncoding.Encode(encoded, compressed.Bytes())
  45  
  46  	fmt.Fprintln(fi, "// Copyright (c) 2015 The btcsuite developers")
  47  	fmt.Fprintln(fi, "// Use of this source code is governed by an ISC")
  48  	fmt.Fprintln(fi, "// license that can be found in the LICENSE file.")
  49  	fmt.Fprintln(fi)
  50  	// fmt.Fprintln(fi,"// +build !gensecp256k1")
  51  	// fmt.Fprintln(fi)
  52  	fmt.Fprintln(fi, "package btcec")
  53  	fmt.Fprintln(fi)
  54  	fmt.Fprintln(fi, "// Auto-generated file (see ../genprecomps.go)")
  55  	fmt.Fprintln(fi, "// DO NOT EDIT")
  56  	fmt.Fprintln(fi)
  57  	fmt.Fprintf(fi, "var secp256k1BytePoints = %q\n", string(encoded))
  58  
  59  	a1, b1, a2, b2 := ecc.S256().EndomorphismVectors()
  60  	fmt.Println("The following values are the computed linearly " +
  61  		"independent vectors needed to make use of the secp256k1 " +
  62  		"endomorphism:")
  63  	fmt.Printf("a1: %x\n", a1)
  64  	fmt.Printf("b1: %x\n", b1)
  65  	fmt.Printf("a2: %x\n", a2)
  66  	fmt.Printf("b2: %x\n", b2)
  67  }
  68