hash_test.go raw

   1  package p256k1
   2  
   3  import (
   4  	"testing"
   5  )
   6  
   7  func TestSHA256(t *testing.T) {
   8  	// Test basic SHA-256 functionality
   9  	h := NewSHA256()
  10  	testData := []byte("For this sample, this 63-byte string will be used as input data")
  11  	h.Write(testData)
  12  	
  13  	var result [32]byte
  14  	h.Finalize(result[:])
  15  	
  16  	// Expected result from C selftest
  17  	expected := [32]byte{
  18  		0xf0, 0x8a, 0x78, 0xcb, 0xba, 0xee, 0x08, 0x2b, 0x05, 0x2a, 0xe0, 0x70, 0x8f, 0x32, 0xfa, 0x1e,
  19  		0x50, 0xc5, 0xc4, 0x21, 0xaa, 0x77, 0x2b, 0xa5, 0xdb, 0xb4, 0x06, 0xa2, 0xea, 0x6b, 0xe3, 0x42,
  20  	}
  21  	
  22  	for i := 0; i < 32; i++ {
  23  		if result[i] != expected[i] {
  24  			t.Errorf("SHA-256 mismatch at byte %d: got 0x%02x, expected 0x%02x", i, result[i], expected[i])
  25  		}
  26  	}
  27  	
  28  	h.Clear()
  29  }
  30  
  31  func TestHMACSHA256(t *testing.T) {
  32  	// Test HMAC-SHA256 with known test vectors
  33  	key := []byte("key")
  34  	message := []byte("The quick brown fox jumps over the lazy dog")
  35  	
  36  	h := NewHMACSHA256(key)
  37  	h.Write(message)
  38  	
  39  	var result [32]byte
  40  	h.Finalize(result[:])
  41  	
  42  	// Basic test - just verify it produces output
  43  	allZero := true
  44  	for i := 0; i < 32; i++ {
  45  		if result[i] != 0 {
  46  			allZero = false
  47  			break
  48  		}
  49  	}
  50  	if allZero {
  51  		t.Error("HMAC-SHA256 produced all zeros")
  52  	}
  53  	
  54  	h.Clear()
  55  }
  56  
  57  func TestRFC6979(t *testing.T) {
  58  	// Test RFC6979 nonce generation
  59  	key := []byte("test key for RFC6979")
  60  	rng := NewRFC6979HMACSHA256(key)
  61  	
  62  	var nonce1 [32]byte
  63  	rng.Generate(nonce1[:])
  64  	
  65  	// Generate more bytes
  66  	var nonce2 [32]byte
  67  	rng.Generate(nonce2[:])
  68  	
  69  	// Nonces should be different
  70  	allSame := true
  71  	for i := 0; i < 32; i++ {
  72  		if nonce1[i] != nonce2[i] {
  73  			allSame = false
  74  			break
  75  		}
  76  	}
  77  	if allSame {
  78  		t.Error("RFC6979 produced identical nonces")
  79  	}
  80  	
  81  	rng.Finalize()
  82  	rng.Clear()
  83  }
  84  
  85  func TestTaggedHash(t *testing.T) {
  86  	// Test tagged hash function
  87  	tag := []byte("BIP0340/challenge")
  88  	data := []byte("test data")
  89  	
  90  	result := TaggedHash(tag, data)
  91  	
  92  	// Verify it produces output
  93  	allZero := true
  94  	for i := 0; i < 32; i++ {
  95  		if result[i] != 0 {
  96  			allZero = false
  97  			break
  98  		}
  99  	}
 100  	if allZero {
 101  		t.Error("TaggedHash produced all zeros")
 102  	}
 103  }
 104  
 105  func TestHashToScalar(t *testing.T) {
 106  	hash := make([]byte, 32)
 107  	for i := 0; i < 32; i++ {
 108  		hash[i] = byte(i)
 109  	}
 110  	
 111  	scalar, err := HashToScalar(hash)
 112  	if err != nil {
 113  		t.Fatalf("HashToScalar failed: %v", err)
 114  	}
 115  	if scalar == nil {
 116  		t.Fatal("HashToScalar returned nil")
 117  	}
 118  }
 119  
 120  func TestHashToField(t *testing.T) {
 121  	hash := make([]byte, 32)
 122  	for i := 0; i < 32; i++ {
 123  		hash[i] = byte(i)
 124  	}
 125  	
 126  	field, err := HashToField(hash)
 127  	if err != nil {
 128  		t.Fatalf("HashToField failed: %v", err)
 129  	}
 130  	if field == nil {
 131  		t.Fatal("HashToField returned nil")
 132  	}
 133  }
 134  
 135