This is one of the uses of "law" that many have struggled with and think it's better to view as Paul speaking about a "principle" or such thing vs. a "law".
I'm not so sure we have to go there. I tend to go directly to the most literal translation and attempt to deal with it first at that level.
Paul is asking rhetorically what kind/sort/nature [of] law excludes boasting - he concludes that boasting is excluded through [a] law [of] faith and not through [a] law [of] the works.
So, we already have the contrast you identified, but we don't IMO yet have the full meaning of law. The reason I'm including these brackets around "of" is because they identify where translators are inserting words that carry much more meaning than "of". Even in English, "of" can mean several different things - see an English dictionary. In Greek we can run through a list of 30+/- different meanings to place into the bracket to replace "of".
Here's a list from Greek Grammar Beyond the Basics by Daniel Wallace identifying the different things Paul could be telling us about faith vs. works in regard to law:
The Genitive Case
Overview of Genitive Uses
Adjectival Genitive
78
1. Descriptive Genitive (“Aporetic” Genitive)
79
2. Possessive Genitive
81
3. Genitive of Relationship
83
4. Partitive Genitive (“Wholative”)
84
5. Attributive Genitive
85
6. Attributed Genitive
89
7. Genitive of Material
91
8. Genitive of Content
92
9. Genitive in Simple Apposition
94
10. Genitive of Apposition
95
11. Genitive of Destination (a. k. a. Direction or Purpose)
100
12. Predicate Genitive
102
13. Genitive of Subordination
103
14. Genitive of Production/Producer
104
15. Genitive of Product
106
Ablatival Genitive
107
1. Genitive of Separation
107
2. Genitive of Source (or Origin)
109
3. Genitive of Comparison
110
Verbal Genitive (i.e., Genitive Related to a Verbal Noun)
112
1. Subjective Genitive
112
2. Objective Genitive
116
3. Plenary Genitive
119
Adverbial Genitive
121
1. Genitive of Price or Value or Quantity
122
2. Genitive of Time (within which or during which)
122
3. Genitive of Place (where or within which)
124
4. Genitive of Means
125
5. Genitive of Agency
126
6. Genitive Absolute
127
7. Genitive of Reference
127
8. Genitive of Association
128
After Certain Words
131
1. Genitive After Certain Verbs (as Direct Object)
131
2. Genitive After Certain Adjectives (and Adverbs)
134
3. Genitive After Certain Nouns
135
4. Genitive After Certain Prepositions
136
So, we know that Paul is describing a kind of law - what kind of law is it? Until we select from this or a similar list a category - or categories in the case of some intended ambiguity - we really don't know.
IOW, I don't think the issue is about the word "law" - I think it's about the inserted word "of".
I also think your attaching this to "the works of the law" is likely correct in whatever sense, and your second attachment may have some merit but may be clarified and maybe modified once the above is sorted out.
Please note also that the phrase "the works [of] the law" also contains the same "of" in the brackets. This is a catch-all translation that mostly indicates the translators are likely not dealing with the actual specificity in the original wording.
IMO we're trying to reach conclusions on author's meanings apart from knowing what they're actually saying. Then we're arguing over opinions at a very surface level.