package qrcsigner import ( "crypto/keccak256" "crypto/sha256" "encoding/hex" "testing" "gno.land/p/nym-rootlatch001/qrc/v1" ) // The commit of the first test vector (see vectors/v1.json). var testID = qrc.ID{ ChainID: "dev", Registry: "gno.land/r/nym-rootlatch001/qrcregistry/v1", Account: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", Generation: 0, } func testEntropy() []byte { entropy := make([]byte, EntropySize) for i := range entropy { entropy[i] = byte(i) } return entropy } func hexOf(b [qrc.HashSize]byte) string { return hex.EncodeToString(b[:]) } // TestVector checks this implementation against the first test vector, which // the Go build of this same code wrote and which the TypeScript library is // tested against: three runtimes, one result. func TestVector(t *testing.T) { kit := New(testEntropy(), testID, qrc.MinHeight) rootSHA256, rootKeccak256 := kit.Roots() if got := hexOf(rootSHA256); got != "7d5cd56c59431c38474c804eef1069aa4b2bd10209ff415c5aaef9becc126d6c" { t.Errorf("sha256 root = %s", got) } if got := hexOf(rootKeccak256); got != "eb2fd77bc5e8be3069f1269629c008abe17c0eed9d56a128c4dd0f451e359ed3" { t.Errorf("keccak256 root = %s", got) } key, _ := hex.DecodeString("96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7b413f47d13ee2fe6c845b2ee141af81d") msg := qrc.BindMessage(testID, 0, "ML-DSA-65", key) proofSHA256, proofKeccak256 := kit.Sign(0, msg) if got := hexOf(sha256.Sum256(proofSHA256)); got != "5230669750006c8d68817925d27db1e9c865fee2f54372b3661e57e0aa04b93c" { t.Errorf("sha256 of the sha256 proof = %s", got) } if got := hexOf(sha256.Sum256(proofKeccak256)); got != "06b65a004eb7ed29ee594da58893ea78281a845d6874004d155f4d06b240042e" { t.Errorf("sha256 of the keccak256 proof = %s", got) } if err := qrc.Verify(qrc.SHA256, rootSHA256, qrc.MinHeight, 0, msg, proofSHA256); err != nil { t.Errorf("sha256: %v", err) } if err := qrc.Verify(qrc.Keccak256, rootKeccak256, qrc.MinHeight, 0, msg, proofKeccak256); err != nil { t.Errorf("keccak256: %v", err) } } func TestSignVerify(t *testing.T) { kit := New(testEntropy(), testID, qrc.MinHeight) rootSHA256, rootKeccak256 := kit.Roots() if rootSHA256 == rootKeccak256 { t.Fatalf("the two commits are equal") } for _, leaf := range []int{0, 1, 42, 63} { msg := qrc.BindMessage(testID, uint32(leaf), "ML-DSA-65", []byte("public key bytes")) proofSHA256, proofKeccak256 := kit.Sign(leaf, msg) if err := qrc.Verify(qrc.SHA256, rootSHA256, qrc.MinHeight, leaf, msg, proofSHA256); err != nil { t.Fatalf("sha256, leaf %d: %v", leaf, err) } if err := qrc.Verify(qrc.Keccak256, rootKeccak256, qrc.MinHeight, leaf, msg, proofKeccak256); err != nil { t.Fatalf("keccak256, leaf %d: %v", leaf, err) } // A proof is good for its own tree, leaf and message only. if err := qrc.Verify(qrc.Keccak256, rootKeccak256, qrc.MinHeight, leaf, msg, proofSHA256); err != qrc.ErrMismatch { t.Fatalf("leaf %d: sha256 proof against the keccak256 commit: %v", leaf, err) } if err := qrc.Verify(qrc.SHA256, rootSHA256, qrc.MinHeight, leaf^1, msg, proofSHA256); err != qrc.ErrMismatch { t.Fatalf("leaf %d: proof accepted for its neighbour: %v", leaf, err) } } } // hmacSHA256 is HMAC as RFC 2104 writes it, for a key of at most 64 bytes. func hmacSHA256(key, msg []byte) [qrc.HashSize]byte { inner := make([]byte, 64, 64+len(msg)) outer := make([]byte, 64, 64+qrc.HashSize) copy(inner, key) copy(outer, key) for i := 0; i < 64; i++ { inner[i] ^= 0x36 outer[i] ^= 0x5c } digest := sha256.Sum256(append(inner, msg...)) return sha256.Sum256(append(outer, digest[:]...)) } // TestDerivation recomputes secrets from the formulas of the package // documentation, written out the long way, and compares them with what the // trees use. func TestDerivation(t *testing.T) { entropy := testEntropy() // RFC 4231, test case 2: the HMAC of this test is the standard one. if got := hexOf(hmacSHA256([]byte("Jefe"), []byte("what do ya want for nothing?"))); got != "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" { t.Fatalf("HMAC-SHA256 test case = %s", got) } seedSHA256 := keccak256.Sum256(append([]byte("QuantumRecoveryCommit/v1/seed/sha256\x00"), entropy...)) seedKeccak256 := sha256.Sum256(append([]byte("QuantumRecoveryCommit/v1/seed/keccak256\x00"), entropy...)) if seedSHA256 != treeSeed(qrc.SHA256, entropy) || seedKeccak256 != treeSeed(qrc.Keccak256, entropy) { t.Fatalf("tree seeds differ from their definition") } treeSHA256 := NewTree(qrc.SHA256, entropy, testID, qrc.MinHeight) treeKeccak256 := NewTree(qrc.Keccak256, entropy, testID, qrc.MinHeight) for _, tc := range []struct{ leaf, bit, side int }{ {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {1, 0, 0}, {63, 255, 1}, {37, 128, 0}, {5, 255, 0}, {62, 7, 1}, } { info := append([]byte("QuantumRecoveryCommit/v1/secret\x00"), testID.Bytes()...) info = append(info, byte(tc.leaf>>24), byte(tc.leaf>>16), byte(tc.leaf>>8), byte(tc.leaf), byte(tc.bit>>8), byte(tc.bit), byte(tc.side)) if want := hmacSHA256(seedSHA256[:], info); treeSHA256.secret(tc.leaf, tc.bit, tc.side) != want { t.Errorf("sha256 secret (%d, %d, %d) differs from its definition", tc.leaf, tc.bit, tc.side) } if want := keccak256.Sum256(append(seedKeccak256[:], info...)); treeKeccak256.secret(tc.leaf, tc.bit, tc.side) != want { t.Errorf("keccak256 secret (%d, %d, %d) differs from its definition", tc.leaf, tc.bit, tc.side) } } } // TestProofLayout checks a proof against the description of package qrc: // in every pair, the secret on the side the digest bit selects and the hash // of the other secret, then the siblings up to the root. func TestProofLayout(t *testing.T) { const leaf = 21 msg := []byte("a message") for _, h := range []qrc.Hash{qrc.SHA256, qrc.Keccak256} { tree := NewTree(h, testEntropy(), testID, qrc.MinHeight) if tree.Hash() != h || tree.Height() != qrc.MinHeight { t.Fatalf("%s: tree reports %s, height %d", h, tree.Hash(), tree.Height()) } proof := tree.Sign(leaf, msg) if len(proof) != qrc.ProofSize(qrc.MinHeight) { t.Fatalf("%s: proof of %d bytes", h, len(proof)) } digest := h.Sum(msg) zeros, ones := 0, 0 for bit := 0; bit < qrc.DigestBits; bit++ { selected := int(digest[bit/8]>>(7-uint(bit%8))) & 1 if selected == 0 { zeros++ } else { ones++ } for side := 0; side < 2; side++ { want := tree.secret(leaf, bit, side) if side != selected { want = h.Sum(want[:]) } at := (2*bit + side) * qrc.HashSize if string(proof[at:at+qrc.HashSize]) != string(want[:]) { t.Fatalf("%s: bit %d, side %d holds the wrong value", h, bit, side) } } } if zeros == 0 || ones == 0 { t.Fatalf("%s: the digest has a single bit value, pick another message", h) } // The path: leaf 21 is 010101 in binary, so its siblings sit to the // left, right, left, right, left, right of the nodes on the way up. node := leaf for level := 0; level < qrc.MinHeight; level++ { sibling := node ^ 1 want := tree.levels[level][sibling*qrc.HashSize : (sibling+1)*qrc.HashSize] at := qrc.SignatureSize + level*qrc.HashSize if string(proof[at:at+qrc.HashSize]) != string(want) { t.Fatalf("%s: wrong sibling at level %d", h, level) } node >>= 1 } } } // TestCommitsAreSeparate checks that nothing but the same entropy and the // same commit ID leads to the same keys. func TestCommitsAreSeparate(t *testing.T) { base := NewTree(qrc.SHA256, testEntropy(), testID, qrc.MinHeight).Root() if again := NewTree(qrc.SHA256, testEntropy(), testID, qrc.MinHeight).Root(); again != base { t.Fatalf("the same entropy and ID gave two different commits") } seen := map[string]string{hexOf(base): "base"} check := func(name string, entropy []byte, id qrc.ID) { root := hexOf(NewTree(qrc.SHA256, entropy, id, qrc.MinHeight).Root()) if other, ok := seen[root]; ok { t.Errorf("%s and %s have the same commit", name, other) } seen[root] = name } entropy := testEntropy() entropy[31] ^= 1 check("another entropy", entropy, testID) id := testID id.ChainID = "onyx-1" check("another chain", testEntropy(), id) id = testID id.Registry = "gno.land/r/other/qrcregistry/v1" check("another registry", testEntropy(), id) id = testID id.Account = "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj" check("another account", testEntropy(), id) id = testID id.Generation = 1 check("another generation", testEntropy(), id) // The two trees of one commit share no secret either. a := NewTree(qrc.SHA256, testEntropy(), testID, qrc.MinHeight) b := NewTree(qrc.Keccak256, testEntropy(), testID, qrc.MinHeight) if a.secret(0, 0, 0) == b.secret(0, 0, 0) { t.Errorf("the two trees share a secret") } } func TestBadArguments(t *testing.T) { for name, f := range map[string]func(){ "unknown hash": func() { NewTree(qrc.Hash(9), testEntropy(), testID, qrc.MinHeight) }, "short entropy": func() { NewTree(qrc.SHA256, testEntropy()[:31], testID, qrc.MinHeight) }, "long entropy": func() { NewTree(qrc.SHA256, append(testEntropy(), 0), testID, qrc.MinHeight) }, "no entropy": func() { NewTree(qrc.SHA256, nil, testID, qrc.MinHeight) }, "height 5": func() { NewTree(qrc.SHA256, testEntropy(), testID, qrc.MinHeight-1) }, "height 17": func() { NewTree(qrc.SHA256, testEntropy(), testID, qrc.MaxHeight+1) }, } { if !panics(f) { t.Errorf("%s: accepted", name) } } tree := NewTree(qrc.SHA256, testEntropy(), testID, qrc.MinHeight) for _, leaf := range []int{-1, 64, 1 << 20} { if !panics(func() { tree.Sign(leaf, []byte("m")) }) { t.Errorf("leaf %d: signed", leaf) } } } func panics(f func()) (panicked bool) { defer func() { if recover() != nil { panicked = true } }() f() return false }