In the Old Testament, Israel could know God’s moral will for his people through the laws set forth in the covenant made at Mount Sinai. If David lived in a manner consistent with those covenant stipulations, he could legitimately speak of his righteousness.
David was not perfect by any means—a fact he readily acknowledged it.
1 Have mercy on me, O God,
according to your unfailing love;
according to your great compassion
blot out my transgressions.
2 Wash away all my iniquity
and cleanse me from my sin.
3 For I know my transgressions,
and my sin is always before me.
4 Against you, you only, have I sinned
and done what is evil in your sight;
so you are right in your verdict
and justified when you judge.
5 Surely I was sinful at birth,
sinful from the time my mother conceived me.
6 Yet you desired faithfulness even in the womb;
you taught me wisdom in that secret place.
7 Cleanse me with hyssop, and I will be clean;
wash me, and I will be whiter than snow.
8 Let me hear joy and gladness;
let the bones you have crushed rejoice.
9 Hide your face from my sins
and blot out all my iniquity.
10 Create in me a pure heart, O God,
and renew a steadfast spirit within me.
11 Do not cast me from your presence
or take your Holy Spirit from me.
12 Restore to me the joy of your salvation
and grant me a willing spirit, to sustain me.
13 Then I will teach transgressors your ways,
so that sinners will turn back to you.
14 Deliver me from the guilt of bloodshed, O God,
you who are God my Savior,
and my tongue will sing of your righteousness.
15 Open my lips, Lord,
and my mouth will declare your praise.
16 You do not delight in sacrifice, or I would bring it;
you do not take pleasure in burnt offerings.
17 My sacrifice, O God, is[b] a broken spirit;
a broken and contrite heart
you, God, will not despise.
18 May it please you to prosper Zion,
to build up the walls of Jerusalem.
19 Then you will delight in the sacrifices of the righteous,
in burnt offerings offered whole;
then bulls will be offered on your altar.
But while it is a great sin to violate God’s law, the covenant provided a means by which confession, repentance, restitution and restoration to God and the community could take place. The sacrificial system provided the Israelites with a process by which to make amends to God and their neighbors when they sinned. But more important than sacrifice is a humble and penitent heart that casts itself entirely on the mercy of God.
When we do this, God “imputes” righteousness to us—that is, he credits the perfect righteousness of Jesus Christ to us.
21 God made him who had no sin to be sin for us, so that in him we might become the righteousness of God.

