main.go raw

   1  // +build js,wasm
   2  
   3  package main
   4  
   5  import (
   6  	"encoding/hex"
   7  	"fmt"
   8  	"os"
   9  
  10  	"git.smesh.lol/orly/pkg/p256k1"
  11  )
  12  
  13  func main() {
  14  	type vec struct {
  15  		sec string
  16  		x   string
  17  		pre byte
  18  	}
  19  	vectors := []vec{
  20  		{"0000000000000000000000000000000000000000000000000000000000000001",
  21  			"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", 0x02},
  22  		{"0000000000000000000000000000000000000000000000000000000000000002",
  23  			"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5", 0x02},
  24  		{"0000000000000000000000000000000000000000000000000000000000000003",
  25  			"f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9", 0x02},
  26  		{"0000000000000000000000000000000000000000000000000000000000000007",
  27  			"5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc", 0x02},
  28  		{"e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35",
  29  			"39a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2", 0x03},
  30  		{"deadbeefcafebabe1234567890abcdef0123456789abcdef0123456789abcdef",
  31  			"e3f23b3410abfe4d3e46cf47ca5f9ef47f2b042414a6d2aa527858fce0ad1146", 0x02},
  32  		{"7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0",
  33  			"00000000000000000000003b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63", 0x03},
  34  		{"5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72",
  35  			"bcace2e99da01887ab0102b696902325872844067f15e98da7bba04400b88fcb", 0x02},
  36  	}
  37  
  38  	fails := 0
  39  
  40  	fmt.Println("=== GLV path (ECPubkeyCreate) ===")
  41  	for _, v := range vectors {
  42  		secBytes, _ := hex.DecodeString(v.sec)
  43  		var pub p256k1.PublicKey
  44  		if err := p256k1.ECPubkeyCreate(&pub, secBytes); err != nil {
  45  			fmt.Printf("FAIL [%s] err: %v\n", v.sec[:8], err)
  46  			fails++
  47  			continue
  48  		}
  49  		var ser [33]byte
  50  		p256k1.ECPubkeySerialize(ser[:], &pub, p256k1.ECCompressed)
  51  		gotX := hex.EncodeToString(ser[1:33])
  52  		ok := gotX == v.x && ser[0] == v.pre
  53  		if ok {
  54  			fmt.Printf("PASS [%s]\n", v.sec[:8])
  55  		} else {
  56  			fmt.Printf("FAIL [%s] got 0x%02x:%s\n", v.sec[:8], ser[0], gotX)
  57  			fails++
  58  		}
  59  	}
  60  
  61  	fmt.Println("\n=== Simple path (non-GLV) ===")
  62  	for _, v := range vectors {
  63  		secBytes, _ := hex.DecodeString(v.sec)
  64  		ser := p256k1.TestEcmultGenSimple(secBytes)
  65  		gotX := hex.EncodeToString(ser[1:33])
  66  		ok := gotX == v.x && ser[0] == v.pre
  67  		if ok {
  68  			fmt.Printf("PASS [%s]\n", v.sec[:8])
  69  		} else {
  70  			fmt.Printf("FAIL [%s] got 0x%02x:%s\n", v.sec[:8], ser[0], gotX)
  71  			fails++
  72  		}
  73  	}
  74  
  75  	if fails > 0 {
  76  		fmt.Printf("\n%d FAILED\n", fails)
  77  		os.Exit(1)
  78  	}
  79  	fmt.Println("\nALL PASS")
  80  }
  81