D.C. Mayor Marion Barry's greatest peril -- and his legal strategy to avert it -- have both emerged sharply in the opening days of trial in federal court.

The peril is a conviction for perjury, the only felony charged in the mayor's 14-count indictment and the only alleged offense for which a prison term is unavoidable. For Barry that peril has a name: Charles Lewis, his onetime friend, who completed two revealing days on the witness stand yesterday.

Others have important supporting roles, but Lewis is the one indispensable witness for the government's accusation that Barry lied three times about drugs to a federal grand jury in January 1989. If the jurors elect to believe him, Lewis has already given them nearly all they need to support a finding of guilt.

R. Kenneth Mundy, the mayor's attorney, made his first attempt yesterday to discredit his client's principal accuser. In 2 1/2 hours of cross-examination -- set to continue this morning -- Mundy wasby turns withering and whimsical. By the end of the day he appeared to have reached some key objectives.

The perjury counts of the indictment boil down to three alleged lies: that Barry knew nothing of Lewis's drug use, that he had never given Lewis any drugs and that he had never received any cocaine from Lewis.

Assistant U.S. Attorney Judith E. Retchin used her direct examination of Lewis to sketch a vivid portrait of friendship based on drugs.

Barry was described as dumping crack cocaine on a table from a reddish-brown Capital Hilton matchbox; sitting on a toilet seat in a bathroom of the Ramada Inn, drawing deeply from a makeshift pipe made with a paper clip, a sherbet glass and aluminum foil; and blowing excess smoke from his mouth to Lewis's when he had drawn too much from the pipe.

Mundy performed the courtroom equivalent of jujitsu, using the wealth of testimonial detail to cast doubt on the credibility of its source. Disbelieve Lewis, Mundy told the jury in effect, precisely because he claimed to remember so much. When Lewis proved unable to remember details of far more recent events -- most notably dozens of hours of interrogation by government investigators -- Mundy wrapped astonishment all over his face.

"You mean to tell me with that memory that you have exhibited that you forgot about a three- or four-hour meeting you had last week with the government and its agents to prepare for your testimony?" Mundy asked.

That question, and the line of inquiry surrounding it, sought to make three simultaneous points for the defense. First, that Lewis is beholden to prosecutors for assistance in his forthcoming sentencing for drug offenses in the Virgin Islands. Second, that Lewis has been well "rehearsed" in his testimony about the mayor. (U.S. District Judge Thomas Penfield Jackson sustained Retchin's objection to the word, but the jury had already heard it.) Third, that Lewis's remarkable memory was somewhat selective.

To those jurors who might be sympathetic to the mayor's plight, Mundy also implicitly signaled the stakes in the felony count. He made it clear to them that a guilty verdict of perjury would expose the mayor to a substantial period of imprisonment.

He was not allowed to tell them that directly. The jurors have been instructed that sentencing is not their concern. But because Lewis's credibility was at issue, Mundy was entitled to ask him how good a deal he had struck with the government. Part of that deal, Lewis testified, was that he would not be prosecuted for his original sworn denials that he had used cocaine with the mayor at the Ramada Inn.

"How many perjury statements do you feel you committed . . . in that grand jury testimony of January 1989?" Mundy asked.

"I don't recall this moment, sir," Lewis replied.

"Do you know what the penalty is for each one?"

"I believe it was 10 years," Lewis said.

Suddenly, prosecutors were faced with a dilemma. They were entitled to what the law calls a "limiting instruction," in which the judge would tell the jury to disregard what they had heard about possible sentences. But the instruction would call attention to its subject. Worse, Lewis had been wrong: perjury's maximum sentence is five years, and under binding sentencing guidelines the term is even shorter.

Mundy was clearly enjoying himself when Retchin asked the judge for the instruction. He had no objection at all, he said, "and I don't even object to the court correcting the 10 years, and that it is a maximum penalty of five years."

Many experienced strategists say this case will be won or lost on cross-examination, and Mundy is regarded as one of the best cross-examiners in the city. On some questions he stood erect at the lectern, head thrown back and ears pricked up like a pointer on the hunt. After others, he wheeled away from the witness stand as though confident that Lewis could not hurt him with his answer.

For all his virtuosity, Mundy did little to disturb the core of Lewis's account. Lewis never strayed from a single legally relevant assertion: Barry had given him cocaine, and Barry had accepted cocaine on other occasions. Far from knowing nothing of Lewis's drug use, Barry had participated in it often for more than three years, Lewis insisted.

"My testimony isn't to convict the mayor," Lewis said. "My testimony to the grand jury wasn't to indict the mayor. I was a witness to talk or testify truthfully and completely."