github.com/annchain/OG@v0.0.9/poc/vrf/vrf_test.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package vrf 15 16 import ( 17 "bytes" 18 "testing" 19 // "fmt" 20 "fmt" 21 "github.com/annchain/OG/common/hexutil" 22 ) 23 24 func TestHonestComplete(t *testing.T) { 25 sk, err := GenerateKey(nil) 26 if err != nil { 27 t.Fatal(err) 28 } 29 pk, _ := sk.Public() 30 alice := []byte("alice") 31 aliceVRF := sk.Compute(alice) 32 aliceVRFFromProof, aliceProof := sk.Prove(alice) 33 34 // fmt.Printf("pk: %X\n", pk) 35 // fmt.Printf("sk: %X\n", *sk) 36 // fmt.Printf("alice(bytes): %X\n", alice) 37 // fmt.Printf("aliceVRF: %X\n", aliceVRF) 38 // fmt.Printf("aliceProof: %X\n", aliceProof) 39 40 if !pk.Verify(alice, aliceVRF, aliceProof) { 41 t.Error("Gen -> Compute -> Prove -> Verify -> FALSE") 42 } 43 if !bytes.Equal(aliceVRF, aliceVRFFromProof) { 44 t.Error("Compute != Prove") 45 } 46 } 47 48 func TestConvertPrivateKeyToPublicKey(t *testing.T) { 49 sk, err := GenerateKey(nil) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 pk, ok := sk.Public() 55 if !ok { 56 t.Fatal("Couldn't obtain public key.") 57 } 58 if !bytes.Equal(sk[32:], pk) { 59 t.Fatal("Raw byte respresentation doesn't match public key.") 60 } 61 } 62 63 func TestFlipBitForgery(t *testing.T) { 64 sk, err := GenerateKey(nil) 65 if err != nil { 66 t.Fatal(err) 67 } 68 pk, _ := sk.Public() 69 alice := []byte("alice") 70 for i := 0; i < 32; i++ { 71 for j := uint(0); j < 8; j++ { 72 aliceVRF := sk.Compute(alice) 73 aliceVRF[i] ^= 1 << j 74 _, aliceProof := sk.Prove(alice) 75 if pk.Verify(alice, aliceVRF, aliceProof) { 76 t.Fatalf("forged by using aliceVRF[%d]^=%d:\n (sk=%x)", i, j, sk) 77 } 78 } 79 } 80 } 81 82 func sampleVectorTest(pk PublicKey, aliceVRF, aliceProof []byte, t *testing.T) { 83 alice := []byte{97, 108, 105, 99, 101} 84 85 // Positive test case 86 if !pk.Verify(alice, aliceVRF, aliceProof) { 87 t.Error("TestSampleVectors HonestVector Failed") 88 } 89 90 // Negative test cases - try increment the first byte of every vector 91 pk[0]++ 92 if pk.Verify(alice, aliceVRF, aliceProof) { 93 t.Error("TestSampleVectors ForgedVector (pk modified) Passed") 94 } 95 pk[0]-- 96 97 alice[0]++ 98 if pk.Verify(alice, aliceVRF, aliceProof) { 99 t.Error("TestSampleVectors ForgedVector (alice modified) Passed") 100 } 101 alice[0]-- 102 103 aliceVRF[0]++ 104 if pk.Verify(alice, aliceVRF, aliceProof) { 105 t.Error("TestSampleVectors ForgedVector (aliceVRF modified) Passed") 106 } 107 aliceVRF[0]-- 108 109 aliceProof[0]++ 110 if pk.Verify(alice, aliceVRF, aliceProof) { 111 t.Error("TestSampleVectors ForgedVector (aliceProof modified) Passed") 112 } 113 aliceProof[0]-- 114 } 115 116 func TestSampleVectorSets(t *testing.T) { 117 t.Skip("TODO: generate new test vectors or remove test") 118 var aliceVRF, aliceProof []byte 119 var pk []byte 120 121 // Following sets of test vectors are collected from TestHonestComplete(), 122 // and are used for testing the JS implementation of vrf.verify() 123 // Reference: https://github.com/yahoo/end-to-end/pull/58 124 125 pk = []byte{194, 191, 96, 139, 106, 249, 24, 253, 198, 131, 88, 169, 100, 231, 7, 211, 70, 171, 171, 207, 24, 30, 150, 114, 77, 124, 240, 123, 191, 14, 29, 111} 126 aliceVRF = []byte{68, 98, 55, 78, 153, 189, 11, 15, 8, 238, 132, 5, 53, 28, 232, 22, 222, 98, 21, 139, 89, 67, 111, 197, 213, 75, 86, 226, 178, 71, 245, 159} 127 aliceProof = []byte{49, 128, 4, 253, 103, 241, 164, 51, 21, 45, 168, 55, 18, 103, 22, 233, 245, 136, 242, 238, 113, 218, 160, 122, 129, 89, 72, 103, 250, 222, 3, 3, 239, 235, 93, 98, 173, 115, 168, 24, 222, 165, 186, 224, 138, 76, 201, 237, 130, 201, 47, 18, 191, 24, 61, 80, 113, 139, 246, 233, 23, 94, 177, 12, 193, 106, 38, 172, 66, 192, 22, 188, 177, 14, 144, 100, 38, 179, 96, 70, 55, 157, 80, 139, 145, 62, 94, 195, 181, 224, 183, 42, 64, 66, 145, 162} 128 sampleVectorTest(pk, aliceVRF, aliceProof, t) 129 130 pk = []byte{133, 36, 180, 21, 60, 103, 35, 92, 204, 245, 236, 174, 242, 50, 212, 69, 124, 230, 1, 106, 94, 95, 201, 55, 208, 252, 195, 13, 12, 96, 87, 170} 131 aliceVRF = []byte{35, 127, 188, 177, 246, 242, 213, 46, 16, 72, 1, 196, 69, 181, 160, 204, 69, 230, 17, 147, 251, 207, 203, 184, 154, 122, 118, 10, 144, 76, 229, 234} 132 aliceProof = []byte{253, 33, 80, 241, 250, 172, 198, 28, 16, 171, 161, 194, 110, 175, 158, 233, 250, 89, 35, 174, 221, 101, 98, 136, 32, 191, 82, 127, 92, 208, 199, 10, 123, 46, 70, 95, 56, 102, 63, 137, 53, 160, 128, 216, 134, 152, 87, 58, 19, 244, 167, 108, 144, 13, 97, 232, 207, 75, 107, 57, 193, 124, 231, 5, 242, 122, 182, 247, 155, 187, 86, 165, 114, 46, 188, 52, 21, 121, 238, 100, 85, 32, 119, 116, 250, 208, 32, 60, 145, 53, 145, 76, 84, 153, 185, 28} 133 sampleVectorTest(pk, aliceVRF, aliceProof, t) 134 135 pk = []byte{85, 126, 176, 228, 114, 43, 110, 223, 111, 129, 204, 38, 215, 110, 165, 148, 223, 232, 79, 254, 150, 107, 61, 29, 216, 14, 238, 104, 55, 163, 121, 185} 136 aliceVRF = []byte{171, 240, 42, 215, 128, 5, 247, 64, 164, 154, 198, 231, 6, 174, 207, 10, 95, 231, 117, 189, 88, 103, 72, 229, 43, 218, 184, 162, 44, 183, 196, 159} 137 aliceProof = []byte{99, 103, 243, 119, 251, 30, 21, 57, 69, 162, 192, 80, 7, 49, 244, 136, 13, 252, 150, 165, 215, 181, 55, 203, 141, 124, 197, 36, 20, 183, 239, 14, 238, 213, 240, 96, 181, 187, 24, 137, 152, 152, 38, 186, 80, 141, 72, 15, 209, 178, 60, 205, 22, 31, 101, 185, 225, 159, 22, 118, 84, 179, 95, 0, 124, 140, 237, 187, 8, 77, 233, 213, 207, 211, 251, 153, 71, 112, 61, 89, 53, 26, 195, 167, 254, 73, 218, 135, 145, 89, 12, 4, 16, 255, 63, 89} 138 sampleVectorTest(pk, aliceVRF, aliceProof, t) 139 140 } 141 142 func BenchmarkHashToGE(b *testing.B) { 143 alice := []byte("alice") 144 b.ResetTimer() 145 for n := 0; n < b.N; n++ { 146 hashToCurve(alice) 147 } 148 } 149 150 func BenchmarkCompute(b *testing.B) { 151 sk, err := GenerateKey(nil) 152 if err != nil { 153 b.Fatal(err) 154 } 155 alice := []byte("alice") 156 b.ResetTimer() 157 for n := 0; n < b.N; n++ { 158 sk.Compute(alice) 159 } 160 } 161 162 func BenchmarkProve(b *testing.B) { 163 sk, err := GenerateKey(nil) 164 if err != nil { 165 b.Fatal(err) 166 } 167 alice := []byte("alice") 168 b.ResetTimer() 169 for n := 0; n < b.N; n++ { 170 vrf, proof := sk.Prove(alice) 171 fmt.Printf("%s %s\n", hexutil.Encode(vrf), hexutil.Encode(proof)) 172 } 173 } 174 175 func BenchmarkVerify(b *testing.B) { 176 sk, err := GenerateKey(nil) 177 if err != nil { 178 b.Fatal(err) 179 } 180 alice := []byte("alice") 181 aliceVRF := sk.Compute(alice) 182 _, aliceProof := sk.Prove(alice) 183 pk, _ := sk.Public() 184 b.ResetTimer() 185 for n := 0; n < b.N; n++ { 186 pk.Verify(alice, aliceVRF, aliceProof) 187 } 188 }