genprecomps.go 1.8 KB

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