v21: “Is the law then contrary to the promises of God?” – If God demanded that people obey the law in order to earn their inheritance, it would be contrary to his original promise to Abraham, but God doesn’t contradict himself. That’s not what the law was for.
“if a law had been given that could give life, then righteousness would indeed be by the law.” – Ephesians 2 says we were dead in our trespasses and sins, and James says faith without works is dead. The law cannot make us or our faith alive. It’s no good telling a dead person “here are some things you can do to make God pleased with you.” If you’re dead, you don’t need to work harder at making yourself righteous, you need to be brought to life, and the law can’t do that. It was never meant to.
v22: The law was given to imprison everything under sin. Why would God do that? “so that” the promised inheritance might be given to those who believe as a free gift by faith in Christ. In order to put our faith in Christ, we need to lose faith in ourselves. The law shows us how incapable we are of earning God’s inheritance by our obedience; while we’re under the law, we’re imprisoned in sin until we realise how helpless we are and we’re forced to give up on ourselves and instead turn to God and claim the inheritance he promises to those who believe, by faith in Christ.