fe25519: take the carry out by multiplying, not by a left shift of a negative value
fe25519_carry's c << 51, with c negative after fe25519_sub, is undefined in C99; the nightly's sanitized mesh boot stopped on it (2026-11-20, run 333), reaching it through CI's signed capsules. c * 2^51 is the same value. test_fe25519 (16 checks: values that send a limb negative, and RFC 8032 tests 1 and 2 signed and verified) runs in make -C v4 test and under ASan+UBSan in sanitize; it failed first under UBSan at fe25519.c:31:26. Gates pass, POST 990/990. The acceptance protocol runs before it reaches a PR (ruled: the next PR, not #198). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
+22
-3
@@ -31,7 +31,7 @@ HERE := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
|
||||
SRCS := $(wildcard $(HERE)/src/*.c)
|
||||
# test_mesh_boot.c is the kernel's boot of the fabric, linked as the hosted product is (its own rule,
|
||||
# `mesh-boot`, below), not one of the per-width tests.
|
||||
TESTS := $(filter-out %/test_mesh_boot.c %/test_inference.c %/test_mkf18a.c,$(wildcard $(HERE)/tests/test_*.c))
|
||||
TESTS := $(filter-out %/test_mesh_boot.c %/test_inference.c %/test_mkf18a.c %/test_fe25519.c,$(wildcard $(HERE)/tests/test_*.c))
|
||||
|
||||
# Cell widths to build and verify. 32 is the F18/Zynq case, 64 the aarch64
|
||||
# host case; see DECOMPOSITION.md D-5.
|
||||
@@ -357,11 +357,30 @@ $(foreach w,$(WIDTHS),$(foreach t,$(TESTS),$(eval $(call TEST_RULE,$(w),$(t)))))
|
||||
$(foreach w,$(WIDTHS),$(foreach t,$(TESTS),$(eval $(call RUN_RULE,$(w),$(t)))))
|
||||
$(foreach w,$(WIDTHS),$(foreach t,$(TESTS),$(eval $(call SAN_RULE,$(w),$(t)))))
|
||||
|
||||
sanitize: $(foreach w,$(WIDTHS),$(foreach t,$(TESTS),san-$(w)-$(notdir $(t)))) san-mesh-boot $(foreach w,$(WIDTHS),san-mkf18a-$(w))
|
||||
sanitize: $(foreach w,$(WIDTHS),$(foreach t,$(TESTS),san-$(w)-$(notdir $(t)))) san-mesh-boot $(foreach w,$(WIDTHS),san-mkf18a-$(w)) san-fe25519
|
||||
@echo "all v4 tests passed under ASan+UBSan"
|
||||
|
||||
.PHONY: $(foreach w,$(WIDTHS),$(foreach t,$(TESTS),run-$(w)-$(notdir $(t)) san-$(w)-$(notdir $(t))))
|
||||
|
||||
# THE KERNEL'S FIELD ARITHMETIC (kernel/src/crypto/fe25519.c), the Ed25519 that verifies signed capsules:
|
||||
# tests/test_fe25519.c, plainly and under ASan+UBSan. Written 2026-11-20 for the nightly's failure that
|
||||
# day (logs/20261120-fe25519/): the sanitized mesh boot reached it only through CI's signed capsules.
|
||||
# Without -Wpedantic, as the kernel and mkcapsule build it: its __int128 is not ISO C.
|
||||
$(BINDIR)/test_fe25519: $(HERE)/tests/test_fe25519.c $(CRYPTO_SRCS) $(HERE)/Makefile
|
||||
@mkdir -p $(BINDIR)
|
||||
$(CC) $(CSTD) -Wall -Wextra -Werror $(OPT) -isystem $(ROOT)/kernel/include $(HERE)/tests/test_fe25519.c $(CRYPTO_SRCS) -o $@
|
||||
$(BINDIR)/san-test_fe25519: $(HERE)/tests/test_fe25519.c $(CRYPTO_SRCS) $(HERE)/Makefile
|
||||
@mkdir -p $(BINDIR)
|
||||
$(CC) $(CSTD) -Wall -Wextra -Werror -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=all \
|
||||
-isystem $(ROOT)/kernel/include $(HERE)/tests/test_fe25519.c $(CRYPTO_SRCS) -o $@
|
||||
.PHONY: fe25519 san-fe25519
|
||||
fe25519: $(BINDIR)/test_fe25519
|
||||
@echo " [fe25519]"
|
||||
@$(BINDIR)/test_fe25519
|
||||
san-fe25519: $(BINDIR)/san-test_fe25519
|
||||
@echo " [ASan+UBSan fe25519]"
|
||||
@$(BINDIR)/san-test_fe25519
|
||||
|
||||
# THE MESH BOOT (system/mesh.c; #189, ENGINE step 4 Phase 1, Task 5b): the kernel's boot of the
|
||||
# fabric, tested on the host with tests/test_mesh_boot.c as its owner. Linked as the hosted product
|
||||
# is, with the registry (src/manage.c) and a mesh node's ROM; 64-bit cells, as the capsules are.
|
||||
@@ -472,7 +491,7 @@ $(foreach w,$(WIDTHS),$(eval $(call F18A_RULE,$(w))))
|
||||
.PHONY: artifacts
|
||||
artifacts: $(foreach w,$(WIDTHS),$(BINDIR)/mkf18a-$(w))
|
||||
|
||||
test: $(addprefix test-,$(WIDTHS)) mesh-boot mesh-boot-on mesh-boot-noop inference artifacts $(foreach w,$(WIDTHS),mkf18a-$(w))
|
||||
test: $(addprefix test-,$(WIDTHS)) mesh-boot mesh-boot-on mesh-boot-noop inference artifacts $(foreach w,$(WIDTHS),mkf18a-$(w)) fe25519
|
||||
@echo "all v4 tests passed"
|
||||
|
||||
clean:
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
|
||||
* Copyright (c) 2023-2026 Robert A. James
|
||||
*/
|
||||
/* test_fe25519.c -- the kernel's field arithmetic mod p = 2^255-19 (kernel/src/crypto/fe25519.c), the
|
||||
* Ed25519 that verifies every signed capsule.
|
||||
*
|
||||
* Written 2026-11-20 for the nightly's failure that day (run 333, on master fdf9c194): under UBSan the
|
||||
* mesh boot verifying CI's signed capsules stopped at fe25519.c:31, "left shift of negative value -1".
|
||||
* fe25519_carry shifts a negative carry left, after fe25519_sub has made a limb negative, which is
|
||||
* undefined in C99 (logs/20261120-fe25519/). This build of the test has no key and needs none: it
|
||||
* reaches the same code by subtracting. Then the whole of what CI's boot ran on that path, an Ed25519
|
||||
* signature made and checked (RFC 8032 section 7.1, tests 1 and 2). Run plainly by `make -C v4 test` and
|
||||
* under ASan+UBSan by `make -C v4 sanitize`, so the gate sees what only CI's signed boot saw.
|
||||
*/
|
||||
#include "starkernel/fe25519.h"
|
||||
#include "starkernel/ed25519.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static int failures = 0, checks = 0;
|
||||
#define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0)
|
||||
|
||||
static void hex(uint8_t *out, const char *h)
|
||||
{
|
||||
unsigned i, v;
|
||||
for (i = 0; h[2 * i]; i++) { sscanf(h + 2 * i, "%2x", &v); out[i] = (uint8_t)v; }
|
||||
}
|
||||
|
||||
/* RFC 8032 section 7.1: the secret key (the seed), the public key, the message, the signature */
|
||||
static const char *const RFC[2][4] = {
|
||||
{ "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60",
|
||||
"d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a", "",
|
||||
"e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b" },
|
||||
{ "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb",
|
||||
"3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c", "72",
|
||||
"92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00" },
|
||||
};
|
||||
|
||||
/* p - 1 = 2^255 - 20, little-endian */
|
||||
static const uint8_t P_MINUS_1[32] = {
|
||||
0xEC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
fe25519 zero, one, two, m1, r, s;
|
||||
uint8_t b[32];
|
||||
unsigned i;
|
||||
|
||||
printf("fe25519: the field mod 2^255-19\n");
|
||||
fe25519_0(&zero);
|
||||
fe25519_1(&one);
|
||||
fe25519_add(&two, &one, &one);
|
||||
|
||||
/* a limb goes negative: the carry is -1 */
|
||||
fe25519_sub(&m1, &zero, &one);
|
||||
fe25519_pack(b, &m1);
|
||||
CHECK(memcmp(b, P_MINUS_1, 32) == 0, "0 - 1 is p - 1");
|
||||
fe25519_add(&r, &m1, &one);
|
||||
CHECK(fe25519_eq(&r, &zero), "(0 - 1) + 1 is 0");
|
||||
fe25519_neg(&r, &one);
|
||||
CHECK(fe25519_eq(&r, &m1), "-1 is 0 - 1");
|
||||
fe25519_mul(&r, &m1, &m1);
|
||||
CHECK(fe25519_eq(&r, &one), "(p - 1)^2 is 1");
|
||||
fe25519_sub(&r, &one, &two);
|
||||
CHECK(fe25519_eq(&r, &m1), "1 - 2 is -1");
|
||||
|
||||
/* every limb negative at once, then back */
|
||||
fe25519_unpack(&s, P_MINUS_1);
|
||||
fe25519_sub(&r, &zero, &s);
|
||||
CHECK(fe25519_eq(&r, &one), "0 - (p - 1) is 1");
|
||||
for (i = 0; i < 64; i++) fe25519_sub(&r, &r, &two);
|
||||
fe25519_add(&r, &r, &two);
|
||||
for (i = 0; i < 63; i++) fe25519_add(&r, &r, &two);
|
||||
CHECK(fe25519_eq(&r, &one), "1 less 2, 64 times, then 2 added back 64 times: 1");
|
||||
fe25519_invert(&r, &m1);
|
||||
CHECK(fe25519_eq(&r, &m1), "the inverse of -1 is -1");
|
||||
|
||||
/* Ed25519, made and checked: the path a signed capsule's check takes */
|
||||
for (i = 0; i < 2; i++) {
|
||||
uint8_t seed[32], pub[32], want_pub[32], msg[1], sig[64], want_sig[64];
|
||||
size_t mlen = strlen(RFC[i][2]) / 2u;
|
||||
hex(seed, RFC[i][0]); hex(want_pub, RFC[i][1]); hex(msg, RFC[i][2]); hex(want_sig, RFC[i][3]);
|
||||
ed25519_keygen(seed, pub);
|
||||
CHECK(memcmp(pub, want_pub, 32) == 0, "RFC 8032 test %u: the public key", i + 1);
|
||||
ed25519_sign(seed, msg, mlen, sig);
|
||||
CHECK(memcmp(sig, want_sig, 64) == 0, "RFC 8032 test %u: the signature", i + 1);
|
||||
CHECK(ed25519_verify(want_pub, msg, mlen, want_sig) == 1, "RFC 8032 test %u: it verifies", i + 1);
|
||||
want_sig[0] ^= 1;
|
||||
CHECK(ed25519_verify(want_pub, msg, mlen, want_sig) == 0, "RFC 8032 test %u: one bit changed, it does not", i + 1);
|
||||
}
|
||||
|
||||
printf(" %d checks, %d failures\n", checks, failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user