Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,29 @@ hardware target.
them into the existing state. The module keeps mutable DRBG and source-check
state, so calls must be serialized. Python calls require the VM's GIL when
threads are enabled; direct C callers own that synchronization.

## Experimental interactive Schnorr anti-exfil

Enable `NGU_INCL_SCHNORR=1` (or `NGU_INCL_MUSIG=1`) when building.
The anti-exfil API is omitted when Schnorr support is disabled.

`ngu.secp256k1.anti_exfil_commit(keypair, message32, commitment32)` returns
the full 33-byte compressed original nonce. It does not return a signature.
`anti_exfil_sign(keypair, message32, commitment32, nonce33, entropy32)`
checks both supplied commitment fields, applies the host's nonzero scalar,
and verifies the resulting BIP340 signature before returning its 64 bytes.
Pass the actual signing keypair, including the Taproot tweak for key-path
signing. Both calls use the same keypair and message.

This implements the draft BIP-EXFIL single-signer protocol; the two hash tags
are provisional and vectors must be regenerated after BIP tag assignment.
It is separate from ordinary BIP340 signing, whose nonce derivation must not
be reused for this additive protocol. The host must independently check the
signature and the relation to its originally recorded nonce. After receiving
a nonce, a retry must reuse the same transcript rather than fresh entropy.
Neither round's transcript belongs in transaction metadata or diagnostic logs.

The shared eight-vector test is `ngu/ngu_tests/test_anti_exfil.py`; run it
with the native MicroPython build that includes ngu. No secret scalar
operations are performed in MicroPython. This experimental implementation
has not received cryptographic review.
128 changes: 128 additions & 0 deletions ngu/anti_exfil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* Experimental BIP-EXFIL single-signer Schnorr protocol. */
#ifndef NGU_ANTI_EXFIL_H
#define NGU_ANTI_EXFIL_H

#include <string.h>
#include "secp256k1/include/secp256k1.h"
#include "secp256k1/include/secp256k1_extrakeys.h"
#include "secp256k1/include/secp256k1_schnorrsig.h"

static void ngu_ae_clear(void *data, size_t len)
{
volatile unsigned char *p = data;
while (len--) *p++ = 0;
}

/* A SHA256 digest is less than 2*n, so exactly one subtraction suffices.
* Select the result without branching on the secret nonce candidate. */
static void ngu_ae_reduce(unsigned char scalar[32])
{
static const unsigned char order[32] = {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,
0xba,0xae,0xdc,0xe6,0xaf,0x48,0xa0,0x3b,
0xbf,0xd2,0x5e,0x8c,0xd0,0x36,0x41,0x41
};
unsigned char reduced[32];
unsigned int borrow = 0, mask;
int i;
for (i = 31; i >= 0; --i) {
unsigned int diff = (unsigned int)scalar[i] - order[i] - borrow;
reduced[i] = diff & 255;
borrow = (diff >> 8) & 1;
}
mask = 0u - (1u - borrow);
for (i = 0; i < 32; ++i) {
scalar[i] = (reduced[i] & mask) | (scalar[i] & ~mask);
}
ngu_ae_clear(reduced, sizeof(reduced));
}

static int ngu_ae_nonce(unsigned char nonce32[32], const unsigned char *msg,
size_t msglen, const unsigned char key32[32],
const unsigned char pk32[32], const unsigned char h[32])
{
static const unsigned char tag[] = "BIP-EXFIL/nonce";
int ok = secp256k1_nonce_function_bip340(nonce32, msg, msglen, key32,
pk32, tag, sizeof(tag)-1, (void *)h);
ngu_ae_reduce(nonce32);
return ok;
}

static int ngu_anti_exfil_commit(const secp256k1_context *ctx,
unsigned char nonce33[33],
const unsigned char msg32[32],
const secp256k1_keypair *keypair,
const unsigned char h[32])
{
unsigned char key32[32], pk32[32], nonce32[32];
secp256k1_xonly_pubkey pk;
secp256k1_pubkey nonce;
size_t len = 33;
int parity = 0, ok;
ok = secp256k1_keypair_sec(ctx, key32, keypair);
ok &= secp256k1_keypair_xonly_pub(ctx, &pk, &parity, keypair);
ok &= secp256k1_xonly_pubkey_serialize(ctx, pk32, &pk);
if (parity) ok &= secp256k1_ec_seckey_negate(ctx, key32);
ok &= ngu_ae_nonce(nonce32, msg32, 32, key32, pk32, h);
if (ok) ok = secp256k1_ec_pubkey_create(ctx, &nonce, nonce32);
if (ok) ok = secp256k1_ec_pubkey_serialize(ctx, nonce33, &len, &nonce, SECP256K1_EC_COMPRESSED);
if (!ok) memset(nonce33, 0, 33);
ngu_ae_clear(key32, sizeof(key32));
ngu_ae_clear(nonce32, sizeof(nonce32));
return ok;
}

typedef struct {
const secp256k1_context *ctx;
const unsigned char *h;
const unsigned char *rho;
} ngu_ae_nonce_data;

static int ngu_ae_tweaked_nonce(unsigned char *nonce32, const unsigned char *msg,
size_t msglen, const unsigned char *key32,
const unsigned char *pk32,
const unsigned char *algo, size_t algolen,
void *data)
{
ngu_ae_nonce_data *args = data;
int ok;
(void)algo;
(void)algolen;
ok = ngu_ae_nonce(nonce32, msg, msglen, key32, pk32, args->h);
/* This also rejects the zero original nonce and the infinite final nonce. */
ok &= secp256k1_ec_seckey_tweak_add(args->ctx, nonce32, args->rho);
return ok;
}

static int ngu_anti_exfil_sign(const secp256k1_context *ctx,
unsigned char sig64[64],
const unsigned char msg32[32],
const secp256k1_keypair *keypair,
const unsigned char h[32],
const unsigned char nonce33[33],
const unsigned char rho[32])
{
static const unsigned char tag[] = "BIP-EXFIL/host";
unsigned char computed_h[32], computed_nonce[33];
secp256k1_xonly_pubkey pk;
secp256k1_schnorrsig_extraparams extra = SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT;
ngu_ae_nonce_data data = {ctx, h, rho};
int ok;
memset(sig64, 0, 64);
if (!secp256k1_ec_seckey_verify(ctx, rho)) return 0;
ok = secp256k1_tagged_sha256(ctx, computed_h, tag, sizeof(tag)-1, rho, 32);
if (!ok || memcmp(h, computed_h, 32)) return 0;
if (!ngu_anti_exfil_commit(ctx, computed_nonce, msg32, keypair, h)) return 0;
if (memcmp(nonce33, computed_nonce, 33)) return 0;
extra.noncefp = ngu_ae_tweaked_nonce;
extra.ndata = &data;
ok = secp256k1_schnorrsig_sign_custom(ctx, sig64, msg32, 32, keypair, &extra);
ok &= secp256k1_keypair_xonly_pub(ctx, &pk, NULL, keypair);
/* Fault detection is mandatory before releasing the signature. */
ok &= secp256k1_schnorrsig_verify(ctx, sig64, msg32, 32, &pk);
if (!ok) memset(sig64, 0, 64);
return ok;
}

#endif
53 changes: 53 additions & 0 deletions ngu/k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "cifra/ext/handy.h"

#include "sec_shared.h"
#if NGU_INCL_SCHNORR
#include "anti_exfil.h"
#endif

// NGU_INCL_SCHNORR / NGU_INCL_MUSIG are set by the build (ngu/micropython.mk),
// default off. They gate the optional BIP340 Schnorr and MuSig2 bindings below.
Expand Down Expand Up @@ -478,6 +481,54 @@ STATIC mp_obj_t s_sign_schnorr(mp_obj_t privkey_in, mp_obj_t digest_in, mp_obj_t
return mp_obj_new_str_from_vstr(&mp_type_bytes, &rv);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(s_sign_schnorr_obj, s_sign_schnorr);

// Experimental interactive single-signer Schnorr protocol. The keypair must
// already include the Taproot tweak; all secret arithmetic stays in native code.
STATIC mp_obj_t s_anti_exfil_commit(mp_obj_t keypair_in, mp_obj_t digest_in, mp_obj_t commitment_in)
{
sec_setup_ctx();
if(mp_obj_get_type(keypair_in) != &s_keypair_type) {
mp_raise_TypeError(MP_ERROR_TEXT("keypair type"));
}
mp_obj_keypair_t *keypair = MP_OBJ_TO_PTR(keypair_in);
mp_buffer_info_t digest, commitment;
mp_get_buffer_raise(digest_in, &digest, MP_BUFFER_READ);
mp_get_buffer_raise(commitment_in, &commitment, MP_BUFFER_READ);
if(digest.len != 32 || commitment.len != 32) {
mp_raise_ValueError(MP_ERROR_TEXT("anti-exfil length"));
}
unsigned char nonce[33];
if(!ngu_anti_exfil_commit(lib_ctx, nonce, digest.buf, &keypair->keypair, commitment.buf)) {
mp_raise_ValueError(MP_ERROR_TEXT("anti-exfil commitment"));
}
return mp_obj_new_bytes(nonce, sizeof(nonce));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(s_anti_exfil_commit_obj, s_anti_exfil_commit);

STATIC mp_obj_t s_anti_exfil_sign(size_t n_args, const mp_obj_t *args)
{
(void)n_args;
sec_setup_ctx();
if(mp_obj_get_type(args[0]) != &s_keypair_type) {
mp_raise_TypeError(MP_ERROR_TEXT("keypair type"));
}
mp_obj_keypair_t *keypair = MP_OBJ_TO_PTR(args[0]);
mp_buffer_info_t fields[4];
const size_t lengths[4] = {32, 32, 33, 32};
for(int i = 0; i < 4; ++i) {
mp_get_buffer_raise(args[i+1], &fields[i], MP_BUFFER_READ);
if(fields[i].len != lengths[i]) {
mp_raise_ValueError(MP_ERROR_TEXT("anti-exfil length"));
}
}
unsigned char signature[64];
if(!ngu_anti_exfil_sign(lib_ctx, signature, fields[0].buf, &keypair->keypair,
fields[1].buf, fields[2].buf, fields[3].buf)) {
mp_raise_ValueError(MP_ERROR_TEXT("anti-exfil signing"));
}
return mp_obj_new_bytes(signature, sizeof(signature));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(s_anti_exfil_sign_obj, 5, 5, s_anti_exfil_sign);
#endif // NGU_INCL_SCHNORR

// KEY PAIRS (private key, with public key computed)
Expand Down Expand Up @@ -1361,6 +1412,8 @@ STATIC const mp_rom_map_elem_t globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_sign), MP_ROM_PTR(&s_sign_obj) },
#if NGU_INCL_SCHNORR
{ MP_ROM_QSTR(MP_QSTR_sign_schnorr), MP_ROM_PTR(&s_sign_schnorr_obj) },
{ MP_ROM_QSTR(MP_QSTR_anti_exfil_commit), MP_ROM_PTR(&s_anti_exfil_commit_obj) },
{ MP_ROM_QSTR(MP_QSTR_anti_exfil_sign), MP_ROM_PTR(&s_anti_exfil_sign_obj) },
{ MP_ROM_QSTR(MP_QSTR_verify_schnorr), MP_ROM_PTR(&s_verify_schnorr_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_ctx_rnd), MP_ROM_PTR(&s_ctx_rnd_obj) },
Expand Down
1 change: 1 addition & 0 deletions ngu/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"ngu_tests/test_hdnode_gen.py",
"ngu_tests/test_hmac.py",
"ngu_tests/test_k1.py",
"ngu_tests/test_anti_exfil.py",
"ngu_tests/test_k1_gen.py",
"ngu_tests/test_schnorr.py",
"ngu_tests/test_musig.py",
Expand Down
1 change: 1 addition & 0 deletions ngu/ngu_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test tests: hash-drbg-cavp gates
$(MPY) test_random.py
$(PY) test_k1.py
$(MPY) test_k1.py
$(MPY) test_anti_exfil.py
$(MPY) test_k1_gen.py
$(MPY) test_schnorr.py
$(MPY) test_musig.py
Expand Down
1 change: 1 addition & 0 deletions ngu/ngu_tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ngu_tests.test_hdnode
import ngu_tests.test_hdnode_gen
import ngu_tests.test_k1
import ngu_tests.test_anti_exfil
import ngu_tests.test_k1_gen
import ngu_tests.test_schnorr
import ngu_tests.test_musig
Expand Down
36 changes: 36 additions & 0 deletions ngu/ngu_tests/test_anti_exfil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Experimental BIP-EXFIL shared vectors. Regenerate after BIP tag assignment.
import ngu
from ubinascii import unhexlify

if not hasattr(ngu.secp256k1, 'sign_schnorr'):
assert not hasattr(ngu.secp256k1, 'anti_exfil_commit')
assert not hasattr(ngu.secp256k1, 'anti_exfil_sign')
print('SKIP - test_anti_exfil: not compiled in (NGU_INCL_SCHNORR=0)')
else:
vectors = [
('0000000000000000000000000000000000000000000000000000000000000001', '0000000000000000000000000000000000000000000000000000000000000000', '2547d8aa53b5e89d2bbe2a4d1853cfbae1d98105b57f03e6cf555c141776780a', '0327ce12dc839961dcfdec901a3110809c0890aeb8a3527549ea7e54b5c0f93dac', '0000000000000000000000000000000000000000000000000000000000000001', '7fe10d622cf4a75e39715e7fea452228788e27e766faad12a7e4525e779d23bbb94bc96ae14de5c4be23683bf284d8399d8b92943736b443086c356016626141'),
('0000000000000000000000000000000000000000000000000000000000000003', '0101010101010101010101010101010101010101010101010101010101010101', '0317de3d5927275e3dd18a99eb6e314e1f314322c385e1804c3cc0cf17570f96', '02f8dd7048d09602edb02f6fbf743697d6aaab2927e42fcec2e44b706d9d120c42', '0000000000000000000000000000000000000000000000000000000000000002', '0a531170e638dc06579794731ea3fea09d79bac7503ebc9ecd04f13d389d3ea70d45a151c85714d42faa504fd86edae959a2253c046e9b8d01034d3610fa2540'),
('0000000000000000000000000000000000000000000000000000000000000006', '0202020202020202020202020202020202020202020202020202020202020202', 'c6a415487f22d3eb78ac30ac560f632039ba9ed41f63390364262ea15278a193', '02db71c56805915075a24af4fe0136d63e0efd3bbfd476e8b93f3c9565290c834b', 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140', '9341a1c8dc62acd9b624e62d5dd442bf7e0cd92018c797d53c67bc3adab141e2df34807e965ddd487e69dd21cb3790eae74427f7d00d53a35066de1e22581567'),
('000000000000000000000000000000000000000000000000000000000000001f', '0303030303030303030303030303030303030303030303030303030303030303', 'eaf14500f7bb699ca7f58bb00827fbf26e17f81770572ff2e87f2115296cdfad', '02cfb27a17b532f5bf0870b766846d1c1b05a9390b0b523ef09d0f73c3463f7070', '0000000000000000000000000000000000000000000000000000000000000100', '52a2955a27bd03713517e3342618c15eb36cc37d87eb63cd3f2d884c924d85cae05b5f2b63c217a551141b2f65a5acca15a8eec031b20241dc7a5b0b64e9189c'),
('00000000000000000000000000000000000000000000000000000000deadbeef', '0404040404040404040404040404040404040404040404040404040404040404', '1d601741395abb46e03cba6631f48c06a0051d8d7068df9697478eaf3ad96b04', '02108d8a97497aa273bebb4706c1d88b146808809a8b5a42533a5bca9e161bd92a', '0000000000000000000000000000000000000000000000000123456789abcdef', 'fc1ee9154aa5541b64b5816fc4c897eb99af8b756e50a9a48d82fe2f64139fbbff8de3339f4d4a5376b79bb272f87a53127b71c3ae06343cc2044eecb96ffad3'),
('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140', '0505050505050505050505050505050505050505050505050505050505050505', '38ebca2170719ecc673123b1cc48d1d91fc3ec435b7187e0e59dcf2c428cb471', '032a62d0682e9f557c9978b4e9f1696f0e3ffbb1af5640b879bb6630fc5a39ba53', 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd036413f', '49f4d4e4fa2e9c0eb3956df6389ec36337560e2a4f505cd7a3cf183d49665d0e7122e55538755d32016354b70a3aa0721bc384b3ad81fa2286d7ea13affdd1f6'),
('000000000000000000000000000000000000000000000000000000000012d687', '0606060606060606060606060606060606060606060606060606060606060606', 'ef5fdf83feea1cb5bd266718149fdac62968d991352a39ed7f2c589c8c3aeb88', '021f42904577acbd9e6e1b57f08630efe443b5bf707a9e2e7222650d5fe1129232', '0000000000000000000000000000000000000000000000000000000000000011', 'b078dd4e584adfd5ec8f04c08198a109597d8eda260e6b674df393ad9109d8902f9e0d7d868629d0349fd2ca3afd3da80d66a6af868fc9b010205b6cb6754817'),
('0000000000000000000000000000000000000000000000000000000087654321', '0707070707070707070707070707070707070707070707070707070707070707', 'a58864f76cb577c648398cd806964bd1c38de20d9773aaaa244ba990e2fb985e', '034cdab88e1b055ed8572f03c4a44c031a7ea551bfa911311d64b62f7db4cb5e04', '0000000000000000000000000000000000000000000000000000000000abcdef', '8a235e3cf29eda31b0f254c54d1e6e112eab3ec574cfacd1c36d5b25f7ee3400e985a6eaa45b0c171c8fbb2be231b26a4670247c734a4548b269a50976a4a865'),

]
for vector in vectors:
secret, msg, h, nonce, rho, expected = [unhexlify(value) for value in vector]
kp = ngu.secp256k1.keypair(secret)
assert ngu.secp256k1.anti_exfil_commit(kp, msg, h) == nonce
assert ngu.secp256k1.anti_exfil_sign(kp, msg, h, nonce, rho) == expected
assert ngu.secp256k1.verify_schnorr(expected, msg, kp.xonly_pubkey())
for index, value in ((1, bytes([msg[0] ^ 1]) + msg[1:]), (2, bytes(32)), (3, bytes(33)), (4, bytes(32)), (4, b"\xff" * 32)):
bad = [kp, msg, h, nonce, rho]
bad[index] = value
try:
ngu.secp256k1.anti_exfil_sign(*bad)
except ValueError:
pass
else:
assert False, "invalid transcript accepted"
print("anti-exfil: eight vectors and negative transcripts passed")