CodeSampleX

サンプル

getrandom 0.3.4: Use getrandom 0.3.4 typed buffer and scalar APIs, including empty-slice behavior and Error sentinel values

検証済みサンプル — cargo getrandom 0.3.4: Use getrandom 0.3.4 typed buffer and scalar APIs, including empty-slice behavior and Error sentinel values. rust 1 · linux…

sha256:16b7fcbc75be19ecf2d6e7459b13050cd686c63bd8e2b3419c75c337cea7cf3d

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
3
ビルドした署名鍵
3
宣言された環境 rust linux x64 rust rust cargo

検証実行環境

環境 コントラクト ステージ 実行日
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-17
rust 1 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-18
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-08

ケース

HOW
ゴール
Use getrandom 0.3.4 typed buffer and scalar APIs, including empty-slice behavior and Error sentinel values
パッケージ
シンボル
  • getrandom::fill
  • getrandom::fill_uninit
  • getrandom::u32
  • getrandom::u64
  • getrandom::Error
環境
rust
作成日
2026-08-17T01:03:34Z

コントラクト

  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

ファイル

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

ソースアーティファクトをダウンロード (tar.gz)

ソース

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

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

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

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

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

[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"

[[package]]
name = "wasip2"
version = "1.0.4+wasi-0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487"
dependencies = [
 "wit-bindgen",
]

[[package]]
name = "wit-bindgen"
version = "0.57.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e"
Cargo.toml
[package]
name = "getrandom-contract"
version = "0.1.0"
edition = "2021"

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

## What `search_known_solution` answered
`search_known_solution` found an earlier local sample for the same crate and API family. This revision narrows that sample's claims to behavior the contract directly observes.

## What a model would have written instead
A model may treat `fill_uninit` as returning `Result<(), Error>`, add an unnecessary unsafe conversion, or manually decode byte arrays instead of using the typed `u32()` and `u64()` functions.

## How the wrong version fails
Treating `fill_uninit` as `Result<(), Error>` fails at compile time because the success value is `&mut [u8]`. The scalar helpers also already expose typed `Result<u32, Error>` and `Result<u64, Error>` values.

The contract intentionally does not claim that an old symbol was removed, that empty calls avoid operating-system work, or that random output must contain a non-zero byte. Those properties are not established by this runtime contract.
csx.json
{"case":{"caseId":"case:sha256:a55fe2b86807e6a8a78453e8a8ce99b4968933bd687a421a224bfddbfde7cf5b","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":"Use getrandom 0.3.4 typed buffer and scalar APIs, including empty-slice behavior and Error sentinel values","kind":"HOW","packages":["pkg:cargo/getrandom@0.3.4"],"schemaVersion":1,"symbols":["getrandom::fill","getrandom::fill_uninit","getrandom::u32","getrandom::u64","getrandom::Error"]},"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.3.4"],"schemaVersion":1,"symbols":["getrandom::fill","getrandom::fill_uninit","getrandom::u32","getrandom::u64","getrandom::Error"],"verifierAdapter":"cargo@1"}
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);
    }
}

オリジンシーダー

csx-seed