Skip to content

Commit

Permalink
wip / expand testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sobeston committed Feb 24, 2025
1 parent 617bda4 commit 7c4e9cb
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 13 deletions.
123 changes: 113 additions & 10 deletions src/runtime/program/precompile_programs/ed25519.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub fn verify(
}
}

// https://github.com/anza-xyz/agave/blob/a8aef04122068ec36a7af0721e36ee58efa0bef2/sdk/src/ed25519_instruction.rs#L35
pub fn newInstruction(
allocator: std.mem.Allocator,
keypair: Ed25519.KeyPair,
Expand Down Expand Up @@ -136,6 +137,7 @@ pub fn newInstruction(
};
}

// https://github.com/anza-xyz/agave/blob/a8aef04122068ec36a7af0721e36ee58efa0bef2/sdk/src/ed25519_instruction.rs#L258
fn test_case(
allocator: std.mem.Allocator,
num_signatures: u16,
Expand All @@ -147,23 +149,15 @@ fn test_case(
instruction_data.appendSliceAssumeCapacity(std.mem.asBytes(&num_signatures));
instruction_data.appendSliceAssumeCapacity(std.mem.asBytes(&offsets));

return try verify(instruction_data.items, &.{});
return try verify(instruction_data.items, &.{&(.{0} ** 100)});
}

test "ed25519 invalid offsets" {
const allocator = std.testing.allocator;
var instruction_data = try std.ArrayListAligned(u8, 2).initCapacity(allocator, ED25519_DATA_START);
defer instruction_data.deinit();

const offsets = Ed25519SignatureOffsets{
.signature_offset = 0,
.signature_instruction_idx = 0,
.pubkey_offset = 0,
.pubkey_instruction_idx = 0,
.message_data_offset = 0,
.message_data_size = 0,
.message_instruction_idx = 0,
};
const offsets = Ed25519SignatureOffsets{};

// Set up instruction data with invalid size
instruction_data.appendSliceAssumeCapacity(std.mem.asBytes(&1));
Expand Down Expand Up @@ -202,3 +196,112 @@ test "ed25519 invalid offsets" {
error.InvalidDataOffsets,
);
}

// https://github.com/anza-xyz/agave/blob/a8aef04122068ec36a7af0721e36ee58efa0bef2/sdk/src/ed25519_instruction.rs#L326
test "ed25519 message data offsets" {
{
const offsets = Ed25519SignatureOffsets{
.message_data_offset = 99,
.message_data_size = 1,
};
try std.testing.expectEqual(
test_case(std.testing.allocator, 1, offsets),
error.InvalidSignature,
);
}

{
const offsets = Ed25519SignatureOffsets{
.message_data_offset = 100,
.message_data_size = 1,
};
try std.testing.expectEqual(
test_case(std.testing.allocator, 1, offsets),
error.InvalidDataOffsets,
);
}

{
const offsets = Ed25519SignatureOffsets{
.message_data_offset = 100,
.message_data_size = 1000,
};
try std.testing.expectEqual(
test_case(std.testing.allocator, 1, offsets),
error.InvalidDataOffsets,
);
}

{
const offsets = Ed25519SignatureOffsets{
.message_data_offset = std.math.maxInt(u16),
.message_data_size = std.math.maxInt(u16),
};
try std.testing.expectEqual(
test_case(std.testing.allocator, 1, offsets),
error.InvalidDataOffsets,
);
}
}

// https://github.com/anza-xyz/agave/blob/a8aef04122068ec36a7af0721e36ee58efa0bef2/sdk/src/ed25519_instruction.rs#L369
test "ed25519 pubkey offset" {
{
const offsets = Ed25519SignatureOffsets{
.pubkey_offset = std.math.maxInt(u16),
};
try std.testing.expectEqual(
test_case(std.testing.allocator, 1, offsets),
error.InvalidDataOffsets,
);
}

{
const offsets = Ed25519SignatureOffsets{
.pubkey_offset = 100 - ED25519_PUBKEY_SERIALIZED_SIZE + 1,
};
try std.testing.expectEqual(
test_case(std.testing.allocator, 1, offsets),
error.InvalidDataOffsets,
);
}
}

// https://github.com/anza-xyz/agave/blob/a8aef04122068ec36a7af0721e36ee58efa0bef2/sdk/src/ed25519_instruction.rs#L389-L390
test "ed25519 signature offset" {
{
const offsets = Ed25519SignatureOffsets{
.signature_offset = std.math.maxInt(u16),
};
try std.testing.expectEqual(
test_case(std.testing.allocator, 1, offsets),
error.InvalidDataOffsets,
);
}

{
const offsets = Ed25519SignatureOffsets{
.signature_offset = 100 - ED25519_SIGNATURE_SERIALIZED_SIZE + 1,
};
try std.testing.expectEqual(
test_case(std.testing.allocator, 1, offsets),
error.InvalidDataOffsets,
);
}
}
// // https://github.com/anza-xyz/agave/blob/a8aef04122068ec36a7af0721e36ee58efa0bef2/sdk/src/ed25519_instruction.rs#L411
// test "ed25519" {
// const keypair = try Ed25519.KeyPair.create(null);

// var instruction = try newInstruction(std.testing.allocator, keypair, "hello");

// const mint_keypair = try Ed25519.KeyPair.create(null);

// const feature_set = sig.runtime.FeatureSet.EMPTY; // should be all enabled to match agave, but doesn't mattter
// const Transaction = sig.core.transaction.Transaction;

// var tx = Transaction {
// .signatures =
// }

// }
4 changes: 2 additions & 2 deletions src/runtime/program/precompile_programs/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ pub fn getInstructionData(
all_instruction_datas: []const []const u8,
instruction_idx: u16,
offset: usize,
) error{ InvalidSignature, InvalidDataOffsets }![]const u8 {
) error{InvalidDataOffsets}![]const u8 {
const data: []const u8 = if (instruction_idx == std.math.maxInt(u16))
current_instruction_data
else data: {
if (instruction_idx >= all_instruction_datas.len) return error.InvalidDataOffsets;
break :data all_instruction_datas[instruction_idx];
};

if (offset + len > data.len) return error.InvalidSignature;
if (offset + len > data.len) return error.InvalidDataOffsets;
return data[offset..][0..len];
}
3 changes: 2 additions & 1 deletion src/runtime/program/precompile_programs/secp256r1.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const PrecompileProgramError = precompile_programs.PrecompileProgramError;
const getInstructionValue = precompile_programs.getInstructionValue;
const getInstructionData = precompile_programs.getInstructionData;

// I don't see where this function is in firedancer?
// firedancer puts this one behind an ifdef. Maybe we don't need it?
//https://github.com/firedancer-io/firedancer/blob/49056135a4c7ba024cb75a45925439239904238b/src/flamenco/runtime/program/fd_precompiles.c#L376pub fn verify(
pub fn verify(
current_instruction_data: []const u8,
all_instruction_datas: []const []const u8,
Expand Down

0 comments on commit 7c4e9cb

Please sign in to comment.