diff --git a/tree_test.go b/tree_test.go index 2de98a0..b178986 100644 --- a/tree_test.go +++ b/tree_test.go @@ -305,6 +305,76 @@ func TestGet(t *testing.T) { c.Check(gettedValue, qt.DeepEquals, BigIntToBytes(bLen, big.NewInt(int64(7*2)))) } +func TestPackAndUnpackSiblings(t *testing.T) { + c := qt.New(t) + + siblingsHex := []string{ + "0000000000000000000000000000000000000000000000000000000000000000", + "0100000000000000000000000000000000000000000000000000000000000000", + "0200000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0300000000000000000000000000000000000000000000000000000000000000", + "0400000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0500000000000000000000000000000000000000000000000000000000000000", + } + siblings := make([][]byte, len(siblingsHex)) + var err error + for i := 0; i < len(siblingsHex); i++ { + siblings[i], err = hex.DecodeString(siblingsHex[i]) + c.Assert(err, qt.IsNil) + } + + packed, err := PackSiblings(HashFunctionPoseidon, siblings) + c.Assert(err, qt.IsNil) + c.Assert(hex.EncodeToString(packed), qt.Equals, "a6000200c604"+ + "0100000000000000000000000000000000000000000000000000000000000000"+ + "0200000000000000000000000000000000000000000000000000000000000000"+ + "0300000000000000000000000000000000000000000000000000000000000000"+ + "0400000000000000000000000000000000000000000000000000000000000000"+ + "0500000000000000000000000000000000000000000000000000000000000000") + + unpacked, err := UnpackSiblings(HashFunctionPoseidon, packed) + c.Assert(err, qt.IsNil) + c.Assert(unpacked, qt.DeepEquals, siblings) + + // another test with other values + siblingsHex = []string{ + "1ce165cb1124ed3a0a94b4e212aaf7e8079f49b2fbef916bc290c593fda9092a", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "33018202c57d898b84338b16d1a4960e133c6a4d656cfec1bd62a9ea00611729", + "bdbee2bd246ba0259a37be9a8740b550eed01c566aff0dca9a07306bcf731d13", + "0000000000000000000000000000000000000000000000000000000000000000", + "0000000000000000000000000000000000000000000000000000000000000000", + "d43b04d7c2d0bba83b4291fea9ba0fec7830d17af54cbe9967fe90b8244d4e0d", + "0000000000000000000000000000000000000000000000000000000000000000", + "7def274dbb3a72dca44f01a8d9f2f21a5be84c171eecef8e2e4112e7277e262a", + } + siblings = make([][]byte, len(siblingsHex)) + for i := 0; i < len(siblingsHex); i++ { + siblings[i], err = hex.DecodeString(siblingsHex[i]) + c.Assert(err, qt.IsNil) + } + + packed, err = PackSiblings(HashFunctionPoseidon, siblings) + c.Assert(err, qt.IsNil) + c.Assert(hex.EncodeToString(packed), qt.Equals, "a60002003105"+ + "1ce165cb1124ed3a0a94b4e212aaf7e8079f49b2fbef916bc290c593fda9092a"+ + "33018202c57d898b84338b16d1a4960e133c6a4d656cfec1bd62a9ea00611729"+ + "bdbee2bd246ba0259a37be9a8740b550eed01c566aff0dca9a07306bcf731d13"+ + "d43b04d7c2d0bba83b4291fea9ba0fec7830d17af54cbe9967fe90b8244d4e0d"+ + "7def274dbb3a72dca44f01a8d9f2f21a5be84c171eecef8e2e4112e7277e262a") + + unpacked, err = UnpackSiblings(HashFunctionPoseidon, packed) + c.Assert(err, qt.IsNil) + c.Assert(unpacked, qt.DeepEquals, siblings) +} + func TestGenProofAndVerify(t *testing.T) { c := qt.New(t) database, err := badgerdb.New(badgerdb.Options{Path: c.TempDir()})