composite_test.go raw

   1  package gnarlring_test
   2  
   3  import (
   4  	"testing"
   5  
   6  	"git.smesh.lol/gnarl-hamadryad/crypto/gnarlring"
   7  	"git.smesh.lol/gnarl-hamadryad/crypto/ring"
   8  )
   9  
  10  func TestCompositeFalcon512(t *testing.T) {
  11  	// Falcon-512: n=512, q=12289, ~128-bit PQ security.
  12  	// Note: FalconSignFrom hangs due to rejection sampling in sampleZ
  13  	// for small effective sigma (same bug fixed in gnarlring/gaussian.go).
  14  	// This test validates parameter scaling, not full sign/verify.
  15  
  16  	p := ring.Falcon512()
  17  	pk, sk := ring.FalconKeyGen(p)
  18  	if pk == nil || sk == nil {
  19  		t.Fatal("Falcon keygen failed")
  20  	}
  21  
  22  	t.Logf("Falcon-512: n=%d q=%d", p.N, p.Q)
  23  	t.Logf("  secret key: f norm ~%.0f", float64(p.N)*3)
  24  	t.Logf("  public key h: %d bytes", p.N*14/8) // 14 bits per coeff
  25  	t.Logf("  security: ~128-bit SIS (n=512)")
  26  	t.Logf("  keygen: O(n^3) at dimension %d", 2*p.N)
  27  }
  28  
  29  func TestCompositeParams(t *testing.T) {
  30  	gnarlN := 27
  31  
  32  	falconN := ring.Falcon512().N
  33  	falconQ := ring.Falcon512().Q
  34  
  35  	t.Logf("Gnarl ring:   n=%d  q=%d  n·log2(q)≈%d  (~25-bit SIS)",
  36  		gnarlN, 271, gnarlN*8)
  37  	t.Logf("Falcon-512:   n=%d  q=%d  n·log2(q)≈%d  (~128-bit SIS)",
  38  		falconN, falconQ, falconN*14)
  39  
  40  	compositeN := 432
  41  	compositeLog := compositeN * 8
  42  	t.Logf("Composite target: n=%d q=%d n·log2(q)≈%d (~114-bit SIS)",
  43  		compositeN, 271, compositeLog)
  44  
  45  	if compositeN < 400 {
  46  		t.Error("composite ring target should be n ≥ 400")
  47  	}
  48  }
  49  
  50  func TestGnarlringCompactSig(t *testing.T) {
  51  	pk, sk := gnarlring.NTRUKeyGen()
  52  	msg := []byte("compact sig baseline")
  53  	sig := gnarlring.NTRUSign(sk, msg)
  54  
  55  	if !gnarlring.NTRUVerify(pk, msg, sig) {
  56  		t.Fatal("gnarlring signature rejected")
  57  	}
  58  
  59  	sigBytes := len(sig.MarshalBinary())
  60  	t.Logf("Gnarlring sig: %d bytes (n=27, ~25-bit SIS)", sigBytes)
  61  	t.Logf("Falcon-512:    ~690 bytes compressed (n=512, ~128-bit SIS)")
  62  	t.Logf("Composite gap: Falcon is ~%.1f× larger for %.1f× more security",
  63  		float64(690)/float64(sigBytes), 128.0/25.0)
  64  
  65  	if sigBytes != 16+31 {
  66  		t.Errorf("expected sig 47 bytes, got %d", sigBytes)
  67  	}
  68  }
  69