> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://www.getfoundry.sh/api/mcp` to find what you need.
>
> **Have feedback?** Use `submit_feedback` on the same MCP server.

## `signWithNonceUnsafe`

### Signature

```solidity
function signWithNonceUnsafe(uint256 privateKey, bytes32 digest, uint256 nonce) external pure returns (uint8 v, bytes32 r, bytes32 s);
```

### Description

Signs `digest` with `privateKey` on the secp256k1 curve, like [`sign`](/reference/cheatcodes/sign), but uses `nonce` as the ECDSA ephemeral value `k` instead of deriving it from the key and digest ([RFC 6979](https://www.rfc-editor.org/rfc/rfc6979)).

Use it to build signatures with a known `k`, for example to test code that must reject or detect a reused nonce. The signature is normalized to a low `s` value, and `v` is `27` or `28`.

`nonce` here is the signature nonce, not the account nonce from [`setNonce`](/reference/cheatcodes/set-nonce).

### Parameters

| Parameter    | Type      | Description                           |
|--------------|-----------|---------------------------------------|
| `privateKey` | `uint256` | The private key to sign with          |
| `digest`     | `bytes32` | The digest to sign                    |
| `nonce`      | `uint256` | The ECDSA nonce `k`                   |

### Returns

| Parameter | Type      | Description                      |
|-----------|-----------|----------------------------------|
| `v`       | `uint8`   | The recovery id, `27` or `28`    |
| `r`       | `bytes32` | The signature's `r` value        |
| `s`       | `bytes32` | The signature's `s` value        |

### Examples

```solidity [test/SignWithNonceUnsafe.t.sol]
// [!include ~/snippets/projects/nonces/test/SignWithNonceUnsafe.t.sol:sign-with-nonce-unsafe]
```

Two signatures with the same `k` share `r`:

```solidity [test/SignWithNonceUnsafe.t.sol]
// [!include ~/snippets/projects/nonces/test/SignWithNonceUnsafe.t.sol:sign-with-nonce-unsafe-reuse]
```

### Gotchas

:::warning
Never sign with a real private key this way. Anyone who sees two signatures with the same `k`, or knows `k`, can recover the private key.
:::

* Reverts with `nonce cannot be 0` for a zero nonce, and with `invalid nonce scalar` when `nonce` is not lower than the secp256k1 curve order.

### Related Cheatcodes

* [`sign`](/reference/cheatcodes/sign) - Signs a digest with a deterministic nonce
* [`signCompact`](/reference/cheatcodes/sign-compact) - Signs a digest, returning an EIP-2098 compact signature
* [`addr`](/reference/cheatcodes/addr) - Computes the address for a given private key
