提交记录 125802


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex_maker test. 自定义测试 Accepted 100 1.194 s 786452 KB C++17 5.05 KB
提交时间 评测时间
2026-10-10 21:27:51 2026-10-10 21:27:55
// DUCK_PUBLIC_SURROGATE_BENCHMARK
// Independent public performance data. No production random generator, seed, or digest.
#pragma GCC optimize("O3")
#pragma GCC target("avx2,bmi2")
#include <x86intrin.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#ifndef DUCK_SHA256_HPP
#define DUCK_SHA256_HPP
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Portable SHA-256; input is the exact byte sequence, with no native-word hashing.
namespace duck_sha256 {
static inline uint32_t rotate(uint32_t x,unsigned n) {return (x>>n)|(x<<(32-n));}
static void compress(uint32_t h[8],const unsigned char block[64]) {
    static const uint32_t k[64]={
      0x428a2f98u,0x71374491u,0xb5c0fbcfu,0xe9b5dba5u,0x3956c25bu,0x59f111f1u,0x923f82a4u,0xab1c5ed5u,
      0xd807aa98u,0x12835b01u,0x243185beu,0x550c7dc3u,0x72be5d74u,0x80deb1feu,0x9bdc06a7u,0xc19bf174u,
      0xe49b69c1u,0xefbe4786u,0x0fc19dc6u,0x240ca1ccu,0x2de92c6fu,0x4a7484aau,0x5cb0a9dcu,0x76f988dau,
      0x983e5152u,0xa831c66du,0xb00327c8u,0xbf597fc7u,0xc6e00bf3u,0xd5a79147u,0x06ca6351u,0x14292967u,
      0x27b70a85u,0x2e1b2138u,0x4d2c6dfcu,0x53380d13u,0x650a7354u,0x766a0abbu,0x81c2c92eu,0x92722c85u,
      0xa2bfe8a1u,0xa81a664bu,0xc24b8b70u,0xc76c51a3u,0xd192e819u,0xd6990624u,0xf40e3585u,0x106aa070u,
      0x19a4c116u,0x1e376c08u,0x2748774cu,0x34b0bcb5u,0x391c0cb3u,0x4ed8aa4au,0x5b9cca4fu,0x682e6ff3u,
      0x748f82eeu,0x78a5636fu,0x84c87814u,0x8cc70208u,0x90befffau,0xa4506cebu,0xbef9a3f7u,0xc67178f2u};
    uint32_t w[64];
    for(unsigned i=0;i<16;++i)w[i]=(uint32_t(block[4*i])<<24)|(uint32_t(block[4*i+1])<<16)|(uint32_t(block[4*i+2])<<8)|block[4*i+3];
    for(unsigned i=16;i<64;++i){uint32_t a=w[i-15],b=w[i-2];w[i]=w[i-16]+(rotate(a,7)^rotate(a,18)^(a>>3))+w[i-7]+(rotate(b,17)^rotate(b,19)^(b>>10));}
    uint32_t a=h[0],b=h[1],c=h[2],d=h[3],e=h[4],f=h[5],g=h[6],v=h[7];
    for(unsigned i=0;i<64;++i){
        uint32_t t1=v+(rotate(e,6)^rotate(e,11)^rotate(e,25))+((e&f)^(~e&g))+k[i]+w[i];
        uint32_t t2=(rotate(a,2)^rotate(a,13)^rotate(a,22))+((a&b)^(a&c)^(b&c));
        v=g;g=f;f=e;e=d+t1;d=c;c=b;b=a;a=t1+t2;
    }
    h[0]+=a;h[1]+=b;h[2]+=c;h[3]+=d;h[4]+=e;h[5]+=f;h[6]+=g;h[7]+=v;
}
inline void digest(const void*data,size_t length,unsigned char out[32]) {
    uint32_t h[8]={0x6a09e667u,0xbb67ae85u,0x3c6ef372u,0xa54ff53au,0x510e527fu,0x9b05688cu,0x1f83d9abu,0x5be0cd19u};
    const unsigned char*p=(const unsigned char*)data;
    size_t whole=length/64;
    for(size_t i=0;i<whole;++i)compress(h,p+64*i);
    unsigned char tail[128]={};size_t remain=length%64;
    if(remain)memcpy(tail,p+64*whole,remain);
    tail[remain]=0x80;size_t padded=remain<56?64:128;
    uint64_t bits=uint64_t(length)*8;
    for(unsigned i=0;i<8;++i)tail[padded-1-i]=(unsigned char)(bits>>(8*i));
    compress(h,tail);if(padded==128)compress(h,tail+64);
    for(unsigned i=0;i<8;++i)for(unsigned j=0;j<4;++j)out[4*i+j]=(unsigned char)(h[i]>>(24-8*j));
}
}
#endif

static uint64_t splitmix64(uint64_t& state){
 state+=0x9e3779b97f4a7c15ULL;
 uint64_t z=state;
 z=(z^(z>>30))*0xbf58476d1ce4e5b9ULL;
 z=(z^(z>>27))*0x94d049bb133111ebULL;
 return z^(z>>31);
}
// One RNG word supplies 64 bool bytes; all bytes are valid 0 or 1.
__attribute__((noinline)) static void generate_bits(unsigned char* data,size_t length){
 uint64_t state=0x60d1ef5768912a3bULL;
 uint64_t* out=(uint64_t*)data;
 for(size_t i=0;i<length/64;++i){
  uint64_t r=splitmix64(state);
  for(unsigned j=0;j<8;++j){out[i*8+j]=_pdep_u64(r,0x0101010101010101ULL);r>>=8;}
 }
}
// Deliberate synthetic output only. It is used to time a full SHA scan,
// not asserted to equal the product of the independent input matrices.
static void synthetic_output(unsigned char* data,size_t length){
 static const unsigned char pattern[64]={0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1};
 for(size_t i=0;i<length;i+=64)memcpy(data+i,pattern,64);
}

int main(){
 constexpr size_t output_bytes=268435456ULL;
 unsigned char*buffer=nullptr;
 if(posix_memalign((void**)&buffer,4096,3*output_bytes))return 1;
 _mm_lfence();const unsigned long long t0=__rdtsc();
 generate_bits(buffer,2*output_bytes);
 _mm_lfence();const unsigned long long tg=__rdtsc();
 synthetic_output(buffer+2*output_bytes,output_bytes);
 _mm_lfence();const unsigned long long tc=__rdtsc();
 const unsigned char expected_digest[32]={0x42,0xbc,0xfa,0x5c,0x3c,0x57,0x69,0x16,0x7f,0xaf,0xce,0x3d,0x6b,0x17,0xef,0x7a,0x3d,0x3c,0x89,0xf8,0xf8,0x35,0x30,0xab,0x16,0x5c,0x68,0x33,0x74,0x31,0x6c,0x60};
 unsigned char actual_digest[32];
 duck_sha256::digest(buffer+2*output_bytes,output_bytes,actual_digest);
 _mm_lfence();const unsigned long long th=__rdtsc();
 if(memcmp(actual_digest,expected_digest,32))return 2;
 // Observe generated input after generation; cannot be dead-store eliminated.
 unsigned input_sum=0;for(size_t i=0;i<2*output_bytes;i+=4096)input_sum+=buffer[i];
 printf("generation_ticks=%llu output_setup_ticks=%llu hash_ticks=%llu input_sample_sum=%u bytes=%zu\n",tg-t0,tc-tg,th-tc,input_sum,output_bytes);
 free(buffer);return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #11.194 s768 MB + 20 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2026-10-13 13:41:48 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠