WASHINGTON (CNN) -- The Supreme Court has ruled the federal government may not deny funds from its global anti-AIDS program to organizations that refuse to actively oppose legalizing prostitution.

The 6-2 decision Thursday was a test of the First Amendment rights of the organizations seeking such aid.

"The Policy Requirement compels as a condition of federal funding the affirmation of a belief that by its nature cannot be confined within the scope of the government program," Chief Justice John Roberts said. "In doing so, it violates the First Amendment and cannot be sustained."

A 2003 congressional law mandated fund recipients adhere to the government's larger message about fighting the disease and its root causes -- that the deadly virus is being spread by sex trafficking and prostitution.

The high court said the anti-prostitution message that the United States sought to promote was not directly related to the AIDS program's "core" mandate.

The high court ruling now tosses out that current policy.

The case is Agency for International Development v. Alliance for Open Society International (12-10).