文件
PazeSSH/tests/dbg_curve.c
T
JGZYES 3e8a5442fa 实现 TLS 1.3 PSK 会话恢复,构建输出移至 bin/ 并新增 standalone/psftp 命令
TLS 1.3 PSK 会话恢复: NewSessionTicket 签发/解析、ticket+binder 校验、selected_identity 回选、恢复握手免证书(pazessl -sess_in/-sess_out 端到端验证); 构建脚本输出 bin/ 并支持独立命令与 psftp; 修复 SFTP 二进制传输与 copy_id -P; 补充 tests/ 调试与 verify_tls
2026-08-13 19:30:07 +08:00

72 行
2.8 KiB
C

/* dbg_curve.c —— 独立验证曲线方程 y^2 = x^3 - 3x + b (mod p)
* 目的:确认模运算正确性,判断 2G 的 y 到底哪个对 */
#include "paze/p256.h"
#include "paze/bignum.h"
#include <stdio.h>
#include <string.h>
static const uint8_t TWO_G_X[32] = {
0x7c,0xf2,0x7b,0x18,0x8d,0x03,0x4f,0x7e,0x8a,0x52,0x38,0x03,0x04,0xb5,0x1a,0xc3,
0xc0,0x89,0x69,0xe2,0x77,0xf2,0x1b,0x35,0xa6,0x0b,0x48,0xfc,0x47,0x66,0x99,0x78
};
/* 记忆中标准 2G y */
static const uint8_t TWO_G_Y_KNOWN[32] = {
0x77,0x75,0x51,0x0d,0xb8,0xed,0xcc,0xa2,0x93,0xdd,0xaa,0xc3,0xf9,0xa3,0xf5,0x8f,
0x6e,0x98,0xa2,0x65,0xf9,0x28,0xcf,0x2b,0x0f,0x43,0xf7,0xee,0x97,0xc1,0xa3,0x0a
};
/* 程序算出的 2G y */
static const uint8_t TWO_G_Y_GOT[32] = {
0x07,0x77,0x55,0x10,0xdb,0x8e,0xd0,0x40,0x29,0x3d,0x9a,0xc6,0x9f,0x74,0x30,0xdb,
0xba,0x7d,0xad,0xe6,0x3c,0xe9,0x82,0x29,0x9e,0x04,0xb7,0x9d,0x22,0x78,0x73,0xd1
};
/* 独立实现曲线方程(不经 p256_from_affine,避免同源 bug) */
static int on_curve(const uint8_t xb[32], const uint8_t yb[32]) {
paze_bn_t p, b, x, y, lhs, rhs, x3, three, t;
paze_p256_load_p(&p);
paze_bn_from_bytes(&b, (const uint8_t *)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32);
paze_bn_from_bytes(&x, xb, 32);
paze_bn_from_bytes(&y, yb, 32);
paze_bn_modmul(&lhs, &y, &y, &p);
paze_bn_modmul(&x3, &x, &x, &p);
paze_bn_modmul(&rhs, &x3, &x, &p);
paze_bn_set_u32(&three, 3);
paze_bn_modmul(&t, &three, &x, &p);
paze_bn_modsub(&rhs, &rhs, &t, &p);
paze_bn_modadd(&rhs, &rhs, &b, &p);
return paze_bn_cmp(&lhs, &rhs) == 0;
}
static void print_bn(const char *tag, const paze_bn_t *v) {
char hex[300];
paze_bn_to_hex(v, hex, sizeof(hex));
printf("%s: %s\n", tag, hex);
}
int main(void) {
printf("known 2G y on curve: %d\n", on_curve(TWO_G_X, TWO_G_Y_KNOWN));
printf("got 2G y on curve: %d\n", on_curve(TWO_G_X, TWO_G_Y_GOT));
/* 打印 x^3-3x+b 和 y^2 的具体值,便于对比 */
paze_bn_t p;
paze_p256_load_p(&p);
paze_bn_t x, y, lhs, rhs, x3, three, t, b;
paze_bn_from_bytes(&x, TWO_G_X, 32);
paze_bn_from_bytes(&y, TWO_G_Y_KNOWN, 32);
paze_bn_from_bytes(&b, (const uint8_t *)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32);
paze_bn_modmul(&lhs, &y, &y, &p);
paze_bn_modmul(&x3, &x, &x, &p);
paze_bn_modmul(&rhs, &x3, &x, &p);
paze_bn_set_u32(&three, 3);
paze_bn_modmul(&t, &three, &x, &p);
paze_bn_modsub(&rhs, &rhs, &t, &p);
paze_bn_modadd(&rhs, &rhs, &b, &p);
print_bn("known: y^2", &lhs);
print_bn("known: x^3-3x+b", &rhs);
paze_bn_from_bytes(&y, TWO_G_Y_GOT, 32);
paze_bn_modmul(&lhs, &y, &y, &p);
print_bn("got : y^2", &lhs);
return 0;
}