github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/secp256k1/schnorr_test.go (about) 1 package secp256k1 2 3 import ( 4 "bytes" 5 "encoding/csv" 6 "encoding/hex" 7 "os" 8 "testing" 9 ) 10 11 func TestSchnorr(t *testing.T) { 12 f, er := os.Open("../test/bip340_test_vectors.csv") 13 if er != nil { 14 t.Error(er.Error()) 15 return 16 } 17 cf := csv.NewReader(f) 18 tas, er := cf.ReadAll() 19 f.Close() 20 if er != nil { 21 t.Error(er.Error()) 22 return 23 } 24 for i := range tas { 25 if i == 0 { 26 continue // skip column names 27 } 28 priv, _ := hex.DecodeString(tas[i][1]) 29 pkey, _ := hex.DecodeString(tas[i][2]) 30 rand, _ := hex.DecodeString(tas[i][3]) 31 hasz, _ := hex.DecodeString(tas[i][4]) 32 sign, _ := hex.DecodeString(tas[i][5]) 33 34 if len(priv) == 32 { 35 _sig := SchnorrSign(hasz, priv, rand) 36 if !bytes.Equal(sign, _sig) { 37 println(hex.EncodeToString(_sig)) 38 println(hex.EncodeToString(sign)) 39 t.Error(i, "Generated signature mismatch") 40 return 41 } 42 } 43 44 if tas[i][6] == "FALSE" { 45 res := SchnorrVerify(pkey, sign, hasz) 46 if res { 47 t.Error("SchnorrVerify not failed") 48 } 49 continue 50 } 51 52 res := SchnorrVerify(pkey, sign, hasz) 53 if !res { 54 t.Error("SchnorrVerify failed", i, tas[i][0]) 55 } 56 hasz[0]++ 57 res = SchnorrVerify(pkey, sign, hasz) 58 if res { 59 t.Error("SchnorrVerify not failed while it should") 60 } 61 } 62 } 63 64 func BenchmarkSchnorrVerify(b *testing.B) { 65 pkey, _ := hex.DecodeString("DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659") 66 hash, _ := hex.DecodeString("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89") 67 sign, _ := hex.DecodeString("6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A") 68 b.ResetTimer() 69 for i := 0; i < b.N; i++ { 70 if !SchnorrVerify(pkey, sign, hash) { 71 b.Fatal("sig_verify failed") 72 } 73 } 74 } 75 76 func BenchmarkCheckPayToContract(b *testing.B) { 77 pkey, _ := hex.DecodeString("afaf8a67be00186668f74740e34ffce748139c2b73c9fbd2c1f33e48a612a75d") 78 base, _ := hex.DecodeString("f1cbd3f2430910916144d5d2bf63d48a6281e5b8e6ade31413adccff3d8839d4") 79 hash, _ := hex.DecodeString("93a760e87123883022cbd462ac40571176cf09d9d2c6168759fee6c2b079fdd8") 80 parity := true 81 b.ResetTimer() 82 for i := 0; i < b.N; i++ { 83 if !CheckPayToContract(pkey, base, hash, parity) { 84 b.Fatal("CheckPayToContract failed") 85 } 86 } 87 }