CodeSampleX

Exemple

getrandom 0.4.3: getrandom::Error, getrandom::fill, getrandom::fill_uninit

Échantillon vérifié pour cargo getrandom 0.4.3: getrandom::Error, getrandom::fill, getrandom::fill_uninit. Le contrat s'est exécuté sur rust 1 · linux…

sha256:fd8fb068e6f768655167644b96ce6d0dbef32f13e59d62632dbb0b26f59ab7ad

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré rust linux x64 rust rust cargo

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-19

Cas

HOW
Objectif
verify pkg:cargo/getrandom@0.4.3
Paquets
Symboles
  • getrandom::Error
  • getrandom::fill
  • getrandom::fill_uninit
  • getrandom::u32
  • getrandom::u64
Environnement
rust
Créé
2026-09-18T16:21:26Z

Contrat

  1. assert getrandom::fill accepts &mut [u8], returns Result<(), Error>, and succeeds for a non-empty buffer on the supported test target
  2. assert getrandom::fill_uninit accepts &mut [MaybeUninit<u8>] and returns a writable &mut [u8] with the input length
  3. assert getrandom::u32 and getrandom::u64 return Result<u32, Error> and Result<u64, Error> and succeed on the supported test target
  4. assert getrandom::fill and fill_uninit return Ok for empty slices and fill_uninit returns a zero-length slice
  5. assert Error::UNSUPPORTED, ERRNO_NOT_POSITIVE, and UNEXPECTED are distinct and Error::new_custom creates a distinct custom value

Fichiers

  • Cargo.lock
  • Cargo.toml
  • NOTES.md
  • PROMPT.md
  • csx.json
  • spec.json
  • src/lib.rs

Télécharger l’artefact source (tar.gz)

Code source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "cfg-if"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"

[[package]]
name = "getrandom"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
dependencies = [
 "cfg-if",
 "libc",
 "r-efi",
]

[[package]]
name = "getrandom-contract"
version = "0.1.0"
dependencies = [
 "getrandom",
]

[[package]]
name = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"

[[package]]
name = "r-efi"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
Cargo.toml
[package]
name = "getrandom-contract"
version = "0.1.0"
edition = "2021"

[dependencies]
getrandom = "=0.4.3"
NOTES.md
# getrandom 0.4.3 Buffer and Scalar API Contracts

## What `search_known_solution` answered
`search_known_solution` returned earlier samples for getrandom 0.3.x (REFERENCE_ONLY), which differed from the 0.4.x version needed. This sample verifies the updated 0.4.3 release contracts.

## What a model would have written instead
A model may treat `fill_uninit` as returning `Result<(), Error>` instead of `Result<&mut [u8], Error>`, add unnecessary unsafe pointers, or try to decode byte slices manually rather than using native `u32()` and `u64()` scalar helpers.

## How the wrong version fails
Treating `fill_uninit` as `Result<(), Error>` produces a compilation error because the signature returns `&mut [u8]`. The scalar functions `getrandom::u32()` and `getrandom::u64()` directly provide typed integers.
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 pkg:cargo/getrandom@0.4.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/getrandom@0.4.3
Demonstrate these symbols/APIs:
  - getrandom::Error
  - getrandom::fill
  - getrandom::fill_uninit
  - getrandom::u32
  - getrandom::u64

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:d7cbe1c02124b34b424a464bcbad3bc68a60bb3db8aa065e4871008438278384","contract":["assert getrandom::fill accepts \u0026mut [u8], returns Result\u003c(), Error\u003e, and succeeds for a non-empty buffer on the supported test target","assert getrandom::fill_uninit accepts \u0026mut [MaybeUninit\u003cu8\u003e] and returns a writable \u0026mut [u8] with the input length","assert getrandom::u32 and getrandom::u64 return Result\u003cu32, Error\u003e and Result\u003cu64, Error\u003e and succeed on the supported test target","assert getrandom::fill and fill_uninit return Ok for empty slices and fill_uninit returns a zero-length slice","assert Error::UNSUPPORTED, ERRNO_NOT_POSITIVE, and UNEXPECTED are distinct and Error::new_custom creates a distinct custom value"],"goal":"verify pkg:cargo/getrandom@0.4.3","kind":"HOW","packages":["pkg:cargo/getrandom@0.4.3"],"schemaVersion":1,"symbols":["getrandom::Error","getrandom::fill","getrandom::fill_uninit","getrandom::u32","getrandom::u64"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/getrandom@0.4.3"],"schemaVersion":1,"subject":"pkg:cargo/getrandom@0.4.3","symbols":["getrandom::Error","getrandom::fill","getrandom::fill_uninit","getrandom::u32","getrandom::u64"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:cargo/getrandom@0.4.3",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/getrandom@0.4.3"
  ],
  "symbols": [
    "getrandom::Error",
    "getrandom::fill",
    "getrandom::fill_uninit",
    "getrandom::u32",
    "getrandom::u64"
  ]
}
src/lib.rs
use core::mem::MaybeUninit;
use getrandom::Error;

/// Fills an initialized byte slice with random bytes.
pub fn generate_random_bytes(dest: &mut [u8]) -> Result<(), Error> {
    getrandom::fill(dest)
}

/// Fills an uninitialized buffer and returns a safe mutable slice of initialized bytes.
pub fn generate_uninit_bytes(dest: &mut [MaybeUninit<u8>]) -> Result<&mut [u8], Error> {
    getrandom::fill_uninit(dest)
}

/// Retrieves a single random u32 scalar.
pub fn generate_random_u32() -> Result<u32, Error> {
    getrandom::u32()
}

/// Retrieves a single random u64 scalar.
pub fn generate_random_u64() -> Result<u64, Error> {
    getrandom::u64()
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::mem::MaybeUninit;

    #[test]
    fn test_fill_initialized_slice() {
        let mut buf = [0u8; 32];
        let res: Result<(), Error> = generate_random_bytes(&mut buf);
        assert!(res.is_ok(), "fill should succeed");
    }

    #[test]
    fn test_fill_uninit_returns_safe_slice() {
        let mut uninit_buf = [MaybeUninit::<u8>::uninit(); 32];
        let res: Result<&mut [u8], Error> = generate_uninit_bytes(&mut uninit_buf);
        assert!(res.is_ok(), "fill_uninit should succeed");
        let initialized_slice = res.unwrap();
        assert_eq!(initialized_slice.len(), 32);
        initialized_slice.fill(0xA5);
        assert!(initialized_slice.iter().all(|&byte| byte == 0xA5));
    }

    #[test]
    fn test_scalar_generators() {
        let scalar32: u32 = generate_random_u32().expect("u32 should succeed");
        let scalar64: u64 = generate_random_u64().expect("u64 should succeed");
        let _: (u32, u64) = (scalar32, scalar64);
    }

    #[test]
    fn test_empty_buffer_no_op() {
        let mut empty = [];
        let res: Result<(), Error> = generate_random_bytes(&mut empty);
        assert!(res.is_ok(), "fill on empty buffer should immediately succeed");

        let mut empty_uninit: [MaybeUninit<u8>; 0] = [];
        let res_uninit: Result<&mut [u8], Error> = generate_uninit_bytes(&mut empty_uninit);
        assert!(res_uninit.is_ok(), "fill_uninit on empty buffer should immediately succeed");
        assert_eq!(res_uninit.unwrap().len(), 0);
    }

    #[test]
    fn test_error_constants_and_custom() {
        assert_ne!(Error::UNSUPPORTED, Error::UNEXPECTED);
        assert_ne!(Error::UNSUPPORTED, Error::ERRNO_NOT_POSITIVE);
        assert_ne!(Error::ERRNO_NOT_POSITIVE, Error::UNEXPECTED);

        let custom_err = Error::new_custom(123);
        assert_ne!(custom_err, Error::UNSUPPORTED);
        assert_ne!(custom_err, Error::ERRNO_NOT_POSITIVE);
        assert_ne!(custom_err, Error::UNEXPECTED);
    }
}

Seeder d'origine

anonyme