@@ -191,3 +191,43 @@ func TestParseNodeSingleSlot(t *testing.T) {
191191 t .Fatalf ("invalid commitment, got %x, expected %x" , lnd .commitment , ln .commitment )
192192 }
193193}
194+
195+ func TestLeaf_FirstC2Write_DoesNotMutate_GlobalIdentity (t * testing.T ) {
196+ // Build a leaf with exactly one value in c1 (suffix 7), so Serialize() picks single-slot encoding.
197+ values := make ([][]byte , NodeWidth )
198+ values [7 ] = word32 (0x11 )
199+
200+ ln , err := NewLeafNode (ffx32KeyTest [:StemSize ], values )
201+ if err != nil {
202+ t .Fatalf ("NewLeafNode: %v" , err )
203+ }
204+ ser , err := ln .Serialize ()
205+ if err != nil {
206+ t .Fatalf ("Serialize: %v" , err )
207+ }
208+ if got := ser [0 ]; got != singleSlotType {
209+ t .Fatalf ("expected single-slot encoding, got type=%d" , got )
210+ }
211+
212+ node , err := ParseNode (ser , 0 )
213+ if err != nil {
214+ t .Fatalf ("ParseNode: %v" , err )
215+ }
216+ leaf , ok := node .(* LeafNode )
217+ if ! ok {
218+ t .Fatalf ("expected *LeafNode, got %T" , node )
219+ }
220+
221+ // Snapshot global identity (value copy) to compare later.
222+ idBefore := banderwagon .Identity
223+
224+ // First write into c2 (suffix 128). This must NOT mutate banderwagon.Identity.
225+ val := word32 (0x03 )
226+ if err := leaf .updateCn (128 , val , leaf .c2 ); err != nil {
227+ t .Fatalf ("updateCn(c2): %v" , err )
228+ }
229+
230+ if idBefore != banderwagon .Identity {
231+ t .Fatalf ("BUG: first c2 write mutated global banderwagon.Identity" )
232+ }
233+ }
0 commit comments