R14:rawptr[X] -> X*(41 文件 / 2037 行改动),X 含模块限定名与嵌套下标(rawptr[GSList[NameEntry]] -> GSList[NameEntry]*)。保留 6 处 rawptr[t.CPtr](规范例外:存纯裸指针);# 注释与字符串里的 rawptr[...] 属元语法描述,未动。 R16(保守子集):t.CConst | str/bytes -> const str/bytes,t.CConst | char | t.CPtr -> const str;typedef 右值位置豁免(const str 在该处无法注册,与 R1 同源)。 验证:TPC 自举三重重编 SUCCESS;TransPyV.exe 全量 77/77 顺序回归通过。
120 lines
5.0 KiB
Plaintext
120 lines
5.0 KiB
Plaintext
import string
|
|
import t, c
|
|
|
|
|
|
# ==============================================
|
|
# SHA256 Hash Algorithm
|
|
# ==============================================
|
|
define SHA256_BLOCK_LEN = 64
|
|
define SHA256_DIGEST_LEN = 32
|
|
|
|
# SHA256 Constants K
|
|
sha256_K: const [64]uint32 = [
|
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
]
|
|
|
|
# SHA256 Auxiliary Functions
|
|
define s0(x: uint32) -> uint32: return ((x >> 7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ ((x >> 3))
|
|
define s1(x: uint32) -> uint32: return ((x >> 17) | (x << 15)) ^ ((x >> 19) | (x << 13)) ^ ((x >> 10))
|
|
define S0(x: uint32) -> uint32: return ((x >> 2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10))
|
|
define S1(x: uint32) -> uint32: return ((x >> 6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x << 7))
|
|
define Ch(x: uint32, y: uint32, z: uint32) -> uint32: return (x & y) ^ (~x & z)
|
|
define Maj(x: uint32, y: uint32, z: uint32) -> uint32: return (x & y) ^ (x & z) ^ (y & z)
|
|
|
|
# SHA256 Context
|
|
class sha256:
|
|
state: [8]uint32 = [
|
|
0x6a09e667, 0xbb67ae85,
|
|
0x3c6ef372, 0xa54ff53a,
|
|
0x510e527f, 0x9b05688c,
|
|
0x1f83d9ab, 0x5be0cd19
|
|
] # 8 32-bit state registers
|
|
count: uint64 = 0 # processed bits
|
|
buf: [SHA256_BLOCK_LEN]uint8
|
|
def __init__(self):
|
|
string.memset(self, 0, sha256.__sizeof__())
|
|
# string.memset(self.buf, 0, SHA256_BLOCK_LEN)
|
|
# SHA256 single-group compression transform
|
|
def transform(self, block: uint8*):
|
|
w: [64]uint32
|
|
for i in range(16):
|
|
w[i] = ((uint32(block[i * 4 + 0]) << 24) |
|
|
(uint32(block[i * 4 + 1]) << 16) |
|
|
(uint32(block[i * 4 + 2]) << 8 ) |
|
|
(uint32(block[i * 4 + 3]) << 0 ))
|
|
# Extend to 64 words
|
|
for i in range(16, 64):
|
|
w[i] = w[i-16] + s0(w[i-15]) + w[i-7] + s1(w[i-2])
|
|
a: uint32 = self.state[0]
|
|
b: uint32 = self.state[1]
|
|
c: uint32 = self.state[2]
|
|
d: uint32 = self.state[3]
|
|
e: uint32 = self.state[4]
|
|
f: uint32 = self.state[5]
|
|
g: uint32 = self.state[6]
|
|
h: uint32 = self.state[7]
|
|
t1: uint32
|
|
t2: uint32
|
|
# 64-round iterations
|
|
for i in range(64):
|
|
t1 = h + S1(e) + Ch(e,f,g) + sha256_K[i] + w[i]
|
|
t2 = S0(a) + Maj(a,b,c)
|
|
h = g; g = f; f = e
|
|
e = d + t1
|
|
d = c; c = b; b = a
|
|
a = t1 + t2
|
|
# Accumulate to state registers
|
|
self.state[0] += a
|
|
self.state[1] += b
|
|
self.state[2] += c
|
|
self.state[3] += d
|
|
self.state[4] += e
|
|
self.state[5] += f
|
|
self.state[6] += g
|
|
self.state[7] += h
|
|
def update(self, s: str):
|
|
data: uint8* = uint8*(s)
|
|
length: sizet = len(s)
|
|
idx: sizet = self.count % SHA256_BLOCK_LEN
|
|
self.count += length
|
|
if idx > 0:
|
|
left: sizet = SHA256_BLOCK_LEN - idx
|
|
if length >= left:
|
|
string.memcpy(self.buf + idx, data, left)
|
|
self.transform(self.buf)
|
|
data += left
|
|
length -= left
|
|
idx = 0
|
|
while length >= SHA256_BLOCK_LEN:
|
|
self.transform(data)
|
|
data += SHA256_BLOCK_LEN
|
|
length -= SHA256_BLOCK_LEN
|
|
if length > 0:
|
|
string.memcpy(self.buf + idx, data, length)
|
|
def final(self, out: [SHA256_DIGEST_LEN]uint8):
|
|
idx: sizet = self.count % SHA256_BLOCK_LEN
|
|
self.buf[idx] = 0x80
|
|
idx += 1
|
|
if idx > SHA256_BLOCK_LEN - 8:
|
|
string.memset(self.buf + idx, 0, SHA256_BLOCK_LEN - idx)
|
|
self.transform(self.buf)
|
|
idx = 0
|
|
string.memset(self.buf + idx, 0, SHA256_BLOCK_LEN - 8 - idx)
|
|
bits: uint64 = self.count << 3
|
|
for i in range(8):
|
|
self.buf[SHA256_BLOCK_LEN-8+i] = (bits >> (56 - i*8)) & 0xFF
|
|
self.transform(self.buf)
|
|
# Byte order 32-bit words
|
|
for i in range(8):
|
|
out[i * 4 + 0] = (self.state[i] >> 24) & 0xFF
|
|
out[i * 4 + 1] = (self.state[i] >> 16) & 0xFF
|
|
out[i * 4 + 2] = (self.state[i] >> 8) & 0xFF
|
|
out[i * 4 + 3] = (self.state[i] >> 0) & 0xFF
|