Skip to content

Commit 1123e35

Browse files
committed
Add "hazmat" module which exposes low-level primitives (scalar, point)
1 parent 8deef00 commit 1123e35

8 files changed

+230
-0
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ option(SECP256K1_ENABLE_MODULE_EXTRAKEYS "Enable extrakeys module." ON)
6262
option(SECP256K1_ENABLE_MODULE_SCHNORRSIG "Enable schnorrsig module." ON)
6363
option(SECP256K1_ENABLE_MODULE_MUSIG "Enable musig module." ON)
6464
option(SECP256K1_ENABLE_MODULE_ELLSWIFT "Enable ElligatorSwift module." ON)
65+
option(SECP256K1_ENABLE_MODULE_HAZMAT "Enable hazmat module." OFF)
6566

6667
# Processing must be done in a topological sorting of the dependency graph
6768
# (dependent module first).
69+
if(SECP256K1_ENABLE_MODULE_HAZMAT)
70+
add_compile_definitions(ENABLE_MODULE_HAZMAT=1)
71+
endif()
72+
6873
if(SECP256K1_ENABLE_MODULE_ELLSWIFT)
6974
add_compile_definitions(ENABLE_MODULE_ELLSWIFT=1)
7075
endif()
@@ -327,6 +332,7 @@ message(" extrakeys ........................... ${SECP256K1_ENABLE_MODULE_EXTRA
327332
message(" schnorrsig .......................... ${SECP256K1_ENABLE_MODULE_SCHNORRSIG}")
328333
message(" musig ............................... ${SECP256K1_ENABLE_MODULE_MUSIG}")
329334
message(" ElligatorSwift ...................... ${SECP256K1_ENABLE_MODULE_ELLSWIFT}")
335+
message(" hazmat .............................. ${SECP256K1_ENABLE_MODULE_HAZMAT}")
330336
message("Parameters:")
331337
message(" ecmult window size .................. ${SECP256K1_ECMULT_WINDOW_SIZE}")
332338
message(" ecmult gen table size ............... ${SECP256K1_ECMULT_GEN_KB} KiB")

Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,7 @@ endif
300300
if ENABLE_MODULE_ELLSWIFT
301301
include src/modules/ellswift/Makefile.am.include
302302
endif
303+
304+
if ENABLE_MODULE_HAZMAT
305+
include src/modules/hazmat/Makefile.am.include
306+
endif

configure.ac

+10
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ AC_ARG_ENABLE(module_ellswift,
192192
AS_HELP_STRING([--enable-module-ellswift],[enable ElligatorSwift module [default=yes]]), [],
193193
[SECP_SET_DEFAULT([enable_module_ellswift], [yes], [yes])])
194194

195+
AC_ARG_ENABLE(module_hazmat,
196+
AS_HELP_STRING([--enable-module-hazmat],[enable hazmat module [default=no]]), [],
197+
[SECP_SET_DEFAULT([enable_module_hazmat], [no], [yes])])
198+
195199
AC_ARG_ENABLE(external_default_callbacks,
196200
AS_HELP_STRING([--enable-external-default-callbacks],[enable external default callback functions [default=no]]), [],
197201
[SECP_SET_DEFAULT([enable_external_default_callbacks], [no], [no])])
@@ -430,6 +434,10 @@ if test x"$enable_module_ecdh" = x"yes"; then
430434
SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DENABLE_MODULE_ECDH=1"
431435
fi
432436

437+
if test x"$enable_module_hazmat" = x"yes"; then
438+
SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DENABLE_MODULE_HAZMAT=1"
439+
fi
440+
433441
if test x"$enable_external_default_callbacks" = x"yes"; then
434442
SECP_CONFIG_DEFINES="$SECP_CONFIG_DEFINES -DUSE_EXTERNAL_DEFAULT_CALLBACKS=1"
435443
fi
@@ -463,6 +471,7 @@ AM_CONDITIONAL([ENABLE_MODULE_EXTRAKEYS], [test x"$enable_module_extrakeys" = x"
463471
AM_CONDITIONAL([ENABLE_MODULE_SCHNORRSIG], [test x"$enable_module_schnorrsig" = x"yes"])
464472
AM_CONDITIONAL([ENABLE_MODULE_MUSIG], [test x"$enable_module_musig" = x"yes"])
465473
AM_CONDITIONAL([ENABLE_MODULE_ELLSWIFT], [test x"$enable_module_ellswift" = x"yes"])
474+
AM_CONDITIONAL([ENABLE_MODULE_HAZMAT], [test x"$enable_module_hazmat" = x"yes"])
466475
AM_CONDITIONAL([USE_EXTERNAL_ASM], [test x"$enable_external_asm" = x"yes"])
467476
AM_CONDITIONAL([USE_ASM_ARM], [test x"$set_asm" = x"arm32"])
468477
AM_CONDITIONAL([BUILD_WINDOWS], [test "$build_windows" = "yes"])
@@ -486,6 +495,7 @@ echo " module extrakeys = $enable_module_extrakeys"
486495
echo " module schnorrsig = $enable_module_schnorrsig"
487496
echo " module musig = $enable_module_musig"
488497
echo " module ellswift = $enable_module_ellswift"
498+
echo " module hazmat = $enable_module_hazmat"
489499
echo
490500
echo " asm = $set_asm"
491501
echo " ecmult window size = $set_ecmult_window"

include/secp256k1_hazmat.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef SECP256K1_HAZMAT_H
2+
#define SECP256K1_HAZMAT_H
3+
4+
#include "secp256k1.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#include <stdint.h>
11+
12+
/* This module provides low-level cryptographic primitives of secp256k1.
13+
* Note that these can be used incorrectly and require an in-depth knowledge
14+
* of the cryptographic concepts at work, therefore we call this the
15+
* "hazardous materials" library or "hazmat" for short.
16+
*/
17+
18+
/* Scalar */
19+
typedef union {
20+
unsigned char data[32];
21+
uint64_t align8; /* ensure alignment on 8-bytes boundaries */
22+
} secp256k1_hazmat_scalar;
23+
24+
SECP256K1_API int secp256k1_hazmat_scalar_parse(secp256k1_hazmat_scalar *s, const unsigned char *bin32);
25+
SECP256K1_API void secp256k1_hazmat_scalar_serialize(unsigned char *bin32, const secp256k1_hazmat_scalar *s);
26+
SECP256K1_API void secp256k1_hazmat_scalar_set_zero(secp256k1_hazmat_scalar *s);
27+
SECP256K1_API int secp256k1_hazmat_scalar_is_zero(const secp256k1_hazmat_scalar *s);
28+
SECP256K1_API void secp256k1_hazmat_scalar_add(secp256k1_hazmat_scalar *sres, const secp256k1_hazmat_scalar *s1, const secp256k1_hazmat_scalar *s2);
29+
SECP256K1_API void secp256k1_hazmat_scalar_mul(secp256k1_hazmat_scalar *sres, const secp256k1_hazmat_scalar *s1, const secp256k1_hazmat_scalar *s2);
30+
SECP256K1_API void secp256k1_hazmat_scalar_negate(secp256k1_hazmat_scalar *s);
31+
32+
/* Point */
33+
typedef union {
34+
unsigned char data[160];
35+
uint64_t align8; /* ensure alignment on 8-bytes boundaries */
36+
} secp256k1_hazmat_point;
37+
38+
SECP256K1_API int secp256k1_hazmat_point_parse(secp256k1_hazmat_point *p, const unsigned char *pubkey33);
39+
SECP256K1_API void secp256k1_hazmat_point_serialize(unsigned char *pubkey33, secp256k1_hazmat_point *p);
40+
SECP256K1_API void secp256k1_hazmat_point_set_infinity(secp256k1_hazmat_point *p);
41+
SECP256K1_API int secp256k1_hazmat_point_is_infinity(const secp256k1_hazmat_point *p);
42+
SECP256K1_API void secp256k1_hazmat_point_add(secp256k1_hazmat_point *pres, secp256k1_hazmat_point *p1, secp256k1_hazmat_point *p2);
43+
SECP256K1_API void secp256k1_hazmat_point_negate(secp256k1_hazmat_point *p);
44+
SECP256K1_API int secp256k1_hazmat_point_equal(const secp256k1_hazmat_point *p1, const secp256k1_hazmat_point *p2);
45+
46+
/* Point multiplication */
47+
SECP256K1_API void secp256k1_hazmat_multiply_with_generator(const secp256k1_context *ctx, secp256k1_hazmat_point *pres, const secp256k1_hazmat_scalar *s);
48+
SECP256K1_API void secp256k1_hazmat_multiply_with_point(secp256k1_hazmat_point *pres, const secp256k1_hazmat_scalar *s, secp256k1_hazmat_point *p);
49+
50+
#ifdef __cplusplus
51+
}
52+
#endif
53+
54+
#endif /* SECP256K1_HAZMAT_H */

src/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ if(SECP256K1_INSTALL)
138138
if(SECP256K1_ENABLE_MODULE_ELLSWIFT)
139139
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_ellswift.h")
140140
endif()
141+
if(SECP256K1_ENABLE_MODULE_HAZMAT)
142+
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_hazmat.h")
143+
endif()
141144
install(FILES ${${PROJECT_NAME}_headers}
142145
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
143146
)
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include_HEADERS += include/secp256k1_hazmat.h
2+
noinst_HEADERS += src/modules/hazmat/main_impl.h

src/modules/hazmat/main_impl.h

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/***********************************************************************
2+
* Distributed under the MIT software license, see the accompanying *
3+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4+
***********************************************************************/
5+
6+
#ifndef SECP256K1_MODULE_HAZMAT_MAIN_H
7+
#define SECP256K1_MODULE_HAZMAT_MAIN_H
8+
9+
#include "../../../include/secp256k1.h"
10+
#include "../../../include/secp256k1_hazmat.h"
11+
#include "../../scalar.h"
12+
#include "../../group.h"
13+
#include "../../eckey.h"
14+
#include "../../ecmult_const.h"
15+
16+
typedef struct {
17+
secp256k1_gej gej;
18+
int z_is_one; /* set if z == 1, i.e. gej can be converted to ge trivially by assigning x/y */
19+
} secp256k1_hazmat_point_struct;
20+
21+
/* Verify that the opaque data types are large enough to hold the underlying structures
22+
(note that this function is never called at run-time and only exists since the STATIC_ASSERT
23+
macro can only be used inside of functions) */
24+
static void secp256k1_hazmat_assertions(void) {
25+
STATIC_ASSERT(sizeof(secp256k1_hazmat_scalar) >= sizeof(secp256k1_scalar));
26+
STATIC_ASSERT(sizeof(secp256k1_hazmat_point) >= sizeof(secp256k1_hazmat_point_struct));
27+
}
28+
29+
int secp256k1_hazmat_scalar_parse(secp256k1_hazmat_scalar *s, const unsigned char *bin32) {
30+
int overflow;
31+
secp256k1_scalar_set_b32((secp256k1_scalar*)s, bin32, &overflow);
32+
return !overflow;
33+
}
34+
35+
void secp256k1_hazmat_scalar_serialize(unsigned char *bin32, const secp256k1_hazmat_scalar *s) {
36+
secp256k1_scalar_get_b32(bin32, (secp256k1_scalar*)s);
37+
}
38+
39+
void secp256k1_hazmat_scalar_set_zero(secp256k1_hazmat_scalar *s) {
40+
*((secp256k1_scalar*)s) = secp256k1_scalar_zero;
41+
}
42+
43+
int secp256k1_hazmat_scalar_is_zero(const secp256k1_hazmat_scalar *s) {
44+
return secp256k1_scalar_is_zero((secp256k1_scalar*)s);
45+
}
46+
47+
void secp256k1_hazmat_scalar_add(secp256k1_hazmat_scalar *sres, const secp256k1_hazmat_scalar *s1, const secp256k1_hazmat_scalar *s2) {
48+
secp256k1_scalar_add((secp256k1_scalar*)sres, (secp256k1_scalar*)s1, (secp256k1_scalar*)s2);
49+
}
50+
51+
void secp256k1_hazmat_scalar_mul(secp256k1_hazmat_scalar *sres, const secp256k1_hazmat_scalar *s1, const secp256k1_hazmat_scalar *s2) {
52+
secp256k1_scalar_mul((secp256k1_scalar*)sres, (secp256k1_scalar*)s1, (secp256k1_scalar*)s2);
53+
}
54+
55+
void secp256k1_hazmat_scalar_negate(secp256k1_hazmat_scalar *s) {
56+
secp256k1_scalar_negate((secp256k1_scalar*)s, (secp256k1_scalar*)s);
57+
}
58+
59+
static void secp256k1_hazmat_point_to_ge(secp256k1_ge *ge, secp256k1_hazmat_point_struct *p) {
60+
if (p->z_is_one) {
61+
secp256k1_ge_set_xy(ge, &p->gej.x, &p->gej.y);
62+
} else {
63+
secp256k1_ge_set_gej(ge, &p->gej);
64+
p->z_is_one = 1;
65+
}
66+
}
67+
68+
int secp256k1_hazmat_point_parse(secp256k1_hazmat_point *p, const unsigned char *pubkey33) {
69+
secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p;
70+
secp256k1_ge ge;
71+
72+
if (!secp256k1_eckey_pubkey_parse(&ge, pubkey33, 33)) {
73+
return 0;
74+
}
75+
secp256k1_gej_set_ge(&ps->gej, &ge);
76+
ps->z_is_one = 1;
77+
return 1;
78+
}
79+
80+
void secp256k1_hazmat_point_serialize(unsigned char *pubkey33, secp256k1_hazmat_point *p) {
81+
secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p;
82+
secp256k1_ge ge;
83+
size_t size;
84+
int ret;
85+
86+
secp256k1_hazmat_point_to_ge(&ge, ps);
87+
ret = secp256k1_eckey_pubkey_serialize(&ge, pubkey33, &size, 1);
88+
VERIFY_CHECK(ret == 1 && size == 33);
89+
(void)ret;
90+
}
91+
92+
void secp256k1_hazmat_point_set_infinity(secp256k1_hazmat_point *p) {
93+
secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p;
94+
95+
secp256k1_gej_set_infinity(&ps->gej);
96+
ps->z_is_one = 0;
97+
}
98+
99+
int secp256k1_hazmat_point_is_infinity(const secp256k1_hazmat_point *p) {
100+
const secp256k1_hazmat_point_struct *ps = (const secp256k1_hazmat_point_struct*)p;
101+
102+
return secp256k1_gej_is_infinity(&ps->gej);
103+
}
104+
105+
void secp256k1_hazmat_point_add(secp256k1_hazmat_point *pres, secp256k1_hazmat_point *p1, secp256k1_hazmat_point *p2) {
106+
secp256k1_hazmat_point_struct *press = (secp256k1_hazmat_point_struct*)pres;
107+
secp256k1_hazmat_point_struct *p1s = (secp256k1_hazmat_point_struct*)p1;
108+
secp256k1_hazmat_point_struct *p2s = (secp256k1_hazmat_point_struct*)p2;
109+
secp256k1_ge ge;
110+
111+
secp256k1_hazmat_point_to_ge(&ge, p2s);
112+
secp256k1_gej_add_ge(&press->gej, &p1s->gej, &ge);
113+
press->z_is_one = 0;
114+
}
115+
116+
void secp256k1_hazmat_point_negate(secp256k1_hazmat_point *p) {
117+
secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p;
118+
119+
secp256k1_gej_neg(&ps->gej, &ps->gej);
120+
/* negation only changes y; z is untouched, so no update of z_is_one is needed */
121+
}
122+
123+
int secp256k1_hazmat_point_equal(const secp256k1_hazmat_point *p1, const secp256k1_hazmat_point *p2) {
124+
const secp256k1_hazmat_point_struct *p1s = (secp256k1_hazmat_point_struct*)p1;
125+
const secp256k1_hazmat_point_struct *p2s = (secp256k1_hazmat_point_struct*)p2;
126+
127+
return secp256k1_gej_eq_var(&p1s->gej, &p2s->gej);
128+
}
129+
130+
void secp256k1_hazmat_multiply_with_generator(const secp256k1_context *ctx, secp256k1_hazmat_point *p, const secp256k1_hazmat_scalar *s) {
131+
secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p;
132+
133+
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &ps->gej, (secp256k1_scalar*)s);
134+
ps->z_is_one = 0;
135+
}
136+
137+
void secp256k1_hazmat_multiply_with_point(secp256k1_hazmat_point *pres, const secp256k1_hazmat_scalar *s, secp256k1_hazmat_point *p) {
138+
secp256k1_hazmat_point_struct *press = (secp256k1_hazmat_point_struct*)pres;
139+
secp256k1_hazmat_point_struct *ps = (secp256k1_hazmat_point_struct*)p;
140+
secp256k1_ge ge;
141+
142+
secp256k1_hazmat_point_to_ge(&ge, ps);
143+
secp256k1_ecmult_const(&press->gej, &ge, (secp256k1_scalar*)s);
144+
press->z_is_one = 0;
145+
}
146+
147+
#endif

src/secp256k1.c

+4
Original file line numberDiff line numberDiff line change
@@ -829,3 +829,7 @@ int secp256k1_tagged_sha256(const secp256k1_context* ctx, unsigned char *hash32,
829829
#ifdef ENABLE_MODULE_ELLSWIFT
830830
# include "modules/ellswift/main_impl.h"
831831
#endif
832+
833+
#ifdef ENABLE_MODULE_HAZMAT
834+
# include "modules/hazmat/main_impl.h"
835+
#endif

0 commit comments

Comments
 (0)