github.com/aidoskuneenold/xmss@v0.0.0-20180805082438-6c701c4aef2c/xmss2_test.go (about) 1 // Copyright (c) 2018 Aidos Developer 2 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package xmss 22 23 import ( 24 "bytes" 25 "encoding/json" 26 "runtime" 27 "testing" 28 29 "github.com/AidosKuneen/numcpu" 30 "github.com/vmihailenco/msgpack" 31 ) 32 33 func TestXMSS2(t *testing.T) { 34 n := numcpu.NumCPU() 35 npref := runtime.GOMAXPROCS(n) 36 seed := generateSeed() 37 mer := NewMerkle(10, seed) 38 msg := []byte("This is a test for XMSS.") 39 var pre []byte 40 for i := 0; i < 1<<10; i++ { 41 sig := mer.Sign(msg) 42 if !Verify(sig, msg, mer.PublicKey()) { 43 t.Error("XMSS sig is incorrect") 44 } 45 if pre != nil && bytes.Equal(pre, sig) { 46 t.Error("sig must not be same") 47 } 48 pre = sig 49 } 50 runtime.GOMAXPROCS(npref) 51 } 52 53 func TestXMSS3(t *testing.T) { 54 n := numcpu.NumCPU() 55 npref := runtime.GOMAXPROCS(n) 56 seed := generateSeed() 57 mer := NewMerkle(10, seed) 58 msg := []byte("This is a test for XMSS.") 59 sig := mer.Sign(msg) 60 msg[0] = 0 61 if Verify(sig, msg, mer.PublicKey()) { 62 t.Error("XMSS sig is incorrect") 63 } 64 runtime.GOMAXPROCS(npref) 65 } 66 func TestXMSS4(t *testing.T) { 67 n := numcpu.NumCPU() 68 npref := runtime.GOMAXPROCS(n) 69 seed := generateSeed() 70 mer := NewMerkle(2, seed) 71 msg := []byte("This is a test for XMSS.") 72 sig := mer.Sign(msg) 73 if !Verify(sig, msg, mer.PublicKey()) { 74 t.Error("XMSS sig is incorrect") 75 } 76 msg[0] = 0 77 if Verify(sig, msg, mer.PublicKey()) { 78 t.Error("XMSS sig is incorrect") 79 } 80 runtime.GOMAXPROCS(npref) 81 } 82 func TestXMSS16(t *testing.T) { 83 n := numcpu.NumCPU() 84 npref := runtime.GOMAXPROCS(n) 85 seed := generateSeed() 86 mer := NewMerkle(16, seed) 87 msg := []byte("This is a test for XMSS height=16.") 88 sig := mer.Sign(msg) 89 if !Verify(sig, msg, mer.PublicKey()) { 90 t.Error("XMSS sig is incorrect") 91 } 92 runtime.GOMAXPROCS(npref) 93 } 94 95 func TestXMSSMarshal(t *testing.T) { 96 n := numcpu.NumCPU() 97 npref := runtime.GOMAXPROCS(n) 98 seed := generateSeed() 99 mer := NewMerkle(10, seed) 100 msg := []byte("This is a test for XMSS height=16.") 101 dat, err := json.Marshal(mer) 102 if err != nil { 103 t.Error(err) 104 } 105 t.Log("marshalled Merkle", string(dat)) 106 t.Log("len of marshalled Merkle", len(dat)) 107 mer2 := Merkle{} 108 if err = json.Unmarshal(dat, &mer2); err != nil { 109 t.Error(err) 110 } 111 sig := mer.Sign(msg) 112 sig2 := mer2.Sign(msg) 113 if !bytes.Equal(sig, sig2) { 114 t.Error("invlaid json marshal") 115 } 116 117 mdat, err := msgpack.Marshal(mer) 118 if err != nil { 119 t.Error(err) 120 } 121 t.Log("marshalled Merkle", len(mdat)) 122 mmer := Merkle{} 123 if err = msgpack.Unmarshal(mdat, &mmer); err != nil { 124 t.Error(err) 125 } 126 sig = mer.Sign(msg) 127 msig := mmer.Sign(msg) 128 if !bytes.Equal(sig, msig) { 129 t.Error("invlaid msgpack marshal") 130 } 131 132 var buf bytes.Buffer 133 enc := msgpack.NewEncoder(&buf).StructAsArray(true) 134 if err = enc.Encode(mer); err != nil { 135 t.Fatal(err) 136 } 137 t.Log("marshalled Merkle", buf.Len()) 138 dec := msgpack.NewDecoder(&buf) 139 mmer2 := Merkle{} 140 if err := dec.Decode(&mmer2); err != nil { 141 t.Fatal(err) 142 } 143 sig = mer.Sign(msg) 144 msig2 := mmer2.Sign(msg) 145 if !bytes.Equal(sig, msig2) { 146 t.Error("invlaid msgpack marshal") 147 } 148 149 runtime.GOMAXPROCS(npref) 150 } 151 152 func BenchmarkXMSS16(b *testing.B) { 153 n := numcpu.NumCPU() 154 npref := runtime.GOMAXPROCS(n) 155 seed := generateSeed() 156 b.ResetTimer() 157 _ = NewMerkle(16, seed) 158 runtime.GOMAXPROCS(npref) 159 } 160 func BenchmarkXMSS16Sign(b *testing.B) { 161 n := numcpu.NumCPU() 162 npref := runtime.GOMAXPROCS(n) 163 seed := generateSeed() 164 mer := NewMerkle(16, seed) 165 msg := []byte("This is a test for XMSS.") 166 b.ResetTimer() 167 for i := 0; i < b.N; i++ { 168 _ = mer.Sign(msg) 169 } 170 runtime.GOMAXPROCS(npref) 171 } 172 func BenchmarkXMSS16Veri(b *testing.B) { 173 n := numcpu.NumCPU() 174 npref := runtime.GOMAXPROCS(n) 175 seed := generateSeed() 176 mer := NewMerkle(16, seed) 177 msg := []byte("This is a test for XMSS.") 178 sig := mer.Sign(msg) 179 b.ResetTimer() 180 for i := 0; i < b.N; i++ { 181 Verify(sig, msg, mer.PublicKey()) 182 } 183 runtime.GOMAXPROCS(npref) 184 } 185 186 func BenchmarkXMSS20(b *testing.B) { 187 n := numcpu.NumCPU() 188 npref := runtime.GOMAXPROCS(n) 189 seed := generateSeed() 190 b.ResetTimer() 191 _ = NewMerkle(20, seed) 192 runtime.GOMAXPROCS(npref) 193 } 194 195 func BenchmarkXMSS10(b *testing.B) { 196 n := numcpu.NumCPU() 197 npref := runtime.GOMAXPROCS(n) 198 seed := generateSeed() 199 b.ResetTimer() 200 for i := 0; i < b.N; i++ { 201 _ = NewMerkle(10, seed) 202 } 203 runtime.GOMAXPROCS(npref) 204 } 205 func BenchmarkXMSS10Sign(b *testing.B) { 206 n := numcpu.NumCPU() 207 npref := runtime.GOMAXPROCS(n) 208 seed := generateSeed() 209 mer := NewMerkle(10, seed) 210 msg := []byte("This is a test for XMSS.") 211 b.ResetTimer() 212 for i := 0; i < b.N; i++ { 213 _ = mer.Sign(msg) 214 } 215 runtime.GOMAXPROCS(npref) 216 } 217 func BenchmarkXMSS10Veri(b *testing.B) { 218 n := numcpu.NumCPU() 219 npref := runtime.GOMAXPROCS(n) 220 seed := generateSeed() 221 mer := NewMerkle(10, seed) 222 msg := []byte("This is a test for XMSS.") 223 sig := mer.Sign(msg) 224 b.ResetTimer() 225 for i := 0; i < b.N; i++ { 226 Verify(sig, msg, mer.PublicKey()) 227 } 228 runtime.GOMAXPROCS(npref) 229 }