“That was one-upmanship,” Kidwai said, smiling proudly as we looked at a photograph of one test, which was hanging on his office wall. “India had conducted only five.” Below the photographs, Kidwai keeps a small fragment of the Chagai mountain under glass, displayed like a moon rock at the Smithsonian. The explosion had turned it bright white.

NO SOONER HAD THE radioactive and diplomatic dust settled from the test site than Kidwai was called in by his army superiors, and ultimately, Gen. Pervez Musharraf, and told that he would now head an urgent project: to come up with a system to protect Pakistan’s new weapon from all of its enemies — the Indians, Western Europeans and the angry Americans. Kidwai knew speed was of the essence. Pakistan’s leaders feared that if the West thought that Pakistan had just a few weapons in its inventory, and no system to assure their safety, they would come under even more pressure to roll back the program and give up the handful they had manufactured. The only way to resist that pressure, they knew, was to create a large arsenal quickly and to hide it in underground facilities where neither the Indians nor the Americans could seize or destroy the warheads. Then they needed to convince the world that Pakistan could become a responsible nuclear power, one capable of securing its weapons as well as the Russians, the Chinese or the Israelis did. That meant Kidwai had to learn the arts of nuclear safety from the Americans, but without teaching his teachers how to neutralize Pakistan’s arsenal.

Kidwai got off to a rocky start. The Pakistani nuclear program owes its very existence to the government-endorsed and government-financed subterfuges of A. Q. Khan, who then turned the country into the biggest source of nuclear-weapons proliferation in atomic history. And while Khan may be the most famous nuclear renegade in Pakistan, he is not the only one. Soon after Kidwai took office, he also faced the case of the eccentric nuclear scientist Sultan Bashiruddin Mahmood, who helped build gas centrifuges for the Pakistani nuclear program, using blueprints Khan had stolen from the Netherlands. Mahmood then moved on to the country’s next huge project: designing the reactor at Khushab that was to produce the fuel Pakistan needed to move to the next level — a plutonium bomb.

An autodidact intellectual with grand aspirations, Mahmood was fascinated by the links between science and the Koran. He wrote a peculiar treatise arguing that when morals degrade, disaster cannot be far behind. Over time, his colleagues began to wonder if Mahmood was mentally sound. They were half amused and half horrified by his fascination with the role sunspots played in triggering the French and Russian Revolutions, World War II and assorted anticolonial uprisings. “This guy was our ultimate nightmare,” an American intelligence official told me in late 2001, when The New York Times first reported on Mahmood. “He had access to the entire Pakistani program. He knew what he was doing. And he was completely out of his mind.”

While Khan appeared to be in the nuclear-proliferation business chiefly for the money, Mahmood made it clear to friends that his interest was religious: Pakistan’s bomb, he told associates, was “the property of a whole Ummah,” referring to the worldwide Muslim community. He wanted to share it with those who might speed “the end of days” and lead the way for Islam to rise as the dominant religious force in the world.

Eventually Mahmood’s religious intensity, combined with his sympathy for Islamic extremism, scared his colleagues. In 1999, just as Kidwai was beginning to examine the staff of the nuclear enterprise, Mahmood was forced to take an early retirement. At a loss for what to do, Mahmood set up a nonprofit charity, Ummah Tameer-e-Nau, which was ostensibly designed to send relief to fellow Muslims in Afghanistan. In August 2001, as the Sept. 11 plotters were making their last preparations in the United States, Mahmood and one of his colleagues at the charity met with Osama bin Laden and his deputy, Ayman al-Zawahiri, over the course of several days in Afghanistan. There is little doubt that Mahmood talked to the two Qaeda leaders about nuclear weapons, or that Al Qaeda desperately wanted the bomb. George Tenet, the C.I.A. chief, wrote later that intelligence reports of the meeting were “frustratingly vague.” They included an account that there was talk of how to design a simple firing mechanism, and that a senior Qaeda leader displayed a canister that may have contained some nuclear material (though almost certainly not bomb-grade).

In the weeks after 9/11, the tales of the meeting were enough to set off panic. Rolf Mowatt-Larssen, a longtime C.I.A. nuclear expert, was given perhaps the most daunting job at the agency in the aftermath of 9/11: to make sure that Al Qaeda did not have a weapon of mass destruction at its disposal. “The worst nightmare we had at that time was that A. Q. Khan and Osama bin Laden were somehow working together,” Mowatt-Larssen told me one day last winter in his basement office in a secure vault at the Energy Department, where he moved after his time at the C.I.A. to head up the department’s intelligence unit. As if to drive home the point to visitors to his underground lair, Mowatt-Larssen, who is leaving the government this month to become a senior fellow at Harvard, keeps a floor-to-ceiling centrifuge in the corner of his office, where most people might put a potted plant. The gleaming silver device, which is meant to spin at terrifying speed to enrich uranium, was seized in Libya — part of the cache that Muammar el-Qaddaffi bought from Khan.