1
1
using . Specs: modp_spec
2
2
3
- # TODO : Add support for @PGroup{p = _p, q = _q} where _q and _p are defined out of the scope
4
3
macro PGroup (expr)
5
4
if expr. head == :braces
6
5
if length (expr. args) == 1 && ! (expr. args[1 ] isa Expr)
@@ -9,15 +8,18 @@ macro PGroup(expr)
9
8
spec = modp_spec (name)
10
9
group = concretize_type (PGroup, spec; name)
11
10
return group
11
+
12
12
else
13
- # Two-argument case: @PGroup{p=23, q=11}
13
+ # Two-argument case: @PGroup{p=23, q=11} or @PGroup{p=my_p, q=my_q}
14
14
p = q = nothing
15
15
for arg in expr. args
16
16
if arg isa Expr && arg. head == :(= )
17
- if arg. args[1 ] == :p
18
- p = arg. args[2 ]
19
- elseif arg. args[1 ] == :q
20
- q = arg. args[2 ]
17
+ lhs = arg. args[1 ]
18
+ rhs = arg. args[2 ]
19
+ if lhs == :p
20
+ p = rhs
21
+ elseif lhs == :q
22
+ q = rhs
21
23
end
22
24
end
23
25
end
@@ -27,16 +29,22 @@ macro PGroup(expr)
27
29
error (" Both p and q must be specified in @PGroup{p=..., q=...}" )
28
30
end
29
31
30
- spec = MODP (p; q)
31
- group = concretize_type (PGroup, spec)
32
- return group
32
+ # Properly escape both p and q values
33
+ return quote
34
+ local p_val = $ (esc (p))
35
+ local q_val = $ (esc (q))
36
+ local spec = MODP (p_val; q= q_val)
37
+ concretize_type (
38
+ PGroup,
39
+ spec
40
+ )
41
+ end
33
42
end
34
43
else
35
44
error (" Invalid syntax. Use @PGroup{p=..., q=...} or @PGroup{some_name}" )
36
45
end
37
46
end
38
47
39
-
40
48
Base. show (io:: IO , :: Type{PGroup} ) = print (io, " PGroup" )
41
49
42
50
function Base. show (io:: IO , :: Type{G} ) where G <: PGroup
66
74
67
75
68
76
macro ECGroup (expr)
69
- if expr. head == :braces && length (expr. args) == 1 && ! (expr. args[1 ] isa Expr)
70
- # Single argument case: @PGroup{some_name}
71
- some_name = expr. args[1 ]
72
-
73
- # If the curve can't be found error here
74
- spec = curve (some_name)
75
- group = concretize_type (ECGroup, spec)
77
+ if expr. head == :braces && length (expr. args) == 1
78
+ arg = expr. args[1 ]
79
+ point_expr = Expr (:macrocall , Symbol (" @ECPoint" ), LineNumberNode (@__LINE__ ),
80
+ Expr (:braces , arg))
81
+ # Use __module__ to get the ECGroup type from the defining module
82
+ return :(ECGroup{$ (esc (point_expr))})
83
+ else
84
+ error (" Invalid syntax. Use @ECGroup{curve_name} or @ECGroup{Module.curve_name}" )
85
+ end
86
+ end
76
87
77
- return group
88
+ # First, let's modify @ECPoint to ensure it handles symbol quoting correctly
89
+ macro ECPoint (expr)
90
+ if expr. head == :braces && length (expr. args) == 1
91
+ arg = expr. args[1 ]
92
+
93
+ # Handle module-qualified names (e.g., OpenSSLGroups.SecP256k1)
94
+ if arg isa Expr && arg. head == :.
95
+ return quote
96
+ local P = $ (esc (arg))
97
+ concretize_type (ECPoint{P}, order (P), cofactor (P); name = nameof (P))
98
+ end
99
+ # Handle simple symbols
100
+ elseif arg isa Symbol
101
+ # Important: Use QuoteNode here for the isdefined check
102
+ return quote
103
+ if isdefined ($ (__module__), $ (QuoteNode (arg)))
104
+ # If defined, use the escaped symbol to access its value
105
+ local P = $ (esc (arg))
106
+ concretize_type (ECPoint{P}, order (P), cofactor (P); name = nameof (P))
107
+ else
108
+ # If not defined, treat it as a curve name
109
+ local spec = curve ($ (QuoteNode (arg)))
110
+ concretize_type (ECPoint, spec)
111
+ end
112
+ end
113
+ else
114
+ error (" Invalid syntax. Use @ECPoint{curve_name} or @ECPoint{Module.curve_name}" )
115
+ end
78
116
else
79
- error (" Invalid syntax. Use @ECGroup{ curve_name}" )
117
+ error (" Invalid syntax. Use @ECPoint{curve_name} or @ECPoint{Module. curve_name}" )
80
118
end
81
119
end
82
120
121
+
83
122
function Base. show (io:: IO , g:: G ) where G <: ECGroup
84
123
show (io, G)
85
124
print (io, " (" )
@@ -101,23 +140,6 @@ function Base.show(io::IO, ::Type{G}) where G <: ECGroup
101
140
end
102
141
end
103
142
104
-
105
- macro ECPoint (expr)
106
- if expr. head == :braces && length (expr. args) == 1 && ! (expr. args[1 ] isa Expr)
107
- # Single argument case: @PGroup{some_name}
108
- some_name = expr. args[1 ]
109
-
110
- spec = curve (some_name)
111
- # If the curve can't be found error here
112
- _curve = concretize_type (ECPoint, spec)
113
-
114
- return _curve
115
- else
116
- error (" Invalid syntax. Use @ECPoint{curve_name}" )
117
- end
118
- end
119
-
120
-
121
143
# ## May need to do epoint seperatelly
122
144
function Base. show (io:: IO , :: Type{P} ) where P <: ECPoint
123
145
if @isdefined P
@@ -131,7 +153,6 @@ function Base.show(io::IO, ::Type{P}) where P <: ECPoint
131
153
end
132
154
end
133
155
134
-
135
156
function Base. show (io:: IO , p:: P ) where P <: ECPoint
136
157
show (io, P)
137
158
print (io, " (" )
144
165
function Base. display (:: Type{P} ) where P <: ECPoint
145
166
show (P)
146
167
end
147
-
148
-
149
-
0 commit comments