Chris Grayling was prevented from turning legal aid into "an instrument of discrimination" today, after three judges found his reforms to be unlawful.

In a devastating judgement which could bring the residency test requirements to a halt, the judges found the lord chancellor had radically overstepped the proper limits of his powers and was trying to create a discriminatory legal system which was incompatible with equality under the law.

“Using powers that were never his to exercise, the lord chancellor has attempted to refashion the legal aid scheme into an instrument of discrimination so that many of the cases parliament itself identified as most worthy of support could never be taken," John Halford of legal firm Bindmans, which fought the case, said.

"The court's judgement on that attempt is emphatic: it is simply unacceptable in a country where all are equal in the eyes of the law.

"Legal aid is, and must remain, the means to safeguard equality in our courts, regardless of people’s origins, nationality or place of residence."

Grayling's proposed reforms would have banned anyone who had been in the UK for less than a year from receiving legal aid.

Critics warned the proposals would prevent some of the most vulnerable people in society from legal protection.

The judges imagined the case of a severely learning disabled adult, who had been "forced to live in a dog kennel outside the house, had been beaten regularly by his brother and mother, and starved over an extensive period of time". Under Grayling's plans, he would have been denied legal aid.

Individuals who were resident abroad who had been subject to "serious abuses" at the hand of British armed forces would also be denied legal aid.

The joint human rights committee recently warned the reforms were illegal under international law and called on the lord chancellor to exclude children from its provisions, but he refused to budge. There were also concerns it could strip trafficked women and domestic abuse survivors of legal protection.

Today's judgement is a much bigger problem for Grayling however, because it found he exceeded his statutory powers when devising it.

The court found that regulations made under an act of parliament must be consistent with the policy and object of the act.

But it concluded that the Legal Aid Sentencing and Punishment of Offenders Act 2012 was intended to prioritise legal aid to the cases of greatest need, while Grayling's reforms would have had the opposite effect.

And in an even more devastating passage, the judges found it to be discriminatory.

While they accepted that the government was entitled to act in a discriminatory manner on policy areas such as welfare, it could not do so when the issue in question was equality before the law, because it is a fundamental cornerstone of Britain's system of government.

"It is and was beyond question that the introduction of such a test was discriminatory. Indeed, that is its declared purpose," the ruling reads.

"Within the system provided in schedule 1 of the Legal Aid Act, the United Kingdom is not permitted to discriminate against non-residents on the grounds that to do so might save costs.

"Certainly it is not possible to justify such discrimination in an area where all are equally subject to the law, resident or not, and equally entitled to its protection, resident or not.

"In my judgement, a residence test cannot be justified in relation to the enforcement of domestic law or the protection afforded by domestic law, which is applicable to all equally, provided they are within its jurisdiction.

"In the context of a discriminatory provision relating to legal assistance, invoking public confidence amounts to little more than reliance on public prejudice."

Jo Hickman of the Public Law Project, a small legal charity which worked on the test case with Bindmans, said: "We are heartened by this judgment, which embodies and articulates the finest traditions of our justice system and provides a timely illustration of the importance of judicial review as a check on unlawful executive action."

Shadow justice minister Andy Slaughter, who spearheaded the parliamentary resistance to the move, celebrated the ruling.

"The Labour party have always maintained that David Cameron's residence test would unfairly penalise the vulnerable, which is why we voted against it last week," he commented.

"We welcome the court's judgement and hope that the government move quickly to drop these unlawful measures."

The Ministry of Justice has said it will appeal the ruling.