Get poly1305 core to kind of work

This commit is contained in:
Byron Lathi
2025-07-04 10:49:48 -07:00
parent 2b8286d180
commit 7f91a8af32
8 changed files with 367 additions and 2 deletions

View File

@@ -0,0 +1,146 @@
<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="gIy_vrPza4QP03Kn0wfk">
<mxGraphModel dx="655" dy="442" 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="GA09nmFLpfHeItamLD5O-24" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="GA09nmFLpfHeItamLD5O-1" target="GA09nmFLpfHeItamLD5O-21">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-25" value="r" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="GA09nmFLpfHeItamLD5O-24">
<mxGeometry x="0.5579" y="-1" relative="1" as="geometry">
<mxPoint x="9" y="5" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-35" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.75;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="GA09nmFLpfHeItamLD5O-1" target="GA09nmFLpfHeItamLD5O-34">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-38" value="s" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="GA09nmFLpfHeItamLD5O-35">
<mxGeometry x="-0.6624" y="1" relative="1" as="geometry">
<mxPoint x="-9" y="-9" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-1" value="r/s" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="360" y="200" width="80" height="40" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-2" value="" style="endArrow=classic;html=1;rounded=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;" edge="1" parent="1" target="GA09nmFLpfHeItamLD5O-1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="320" y="210" as="sourcePoint" />
<mxPoint x="410" y="270" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-3" value="otk" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="GA09nmFLpfHeItamLD5O-2">
<mxGeometry x="-0.3946" y="1" relative="1" as="geometry">
<mxPoint x="-22" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-10" 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="GA09nmFLpfHeItamLD5O-4" target="GA09nmFLpfHeItamLD5O-6">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-4" value="64-&amp;gt;128" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="175" y="130" width="50" height="20" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-5" value="" style="endArrow=classic;html=1;rounded=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" target="GA09nmFLpfHeItamLD5O-4">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="120" y="140" as="sourcePoint" />
<mxPoint x="290" y="110" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-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="GA09nmFLpfHeItamLD5O-6" target="GA09nmFLpfHeItamLD5O-14">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-40" value="data_one_extended" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="GA09nmFLpfHeItamLD5O-15">
<mxGeometry x="-0.3532" y="-1" relative="1" as="geometry">
<mxPoint x="7" y="29" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-6" value="bit add" style="ellipse;whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
<mxGeometry x="240" y="120" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-8" value="" style="endArrow=classic;html=1;rounded=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" target="GA09nmFLpfHeItamLD5O-6">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="260" y="80" as="sourcePoint" />
<mxPoint x="290" y="70" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-9" value="tkeep" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="GA09nmFLpfHeItamLD5O-8">
<mxGeometry x="-0.699" relative="1" as="geometry">
<mxPoint y="-16" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-11" value="P" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="540" y="180" width="80" height="20" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-18" 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="GA09nmFLpfHeItamLD5O-12" target="GA09nmFLpfHeItamLD5O-14">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="340" y="100" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-36" 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="GA09nmFLpfHeItamLD5O-12" target="GA09nmFLpfHeItamLD5O-34">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="400" y="60" />
<mxPoint x="660" y="60" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-12" value="acc" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="360" y="80" width="80" height="40" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-14" value="+" style="ellipse;whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
<mxGeometry x="320" y="120" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-32" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="GA09nmFLpfHeItamLD5O-21" target="GA09nmFLpfHeItamLD5O-31">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-21" value="*" style="ellipse;whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
<mxGeometry x="440" y="120" width="40" height="40" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-22" 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="GA09nmFLpfHeItamLD5O-14" target="GA09nmFLpfHeItamLD5O-21">
<mxGeometry relative="1" as="geometry">
<mxPoint x="460" y="140" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-41" value="data_post_add" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="GA09nmFLpfHeItamLD5O-22">
<mxGeometry x="-0.1925" y="-1" relative="1" as="geometry">
<mxPoint x="8" y="19" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-26" 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="GA09nmFLpfHeItamLD5O-29" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.51;entryY=1.007;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="GA09nmFLpfHeItamLD5O-11" target="GA09nmFLpfHeItamLD5O-26">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-30" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=1.022;entryY=0.482;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="GA09nmFLpfHeItamLD5O-26" target="GA09nmFLpfHeItamLD5O-12">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="580" y="99" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-33" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="GA09nmFLpfHeItamLD5O-31" target="GA09nmFLpfHeItamLD5O-26">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-31" value="reg" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="500" y="130" width="40" height="20" as="geometry" />
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-37" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="GA09nmFLpfHeItamLD5O-34">
<mxGeometry relative="1" as="geometry">
<mxPoint x="720" y="140" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-39" value="tag" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="GA09nmFLpfHeItamLD5O-37">
<mxGeometry x="0.6531" y="2" relative="1" as="geometry">
<mxPoint x="17" y="2" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="GA09nmFLpfHeItamLD5O-34" value="+" style="ellipse;whiteSpace=wrap;html=1;aspect=fixed;" vertex="1" parent="1">
<mxGeometry x="640" y="120" width="40" height="40" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

View File

@@ -0,0 +1,7 @@
tests:
- name: "poly1305_core"
toplevel: "poly1305_core_harness"
modules:
- "poly1305_core"
sources: "sources.list"
waves: True

View File

@@ -0,0 +1,73 @@
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
CLK_PERIOD = 4
class TB:
def __init__(self, dut):
self.dut = dut
self.log = logging.getLogger("cocotb.tb")
self.log.setLevel(logging.INFO)
cocotb.start_soon(Clock(self.dut.i_clk, CLK_PERIOD, units="ns").start())
self.s_data_axis = AxiStreamSource(AxiStreamBus.from_prefix(dut.s_data_axis, ""), dut.i_clk, dut.i_rst)
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)
@cocotb.test
async def test_sanity(dut):
tb = TB(dut)
await tb.cycle_reset()
s = 0x1bf54941aff6bf4afdb20dfb8a800301
r = 0xa806d542fe52447f336d555778bed685
r_masked = 0x0806d5400e52447c036d555408bed685
result = 0xa927010caf8b2bc2c6365130c11d06a8
msg = b"Cryptographic Forum Research Group"
tb.dut.i_otk.value = ((r << 128) | s)
tb.dut.i_otk_valid.value = 1
await RisingEdge(tb.dut.i_clk)
tb.dut.i_otk_valid.value = 0
await RisingEdge(tb.dut.i_clk)
dut_s = tb.dut.u_dut.poly1305_s.value.integer
dut_r = tb.dut.u_dut.poly1305_r.value.integer
assert dut_s == s
assert dut_r == r_masked
await tb.s_data_axis.send(msg)
await RisingEdge(tb.dut.o_tag_valid)
tag = tb.dut.o_tag.value.integer
tb.log.info(f"tag: {tag:x}")
await Timer(1, "us")

View File

@@ -0,0 +1,26 @@
module poly1305_core_harness();
taxi_axis_if #(.DATA_W(128)) s_data_axis();
logic i_clk;
logic i_rst;
logic [255:0] i_otk;
logic i_otk_valid;
logic [127:0] o_tag;
logic o_tag_valid;
poly1305_core u_dut (
.i_clk (i_clk),
.i_rst (i_rst),
.i_otk (i_otk),
.i_otk_valid (i_otk_valid),
.o_tag (o_tag),
.o_tag_valid (o_tag_valid),
.s_data_axis (s_data_axis)
);
endmodule

View File

@@ -1 +1,4 @@
../src/sources.list
poly1305_core_harness.sv
../src/sources.list
../../common/sim/sub/taxi/src/axis/rtl/taxi_axis_if.sv

View File

@@ -0,0 +1,24 @@
module chacha20_poly1305_64 (
input i_clk,
input i_rst,
taxi_axis_if.snk s_ctrl_axis,
taxi_axis_if.snk s_data_axis,
taxi_axis_if.src m_data_axis
);
//TODO the rest of this
// control axis decoder.
localparam R_MASK = 128'h0ffffffc0ffffffc0ffffffc0fffffff;
chacha20_pipelined_block u_chacha20_pipelined_block (
);
poly1305 u_poly1305 (
);
endmodule

View File

@@ -0,0 +1,84 @@
module poly1305_core #(
) (
input i_clk,
input i_rst,
input [255:0] i_otk,
input i_otk_valid,
output [127:0] o_tag,
output o_tag_valid,
taxi_axis_if.snk s_data_axis
);
// incoming data must be 128 bit and packed, i.e. tkeep is 1 except for the last beat with no gaps
localparam R_MASK = 128'h0ffffffc0ffffffc0ffffffc0fffffff;
localparam P130M5 = 258'h3fffffffffffffffffffffffffffffffb;
logic [127:0] poly1305_r, poly1305_s;
logic [129:0] accumulator, accumulator_next;
logic [129:0] data_one_extended;
logic [130:0] data_post_add;
logic [257:0] data_post_mul, data_post_mul_reg;
logic phase;
logic [1:0] valid_sr;
function logic [129:0] tkeep_expand (input [15:0] tkeep);
tkeep_expand = '0;
for (int i = 0; i < 16; i++) begin
tkeep_expand[i*8 +: 8] = {8{tkeep[i]}};
end
endfunction
// only ready in phase 0
assign s_data_axis.tready = phase == 0;
assign o_tag_valid = valid_sr[1];
always_ff @(posedge i_clk) begin
if (i_rst) begin
phase <= '0;
valid_sr <= '0;
end
valid_sr <= {valid_sr[0], s_data_axis.tlast & s_data_axis.tvalid & s_data_axis.tready & ~phase};
if (i_otk_valid) begin
poly1305_r <= i_otk[255:128] & R_MASK;
poly1305_s <= i_otk[127:0];
end
if (s_data_axis.tvalid && phase == 0) begin
data_post_mul_reg <= data_post_mul;
phase <= '1;
end
if (phase == '1) begin
accumulator <= accumulator_next;
phase <= '0;
end
end
always_comb begin
accumulator_next = accumulator;
data_post_mul = '0;
// phase == 0
data_one_extended = (tkeep_expand(s_data_axis.tkeep) + 1) | {2'b0, s_data_axis.tdata};
data_post_add = data_one_extended + accumulator;
data_post_mul = data_post_add * poly1305_r;
// phase == 1
accumulator_next = 130'(data_post_mul_reg % P130M5);
end
assign o_tag = accumulator[127:0] + poly1305_s;
endmodule

View File

@@ -1,4 +1,6 @@
chacha20_qr.sv
chacha20_block.sv
chacha20_pipelined_round.sv
chacha20_pipelined_block.sv
chacha20_pipelined_block.sv
poly1305_core.sv