CodeSampleX

Exemplo

rand_pcg 0.10.2: rand_pcg::Pcg64Dxsm

Amostra verificada para cargo rand_pcg 0.10.2: rand_pcg::Pcg64Dxsm. O contrato rodou em rust 1 · linux alpine/x64 · docker e passou.

sha256:e4620457dcbefb2c4cf62e37e195631108724b6014dff8bc53f7bd6e9d95d556

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux x64 cargo

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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-11-27

Caso

HOW
Objetivo
verify rand_pcg::Pcg64Dxsm in pkg:cargo/rand_pcg@0.10.2
Pacotes
Símbolos
  • rand_pcg::Pcg64Dxsm
Criado
2026-11-27T23:14:52Z

Contrato

  1. Pcg64Dxsm is an alias for Lcg128CmDxsm64; Pcg64Dxsm::new(42, 54) matches the 6-value PCG64DXSM reference vector and Pcg64Dxsm::seed_from_u64(0).next_u64() yields 4111470453933123814
  2. SeedableRng::from_seed accepts a 32-byte seed ignoring bit 0 of seed[16] to force an odd increment, and SeedableRng::from_rng deterministically seeds a child Pcg64Dxsm
  3. rand_pcg::rand_core::{Rng, TryRng} work on Pcg64Dxsm with next_u32 returning the low 32 bits of next_u64 per step and try_fill_bytes matching fill_bytes
  4. Pcg64Dxsm::advance(delta) matches sequential next_u64 calls and supports backward jumps via delta.wrapping_neg()
  5. Pcg64Dxsm::new discards bit 127 of stream and initializes state, whereas state()/stream()/from_state() preserve exact state for identical reconstruction, and Debug redacts internal state as "Lcg128CmDxsm64 {}"

Arquivos

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/main.rs
  • tests/contract.rs

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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

[[package]]
name = "rand_core"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"

[[package]]
name = "rand_pcg"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a"
dependencies = [
 "rand_core",
]

[[package]]
name = "sample-rand-pcg-dxsm"
version = "0.1.0"
dependencies = [
 "rand_pcg",
]
Cargo.toml
[package]
name = "sample-rand-pcg-dxsm"
version = "0.1.0"
edition = "2021"

[dependencies]
rand_pcg = "=0.10.2"
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 rand_pcg::Pcg64Dxsm in pkg:cargo/rand_pcg@0.10.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/rand_pcg@0.10.2
Demonstrate these symbols/APIs:
  - rand_pcg::Pcg64Dxsm
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:73d134e88fee01d013c7a6ab02553906439aee0ee8c82c24f31052574dd52b4c","contract":["Pcg64Dxsm is an alias for Lcg128CmDxsm64; Pcg64Dxsm::new(42, 54) matches the 6-value PCG64DXSM reference vector and Pcg64Dxsm::seed_from_u64(0).next_u64() yields 4111470453933123814","SeedableRng::from_seed accepts a 32-byte seed ignoring bit 0 of seed[16] to force an odd increment, and SeedableRng::from_rng deterministically seeds a child Pcg64Dxsm","rand_pcg::rand_core::{Rng, TryRng} work on Pcg64Dxsm with next_u32 returning the low 32 bits of next_u64 per step and try_fill_bytes matching fill_bytes","Pcg64Dxsm::advance(delta) matches sequential next_u64 calls and supports backward jumps via delta.wrapping_neg()","Pcg64Dxsm::new discards bit 127 of stream and initializes state, whereas state()/stream()/from_state() preserve exact state for identical reconstruction, and Debug redacts internal state as \"Lcg128CmDxsm64 {}\""],"goal":"verify rand_pcg::Pcg64Dxsm in pkg:cargo/rand_pcg@0.10.2","kind":"HOW","packages":["pkg:cargo/rand_pcg@0.10.2"],"schemaVersion":1,"symbols":["rand_pcg::Pcg64Dxsm"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/rand_pcg@0.10.2"],"schemaVersion":1,"subject":"pkg:cargo/rand_pcg@0.10.2","symbols":["rand_pcg::Pcg64Dxsm"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify rand_pcg::Pcg64Dxsm in pkg:cargo/rand_pcg@0.10.2",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/rand_pcg@0.10.2"
  ],
  "symbols": [
    "rand_pcg::Pcg64Dxsm"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/main.rs
use rand_pcg::rand_core::{Rng, SeedableRng};
use rand_pcg::Pcg64Dxsm;

fn main() {
    let mut rng = Pcg64Dxsm::new(42, 54);
    let first = rng.next_u64();
    assert_eq!(first, 17_331_114_245_835_578_256);

    let state = rng.state();
    let stream = rng.stream();
    let mut restored = Pcg64Dxsm::from_state(state, stream);
    assert_eq!(rng.next_u64(), restored.next_u64());

    let mut stepped = Pcg64Dxsm::seed_from_u64(0);
    let mut jumped = stepped.clone();
    for _ in 0..16 {
        let _ = stepped.next_u64();
    }
    jumped.advance(16);
    assert_eq!(stepped.next_u64(), jumped.next_u64());

    println!("CONTRACT_OK");
}
tests/contract.rs
use rand_pcg::rand_core::{Rng, SeedableRng, TryRng};
use rand_pcg::{Lcg128CmDxsm64, Pcg64Dxsm};

#[test]
fn reference_vector_and_type_alias() {
    let mut rng: Pcg64Dxsm = Lcg128CmDxsm64::new(42, 54);
    let expected: [u64; 6] = [
        17_331_114_245_835_578_256,
        10_267_467_544_499_227_306,
        9_726_600_296_081_716_989,
        10_165_951_391_103_677_450,
        12_131_334_649_314_727_261,
        10_134_094_537_930_450_875,
    ];
    for value in expected {
        assert_eq!(rng.next_u64(), value);
    }

    let mut seeded = Pcg64Dxsm::seed_from_u64(0);
    assert_eq!(seeded.next_u64(), 4_111_470_453_933_123_814);
}

#[test]
fn seedable_rng_and_ignored_increment_lsb() {
    let mut seed_even = [0u8; 32];
    for (idx, byte) in seed_even.iter_mut().enumerate() {
        *byte = (idx as u8).wrapping_add(1);
    }
    seed_even[16] &= !1;
    let mut seed_odd = seed_even;
    seed_odd[16] |= 1;

    // from_seed forces the 128-bit increment odd (ORing bit 0 of seed[16])
    let mut rng_a = Pcg64Dxsm::from_seed(seed_even);
    let mut rng_b = Pcg64Dxsm::from_seed(seed_odd);
    assert_eq!(rng_a, rng_b);
    assert_eq!(rng_a.next_u64(), rng_b.next_u64());

    // from_rng deterministically seeds a child generator from a parent RNG
    let mut parent_a = Pcg64Dxsm::seed_from_u64(99);
    let mut parent_b = Pcg64Dxsm::seed_from_u64(99);
    let mut child_a = Pcg64Dxsm::from_rng(&mut parent_a);
    let mut child_b = Pcg64Dxsm::from_rng(&mut parent_b);
    assert_eq!(child_a, child_b);
    assert_eq!(child_a.next_u64(), child_b.next_u64());
}

#[test]
fn rng_and_try_rng_outputs_match() {
    let mut r1 = Pcg64Dxsm::new(0xcafef00d_d15ea5e5, 0xa02bdbf7_bb3c0a7a);
    let mut r2 = r1.clone();
    let mut r3 = r1.clone();

    // next_u32 returns the low 32 bits of next_u64 and advances one 64-bit step
    let word64 = r1.next_u64();
    let word32 = r2.next_u32();
    assert_eq!(word32, word64 as u32);
    assert_eq!(r1, r2);

    // TryRng returns Ok with Infallible error and matches Rng
    assert_eq!(r3.try_next_u64(), Ok(word64));
    assert_eq!(r3.try_next_u32(), Ok(r1.next_u32()));

    let mut bytes_a = [0u8; 19];
    let mut bytes_b = [0u8; 19];
    r1.fill_bytes(&mut bytes_a);
    assert_eq!(r3.try_fill_bytes(&mut bytes_b), Ok(()));
    assert_eq!(bytes_a, bytes_b);
}

#[test]
fn advance_forward_and_backward() {
    let mut stepped = Pcg64Dxsm::new(1234, 567);
    let mut jumped = stepped.clone();

    for _ in 0..37 {
        let _ = stepped.next_u64();
    }
    jumped.advance(37);
    assert_eq!(stepped, jumped);
    assert_eq!(stepped.next_u64(), jumped.next_u64());

    // Jump backwards using two's complement wrapping_neg
    let snapshot = jumped.clone();
    jumped.advance(250);
    assert_ne!(jumped, snapshot);
    jumped.advance(250u128.wrapping_neg());
    assert_eq!(jumped, snapshot);
    assert_eq!(jumped.next_u64(), snapshot.clone().next_u64());
}

#[test]
fn state_stream_roundtrip_masking_and_debug_redaction() {
    let stream_val: u128 = 0x0123_4567_89ab_cdef;
    let rng_low = Pcg64Dxsm::new(777, stream_val);
    let rng_high = Pcg64Dxsm::new(777, stream_val | (1u128 << 127));

    // Highest bit of stream is discarded by new() and stream()
    assert_eq!(rng_low, rng_high);
    assert_eq!(rng_low.stream(), stream_val);
    assert_eq!(rng_high.stream(), stream_val);

    // new() scrambles initial state, whereas from_state() preserves exact state
    assert_ne!(rng_low.state(), 777);
    let direct = Pcg64Dxsm::from_state(777, stream_val | (1u128 << 127));
    assert_eq!(direct.state(), 777);
    assert_eq!(direct.stream(), stream_val);

    // Checkpoint and restore after advancing
    let mut orig = rng_low;
    for _ in 0..50 {
        let _ = orig.next_u64();
    }
    let mut restored = Pcg64Dxsm::from_state(orig.state(), orig.stream());
    assert_eq!(orig, restored);
    for _ in 0..16 {
        assert_eq!(orig.next_u64(), restored.next_u64());
    }

    // Debug implementation redacts internal state and increment
    assert_eq!(format!("{:?}", restored), "Lcg128CmDxsm64 {}");
}

Seeder de origem

anônimo