Verified Commit e2b4b324 authored by Minyong Li's avatar Minyong Li 💬
Browse files

core.ChaChaBlock{,Test}: merge w/ ChaChaInnerBlock

A redesign is also performed.
parent 052643e4
...@@ -5,20 +5,25 @@ package uk.ac.soton.ecs.can.core ...@@ -5,20 +5,25 @@ package uk.ac.soton.ecs.can.core
import chisel3._ import chisel3._
class ChaChaBlock extends MultiIOModule { class ChaChaBlock(val regBetweenRounds: Boolean = true) extends MultiIOModule {
val muxIn = IO(Input(Bool())) val roundLoop = IO(Input(Bool()))
val initialState = IO(Input(Vec(16, UInt(32.W))))
val in = IO(Input(Vec(16, UInt(32.W)))) val in = IO(Input(Vec(16, UInt(32.W))))
val out = IO(Output(Vec(16, UInt(32.W)))) val out = IO(Output(Vec(16, UInt(32.W))))
val initialState = Reg(Vec(16, UInt(32.W))) private val columnRound = Module(new ColumnRound)
val doubleRound = Module(new ChaChaInnerBlock(regBetweenRounds = true)) private val diagonalRound = Module(new DiagonalRound)
val doubleRoundState = Reg(Vec(16, UInt(32.W))) private val betweenRounds =
if (regBetweenRounds) Reg(Vec(16, UInt(32.W)))
else Wire(Vec(16, UInt(32.W)))
private val afterRounds = Reg(Vec(16, UInt(32.W)))
initialState := in private val muxRoundLoop = Mux(roundLoop, afterRounds, in)
doubleRound.in := Mux(muxIn, initialState, doubleRoundState) private val sumInRound = in.zip(afterRounds).map { case (i, r) => i + r }
doubleRoundState := doubleRound.out
val addedState = doubleRoundState.zip(initialState).map(t => t._1 + t._2) columnRound.in := muxRoundLoop
betweenRounds := columnRound.out
out := addedState diagonalRound.in := betweenRounds
afterRounds := diagonalRound.out
out := sumInRound
} }
// SPDX-FileCopyrightText: 2021 Minyong Li <ml10g20@soton.ac.uk>
// SPDX-License-Identifier: CERN-OHL-W-2.0
package uk.ac.soton.ecs.can.core
import chisel3._
class ChaChaInnerBlock(regBetweenRounds: Boolean) extends MultiIOModule {
val in = IO(Input(Vec(16, UInt(32.W))))
val out = IO(Output(Vec(16, UInt(32.W))))
val betweenRounds =
if (regBetweenRounds)
Reg(Vec(16, UInt(32.W)))
else
Wire(Vec(16, UInt(32.W)))
val columnRound = Module(new ColumnRound)
val diagonalRound = Module(new DiagonalRound)
columnRound.in := in
betweenRounds := columnRound.out
diagonalRound.in := betweenRounds
out := diagonalRound.out
}
...@@ -64,40 +64,43 @@ class ChaChaBlockTest extends FlatSpec with ChiselScalatestTester { ...@@ -64,40 +64,43 @@ class ChaChaBlockTest extends FlatSpec with ChiselScalatestTester {
) )
private def doTest(c: ChaChaBlock, testVector: Seq[(UInt, UInt)]) { private def doTest(c: ChaChaBlock, testVector: Seq[(UInt, UInt)]) {
c.in.zip(testVector).foreach { t => c.in.zip(testVector).foreach { case (blockIn, (vectorIn, _)) =>
t._1.poke(t._2._1) blockIn.poke(vectorIn)
} }
// Shift inputs into the initial state register // Select the input port as the input to the rounds
c.clock.step() c.roundLoop.poke(false.B)
// Select the initial state register as the input to the 2-round circuit // Shift the state through the rounds
c.muxIn.poke(true.B) c.clock.step(if (c.regBetweenRounds) 2 else 1)
// Shift the 2-rounded state to the round register // Select the round register as the input to the rounds
c.clock.step(2) c.roundLoop.poke(true.B)
// Select the round register as the input to the 2-round circuit
c.muxIn.poke(false.B)
// Depending on the ChaCha variant and the pipeline configuration, wait // Depending on the ChaCha variant and the pipeline configuration, wait
// for the correct time for the correct result. Note that one 2-round has // for the correct time for the correct result. Note that one 2-round has
// been processed in the above steps. // been processed in the above steps.
c.clock.step(19) c.clock.step(if (c.regBetweenRounds) 19 else 9)
c.out.zip(testVector).foreach { t => c.out.zip(testVector).foreach { case (blockOut, (_, vectorOut)) =>
t._1.expect(t._2._2) blockOut.expect(vectorOut)
} }
} }
behavior of "The ChaCha Block Function" behavior of "The ChaCha Block Function"
it should "compute RFC8439 2.3.2 test vector correctly" in it should "compute RFC8439 2.3.2 test vector correctly" in {
test(new ChaChaBlock)(doTest(_, rfc8439232TestVector)) test(new ChaChaBlock(true))(doTest(_, rfc8439232TestVector))
test(new ChaChaBlock(false))(doTest(_, rfc8439232TestVector))
}
it should "compute RFC8439 2.4.2 test vector (first block) correctly" in it should "compute RFC8439 2.4.2 test vector (first block) correctly" in {
test(new ChaChaBlock)(doTest(_, rfc8439242B1TestVector)) test(new ChaChaBlock(true))(doTest(_, rfc8439242B1TestVector))
test(new ChaChaBlock(false))(doTest(_, rfc8439242B1TestVector))
}
it should "compute RFC8439 2.4.2 test vector (second block) correctly" in it should "compute RFC8439 2.4.2 test vector (second block) correctly" in {
test(new ChaChaBlock)(doTest(_, rfc8439242B2TestVector)) test(new ChaChaBlock(true))(doTest(_, rfc8439242B2TestVector))
test(new ChaChaBlock(false))(doTest(_, rfc8439242B2TestVector))
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment