WASHINGTON (Reuters) - The U.S. Supreme Court on Thursday upheld the federal government’s authority under a 2006 law to require thousands of sex offenders to register with authorities in the states where they live, as the justices ruled against a child rapist convicted in Maryland.

In its 5-3 decision, the court rejected convicted sex offender Herman Gundy’s argument that in passing the law, Congress handed too much power to the U.S. attorney general in violation of a principal of constitutional law called the nondelegation doctrine. This doctrine forbids Congress from assigning its legislative powers to the federal government’s executive branch.

Gundy’s lawyers did not immediately comment on Thursday’s ruling.

Justice Brett Kavanaugh, who had not yet joined the court when the case was argued last October, did not participate in the decision.

Gundy was convicted in 2005 in Maryland of raping an 11-year-old girl. After serving seven years in prison, Gundy was arrested in New York in 2012 for failing to register as a sex offender there.

He challenged the indictment over the nondelegation question, but his claim was rejected by a district court judge and then in 2017 by the New York-based 2nd U.S. Circuit Court of Appeals.

The law, known as the Sex Offender Registration and Notification Act (SORNA), requires anyone convicted of a sex offense to register as a sex offender in any jurisdiction in which they live. But the law did not specify whether the requirement would apply to roughly 500,000 people, like Gundy, already convicted of sex offenses when the law was passed.

Instead, Congress said the attorney general - the top U.S. law enforcement official - should decide, which Gundy’s lawyers argued violates the nondelegation doctrine. Since the law was enacted, different U.S. attorneys general under Republican and Democratic presidents have interpreted the law, with all saying it does apply retroactively in some fashion.

Liberal Justice Elena Kagan, who wrote the decision, said the amount of discretion Congress gave the attorney general is legal because it is limited, in keeping with Congress’ need for administration officials to carry out its programs.

“Indeed, if SORNA’s delegation is unconstitutional, then most of government is unconstitutional,” Kagan quipped.

Kagan was joined by the courts other three liberal justices in the ruling. Conservative Justice Samuel Alito also joined the judgment but suggested that the court should reconsider more generally its longstanding support for delegations of authority by Congress.

In dissent, conservative Justice Neil Gorsuch warned that such delegations imperil individual freedom. If “a single executive branch official can write laws restricting the liberty of this group of persons,” he wrote, “what does that mean for the next?”

Even before the law was passed, all 50 states had laws requiring registration. The federal law was designed to create a uniform system to be used nationwide that would prevent sex offenders from falling off the radar. As recently as May 2018, only 17 states have fully implemented the federal requirements, according to court papers.