ed25519_vectors.mx raw

   1  /*
   2  crypto/ed25519 against a fixed vector rather than a round trip.
   3  
   4  The round-trip fixture proves the API hangs together; it cannot tell whether the
   5  bytes are right, because both sides of every check come from the same code. This
   6  fixture pins the derivation: seed 0x00..0x1f must give this public key and this
   7  signature for this message, which is what Go 1.25.3 produces for the same inputs.
   8  
   9  It exists because the key structs' byte fields became slices (they were fixed
  10  arrays), which moves their storage into the constructors. A wrong length, a
  11  shared buffer or a lost copy shows up here as a changed public key or signature.
  12  
  13  Reference (Go 1.25.3, crypto/ed25519.NewKeyFromSeed):
  14    pub 03a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b8
  15    sig 8efcf3940fc929b9bb03d770c4d7ba406659c57ded7ad2cb3d74df78fb9e2958fe41b71f704e45e4e42b7582e26671ccec6d68632e7e1b7ad12f1abc1dc0170b
  16  */
  17  package main
  18  
  19  import (
  20  	"crypto/ed25519"
  21  	"os"
  22  )
  23  
  24  const wantPub = "03a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b8"
  25  const wantSig = "8efcf3940fc929b9bb03d770c4d7ba406659c57ded7ad2cb3d74df78fb9e2958fe41b71f704e45e4e42b7582e26671ccec6d68632e7e1b7ad12f1abc1dc0170b"
  26  
  27  func hex(b []byte) (s string) {
  28  	const t = "0123456789abcdef"
  29  	o := []byte{:len(b) * 2}
  30  	for i := 0; i < len(b); i++ {
  31  		o[2*i] = t[b[i]>>4]
  32  		o[2*i+1] = t[b[i]&0xf]
  33  	}
  34  	return string(o)
  35  }
  36  
  37  func check(name string, got, want string) {
  38  	if got != want {
  39  		println("FAIL: " | name)
  40  		println("  got  " | got)
  41  		println("  want " | want)
  42  		os.Exit(1)
  43  	}
  44  }
  45  
  46  func main() {
  47  	// Termination: a batch program must still be stoppable, so the entry
  48  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  49  	select {
  50  	case <-shutdown:
  51  		return
  52  	default:
  53  	}
  54  
  55  	seed := []byte{:32}
  56  	for i := 0; i < 32; i++ {
  57  		seed[i] = byte(i)
  58  	}
  59  	priv := ed25519.NewKeyFromSeed(seed)
  60  	check("seed round trip", hex(priv.Seed()), hex(seed))
  61  
  62  	pub := priv.Public().(ed25519.PublicKey)
  63  	check("public key", hex(pub.Bytes()), wantPub)
  64  
  65  	msg := []byte("moxie ed25519 differential message")
  66  	sig := ed25519.Sign(priv, msg)
  67  	check("signature", hex(sig), wantSig)
  68  	if !ed25519.Verify(pub, msg, sig) {
  69  		println("FAIL: Verify rejected the reference signature")
  70  		os.Exit(1)
  71  	}
  72  
  73  	// A key rebuilt from the public bytes verifies the same signature, and the
  74  	// private key rebuilt from its seed signs it again identically.
  75  	again := ed25519.PublicKey(pub.Bytes())
  76  	if !ed25519.Verify(again, msg, sig) {
  77  		println("FAIL: a key rebuilt from its bytes does not verify")
  78  		os.Exit(1)
  79  	}
  80  	priv2 := ed25519.NewKeyFromSeed(priv.Seed())
  81  	check("rebuilt key signature", hex(ed25519.Sign(priv2, msg)), wantSig)
  82  
  83  	// Tampering must be visible in both directions: the message and the bytes.
  84  	if ed25519.Verify(pub, []byte("moxie ed25519 differential messagf"), sig) {
  85  		println("FAIL: Verify accepted a different message")
  86  		os.Exit(1)
  87  	}
  88  	raw := priv.Bytes()
  89  	raw[0] = raw[0] ^ 0xff
  90  	bad := ed25519.NewKeyFromSeed(raw[:32])
  91  	if ed25519.Verify(again, msg, ed25519.Sign(bad, msg)) {
  92  		println("FAIL: a different seed produced a verifying signature")
  93  		os.Exit(1)
  94  	}
  95  	println("ok")
  96  }
  97