CodeSampleX

샘플

golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d: chacha20poly1305.New

검증된 샘플 — golang golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d: chacha20poly1305.New. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다.

sha256:921da7c2106cc9048245f5ece500700d0c7a70d117cd9712e591513c8ffdcd87

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

검증 실행 환경

환경 컨트랙트 단계 실행일
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-04

케이스

HOW
목표
verify golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d
패키지
심벌
  • golang.org/x/crypto/chacha20poly1305.New
환경
go 1.26.6
생성일
2026-09-04T13:24:36Z

컨트랙트

  1. chacha20poly1305.New requires a 32-byte key and rejects invalid key lengths
  2. chacha20poly1305.New returns cipher.AEAD with NonceSize 12 and Overhead 16
  3. chacha20poly1305.New creates cipher.AEAD that seals and opens plaintext with matching additional data
  4. chacha20poly1305.New creates cipher.AEAD that rejects altered ciphertext, incorrect nonce, or mismatched additional data

파일

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

소스 아티팩트 내려받기 (tar.gz)

소스

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d
Demonstrate these symbols/APIs:
  - golang.org/x/crypto/chacha20poly1305.New
Required runtime conditions:
  - ecosystem: golang
  - language: go
  - packageManager: go@1.26.6
  - runtime: go@1.26.6

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:d3b00252102def52258eb607db9e47182fcb3865ddec15466ae089bcf8c52500","contract":["chacha20poly1305.New requires a 32-byte key and rejects invalid key lengths","chacha20poly1305.New returns cipher.AEAD with NonceSize 12 and Overhead 16","chacha20poly1305.New creates cipher.AEAD that seals and opens plaintext with matching additional data","chacha20poly1305.New creates cipher.AEAD that rejects altered ciphertext, incorrect nonce, or mismatched additional data"],"goal":"verify golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d","kind":"HOW","packages":["pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d"],"schemaVersion":1,"symbols":["golang.org/x/crypto/chacha20poly1305.New"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d","symbols":["golang.org/x/crypto/chacha20poly1305.New"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d

require golang.org/x/sys v0.10.0 // indirect
go.sum
golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d h1:LiA25/KWKuXfIq5pMIBq1s5hz3HQxhJJSu/SUGlD+SM=
golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
main.go
package main

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"io"

	"golang.org/x/crypto/chacha20poly1305"
)

// Encrypt encrypts plaintext with ChaCha20-Poly1305 AEAD.
func Encrypt(key, nonce, plaintext, additionalData []byte) ([]byte, error) {
	aead, err := chacha20poly1305.New(key)
	if err != nil {
		return nil, fmt.Errorf("create cipher: %w", err)
	}
	return aead.Seal(nil, nonce, plaintext, additionalData), nil
}

// Decrypt decrypts ciphertext with ChaCha20-Poly1305 AEAD.
func Decrypt(key, nonce, ciphertext, additionalData []byte) ([]byte, error) {
	aead, err := chacha20poly1305.New(key)
	if err != nil {
		return nil, fmt.Errorf("create cipher: %w", err)
	}
	plaintext, err := aead.Open(nil, nonce, ciphertext, additionalData)
	if err != nil {
		return nil, fmt.Errorf("open ciphertext: %w", err)
	}
	return plaintext, nil
}

func main() {
	key := make([]byte, chacha20poly1305.KeySize)
	if _, err := io.ReadFull(rand.Reader, key); err != nil {
		panic(err)
	}

	nonce := make([]byte, chacha20poly1305.NonceSize)
	if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
		panic(err)
	}

	plaintext := []byte("Hello, ChaCha20-Poly1305 authenticated encryption!")
	ad := []byte("metadata-header")

	ciphertext, err := Encrypt(key, nonce, plaintext, ad)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Ciphertext (%d bytes): %s\n", len(ciphertext), hex.EncodeToString(ciphertext))

	decrypted, err := Decrypt(key, nonce, ciphertext, ad)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Decrypted: %s\n", string(decrypted))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d"
  ],
  "symbols": [
    "golang.org/x/crypto/chacha20poly1305.New"
  ],
  "runtimeConditions": {
    "ecosystem": "golang",
    "language": "go",
    "packageManager": "go@1.26.6",
    "runtime": "go@1.26.6"
  }
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

	"golang.org/x/crypto/chacha20poly1305"
)

func main() {
	// 1. chacha20poly1305.New requires a 32-byte key and rejects invalid key lengths
	{
		invalidLengths := []int{0, 16, 24, 31, 33, 64}
		for _, l := range invalidLengths {
			badKey := make([]byte, l)
			_, err := chacha20poly1305.New(badKey)
			if err == nil {
				fmt.Fprintf(os.Stderr, "FAIL: expected error for invalid key length %d, got nil\n", l)
				os.Exit(1)
			}
		}

		validKey := make([]byte, chacha20poly1305.KeySize) // 32 bytes
		aead, err := chacha20poly1305.New(validKey)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected valid 32-byte key to succeed, got %v\n", err)
			os.Exit(1)
		}
		if aead == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected non-nil cipher.AEAD\n")
			os.Exit(1)
		}
	}

	// 2. chacha20poly1305.New returns cipher.AEAD with NonceSize 12 and Overhead 16
	{
		key := []byte("01234567890123456789012345678901") // 32 bytes
		aead, err := chacha20poly1305.New(key)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: unexpected error creating AEAD: %v\n", err)
			os.Exit(1)
		}

		if nonceSize := aead.NonceSize(); nonceSize != 12 {
			fmt.Fprintf(os.Stderr, "FAIL: expected NonceSize 12, got %d\n", nonceSize)
			os.Exit(1)
		}
		if overhead := aead.Overhead(); overhead != 16 {
			fmt.Fprintf(os.Stderr, "FAIL: expected Overhead 16, got %d\n", overhead)
			os.Exit(1)
		}
	}

	// 3. chacha20poly1305.New creates cipher.AEAD that seals and opens plaintext with matching additional data
	{
		key := []byte("01234567890123456789012345678901")
		nonce := []byte("nonce1234567") // 12 bytes
		plaintext := []byte("confidential message content")
		ad := []byte("header-metadata-ad")

		aead, err := chacha20poly1305.New(key)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: unexpected error creating AEAD: %v\n", err)
			os.Exit(1)
		}

		ciphertext := aead.Seal(nil, nonce, plaintext, ad)
		expectedCiphertextLen := len(plaintext) + aead.Overhead()
		if len(ciphertext) != expectedCiphertextLen {
			fmt.Fprintf(os.Stderr, "FAIL: expected ciphertext length %d, got %d\n", expectedCiphertextLen, len(ciphertext))
			os.Exit(1)
		}

		opened, err := aead.Open(nil, nonce, ciphertext, ad)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected Open to succeed, got error: %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(opened, plaintext) {
			fmt.Fprintf(os.Stderr, "FAIL: decrypted plaintext does not match original\n")
			os.Exit(1)
		}

		// Also verify empty additional data works
		ciphertextNoAD := aead.Seal(nil, nonce, plaintext, nil)
		openedNoAD, err := aead.Open(nil, nonce, ciphertextNoAD, nil)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected Open with nil AD to succeed, got %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(openedNoAD, plaintext) {
			fmt.Fprintf(os.Stderr, "FAIL: decrypted plaintext with nil AD does not match\n")
			os.Exit(1)
		}
	}

	// 4. chacha20poly1305.New creates cipher.AEAD that rejects altered ciphertext, incorrect nonce, or mismatched additional data
	{
		key := []byte("01234567890123456789012345678901")
		nonce := []byte("nonce1234567")
		plaintext := []byte("confidential message content")
		ad := []byte("header-metadata-ad")

		aead, err := chacha20poly1305.New(key)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: unexpected error: %v\n", err)
			os.Exit(1)
		}

		ciphertext := aead.Seal(nil, nonce, plaintext, ad)

		// Altered ciphertext byte
		corruptedCiphertext := append([]byte(nil), ciphertext...)
		corruptedCiphertext[0] ^= 0x01
		if _, err := aead.Open(nil, nonce, corruptedCiphertext, ad); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error when decrypting corrupted ciphertext, got nil\n")
			os.Exit(1)
		}

		// Altered tag byte (last byte)
		corruptedTag := append([]byte(nil), ciphertext...)
		corruptedTag[len(corruptedTag)-1] ^= 0x01
		if _, err := aead.Open(nil, nonce, corruptedTag, ad); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error when decrypting corrupted tag, got nil\n")
			os.Exit(1)
		}

		// Incorrect nonce
		wrongNonce := []byte("nonceWrong99")
		if _, err := aead.Open(nil, wrongNonce, ciphertext, ad); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error with wrong nonce, got nil\n")
			os.Exit(1)
		}

		// Mismatched additional data
		wrongAD := []byte("different-metadata-ad")
		if _, err := aead.Open(nil, nonce, ciphertext, wrongAD); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error with wrong additional data, got nil\n")
			os.Exit(1)
		}

		// Truncated ciphertext (shorter than overhead)
		truncated := ciphertext[:aead.Overhead()-1]
		if _, err := aead.Open(nil, nonce, truncated, ad); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error with truncated ciphertext, got nil\n")
			os.Exit(1)
		}
	}

	fmt.Println("PASS: all chacha20poly1305 contract assertions passed")
}

오리진 시더

익명