Add mult, but it doesn't quite work
This commit is contained in:
132
ChaCha20_Poly1305_64/doc/friendly_modular_mult.drawio
Normal file
132
ChaCha20_Poly1305_64/doc/friendly_modular_mult.drawio
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
<mxfile host="Electron" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/26.2.2 Chrome/134.0.6998.178 Electron/35.1.2 Safari/537.36" version="26.2.2">
|
||||||
|
<diagram name="Page-1" id="b4c9RxKzofB-lxyaVzG6">
|
||||||
|
<mxGraphModel dx="289" dy="195" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
|
||||||
|
<root>
|
||||||
|
<mxCell id="0" />
|
||||||
|
<mxCell id="1" parent="0" />
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-4" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-1" target="yBq3zbYGeky0_LNz2CMc-2">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="680" y="100" />
|
||||||
|
<mxPoint x="580" y="100" />
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-28" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-1">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<mxPoint x="760" y="140" as="targetPoint" />
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-29" value="result" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="yBq3zbYGeky0_LNz2CMc-28">
|
||||||
|
<mxGeometry x="0.7628" y="1" relative="1" as="geometry">
|
||||||
|
<mxPoint x="25" y="1" as="offset" />
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-1" value="accumulator w/ wrap" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="640" y="120" width="80" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-3" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-2" target="yBq3zbYGeky0_LNz2CMc-1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-2" value="+" style="ellipse;whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="560" y="120" width="40" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-6" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-5" target="yBq3zbYGeky0_LNz2CMc-2">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-5" value="Friendly Modulo" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="360" y="80" width="160" height="120" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-14" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-7" target="yBq3zbYGeky0_LNz2CMc-13">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-7" value="X" style="ellipse;whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="200" y="120" width="40" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-9" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-8" target="yBq3zbYGeky0_LNz2CMc-7">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-8" value="Data (128 bit)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="80" width="120" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-11" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-10" target="yBq3zbYGeky0_LNz2CMc-7">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-10" value="h (26x5 bit)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="160" width="120" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-15" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-13" target="yBq3zbYGeky0_LNz2CMc-5">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-13" value="pipe reg" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="280" y="120" width="40" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-25" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-16" target="yBq3zbYGeky0_LNz2CMc-22">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-16" value="pipe reg" style="whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="280" y="160" width="40" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-19" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-18" target="yBq3zbYGeky0_LNz2CMc-10">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-20" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-18" target="yBq3zbYGeky0_LNz2CMc-16">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="100" y="220" />
|
||||||
|
<mxPoint x="260" y="220" />
|
||||||
|
<mxPoint x="260" y="180" />
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-18" value="state counter" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="240" width="120" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-21" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.833;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-16" target="yBq3zbYGeky0_LNz2CMc-5">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-22" value="pipe reg" style="whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="360" y="240" width="40" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-23" value="pipe reg" style="whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="400" y="240" width="40" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-26" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-24" target="yBq3zbYGeky0_LNz2CMc-1">
|
||||||
|
<mxGeometry relative="1" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-27" value="reset" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="yBq3zbYGeky0_LNz2CMc-26">
|
||||||
|
<mxGeometry x="-0.2" y="1" relative="1" as="geometry">
|
||||||
|
<mxPoint x="-10" y="-9" as="offset" />
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-31" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-24" target="yBq3zbYGeky0_LNz2CMc-30">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="680" y="260" />
|
||||||
|
<mxPoint x="680" y="200" />
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-24" value="pipe reg" style="whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="440" y="240" width="40" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-32" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="yBq3zbYGeky0_LNz2CMc-30">
|
||||||
|
<mxGeometry relative="1" as="geometry">
|
||||||
|
<mxPoint x="800" y="200" as="targetPoint" />
|
||||||
|
<Array as="points">
|
||||||
|
<mxPoint x="800" y="200" />
|
||||||
|
</Array>
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-33" value="done flag" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="yBq3zbYGeky0_LNz2CMc-32">
|
||||||
|
<mxGeometry x="0.1299" y="1" relative="1" as="geometry">
|
||||||
|
<mxPoint x="7" y="-19" as="offset" />
|
||||||
|
</mxGeometry>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="yBq3zbYGeky0_LNz2CMc-30" value="==4?" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="700" y="180" width="60" height="40" as="geometry" />
|
||||||
|
</mxCell>
|
||||||
|
</root>
|
||||||
|
</mxGraphModel>
|
||||||
|
</diagram>
|
||||||
|
</mxfile>
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
import random
|
||||||
|
|
||||||
from modulo_theory import friendly_modular_mult, friendly_modulo
|
from modulo_theory import friendly_modular_mult, friendly_modulo
|
||||||
|
|
||||||
@@ -111,11 +112,19 @@ def test_on_long_string():
|
|||||||
print(f"{regular_result:x}")
|
print(f"{regular_result:x}")
|
||||||
print(f"{parallel_result:x}")
|
print(f"{parallel_result:x}")
|
||||||
|
|
||||||
|
def test_random():
|
||||||
|
r = mask_r(random.randint(0, 2**128-1))
|
||||||
|
s = random.randint(0, 2**128-1)
|
||||||
|
|
||||||
|
msg = random.randbytes(random.randint(16, 1500))
|
||||||
|
|
||||||
|
parallel_poly1305(msg, r, s, 8)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test_regular()
|
test_regular()
|
||||||
test_parallel()
|
test_parallel()
|
||||||
test_on_long_string()
|
test_on_long_string()
|
||||||
|
test_random()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -57,6 +57,9 @@ def friendly_modular_mult(value_a: int, value_b: int) -> int:
|
|||||||
|
|
||||||
mods = [friendly_modulo(prod, 26*i) for i, prod in enumerate(prods)]
|
mods = [friendly_modulo(prod, 26*i) for i, prod in enumerate(prods)]
|
||||||
|
|
||||||
|
if sum(mods) >= 2*PRIME:
|
||||||
|
print("Saw greater than 2x prime!!!")
|
||||||
|
|
||||||
|
|
||||||
mod_sum = friendly_modulo(sum(mods), 0)
|
mod_sum = friendly_modulo(sum(mods), 0)
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,15 @@ tests:
|
|||||||
- "poly1305_core"
|
- "poly1305_core"
|
||||||
sources: "sources.list"
|
sources: "sources.list"
|
||||||
waves: True
|
waves: True
|
||||||
- name: "friendly_modulo"
|
- name: "poly1305_friendly_modulo"
|
||||||
toplevel: "poly1305_friendly_modulo"
|
toplevel: "poly1305_friendly_modulo"
|
||||||
modules:
|
modules:
|
||||||
- "poly1305_friendly_modulo"
|
- "poly1305_friendly_modulo"
|
||||||
sources: sources.list
|
sources: sources.list
|
||||||
|
waves: True
|
||||||
|
- name: "poly1305_friendly_modular_mult"
|
||||||
|
toplevel: "poly1305_friendly_modular_mult"
|
||||||
|
modules:
|
||||||
|
- "poly1305_friendly_modular_mult"
|
||||||
|
sources: sources.list
|
||||||
waves: True
|
waves: True
|
||||||
96
ChaCha20_Poly1305_64/sim/poly1305_friendly_modular_mult.py
Normal file
96
ChaCha20_Poly1305_64/sim/poly1305_friendly_modular_mult.py
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
import cocotb
|
||||||
|
from cocotb.clock import Clock
|
||||||
|
from cocotb.triggers import Timer, RisingEdge, FallingEdge
|
||||||
|
from cocotb.queue import Queue
|
||||||
|
|
||||||
|
from cocotbext.axi import AxiStreamBus, AxiStreamSource
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
|
PRIME = 2**130-5
|
||||||
|
|
||||||
|
CLK_PERIOD = 4
|
||||||
|
|
||||||
|
|
||||||
|
class TB:
|
||||||
|
def __init__(self, dut):
|
||||||
|
self.dut = dut
|
||||||
|
|
||||||
|
self.log = logging.getLogger("cocotb.tb")
|
||||||
|
self.log.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
self.input_queue = Queue()
|
||||||
|
|
||||||
|
self.expected_queue = Queue()
|
||||||
|
self.output_queue = Queue()
|
||||||
|
|
||||||
|
cocotb.start_soon(Clock(self.dut.i_clk, CLK_PERIOD, units="ns").start())
|
||||||
|
|
||||||
|
cocotb.start_soon(self.run_input())
|
||||||
|
cocotb.start_soon(self.run_output())
|
||||||
|
|
||||||
|
async def cycle_reset(self):
|
||||||
|
await self._cycle_reset(self.dut.i_rst, self.dut.i_clk)
|
||||||
|
|
||||||
|
async def _cycle_reset(self, rst, clk):
|
||||||
|
rst.setimmediatevalue(0)
|
||||||
|
await RisingEdge(clk)
|
||||||
|
await RisingEdge(clk)
|
||||||
|
rst.value = 1
|
||||||
|
await RisingEdge(clk)
|
||||||
|
await RisingEdge(clk)
|
||||||
|
rst.value = 0
|
||||||
|
await RisingEdge(clk)
|
||||||
|
await RisingEdge(clk)
|
||||||
|
|
||||||
|
async def write_input(self, data: int, h: int):
|
||||||
|
await self.input_queue.put((data, h))
|
||||||
|
await self.expected_queue.put((data * h) % PRIME)
|
||||||
|
|
||||||
|
async def run_input(self):
|
||||||
|
while True:
|
||||||
|
data, h = await self.input_queue.get()
|
||||||
|
self.dut.i_valid.value = 1
|
||||||
|
self.dut.i_data.value = data
|
||||||
|
self.dut.i_accumulator.value = h
|
||||||
|
while True:
|
||||||
|
await RisingEdge(self.dut.i_clk)
|
||||||
|
if (self.dut.o_ready.value == 1):
|
||||||
|
break
|
||||||
|
self.dut.i_valid.value = 0
|
||||||
|
self.dut.i_data.value = 0
|
||||||
|
self.dut.i_accumulator.value = 0
|
||||||
|
|
||||||
|
async def run_output(self):
|
||||||
|
while True:
|
||||||
|
await RisingEdge(self.dut.i_clk)
|
||||||
|
if self.dut.o_valid.value:
|
||||||
|
await self.output_queue.put(self.dut.o_result.value.integer)
|
||||||
|
|
||||||
|
@cocotb.test
|
||||||
|
async def test_sanity(dut):
|
||||||
|
tb = TB(dut)
|
||||||
|
|
||||||
|
await tb.cycle_reset()
|
||||||
|
|
||||||
|
count = 1
|
||||||
|
|
||||||
|
for _ in range(count):
|
||||||
|
await tb.write_input(random.randint(1,2**128-1), random.randint(0, 2**130-6))
|
||||||
|
|
||||||
|
fail = False
|
||||||
|
|
||||||
|
for _ in range(count):
|
||||||
|
sim_val = await tb.expected_queue.get()
|
||||||
|
dut_val = await tb.output_queue.get()
|
||||||
|
|
||||||
|
if sim_val != dut_val:
|
||||||
|
tb.log.info(f"{sim_val:x} -> {dut_val:x}")
|
||||||
|
fail = True
|
||||||
|
|
||||||
|
# assert not fail
|
||||||
|
|
||||||
|
await Timer(1, "us")
|
||||||
101
ChaCha20_Poly1305_64/src/poly1305_friendly_modular_mult.sv
Normal file
101
ChaCha20_Poly1305_64/src/poly1305_friendly_modular_mult.sv
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
module poly1305_friendly_modular_mult #(
|
||||||
|
parameter DATA_WIDTH = 128,
|
||||||
|
parameter ACC_WIDTH = 130
|
||||||
|
) (
|
||||||
|
input logic i_clk,
|
||||||
|
input logic i_rst,
|
||||||
|
|
||||||
|
input logic i_valid,
|
||||||
|
output logic o_ready,
|
||||||
|
input logic [DATA_WIDTH-1:0] i_data,
|
||||||
|
input logic [ACC_WIDTH-1:0] i_accumulator,
|
||||||
|
|
||||||
|
output logic o_valid,
|
||||||
|
output logic [ACC_WIDTH-1:0] o_result
|
||||||
|
);
|
||||||
|
|
||||||
|
localparam [129:0] PRIME = (1 << 130) - 5;
|
||||||
|
|
||||||
|
logic [2:0] state_counter, state_counter_next;
|
||||||
|
|
||||||
|
logic [2:0] state_counter_p [4];
|
||||||
|
|
||||||
|
logic [ACC_WIDTH-1:0] accumulator, accumulator_next; // accumulator is outgoing
|
||||||
|
|
||||||
|
logic [DATA_WIDTH-1:0] data, data_next;
|
||||||
|
logic [ACC_WIDTH-1:0] h, h_next; // h is incoming
|
||||||
|
|
||||||
|
logic [DATA_WIDTH+26-1:0] mult_product, mult_product_next;
|
||||||
|
|
||||||
|
logic [ACC_WIDTH-1:0] modulo_result;
|
||||||
|
|
||||||
|
assign o_ready = state_counter >= 3'h4;
|
||||||
|
assign o_result = accumulator;
|
||||||
|
|
||||||
|
always_ff @(posedge i_clk) begin
|
||||||
|
if (i_rst) begin
|
||||||
|
state_counter <= 3'h5;
|
||||||
|
state_counter_p <= '{default: 3'h5};
|
||||||
|
end else begin
|
||||||
|
state_counter <= state_counter_next;
|
||||||
|
accumulator <= accumulator_next;
|
||||||
|
data <= data_next;
|
||||||
|
h <= h_next;
|
||||||
|
mult_product <= mult_product_next;
|
||||||
|
|
||||||
|
state_counter_p[0] <= state_counter;
|
||||||
|
|
||||||
|
o_valid <= state_counter_p[3] == 3'h4;
|
||||||
|
|
||||||
|
for (int i = 1; i < 4; i++) begin
|
||||||
|
state_counter_p[i] <= state_counter_p[i-1];
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
always_comb begin
|
||||||
|
data_next = data;
|
||||||
|
h_next = h;
|
||||||
|
|
||||||
|
state_counter_next = state_counter;
|
||||||
|
|
||||||
|
accumulator_next = '0;
|
||||||
|
mult_product_next = '0;
|
||||||
|
|
||||||
|
|
||||||
|
if (state_counter >= 3'h4 && i_valid) begin
|
||||||
|
data_next = i_data;
|
||||||
|
h_next = i_accumulator;
|
||||||
|
state_counter_next = '0;
|
||||||
|
end
|
||||||
|
|
||||||
|
if (state_counter < 3'h5) begin
|
||||||
|
mult_product_next = h[state_counter*26 +: 26] * data;
|
||||||
|
state_counter_next = state_counter + 1;
|
||||||
|
end
|
||||||
|
|
||||||
|
if (state_counter_p[3] == '0) begin
|
||||||
|
accumulator_next = modulo_result;
|
||||||
|
end else begin
|
||||||
|
if (accumulator + modulo_result > PRIME) begin
|
||||||
|
accumulator_next = accumulator + modulo_result - PRIME;
|
||||||
|
end else begin
|
||||||
|
accumulator_next = accumulator + modulo_result;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
poly1305_friendly_modulo u_modulo (
|
||||||
|
.i_clk (i_clk),
|
||||||
|
.i_rst (i_rst),
|
||||||
|
|
||||||
|
.i_valid ('1),
|
||||||
|
.i_val ((2*ACC_WIDTH)'(mult_product)),
|
||||||
|
.i_shift_amount (state_counter_p[0]),
|
||||||
|
|
||||||
|
.o_valid (),
|
||||||
|
.o_result (modulo_result)
|
||||||
|
);
|
||||||
|
|
||||||
|
endmodule
|
||||||
@@ -4,4 +4,5 @@ chacha20_pipelined_round.sv
|
|||||||
chacha20_pipelined_block.sv
|
chacha20_pipelined_block.sv
|
||||||
|
|
||||||
poly1305_core.sv
|
poly1305_core.sv
|
||||||
poly1305_friendly_modulo.sv
|
poly1305_friendly_modulo.sv
|
||||||
|
poly1305_friendly_modular_mult.sv
|
||||||
Reference in New Issue
Block a user