Skip to main content

Crate tide_fn_dsa

Crate tide_fn_dsa 

Source
Expand description

§FN-DSA implementation

This crate is really a wrapper for the tide-fn-dsa-kgen, tide-fn-dsa-sign and tide-fn-dsa-vrfy crates that implement the various elements of the FN-DSA signature algorithm. All the relevant types, functions and constants are re-exported here. Users of this implementation only need to import this crate; the division into sub-crates is meant to help with specialized situations where code footprint reduction is important (typically, embedded systems that only need to verify signatures, but not generate keys or signatures).

§Tidecoin compatibility

Use sign_falcon / verify_falcon (on the corresponding signing or verifying type) with an explicit FalconProfile. TidecoinStrict requires canonical, exactly consumed compressed signatures for degrees 512 and 1024. TidecoinLegacyFalcon512 preserves historical Falcon-512 encodings and the exclusive legacy norm bound. PqClean retains its padded-format compatibility. The generic API described below is separate. The raw Falcon API consumes the supplied message bytes without adding FN-DSA context/domain metadata or a transaction sighash byte.

§WARNING

The generic FN-DSA API is experimental and is not claimed to conform to a final FIPS 206 standard. Its encodings, pre-hashing and domain separation may evolve. The explicit Tidecoin profiles and deterministic PQHD key mapping have a separate compatibility contract and must not be changed silently to follow future FN-DSA revisions.

§Implementation notes

The whole code is written in pure Rust and is compatible with no_std. Production dependencies include rand_core, zeroize, sha2 and cpufeatures; unit tests use additional crates.

On x86 (both 32-bit and 64-bit), AVX2 opcodes are automatically used for faster operations if their support is detected at runtime. No special compilation flag nor extra runtime check is needed for that; the compiled code remains compatible with plain non-AVX2-aware CPUs.

On 64-bit x86 (x86_64) and ARMv8 (aarch64, arm64ec), native (hardware) floating-point support is used, since in both these cases the architecture ABI mandates a strict IEEE-754 unit and can more or less be assumed to operate in constant-time for non-exceptional inputs. This makes signature generation much faster on these platforms (on x86_64, this furthermore combines with AVX2 optimizations if available in the current CPU). On other platforms, a portable emulation of floating-point operations is used (this emulation makes a best effort at operating in constant-time, though some recent compiler optimizations might introduce variable-time operations).

Numeric behavior differs by subsystem:

  • classic/native key generation uses integer and fixed-point arithmetic
  • PQClean/Tidecoin deterministic key generation uses an integer-only emulation of Falcon FLR/FFT semantics
  • signature verification uses integer arithmetic

The key pair generation implementation is a translation of the ntrugen code, which is faster than the originally submitted Falcon code. The signature generation engine follows the steps of the sign_dyn operations from the original falcon code (indeed, an internal unit tests checks that the sampler returns the same values for the same inputs). Achieved performance on x86_64 is very close to that offered by the C code (signature verification performance is even better).

§Example usage

For deterministic Falcon key derivation, there are now two explicit seeded key-generation families on the Falcon key generators:

  • keygen_from_seed_native() uses the upstream fn-dsa / ntrugen seeded Falcon path.
  • keygen_from_seed_pqclean() and keygen_from_stream_key_tidecoin() use PQClean/Tidecoin-compatible seeded Falcon key generation.

These APIs are intentionally separate because the same 48-byte seed does not map to the same Falcon key pair in the two families. The PQClean/Tidecoin APIs are compatibility-oriented deterministic key-derivation entrypoints; they preserve the original seeded Falcon mapping and rejection behavior, but they do not imply that PQClean code is used for signing or verification.

use tide_fn_dsa::{
    SIGN_KEY_SIZE_512, VRFY_KEY_SIZE_512, SIGNATURE_SIZE_512, FN_DSA_LOGN_512,
    KeyPairGenerator, KeyPairGeneratorStandard,
    SigningKey, SigningKeyStandard,
    VerifyingKey, VerifyingKeyStandard,
    DOMAIN_NONE, HASH_ID_RAW, CryptoRng, RngCore, RngError,
};

// Generate key pair.
let mut rng = DemoRng(1);
let mut kg = KeyPairGeneratorStandard::default();
let mut sign_key = [0u8; SIGN_KEY_SIZE_512];
let mut vrfy_key = [0u8; VRFY_KEY_SIZE_512];
kg.keygen(FN_DSA_LOGN_512, &mut rng, &mut sign_key, &mut vrfy_key)
    .unwrap();

// Sign a message with the signing key.
let mut sk = SigningKeyStandard::decode(&sign_key)
    .expect("valid signing key bytes");
let mut sig = [0u8; SIGNATURE_SIZE_512];
sk.sign(&mut rng, &DOMAIN_NONE, &HASH_ID_RAW, b"message", &mut sig)
    .unwrap();

// Verify a signature with the verifying key.
match VerifyingKeyStandard::decode(&vrfy_key) {
    Some(vk) => {
        if vk.verify(&sig, &DOMAIN_NONE, &HASH_ID_RAW, b"message") {
            // signature is valid
        } else {
            // signature is not valid
        }
    }
    _ => {
        // could not decode verifying key
    }
}

Falcon compatibility APIs are also exposed explicitly:

use tide_fn_dsa::{
    SIGN_KEY_SIZE_512, VRFY_KEY_SIZE_512, TIDECOIN_LEGACY_FALCON512_SIG_MAX,
    FN_DSA_LOGN_512, FalconProfile,
    KeyPairGenerator, KeyPairGeneratorStandard,
    SigningKey, SigningKeyStandard,
    VerifyingKey, VerifyingKeyStandard,
    CryptoRng, RngCore, RngError,
};

let mut rng = DemoRng(7);
let mut kg = KeyPairGeneratorStandard::default();
let mut sign_key = [0u8; SIGN_KEY_SIZE_512];
let mut vrfy_key = [0u8; VRFY_KEY_SIZE_512];
kg.keygen(FN_DSA_LOGN_512, &mut rng, &mut sign_key, &mut vrfy_key)
    .unwrap();

let mut sk = SigningKeyStandard::decode(&sign_key).unwrap();
let vk = VerifyingKeyStandard::decode(&vrfy_key).unwrap();
let mut sig = [0u8; TIDECOIN_LEGACY_FALCON512_SIG_MAX];
let sig_len = sk.sign_falcon(
    &mut rng,
    FalconProfile::TidecoinLegacyFalcon512,
    b"message",
    &mut sig,
).unwrap();
assert!(vk.verify_falcon(
    FalconProfile::TidecoinLegacyFalcon512,
    &sig[..sig_len],
    b"message",
));

Structs§

DomainContext
When a message is signed or verified, it is accompanied with a domain separation context, which is an arbitrary sequence of bytes of length at most 255. Such a context is wrapped in a DomainContext structure.
HashIdentifier
The message for which a signature is to be generated or verified is pre-hashed by the caller and provided as a hash value along with an identifier of the used hash function. The identifier is normally an encoded ASN.1 OID. A special identifier is used for “raw” messages (i.e. not pre-hashed at all); it uses a single byte of value 0x00.
KeyPairGenerator512
Key pair generator for degrees (logn) 9 to 9 only.
KeyPairGenerator1024
Key pair generator for degrees (logn) 10 to 10 only.
KeyPairGeneratorStandard
Key pair generator for degrees (logn) 9 to 10 only.
KeyPairGeneratorWeak
Key pair generator for degrees (logn) 2 to 8 only.
RngError
Error type of random number generators
SHA3_224
SHA3-224 implementation.
SHA3_256
SHA3-256 implementation.
SHA3_384
SHA3-384 implementation.
SHA3_512
SHA3-512 implementation.
SHAKE
SHAKE implementation.
SigningKey512
Signature generator for degrees (logn) 9 to 9 only.
SigningKey1024
Signature generator for degrees (logn) 10 to 10 only.
SigningKeyStandard
Signature generator for degrees (logn) 9 to 10 only.
SigningKeyWeak
Signature generator for degrees (logn) 2 to 8 only.
VerifyingKey512
Signature verifier for degrees (logn) 9 to 9 only.
VerifyingKey1024
Signature verifier for degrees (logn) 10 to 10 only.
VerifyingKeyStandard
Signature verifier for degrees (logn) 9 to 10 only.
VerifyingKeyWeak
Signature verifier for degrees (logn) 2 to 8 only.

Enums§

DeterministicKeyGenError
Error type for deterministic Falcon key pair generation.
DomainContextError
Error type for domain-separation contexts.
FalconProfile
Falcon-compatible raw-message profile.
HashIdentifierError
Error type for hash identifiers.
HashToPointError
Error type for message hashing into a point.
KeyGenError
Error type for key pair generation.
LogNError
Error type for invalid degree parameters.
ShakeError
Error type for SHAKE state transitions.
SigningKeyError
Error type for signing-key operations with caller-supplied buffers.

Constants§

DOMAIN_NONE
Empty domain separation context.
FALCON_KEYGEN_SEED_SIZE
Seed length for deterministic Falcon key generation.
FALCON_NONCE_LEN
Falcon nonce length in bytes.
FN_DSA_LOGN_512
Symbolic constant for FN-DSA with degree 512 (logn = 9).
FN_DSA_LOGN_1024
Symbolic constant for FN-DSA with degree 1024 (logn = 10).
HASH_ID_RAW
Hash function identifier: none.
HASH_ID_SHA3_256
Hash function identifier: SHA3-256
HASH_ID_SHA3_384
Hash function identifier: SHA3-384
HASH_ID_SHA3_512
Hash function identifier: SHA3-512
HASH_ID_SHA256
Hash function identifier: SHA-256
HASH_ID_SHA384
Hash function identifier: SHA-384
HASH_ID_SHA512
Hash function identifier: SHA-512
HASH_ID_SHA512_256
Hash function identifier: SHA-512-256
HASH_ID_SHAKE128
Hash function identifier: SHAKE128
HASH_ID_SHAKE256
Hash function identifier: SHAKE256
PQHD_KEYGEN_STREAM_SIZE
Stream-key length for Tidecoin PQHD deterministic Falcon key generation.
PQHD_MAX_DETERMINISTIC_ATTEMPTS
Maximum number of deterministic stream-key retry attempts.
SIGNATURE_SIZE_512
Signature size for degree 512 (logn = 9).
SIGNATURE_SIZE_1024
Signature size for degree 1024 (logn = 10).
SIGN_KEY_SIZE_512
Signing key size for degree 512 (logn = 9).
SIGN_KEY_SIZE_1024
Signing key size for degree 1024 (logn = 10).
TIDECOIN_LEGACY_FALCON512_SIG_BODY_MAX
Historical Falcon-512 signer body capacity; not a verification limit.
TIDECOIN_LEGACY_FALCON512_SIG_MAX
Historical Falcon-512 signer capacity; not the node wallet signing bound (666 bytes) and not a verification limit.
VRFY_KEY_SIZE_512
Verifying key size for degree 512 (logn = 9).
VRFY_KEY_SIZE_1024
Verifying key size for degree 1024 (logn = 10).

Traits§

CryptoRng
A marker trait used to indicate that an RngCore or BlockRngCore implementation is supposed to be cryptographically secure.
KeyPairGenerator
Key pair generator and temporary buffers.
RngCore
The core of a random number generator.
SigningKey
Signing key handler and temporary buffers.
VerifyingKey
Verifying key handler.

Functions§

hash_to_point
Hash a message into a polynomial modulo q = 12289.
hash_to_point_falcon
Hash a raw Falcon/PQClean message into a polynomial modulo q = 12289.
is_noncanonical_legacy_falcon512
Format-only classifier for historical Tidecoin Falcon-512 encodings. Classify a complete raw Falcon-512 signature as a noncanonical historical encoding. Returns false for canonical encodings and malformed buffers, including trailing bytes. This does not verify the signature, strip a transaction sighash byte, or select relay policy.
sign_key_size
Get the size (in bytes) of a signing key for the provided degree (degree is n = 2^logn, with 2 <= logn <= 10).
signature_size
Get the size (in bytes) of a signature for the provided degree (degree is n = 2^logn, with 2 <= logn <= 10).
vrfy_key_size
Get the size (in bytes) of a verifying key for the provided degree (degree is n = 2^logn, with 2 <= logn <= 10).

Type Aliases§

SHAKE128
Type specialization for SHAKE128.
SHAKE256
Type specialization for SHAKE256.