Our righteousness is thankfully imputed on the basis of our faith in the gospel elements of Jesus, His blood, body, death and resurrection. Righteousness is awarded by God, not earned like a delivery man gets paid for making his route.
Romans 4:23-25 (KJV)
23 Now it was not written for his sake alone, that it was imputed to him;
24 But for us also, to whom it shall be imputed, if we believe on him that raised up Jesus our Lord from the dead;
25 Who was delivered for our offences, and was raised again for our justification .
Justification there is from the Gr. dikaiosis, the Judge declaring acquittal of persons, but it doesn't stop there, as when on trial for an accusation one's reputation may be damaged even though let go. God takes it to reckoning the person as esteemed, acceptable. But wait, it doesn't stop there. The person is also declared righteous. He leaves the courtroom completely undefiled and honored by God. There is a big "however" applicable. A Jew had to keep every law perfectly, or be condemned upon one failure. The Judge had to punish. A Christian has Christ in him so that Jesus stood in for the believer, taking his punishment. Jesus became sin for the person whose still mortal body is tagged with a sin in it's past, but goes free not be good works, but those of Christ in him. A work IN him, not by the sinner of record.
It is all by Christ in us, our hope of glory. The principle of those benefits from faith applied to Abraham, before the Law entered. That principle of friendship with God had nothing to do with the Law of sin and death.
After Paul explained that he then made the case of a sinner in Rom 7, showing how miserable a person is before being spared from the wrath of God, then personally forgiven, then declared righteous and justified as a son of God. Ignore the caption "CHAPTER 8", as those words were added by men for the purpose of breaking up the scriptures for easier reference. Doing that, one discovers the message continues unbroken into the glory of the benefits in Christ. Paul was not personally wrestling with sin in 7, but was empathizing with the part of one of us under conviction of sin, then after the moment of believing in Jesus, empowered by the Holy Spirit within. He included the effects of being condemned by the Law, being a Jew, and the warfare it added, but it should not be necessary for a person to be aware of the Law of sin and death in order to believe the gospel of Christ. Most of the world doesn't even have a Bible, few of those having the Old Testament, but might be told some of it when introduced to Christ.
There are three expressions (senses) of law in Romans. The Mosaic law, the principle of sin and death, and the law of conscience. The whole of LAW covers from innermost by nature, to the extreme external delivered by angels. If not adequately applying those to real life experiences, error is prevalent, and debates separate friends.
Seems maybe a short course in Romans 6-8 is needed. I can't condense it better with a "shotgun" post like this. Please go study 6-8 folks, line by line, word by word.