Files
dex/pkg/lx/signed_order_cpu.go
zooqueen f2e3705e96 lx: make CGO=1 build cross-platform; honest secp256k1 GPU fallback
CGO_ENABLED=1 go build ./... failed to link every binary importing
pkg/lx: signed_order_gpu.go unconditionally force-linked the Apple-only
libsecp256k1_metal.a via a hardcoded build-tree -L path and the
signed-order Metal anchor. That archive is not built by default and has
no pkg-config bundle, so the link aborted with 'library secp256k1_metal
not found'. The GPU-vs-CPU choice must be a runtime decision, never a
hard build-time link dependency on an absent accelerator.

- Gate the secp256k1 Metal force-link anchor (.go + .c) behind the
  opt-in 'lux_secp256k1_metal' build tag and move the Metal/Foundation
  LDFLAGS there. Default CGO builds now link and run the secp256k1 CPU
  ecrecover pipeline (via the working lux-crypto-secp256k1 .pc); the
  Metal accelerator, when its archive is built and the tag is set, is
  still resolved at runtime by cpp/ecrecover.cpp via dlsym gated on
  LUX_SECP256K1_METALLIB / LUX_SECP256K1_BACKEND.
- signed_order_gpu.go keeps only the CPU pkg-config; the anchor file
  documents the build + opt-in recipe.
- signed_order_cpu.go: verifyOrdersCPU returns nil for empty input,
  matching the cgo BatchVerifyOrders contract so CPU and cgo paths agree
  byte-for-byte (fixes TestBatchVerifyOrders_Empty under CGO=0).
- README: remove the non-existent 'dex_gpu' build-tag claim; document
  the real model — AMM/CLOB GPU paths live in pkg/lx, bind the luxcpp
  DEX kernels via pkg-config (lux-dex-amm-metal / -amm-cuda / -clob-cuda),
  and select GPU-vs-CPU at runtime via crypto/backend detection with a
  logged, honest CPU fallback.

The DEX AMM Metal bridge (amm_xyk_batch_metal) now compiles, links, and
its dispatch is byte-identical to the CPU oracle (parity test green
under CGO=1). CLOB GPU primitive (MatchOrderGPU) stays a flat-buffer
parity/backtest primitive, intentionally not wired into the live
linked-list MatchOrders() engine.

Verified: CGO_ENABLED=0 go test ./... green (16 pkgs, 1360 cases);
CGO_ENABLED=1 go build ./... exit 0.
2026-06-21 22:37:05 -07:00

40 lines
1.2 KiB
Go

// Copyright (C) 2025-2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package lx
import (
"github.com/luxfi/crypto"
"github.com/luxfi/geth/common"
)
// verifyOrdersCPU is the per-order ecrecover oracle used by the test as the
// reference and as the fallback for non-cgo / non-darwin builds. For each
// order it recomputes the signing hash, recovers the pubkey from the sig,
// derives the 20-byte address and compares with order.Sender.
func verifyOrdersCPU(orders []SignedOrder) ([]bool, error) {
// Empty input round-trips as nil, matching the cgo BatchVerifyOrders
// contract (signed_order_gpu.go) so both paths agree byte-for-byte.
if len(orders) == 0 {
return nil, nil
}
out := make([]bool, len(orders))
for i := range orders {
hash, err := orders[i].SigningHash()
if err != nil {
out[i] = false
continue
}
pub, err := crypto.Ecrecover(hash[:], orders[i].Sig[:])
if err != nil || len(pub) != 65 {
out[i] = false
continue
}
// pub is the uncompressed pubkey (0x04 || X || Y). Address is
// last 20 bytes of keccak256(X || Y).
addr := common.BytesToAddress(crypto.Keccak256(pub[1:])[12:])
out[i] = addr == orders[i].Sender
}
return out, nil
}