Skip to content

Cache #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ectf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ jobs:
run: docker run --rm -v ./build_out:/out -v ./:/decoder -v ./../secrets:/secrets -e DECODER_ID=0xdeadbeef decoder

- name: Create artifact
if: ${{ (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main' }}
uses: actions/upload-artifact@v4
with:
name: max78000.bin
path: decoder/build_out/max78000.bin

- name: Get tag name
if: ${{ (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main' }}
id: tag-name
run: |
echo "tagname=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Release
if: ${{ (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main' }}
env:
GH_TOKEN: ${{ github.token }}
run: |
Expand Down
2 changes: 1 addition & 1 deletion benches/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main():

# Encode all the frames before starting the timer
encoder = Encoder(secrets=secrets)
encoded_frames = [encoder.encode(channel=channel, frame=frame, timestamp=i+1) for (i, frame) in enumerate(raw_frames)]
encoded_frames = [encoder.encode(channel=channel, frame=frame, timestamp=(i+1)*1000) for (i, frame) in enumerate(raw_frames)]

t = time.perf_counter()
for (raw_frame, encoded_frame) in zip(raw_frames, encoded_frames):
Expand Down
8 changes: 3 additions & 5 deletions decoder/flash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ pub fn init() !void {
for (meta.valid, 1..) |valid, i| {
if (valid) {
messaging.debugMessage("Reading saved subscription {}", .{i});
root.subscriptions[i - 1] = messaging.Subscription{
.start = 0,
.end = 0,
};
msdk.MXC_FLC_Read(@intCast(FLASH_START_ADDR + (i * msdk.MXC_FLASH_PAGE_SIZE)), &root.subscriptions[i - 1].?, @sizeOf(@TypeOf(root.subscriptions[i - 1].?)));
var subscriptionBytes: shared.hashtree.Subscription.Bytes = undefined;
read(FLASH_START_ADDR + (i * msdk.MXC_FLASH_PAGE_SIZE), &subscriptionBytes);
root.subscriptions[i - 1] = shared.hashtree.Subscription.init(subscriptionBytes.start, subscriptionBytes.end, std.mem.asBytes(&subscriptionBytes.root_hashes));
}
}
}
Expand Down
39 changes: 10 additions & 29 deletions decoder/host_messaging.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn list() !void {
var channelIndex: usize = 0;
for (root.subscriptions, 0..) |subscription, i| {
if (subscription) |sub| {
listChannelResponse.subscriptions[channelIndex] = SubscriptionEntry{ .channel_id = i + 1, .start = sub.start, .end = sub.end };
listChannelResponse.subscriptions[channelIndex] = SubscriptionEntry{ .channel_id = i + 1, .start = sub.serialized.start, .end = sub.serialized.end };
channelIndex += 1;
}
}
Expand Down Expand Up @@ -113,19 +113,16 @@ pub fn decode(body: []u8) !void {
debugMessage("Channel too large", .{});
return error.ChannelTooLarge;
}
const subscription: Subscription = root.subscriptions[dec.channel - 1] orelse {
var subscription = &(root.subscriptions[dec.channel - 1] orelse {
debugMessage("No subscription", .{});
return error.NoSubscription;
};
if (dec.timestamp < subscription.start or subscription.end < dec.timestamp) {
});
if (dec.timestamp < subscription.serialized.start or subscription.serialized.end < dec.timestamp) {
debugMessage("Not in subscription time range", .{});
return error.NotInSubscriptionTimeRange;
}
var roots: [126]shared.hashtree.RootPosition = undefined;
_ = shared.hashtree.getRootPositions(subscription.start, subscription.end, &roots);
var key: [16]u8 = undefined;
shared.hashtree.getKey(&roots, &subscription.hashes, dec.timestamp, &key);

const key = subscription.getKey(dec.timestamp);
shared.crypto.decrypt(&dec.message, key);
}

Expand All @@ -138,32 +135,16 @@ pub const SubscribeHeader = extern struct {
channel: u8 align(1),
};

pub const Subscription = extern struct {
start: u64,
end: u64,
hashes: [126][16]u8 = undefined,
};

pub fn subscribe(body: []u8) !void {
const key = secrets.subscriptionKey;
std.crypto.stream.salsa.Salsa20.xor(body, body, 0, key, std.mem.zeroes([8]u8));

const header: *const SubscribeHeader = @ptrCast(body.ptr);
const sub = &root.subscriptions[header.channel - 1];

sub.* = .{
.start = header.start,
.end = header.end,
};

var iter = std.mem.window(u8, body[@sizeOf(SubscribeHeader)..], 16, 16);
var i: usize = 0;
while (iter.next()) |hash| {
@memcpy(&sub.*.?.hashes[i], hash);
i += 1;
}
const channelIndex = header.channel - 1;

root.subscriptions[channelIndex] = shared.hashtree.Subscription.init(header.start, header.end, body[@sizeOf(SubscribeHeader)..]);

try flash.saveSubscriptions(@truncate(header.channel - 1));
try flash.saveSubscriptions(@truncate(channelIndex));

try sendMessageWithAcks('S', &.{});
}
Expand All @@ -173,7 +154,7 @@ pub fn ack() void {
uart.writeBytes(&packet.asBytes());
}

var debugMessageBuffer: [65536]u8 = undefined;
var debugMessageBuffer: [256]u8 = undefined;

pub fn debugMessage(comptime format: []const u8, args: anytype) void {
const text = std.fmt.bufPrint(debugMessageBuffer[4..], format, args) catch unreachable;
Expand Down
4 changes: 2 additions & 2 deletions decoder/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const flash = @import("flash.zig");
const uart = @import("uart.zig");
const messaging = @import("host_messaging.zig");

pub var subscriptions = [8]?messaging.Subscription{ null, null, null, null, null, null, null, null };
const MAXIMUM_MESSAGE_SIZE = @sizeOf(messaging.SubscribeHeader) + @sizeOf(messaging.Subscription);
pub var subscriptions = [8]?shared.hashtree.Subscription{ null, null, null, null, null, null, null, null };
const MAXIMUM_MESSAGE_SIZE = @sizeOf(messaging.SubscribeHeader) + @sizeOf(shared.hashtree.Subscription.Bytes);
var message_body_buffer: [MAXIMUM_MESSAGE_SIZE]u8 = undefined;

/// High level entrypoint for the decoder
Expand Down
Loading