git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/internal/blake3/bao_test.go (about) 1 package blake3_test 2 3 import ( 4 "bytes" 5 "os" 6 "testing" 7 8 "lukechampine.com/blake3" 9 ) 10 11 func TestBaoGolden(t *testing.T) { 12 data, err := os.ReadFile("testdata/vectors.json") 13 if err != nil { 14 t.Fatal(err) 15 } 16 goldenInterleaved, err := os.ReadFile("testdata/bao-golden.bao") 17 if err != nil { 18 t.Fatal(err) 19 } 20 goldenOutboard, err := os.ReadFile("testdata/bao-golden.obao") 21 if err != nil { 22 t.Fatal(err) 23 } 24 25 interleaved, root := blake3.BaoEncodeBuf(data, false) 26 if toHex(root[:]) != "6654fbd1836b531b25e2782c9cc9b792c80abb36b024f59db5d5f6bd3187ddfe" { 27 t.Errorf("bad root: %x", root) 28 } else if !bytes.Equal(interleaved, goldenInterleaved) { 29 t.Error("bad interleaved encoding") 30 } 31 32 outboard, root := blake3.BaoEncodeBuf(data, true) 33 if toHex(root[:]) != "6654fbd1836b531b25e2782c9cc9b792c80abb36b024f59db5d5f6bd3187ddfe" { 34 t.Errorf("bad root: %x", root) 35 } else if !bytes.Equal(outboard, goldenOutboard) { 36 t.Error("bad outboard encoding") 37 } 38 39 // test empty input 40 interleaved, root = blake3.BaoEncodeBuf(nil, false) 41 if toHex(root[:]) != "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262" { 42 t.Errorf("bad root: %x", root) 43 } else if toHex(interleaved[:]) != "0000000000000000" { 44 t.Errorf("bad interleaved encoding: %x", interleaved) 45 } else if !blake3.BaoVerifyBuf(interleaved, nil, root) { 46 t.Error("verify failed") 47 } 48 outboard, root = blake3.BaoEncodeBuf(nil, false) 49 if toHex(root[:]) != "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262" { 50 t.Errorf("bad root: %x", root) 51 } else if toHex(outboard[:]) != "0000000000000000" { 52 t.Errorf("bad outboard encoding: %x", outboard) 53 } else if !blake3.BaoVerifyBuf(nil, outboard, root) { 54 t.Error("verify failed") 55 } 56 } 57 58 func TestBaoInterleaved(t *testing.T) { 59 data, _ := os.ReadFile("testdata/vectors.json") 60 interleaved, root := blake3.BaoEncodeBuf(data, false) 61 if !blake3.BaoVerifyBuf(interleaved, nil, root) { 62 t.Fatal("verify failed") 63 } 64 badRoot := root 65 badRoot[0] ^= 1 66 if blake3.BaoVerifyBuf(interleaved, nil, badRoot) { 67 t.Fatal("verify succeeded with bad root") 68 } 69 badPrefix := append([]byte(nil), interleaved...) 70 badPrefix[0] ^= 1 71 if blake3.BaoVerifyBuf(badPrefix, nil, root) { 72 t.Fatal("verify succeeded with bad length prefix") 73 } 74 badCVs := append([]byte(nil), interleaved...) 75 badCVs[8] ^= 1 76 if blake3.BaoVerifyBuf(badCVs, nil, root) { 77 t.Fatal("verify succeeded with bad cv data") 78 } 79 badData := append([]byte(nil), interleaved...) 80 badData[len(badData)-1] ^= 1 81 if blake3.BaoVerifyBuf(badData, nil, root) { 82 t.Fatal("verify succeeded with bad content") 83 } 84 } 85 86 func TestBaoOutboard(t *testing.T) { 87 data, _ := os.ReadFile("testdata/vectors.json") 88 outboard, root := blake3.BaoEncodeBuf(data, true) 89 if !blake3.BaoVerifyBuf(data, outboard, root) { 90 t.Fatal("verify failed") 91 } 92 badRoot := root 93 badRoot[0] ^= 1 94 if blake3.BaoVerifyBuf(data, outboard, badRoot) { 95 t.Fatal("verify succeeded with bad root") 96 } 97 badPrefix := append([]byte(nil), outboard...) 98 badPrefix[0] ^= 1 99 if blake3.BaoVerifyBuf(data, badPrefix, root) { 100 t.Fatal("verify succeeded with bad length prefix") 101 } 102 badCVs := append([]byte(nil), outboard...) 103 badCVs[8] ^= 1 104 if blake3.BaoVerifyBuf(data, badCVs, root) { 105 t.Fatal("verify succeeded with bad cv data") 106 } 107 }