Skip to content

Commit 6a37f53

Browse files
committed
EC: add SECP256K1 support (tests are failing, needs further investigation)
1 parent 8d63048 commit 6a37f53

17 files changed

+27736
-4
lines changed

Diff for: ec-freestanding/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ FREESTANDING_CFLAGS := $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --c
1414
DISCOVER_CFLAGS := $(shell sed 's/^(\(.*\))$$/\1/' ../ec/cflags_optimized.sexp | tr -d '"')
1515
CFLAGS := -O3 -I../ec/native -I../src/native $(DISCOVER_CFLAGS) $(FREESTANDING_CFLAGS)
1616

17-
OBJS=p224_stubs.o np224_stubs.o p256_stubs.o np256_stubs.o p384_stubs.o np384_stubs.o
17+
OBJS=p224_stubs.o np224_stubs.o p256_stubs.o np256_stubs.o p384_stubs.o np384_stubs.o secp256k1_stubs.o nsecp256k1_stubs.o
1818

1919
libmirage_crypto_ec_freestanding_stubs.a: $(OBJS)
2020
$(AR) r $@ $^

Diff for: ec-freestanding/dune

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
(rule
44
(deps ../ec/cflags_optimized.sexp ../src/native/mirage_crypto.h
55
../src/native/bitfn.h Makefile p224_stubs.c np224_stubs.c p256_stubs.c
6-
np256_stubs.c p384_stubs.c np384_stubs.c)
6+
np256_stubs.c p384_stubs.c np384_stubs.c secp256k1_stubs.c
7+
nsecp256k1_stubs.c)
78
(targets libmirage_crypto_ec_freestanding_stubs.a)
89
(action
910
(no-infer

Diff for: ec/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(libraries cstruct eqaf.cstruct hex mirage-crypto mirage-crypto-rng)
55
(foreign_stubs
66
(language c)
7-
(names p224_stubs np224_stubs p256_stubs np256_stubs p384_stubs np384_stubs)
7+
(names p224_stubs np224_stubs p256_stubs np256_stubs p384_stubs np384_stubs secp256k1_stubs nsecp256k1_stubs)
88
(flags
99
(:standard -I../src/native)
1010
(:include cflags_optimized.sexp))))

Diff for: ec/mirage_crypto_ec.ml

+46
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,52 @@ module P256 : S = struct
666666
module Dsa = Make_dsa(Params)(Foreign_n)(P)(S)(Mirage_crypto.Hash.SHA256)
667667
end
668668

669+
module SECP256K1 : S = struct
670+
module Params = struct
671+
let a = `Hex "0000000000000000000000000000000000000000000000000000000000000000"
672+
let b = `Hex "0000000000000000000000000000000000000000000000000000000000000007"
673+
let g_x =
674+
`Hex "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"
675+
let g_y =
676+
`Hex "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"
677+
let p = `Hex "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"
678+
let n = `Hex "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"
679+
let byte_length = 32
680+
end
681+
682+
module Foreign = struct
683+
external mul : field_element -> field_element -> field_element -> unit = "mc_secp256k1_mul" [@@noalloc]
684+
external sub : field_element -> field_element -> field_element -> unit = "mc_secp256k1_sub" [@@noalloc]
685+
external add : field_element -> field_element -> field_element -> unit = "mc_secp256k1_add" [@@noalloc]
686+
external to_montgomery : field_element -> unit = "mc_secp256k1_to_montgomery" [@@noalloc]
687+
external from_bytes_buf : field_element -> Cstruct.buffer -> unit = "mc_secp256k1_from_bytes" [@@noalloc]
688+
external set_one : field_element -> unit = "mc_secp256k1_set_one" [@@noalloc]
689+
external nz : field_element -> bool = "mc_secp256k1_nz" [@@noalloc]
690+
external sqr : field_element -> field_element -> unit = "mc_secp256k1_sqr" [@@noalloc]
691+
external from_montgomery : field_element -> unit = "mc_secp256k1_from_montgomery" [@@noalloc]
692+
external to_bytes_buf : Cstruct.buffer -> field_element -> unit = "mc_secp256k1_to_bytes" [@@noalloc]
693+
external inv : field_element -> field_element -> unit = "mc_secp256k1_inv" [@@noalloc]
694+
external select_c : field_element -> bool -> field_element -> field_element -> unit = "mc_secp256k1_select" [@@noalloc]
695+
696+
external double_c : point -> point -> unit = "mc_secp256k1_point_double" [@@noalloc]
697+
external add_c : point -> point -> point -> unit = "mc_secp256k1_point_add" [@@noalloc]
698+
end
699+
700+
module Foreign_n = struct
701+
external mul : field_element -> field_element -> field_element -> unit = "mc_nsecp256k1_mul" [@@noalloc]
702+
external add : field_element -> field_element -> field_element -> unit = "mc_nsecp256k1_add" [@@noalloc]
703+
external inv : field_element -> field_element -> unit = "mc_nsecp256k1_inv" [@@noalloc]
704+
external one : field_element -> unit = "mc_nsecp256k1_one" [@@noalloc]
705+
external from_montgomery : field_element -> field_element -> unit = "mc_nsecp256k1_from_montgomery" [@@noalloc]
706+
external to_montgomery : field_element -> field_element -> unit = "mc_nsecp256k1_to_montgomery" [@@noalloc]
707+
end
708+
709+
module P = Make_point(Params)(Foreign)
710+
module S = Make_scalar(Params)(P)
711+
module Dh = Make_dh(Params)(P)(S)
712+
module Dsa = Make_dsa(Params)(Foreign_n)(P)(S)(Mirage_crypto.Hash.SHA256)
713+
end
714+
669715
module P384 : S = struct
670716
module Params = struct
671717
let a = `Hex "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC"

Diff for: ec/mirage_crypto_ec.mli

+1
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ end
8484

8585
module P224 : S
8686
module P256 : S
87+
module SECP256K1 : S
8788
module P384 : S

Diff for: ec/native/GNUmakefile

+52
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ np256_32.h:
5858
.PHONY: p256
5959
p256: p256_64.h p256_32.h np256_64.h np256_32.h
6060

61+
# The curve SECP256K1
62+
SECP256K1="2^256 - 2^32 - 977"
63+
64+
.PHONY: secp256k1_64.h
65+
secp256k1_64.h:
66+
$(WBW_MONT) --static secp256k1 64 $(SECP256K1) > $@
67+
68+
.PHONY: secp256k1_32.h
69+
secp256k1_32.h:
70+
$(WBW_MONT) --static secp256k1 32 $(SECP256K1) > $@
71+
72+
# The group order N of SECP256K1
73+
SECP256K1N="0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"
74+
75+
.PHONY: nsecp256k1_64.h
76+
nsecp256k1_64.h:
77+
$(WBW_MONT) --static nsecp256k1 64 $(SECP256K1N) $(N_FUNCS) > $@
78+
79+
.PHONY: nsecp256k1_32.h
80+
nsecp256k1_32.h:
81+
$(WBW_MONT) --static nsecp256k1 32 $(SECP256K1N) $(N_FUNCS) > $@
82+
83+
.PHONY: secp256k1
84+
secp256k1: secp256k1_64.h secp256k1_32.h nsecp256k1_64.h nsecp256k1_32.h
85+
6186
# The NIST curve P-384 (AKA SECP384R1)
6287
P384="2^384 - 2^128 - 2^96 + 2^32 - 1"
6388

@@ -83,8 +108,35 @@ np384_32.h:
83108
.PHONY: p384
84109
p384: p384_64.h p384_32.h np384_64.h np384_32.h
85110

111+
# The NIST curve P-521 (AKA SECP521R1)
112+
P521="2^521 - 1"
113+
114+
.PHONY: p521_64.h
115+
p521_64.h:
116+
$(WBW_MONT) --static p521 64 $(P521) > $@
117+
118+
.PHONY: p521_32.h
119+
p521_32.h:
120+
$(WBW_MONT) --static p521 32 $(P521) > $@
121+
122+
# The group order N of P-384
123+
P521N="0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
124+
125+
.PHONY: np521_64.h
126+
np521_64.h:
127+
$(WBW_MONT) --static np521 64 $(P521N) $(N_FUNCS) > $@
128+
129+
.PHONY: np521_32.h
130+
np521_32.h:
131+
$(WBW_MONT) --static np521 32 $(P521N) $(N_FUNCS) > $@
132+
133+
.PHONY: p521
134+
p521: p521_64.h p521_32.h np521_64.h np521_32.h
135+
86136
.PHONY: clean
87137
clean:
88138
$(RM) p224_32.h p224_64.h np224_32.h np224_64.h
89139
$(RM) p256_32.h p256_64.h np256_32.h np256_64.h
140+
$(RM) secp256k1_32.h secp256k1_64.h nsecp256k1_32.h nsecp256k1_64.h
90141
$(RM) p384_32.h p384_64.h np384_32.h np384_64.h
142+
$(RM) p521_32.h p521_64.h np521_32.h np521_64.h

0 commit comments

Comments
 (0)