github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/merkle/encoder_test.go (about) 1 package merkle 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/keybase/client/go/protocol/keybase1" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func dummy(i int) []byte { 12 return bytes.Repeat([]byte{byte(i)}, 32) 13 } 14 func dummy16(i int) (ret [16]byte) { 15 x := dummy(i) 16 copy(ret[:], x) 17 return ret 18 } 19 func dummy32(i int) (ret [32]byte) { 20 x := dummy(i) 21 copy(ret[:], x) 22 return ret 23 } 24 25 func TestEncode(t *testing.T) { 26 var tests = []struct { 27 desc string 28 encodingType EncodingType 29 leaf Leaf 30 key []byte 31 secret []byte 32 expectedBlinder []byte 33 }{ 34 { 35 desc: "basic", 36 encodingType: EncodingTypeBlindedSHA512_256v1, 37 leaf: Teamv1Leaf{ 38 TeamID: dummy16(0), 39 Tails: map[keybase1.SeqType]Teamv1HiddenTail{ 40 0: { 41 SigID: dummy(1), 42 LinkID: dummy32(2), 43 Seqno: 123, 44 }, 45 }, 46 }, 47 key: dummy(3), 48 secret: dummy(4), 49 expectedBlinder: []byte{0xed, 0x81, 0x7b, 0x7a, 0x7, 0xec, 0x6, 50 0x5f, 0x1a, 0x93, 0x4c, 0xf8, 0x6f, 0x9f, 0xf9, 0x53, 0xdd, 0xff, 51 0x3, 0x26, 0x63, 0xcd, 0x15, 0xf2, 0x9e, 0x2e, 0x55, 0x3e, 0x25, 52 0x67, 0x22, 0xd6}, 53 }, 54 { 55 desc: "ensure different secret produces different blinder with same leaf", 56 encodingType: EncodingTypeBlindedSHA512_256v1, 57 leaf: Teamv1Leaf{ 58 Tails: map[keybase1.SeqType]Teamv1HiddenTail{ 59 0: { 60 SigID: dummy(1), 61 LinkID: dummy32(2), 62 Seqno: 123, 63 }, 64 }, 65 }, 66 key: dummy(3), 67 secret: dummy(5), 68 expectedBlinder: []byte{0xca, 0xb7, 0xd3, 0x13, 0x3f, 0xb0, 0xb8, 69 0x24, 0x8e, 0xe6, 0xad, 0x70, 0xa9, 0x1c, 0x4, 0x62, 0x8f, 0x11, 70 0x35, 0x85, 0x6a, 0xea, 0x1f, 0x7c, 0x41, 0xb3, 0x33, 0xbd, 0x8b, 71 0x9b, 0xa8, 0xf8}, 72 }, 73 { 74 desc: "ensure different leaf produces different blinder with same secret", 75 encodingType: EncodingTypeBlindedSHA512_256v1, 76 leaf: Teamv1Leaf{ 77 TeamID: dummy16(0), 78 Tails: map[keybase1.SeqType]Teamv1HiddenTail{ 79 0: { 80 SigID: dummy(1), 81 LinkID: dummy32(3), 82 Seqno: 123, 83 }, 84 }, 85 }, 86 key: dummy(3), 87 secret: dummy(4), 88 expectedBlinder: []byte{0x8c, 0x9b, 0x7e, 0xdf, 0xc4, 0x3d, 0xdd, 89 0xa8, 0xcf, 0xb2, 0x20, 0xe0, 0x11, 0x7c, 0x6a, 0x65, 0x59, 0x38, 90 0x18, 0x43, 0x8a, 0x2e, 0x16, 0xc1, 0xbc, 0xa9, 0xa, 0xac, 0xd2, 91 0xdb, 0xd5, 0x58}, 92 }, 93 } 94 for _, tt := range tests { 95 e := NewEncoder(tt.encodingType) 96 t.Run(tt.desc, func(t *testing.T) { 97 blinder, err := e.Encode(tt.leaf, NewKey(tt.key), NewSecret(tt.secret)) 98 require.NoError(t, err) 99 require.Equal(t, tt.expectedBlinder, blinder) 100 101 preimage, err := e.BlindedPreimage(tt.leaf, NewKey(tt.key), NewSecret(tt.secret)) 102 require.NoError(t, err) 103 blinder2, err := e.Hash(preimage) 104 require.NoError(t, err) 105 require.Equal(t, blinder, blinder2, "got same blinder via validation route") 106 }) 107 } 108 109 } 110 111 func TestGenerateSecret(t *testing.T) { 112 e := NewEncoder(EncodingTypeBlindedSHA512_256v1) 113 x, err := e.GenerateSecret() 114 require.NoError(t, err) 115 y, err := e.GenerateSecret() 116 require.NoError(t, err) 117 118 require.NotEqual(t, x, y) 119 }