1
1
import lldb
2
2
3
- # Define the bn254 modulus
4
- N = 21888242871839275222246405745257275088548364400416034343698204186575808495617
3
+ class Fr :
4
+ # Define the bn254 modulus
5
+ N = 21888242871839275222246405745257275088548364400416034343698204186575808495617
5
6
6
- # Define R as a power of 2 such that R > N (commonly used R for bn254 is 2^256)
7
- R = 2 ** 256
7
+ # Define R as a power of 2 such that R > N (commonly used R for bn254 is 2^256)
8
+ R = 2 ** 256
8
9
9
- # Compute R inverse modulo N
10
- R_inv = pow (R , - 1 , N )
10
+ # Compute R inverse modulo N
11
+ R_inv = pow (R , - 1 , N )
11
12
12
- def montgomery_to_standard (montgomery_value ):
13
+ class Fq :
14
+ # Define the bn254 modulus
15
+ N = 21888242871839275222246405745257275088696311157297823662689037894645226208759
16
+
17
+ # Define R as a power of 2 such that R > N (commonly used R for bn254 is 2^256)
18
+ R = 2 ** 256
19
+
20
+ # Compute R inverse modulo N
21
+ R_inv = pow (R , - 1 , N )
22
+
23
+ def from_montgomery (field_type , montgomery_value ):
13
24
# Convert from Montgomery form to standard representation
14
- standard_value = (montgomery_value * R_inv ) % N
25
+ standard_value = (montgomery_value * field_type . R_inv ) % field_type . N
15
26
return standard_value
16
27
17
- def montgomery_summary ( valobj , internal_dict ):
28
+ def from_montgomery_field ( field_type , valobj , internal_dict ):
18
29
try :
19
30
data = valobj .GetChildMemberWithName ('data' )
20
31
data_0 = data .GetChildAtIndex (0 ).GetValueAsUnsigned ()
@@ -29,17 +40,63 @@ def montgomery_summary(valobj, internal_dict):
29
40
(data_3 << 192 )
30
41
)
31
42
32
- standard_value = montgomery_to_standard ( montgomery_value )
43
+ standard_value = from_montgomery ( field_type , montgomery_value )
33
44
return hex (standard_value )
34
45
except Exception as e :
35
46
return f"Error: { e } "
36
47
48
+ def from_montgomery_fr (valobj , internal_dict ):
49
+ return from_montgomery_field (Fr , valobj , internal_dict )
37
50
38
- def montgomery_summary2 (valobj , internal_dict ):
39
- return montgomery_summary ( valobj . EvaluateExpression ( "get_value()" ) , internal_dict )
51
+ def from_montgomery_fq (valobj , internal_dict ):
52
+ return from_montgomery_field ( Fq , valobj , internal_dict )
40
53
54
+ def from_montgomery_field_t_fr (valobj , internal_dict ):
55
+ return from_montgomery_fr (Fr , valobj .EvaluateExpression ("get_value()" ), internal_dict )
56
+
57
+ def from_uint256 (valobj , internal_dict ):
58
+ """Summarize a uint256_t (with a uint64_t[4] named 'data') as hex."""
59
+ try :
60
+ data = valobj .GetChildMemberWithName ('data' )
61
+ low64 = data .GetChildAtIndex (0 ).GetValueAsUnsigned ()
62
+ mid64a = data .GetChildAtIndex (1 ).GetValueAsUnsigned ()
63
+ mid64b = data .GetChildAtIndex (2 ).GetValueAsUnsigned ()
64
+ high64 = data .GetChildAtIndex (3 ).GetValueAsUnsigned ()
65
+
66
+ combined = (
67
+ low64
68
+ | (mid64a << 64 )
69
+ | (mid64b << 128 )
70
+ | (high64 << 192 )
71
+ )
72
+ return hex (combined )
73
+ except Exception as e :
74
+ return f"[uint256_t Error: { e } ]"
41
75
42
76
def __lldb_init_module (debugger , internal_dict ):
43
- debugger .HandleCommand ("type summary add --python-function lldb_format.montgomery_summary bb::fr" )
44
- debugger .HandleCommand ("type summary add --python-function lldb_format.montgomery_summary2 -x \" bb::stdlib::field_t.*\" " )
45
- print ('The "formatter" command has been installed!' )
77
+ commands = [
78
+ # Matches:
79
+ # "bb::fr",
80
+ # any optional namespace followed by "Fr" (e.g. "foo::Fr"),
81
+ # "bb::field<bb::Bn254FrParams>"
82
+ # with or without "const"
83
+ (r'^(const\s+)?(bb::fr|(\w+::)?Fr|bb::field<bb::Bn254FrParams>)$' , 'lldb_format.from_montgomery_fr' ),
84
+
85
+ # Matches:
86
+ # any optional namespace followed by "Fq" (e.g. "foo::Fq"),
87
+ # "bb::field<bb::Bn254FqParams>"
88
+ # with or without "const"
89
+ (r'^(const\s+)?((\w+::)?Fq|bb::field<bb::Bn254FqParams>)$' , 'lldb_format.from_montgomery_fq' ),
90
+
91
+ # Matches:
92
+ # "bb::stdlib::field_t" with any additional characters
93
+ # with or without "const"
94
+ (r'^(const\s+)?(bb::stdlib::field_t.*)$' , 'lldb_format.from_montgomery_field_t_fr' ),
95
+ ('bb::numeric::uint256_t' , 'lldb_format.from_uint256' ),
96
+ ]
97
+
98
+ for pattern , py_func in commands :
99
+ cmd = f'type summary add -x "{ pattern } " --python-function { py_func } '
100
+ debugger .HandleCommand (cmd )
101
+
102
+ print ('lldb_format.py commands have been installed!' )
0 commit comments