Files
precompile/sr25519/verify_nocgo.go
T
zeekay e41cd6d4ab crypto precompiles: unvalidated points, a price that could not be paid, and free calls
Across the curve and hash precompiles: decode refuses a point that is not on
its curve or not canonically encoded, rather than handing an off-curve point to
an arithmetic routine; pasta's inversion no longer treats a multiple of p as
invertible, where ModInverse returns nil and leaves the divisor untouched;
compute refuses a quoted price wider than 256 bits instead of returning it
modulo, and enforces readOnly; poseidon's sponge fee grows with its input; hqc
reports an unwired backend as itself rather than as a verification failure; and
sr25519 pins that the cgo and non-cgo verifiers name the same backend and agree,
which is a consensus split when they do not. Every path deducts gas before it
works, so no call is free. KAT vectors accompany each.
2026-08-28 15:39:27 -07:00

44 lines
1.3 KiB
Go

// Copyright (C) 2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
//go:build !cgo
package sr25519
import (
schnorrkel "github.com/ChainSafe/go-schnorrkel"
)
// backend names the verifier this build linked. The two builds must return the
// same verdict for every input; see the parity corpus in the tests.
const backend = "go-schnorrkel (pure Go)"
// verifySR25519 is the pure-Go fallback when CGO is disabled.
// Uses ChainSafe/go-schnorrkel for Substrate-compatible sr25519 verification.
func verifySR25519(publicKey, signature, message []byte) bool {
if len(publicKey) != PublicKeySize || len(signature) != SignatureSize || len(message) == 0 {
return false
}
var pubBytes [32]byte
copy(pubBytes[:], publicKey)
pub, err := schnorrkel.NewPublicKey(pubBytes)
if err != nil {
return false
}
var sigBytes [64]byte
copy(sigBytes[:], signature)
sig := new(schnorrkel.Signature)
if err := sig.Decode(sigBytes); err != nil {
return false
}
// "substrate" is the default signing context used by all Substrate runtimes
// and polkadot-js. This matches the sr25519-donna C library.
transcript := schnorrkel.NewSigningContext([]byte("substrate"), message)
ok, err := pub.Verify(sig, transcript)
return err == nil && ok
}