Skip to content

Commit 0ebb29e

Browse files
authored
chore(bb): debugging helpers (#13584)
Allows for starting a vscode debugging session with whatever the cmake target currently selected is and with pretty-print helpers for fq, fr, uint256_t
1 parent 3048a14 commit 0ebb29e

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
"request": "attach",
2020
"name": "Attach",
2121
"port": 9229
22+
},
23+
{
24+
"name": "Debug Current CMake Target",
25+
"type": "lldb",
26+
"request": "launch",
27+
"program": "${command:cmake.launchTargetPath}",
28+
"args": [],
29+
"cwd": "${workspaceFolder}/barretenberg/cpp/build",
30+
"initCommands": [
31+
"command script import ${workspaceFolder}/barretenberg/cpp/scripts/lldb_format.py"
32+
],
2233
}
2334
]
2435
}
Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
import lldb
22

3-
# Define the bn254 modulus
4-
N = 21888242871839275222246405745257275088548364400416034343698204186575808495617
3+
class Fr:
4+
# Define the bn254 modulus
5+
N = 21888242871839275222246405745257275088548364400416034343698204186575808495617
56

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
89

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)
1112

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):
1324
# 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
1526
return standard_value
1627

17-
def montgomery_summary(valobj, internal_dict):
28+
def from_montgomery_field(field_type, valobj, internal_dict):
1829
try:
1930
data = valobj.GetChildMemberWithName('data')
2031
data_0 = data.GetChildAtIndex(0).GetValueAsUnsigned()
@@ -29,17 +40,63 @@ def montgomery_summary(valobj, internal_dict):
2940
(data_3 << 192)
3041
)
3142

32-
standard_value = montgomery_to_standard(montgomery_value)
43+
standard_value = from_montgomery(field_type, montgomery_value)
3344
return hex(standard_value)
3445
except Exception as e:
3546
return f"Error: {e}"
3647

48+
def from_montgomery_fr(valobj, internal_dict):
49+
return from_montgomery_field(Fr, valobj, internal_dict)
3750

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)
4053

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}]"
4175

4276
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

Comments
 (0)