Files
lucioandClaude Opus 5.5 f5152fb709 Anti-scraping: 3x3 grids, decoy pools, robot check
Every pyramid level is cut into at least min_grid x min_grid tiles.
Each photo's tile pool mixes real tiles with hard-linked decoys from
other photos; the grid-to-pool map, byte sizes, seed and scramble seed
are sealed per build and unlocked by a press-and-hold check backed by
a static proof of work. Cell scrambling is on by default, and
robots.txt disallows tiles and pool files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 21:39:30 -08:00

214 lines
5.4 KiB
Go

package process
import (
"crypto/hmac"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"strconv"
"sync"
)
const (
ScrambleCell = 64
scrambleWorkers = 2
vipsHeaderSize = 64
)
var bandFormatSize = map[int32]int{0: 1, 1: 1, 2: 2, 3: 2, 4: 4, 5: 4, 6: 4, 7: 8, 8: 8, 9: 16}
func (p *Processor) ScrambleSeed(hash string) string {
mac := hmac.New(sha256.New, p.key)
mac.Write([]byte("scramble|" + hash))
return hex.EncodeToString(mac.Sum(nil))[:16]
}
func tileSeed(seed string, level, x, y int) uint32 {
h := uint32(2166136261)
for _, c := range []byte(seed + "|" + strconv.Itoa(level) + "|" + strconv.Itoa(x) + "|" + strconv.Itoa(y)) {
h ^= uint32(c)
h *= 16777619
}
return h
}
type mulberry32 struct {
state uint32
}
func (m *mulberry32) next() float64 {
m.state += 0x6D2B79F5
a := m.state
t := (a ^ (a >> 15)) * (a | 1)
t = (t + (t^(t>>7))*(t|61)) ^ t
return float64(t^(t>>14)) / 4294967296
}
func CellPermutation(n int, seed uint32) []int {
perm := make([]int, n)
for i := range perm {
perm[i] = i
}
rng := mulberry32{state: seed}
for i := n - 1; i > 0; i-- {
j := int(rng.next() * float64(i+1))
perm[i], perm[j] = perm[j], perm[i]
}
return perm
}
type vHeader struct {
width, height int
pixelBytes int
}
func readVHeader(f *os.File) (vHeader, error) {
var raw [vipsHeaderSize]byte
if _, err := io.ReadFull(io.NewSectionReader(f, 0, vipsHeaderSize), raw[:]); err != nil {
return vHeader{}, err
}
var order binary.ByteOrder
switch {
case raw[0] == vipsMagicIntel[0] && raw[1] == vipsMagicIntel[1] && raw[2] == vipsMagicIntel[2] && raw[3] == vipsMagicIntel[3]:
order = binary.LittleEndian
case raw[0] == vipsMagicSparc[0] && raw[1] == vipsMagicSparc[1] && raw[2] == vipsMagicSparc[2] && raw[3] == vipsMagicSparc[3]:
order = binary.BigEndian
default:
return vHeader{}, errors.New("not a vips image")
}
width := int(int32(order.Uint32(raw[4:8])))
height := int(int32(order.Uint32(raw[8:12])))
bands := int(int32(order.Uint32(raw[12:16])))
format := int32(order.Uint32(raw[20:24]))
coding := int32(order.Uint32(raw[24:28]))
size, ok := bandFormatSize[format]
if !ok || coding != 0 || width <= 0 || height <= 0 || bands <= 0 {
return vHeader{}, fmt.Errorf("unsupported vips image (format %d, coding %d)", format, coding)
}
return vHeader{width: width, height: height, pixelBytes: bands * size}, nil
}
func ScrambleV(src, dst, seed string, level, tileSize, cell int) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
header, err := readVHeader(in)
if err != nil {
return fmt.Errorf("%s: %w", src, err)
}
out, err := os.Create(dst)
if err != nil {
return err
}
ok := false
defer func() {
out.Close()
if !ok {
os.Remove(dst)
}
}()
if _, err := io.Copy(out, io.NewSectionReader(in, 0, vipsHeaderSize)); err != nil {
return err
}
rowBytes := int64(header.width) * int64(header.pixelBytes)
dataEnd := vipsHeaderSize + rowBytes*int64(header.height)
bands := (header.height + tileSize - 1) / tileSize
jobs := make(chan int)
errs := make([]error, scrambleWorkers)
var wg sync.WaitGroup
for w := 0; w < min(scrambleWorkers, bands); w++ {
wg.Add(1)
go func(w int) {
defer wg.Done()
buf := make([]byte, int64(min(tileSize, header.height))*rowBytes)
tmp := make([]byte, cell*cell*header.pixelBytes)
for band := range jobs {
if errs[w] != nil {
continue
}
errs[w] = scrambleBand(in, out, buf, tmp, header, band, rowBytes, seed, level, tileSize, cell)
}
}(w)
}
for band := 0; band < bands; band++ {
jobs <- band
}
close(jobs)
wg.Wait()
if err := errors.Join(errs...); err != nil {
return err
}
info, err := in.Stat()
if err != nil {
return err
}
if info.Size() > dataEnd {
if _, err := io.Copy(io.NewOffsetWriter(out, dataEnd), io.NewSectionReader(in, dataEnd, info.Size()-dataEnd)); err != nil {
return err
}
}
ok = true
return nil
}
func scrambleBand(in, out *os.File, buf, tmp []byte, header vHeader, band int, rowBytes int64, seed string, level, tileSize, cell int) error {
top := band * tileSize
rows := min(tileSize, header.height-top)
data := buf[:int64(rows)*rowBytes]
offset := vipsHeaderSize + int64(top)*rowBytes
if _, err := in.ReadAt(data, offset); err != nil {
return err
}
cellRows := rows / cell
cellBytes := cell * header.pixelBytes
tiles := (header.width + tileSize - 1) / tileSize
for tx := 0; tx < tiles; tx++ {
width := min(tileSize, header.width-tx*tileSize)
cols := width / cell
if cols < 2 || cellRows < 2 {
continue
}
n := cols * cellRows
perm := CellPermutation(n, tileSeed(seed, level, tx, band))
origin := tx * tileSize * header.pixelBytes
at := func(index, row int) []byte {
cy, cx := index/cols, index%cols
start := int64(cy*cell+row)*rowBytes + int64(origin+cx*cellBytes)
return data[start : start+int64(cellBytes)]
}
done := make([]bool, n)
for start := 0; start < n; start++ {
if done[start] || perm[start] == start {
done[start] = true
continue
}
for r := 0; r < cell; r++ {
copy(tmp[r*cellBytes:(r+1)*cellBytes], at(start, r))
}
i := start
for {
done[i] = true
j := perm[i]
if j == start {
for r := 0; r < cell; r++ {
copy(at(i, r), tmp[r*cellBytes:(r+1)*cellBytes])
}
break
}
for r := 0; r < cell; r++ {
copy(at(i, r), at(j, r))
}
i = j
}
}
}
_, err := out.WriteAt(data, offset)
return err
}