From 0cf3a2dec2bd1f6e8caf35f9b273f7508bb37164 Mon Sep 17 00:00:00 2001 From: ziffee Date: Thu, 12 Dec 2024 06:44:47 +0700 Subject: [PATCH 1/2] gw/secp: add priv-to-pub jets --- ext/urcrypt/build.zig.zon | 5 +++-- pkg/noun/jets.c | 26 ++++++++++++++++---------- pkg/noun/jets/137/tree.c | 2 ++ pkg/noun/jets/e/secp.c | 32 ++++++++++++++++++++++++++++++++ pkg/noun/jets/w.h | 2 ++ 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/ext/urcrypt/build.zig.zon b/ext/urcrypt/build.zig.zon index 9de2a9ad72..b9fece161b 100644 --- a/ext/urcrypt/build.zig.zon +++ b/ext/urcrypt/build.zig.zon @@ -14,8 +14,9 @@ .hash = "1220bb683a6df744e618f58a008eaae3eb62b70a78334cec676bd82b1b9e8e944eeb", }, .urcrypt = .{ - .url = "https://github.com/urbit/urcrypt/archive/e77a0998809f7552c455cf7bf8c1b68c141cce18.tar.gz", - .hash = "12202efcf40dcfed9a9b59bba4743cfd0c31364a6d7504724bd4d643b474ff69245b", + .path = "../../../urcrypt", + //.url = "https://github.com/urbit/urcrypt/archive/e77a0998809f7552c455cf7bf8c1b68c141cce18.tar.gz", + //.hash = "12202efcf40dcfed9a9b59bba4743cfd0c31364a6d7504724bd4d643b474ff69245b", }, }, .paths = .{ diff --git a/pkg/noun/jets.c b/pkg/noun/jets.c index 200f7d46ef..eba80f5c5f 100644 --- a/pkg/noun/jets.c +++ b/pkg/noun/jets.c @@ -882,9 +882,9 @@ _cj_kick_z(u3_noun cor, u3j_core* cop_u, u3j_harm* ham_u, u3_atom axe) else { u3_weak pro, ame; - ham_u->ice = c3y; + //ham_u->ice = c3y; pro = ham_u->fun_f(cor); - ham_u->ice = c3n; + //ham_u->ice = c3n; #ifdef U3_MEMORY_DEBUG u3a_lop(cod_w); @@ -893,23 +893,29 @@ _cj_kick_z(u3_noun cor, u3j_core* cop_u, u3j_harm* ham_u, u3_atom axe) u3z(cor); return pro; } - ham_u->liv = c3n; + //ham_u->liv = c3n; ame = _cj_soft(cor, axe); - ham_u->liv = c3y; + //ham_u->liv = c3y; if ( c3n == u3r_sing(ame, pro) ) { - u3l_log("test: %s %s: mismatch: good %x, bad %x", + //u3l_log("test: %s %s: mismatch: good %x, bad %x", + // cop_u->cos_c, + // (!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c, + // u3r_mug(ame), + // u3r_mug(pro)); + //ham_u->liv = c3n; + u3l_log("test: %s %s: mismatch", cop_u->cos_c, - (!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c, - u3r_mug(ame), - u3r_mug(pro)); - ham_u->liv = c3n; + (!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c + ); + u3m_p("good", ame); + u3m_p("baad", pro); return u3m_bail(c3__fail); } else { -#if 0 +#if 1 u3l_log("test: %s %s", cop_u->cos_c, (!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c); diff --git a/pkg/noun/jets/137/tree.c b/pkg/noun/jets/137/tree.c index fec03edf1f..60a25a6743 100644 --- a/pkg/noun/jets/137/tree.c +++ b/pkg/noun/jets/137/tree.c @@ -183,6 +183,7 @@ static u3j_core _137_hex_scr_d[] = static u3j_harm _137_hex_secp_secp256k1_make_a[] = {{".2", u3we_make, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_sign_a[] = {{".2", u3we_sign, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_reco_a[] = {{".2", u3we_reco, c3y}, {}}; +static u3j_harm _137_hex_secp_secp256k1_priv_to_pub_a[] = {{".2", u3we_priv_to_pub, c3n}, {}}; static u3j_harm _137_hex_secp_secp256k1_schnorr_sosi_a[] = {{".2", u3we_sosi}, {}}; @@ -202,6 +203,7 @@ static u3j_core _137_hex_secp_secp256k1_d[] = { { "make", 7, _137_hex_secp_secp256k1_make_a, 0, no_hashes }, { "sign", 7, _137_hex_secp_secp256k1_sign_a, 0, no_hashes }, { "reco", 7, _137_hex_secp_secp256k1_reco_a, 0, no_hashes }, + { "priv-to-pub", 7, _137_hex_secp_secp256k1_priv_to_pub_a, 0, no_hashes }, { "schnorr", 7, 0, _137_hex_secp_secp256k1_schnorr_d, no_hashes }, diff --git a/pkg/noun/jets/e/secp.c b/pkg/noun/jets/e/secp.c index fa0fbd20c4..995935e570 100644 --- a/pkg/noun/jets/e/secp.c +++ b/pkg/noun/jets/e/secp.c @@ -296,3 +296,35 @@ u3we_sove(u3_noun cor) return _cqes_sove(pub, mes, sig); } } + +static u3_atom +_cqes_priv_to_pub(u3_atom sek) { + c3_y scalar[32], point[65]; + + if (0 != u3r_bytes_fit(32, scalar, sek)) { + return u3m_bail(c3__exit); + } + + if (0 != urcrypt_secp_point_from_scalar(sec_u, scalar, point)) { + return u3m_bail(c3__exit); + } + + return u3nc( + u3i_bytes(32, point + 1), + u3i_bytes(32, point + 33) + ); +} + +u3_noun +u3we_priv_to_pub(u3_noun cor) +{ + + u3_noun a = u3r_at(u3x_sam, cor); + + if ( (u3_none == a) || (c3n == u3ud(a)) ) { + return u3m_bail(c3__exit); + } + else { + return _cqes_priv_to_pub(a); + } +} diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 78494314fe..ef4c4f8b54 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -231,6 +231,8 @@ u3_noun u3we_sosi(u3_noun); u3_noun u3we_sove(u3_noun); + u3_noun u3we_priv_to_pub(u3_noun); + u3_noun u3we_en_base16(u3_noun); u3_noun u3we_de_base16(u3_noun); From ad533b297c64a94a75703782b060bab8068c09c7 Mon Sep 17 00:00:00 2001 From: cyclomancer Date: Tue, 14 Jan 2025 09:38:03 -0600 Subject: [PATCH 2/2] Add point tweak, compressed points --- pkg/noun/jets/137/tree.c | 4 ++ pkg/noun/jets/e/secp.c | 83 +++++++++++++++++++++++++++++++++++++--- pkg/noun/jets/w.h | 2 + 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/pkg/noun/jets/137/tree.c b/pkg/noun/jets/137/tree.c index 60a25a6743..bcdaa37b76 100644 --- a/pkg/noun/jets/137/tree.c +++ b/pkg/noun/jets/137/tree.c @@ -184,6 +184,8 @@ static u3j_harm _137_hex_secp_secp256k1_make_a[] = {{".2", u3we_make, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_sign_a[] = {{".2", u3we_sign, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_reco_a[] = {{".2", u3we_reco, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_priv_to_pub_a[] = {{".2", u3we_priv_to_pub, c3n}, {}}; +static u3j_harm _137_hex_secp_secp256k1_tweak_sec_a[] = {{".2", u3we_tweak_sec, c3n}, {}}; +static u3j_harm _137_hex_secp_secp256k1_tweak_pub_a[] = {{".2", u3we_tweak_pub, c3n}, {}}; static u3j_harm _137_hex_secp_secp256k1_schnorr_sosi_a[] = {{".2", u3we_sosi}, {}}; @@ -204,6 +206,8 @@ static u3j_core _137_hex_secp_secp256k1_d[] = { "sign", 7, _137_hex_secp_secp256k1_sign_a, 0, no_hashes }, { "reco", 7, _137_hex_secp_secp256k1_reco_a, 0, no_hashes }, { "priv-to-pub", 7, _137_hex_secp_secp256k1_priv_to_pub_a, 0, no_hashes }, + { "tweak-sec", 7, _137_hex_secp_secp256k1_tweak_sec_a, 0, no_hashes }, + { "tweak-pub", 7, _137_hex_secp_secp256k1_tweak_pub_a, 0, no_hashes }, { "schnorr", 7, 0, _137_hex_secp_secp256k1_schnorr_d, no_hashes }, diff --git a/pkg/noun/jets/e/secp.c b/pkg/noun/jets/e/secp.c index 995935e570..7a3ca706a5 100644 --- a/pkg/noun/jets/e/secp.c +++ b/pkg/noun/jets/e/secp.c @@ -298,21 +298,92 @@ u3we_sove(u3_noun cor) } static u3_atom +_cqes_tweak_sec(u3_atom sek, u3_atom tek) { + c3_y scalar[32], tweak[32]; + + if (0 != u3r_bytes_fit(32, scalar, sek)) { + return u3m_bail(c3__exit); + } + + if (0 != u3r_bytes_fit(32, tweak, tek)) { + return u3m_bail(c3__exit); + } + + if (0 != urcrypt_secp_scalar_tweak_add(sec_u, scalar, tweak)) { + return u3m_bail(c3__exit); + } + + return u3i_bytes(32, scalar); +} + +u3_noun +u3we_tweak_sec(u3_noun cor) +{ + u3_noun sek, tek; + + if ( (c3n == u3r_mean(cor, + u3x_sam_2, &sek, + u3x_sam_3, &tek, + 0)) || + (c3n == u3ud(sek)) || + (c3n == u3ud(tek))) { + return u3m_bail(c3__exit); + } + else { + return _cqes_tweak_sec(sek, tek); + } +} + +static u3_atom +_cqes_tweak_pub(u3_atom pub, u3_atom tek) { + c3_y cmp_point[33], tweak[32]; + + if (0 != u3r_bytes_fit(33, cmp_point, pub)) { + return u3m_bail(c3__exit); + } + + if (0 != u3r_bytes_fit(32, tweak, tek)) { + return u3m_bail(c3__exit); + } + + if (0 != urcrypt_secp_cmp_point_tweak_add(sec_u, cmp_point, tweak)) { + return u3m_bail(c3__exit); + } + + return u3i_bytes(33, cmp_point); +} + +u3_noun +u3we_tweak_pub(u3_noun cor) +{ + u3_noun pub, tek; + + if ( (c3n == u3r_mean(cor, + u3x_sam_2, &pub, + u3x_sam_3, &tek, + 0)) || + (c3n == u3ud(pub)) || + (c3n == u3ud(tek))) { + return u3m_bail(c3__exit); + } + else { + return _cqes_tweak_pub(pub, tek); + } +} + +static u3_noun _cqes_priv_to_pub(u3_atom sek) { - c3_y scalar[32], point[65]; + c3_y scalar[32], cmp_point[33]; if (0 != u3r_bytes_fit(32, scalar, sek)) { return u3m_bail(c3__exit); } - if (0 != urcrypt_secp_point_from_scalar(sec_u, scalar, point)) { + if (0 != urcrypt_secp_cmp_point_from_scalar(sec_u, scalar, cmp_point)) { return u3m_bail(c3__exit); } - return u3nc( - u3i_bytes(32, point + 1), - u3i_bytes(32, point + 33) - ); + return u3i_bytes(33, cmp_point); } u3_noun diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index ef4c4f8b54..6c2caf4f72 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -232,6 +232,8 @@ u3_noun u3we_sove(u3_noun); u3_noun u3we_priv_to_pub(u3_noun); + u3_noun u3we_tweak_sec(u3_noun); + u3_noun u3we_tweak_pub(u3_noun); u3_noun u3we_en_base16(u3_noun); u3_noun u3we_de_base16(u3_noun);