Skip to content

Commit

Permalink
feat: add KeccakInstanceDef and EcOpInstanceDef with unit tests (k…
Browse files Browse the repository at this point in the history
…eep-starknet-strange#292)

add KeccakInstanceDef and EcOpInstanceDef with unit tests

Co-authored-by: lanaivina <[email protected]>
  • Loading branch information
tcoratger and lana-shanghai authored Jan 4, 2024
1 parent 996acd7 commit fd67919
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 92 deletions.
64 changes: 32 additions & 32 deletions src/vm/builtins/builtin_runner/keccak.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ pub const KeccakBuiltinRunner = struct {
.base = 0,
.n_input_cells = @as(
u32,
@intCast(instance_def._state_rep.items.len),
@intCast(instance_def.state_rep.items.len),
),
.cells_per_instance = instance_def.cells_per_builtin(),
.cells_per_instance = instance_def.cellsPerBuiltin(),
.stop_ptr = null,
.included = included,
.state_rep = instance_def._state_rep,
.instances_per_component = instance_def._instance_per_component,
.state_rep = instance_def.state_rep,
.instances_per_component = instance_def.instance_per_component,
.cache = AutoHashMap(Relocatable, Felt252).init(allocator),
};
}
Expand Down Expand Up @@ -482,7 +482,7 @@ pub const KeccakBuiltinRunner = struct {
};

test "KeccakBuiltinRunner: initialStack should return an empty array list if included is false" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -500,7 +500,7 @@ test "KeccakBuiltinRunner: initialStack should return an empty array list if inc
}

test "KeccakBuiltinRunner: initialStack should return an a proper array list if included is true" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -524,7 +524,7 @@ test "KeccakBuiltinRunner: initialStack should return an a proper array list if
}

test "KeccakBuiltinRunner: initSegments should modify base field of Keccak built in" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -542,7 +542,7 @@ test "KeccakBuiltinRunner: initSegments should modify base field of Keccak built
}

test "KeccakBuiltinRunner: getUsedCells should return memory error if segment used size is null" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -558,7 +558,7 @@ test "KeccakBuiltinRunner: getUsedCells should return memory error if segment us
}

test "KeccakBuiltinRunner: getUsedCells should return the number of used cells" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -578,7 +578,7 @@ test "KeccakBuiltinRunner: getUsedCells should return the number of used cells"
}

test "KeccakBuiltinRunner: getMemorySegmentAddresses should return base and stop pointer" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -596,7 +596,7 @@ test "KeccakBuiltinRunner: getMemorySegmentAddresses should return base and stop
}

test "KeccakBuiltinRunner: getUsedInstances should return memory error if segment used size is null" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -612,7 +612,7 @@ test "KeccakBuiltinRunner: getUsedInstances should return memory error if segmen
}

test "KeccakBuiltinRunner: getUsedInstances should return the number of used instances" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -629,7 +629,7 @@ test "KeccakBuiltinRunner: getUsedInstances should return the number of used ins
}

test "KeccakBuiltinRunner: getMemoryAccesses should return memory error if segment used size is null" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -653,7 +653,7 @@ test "KeccakBuiltinRunner: getMemoryAccesses should return memory error if segme
}

test "KeccakBuiltinRunner: getMemoryAccesses should return the memory accesses" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -757,7 +757,7 @@ test "KeccakBuiltinRunner: keccakF" {
}

test "KeccakBuiltinRunner: finalStack should return relocatable pointer if not included" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -780,7 +780,7 @@ test "KeccakBuiltinRunner: finalStack should return relocatable pointer if not i
}

test "KeccakBuiltinRunner: finalStack should return NoStopPointer error if pointer offset is 0" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -799,7 +799,7 @@ test "KeccakBuiltinRunner: finalStack should return NoStopPointer error if point
}

test "KeccakBuiltinRunner: finalStack should return NoStopPointer error if no data in memory at the given stop pointer address" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand All @@ -818,7 +818,7 @@ test "KeccakBuiltinRunner: finalStack should return NoStopPointer error if no da
}

test "KeccakBuiltinRunner: finalStack should return TypeMismatchNotRelocatable error if data in memory at the given stop pointer address is not Relocatable" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -851,7 +851,7 @@ test "KeccakBuiltinRunner: finalStack should return TypeMismatchNotRelocatable e
}

test "KeccakBuiltinRunner: finalStack should return InvalidStopPointerIndex error if segment index of stop pointer is not KeccakBuiltinRunner base" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -888,7 +888,7 @@ test "KeccakBuiltinRunner: finalStack should return InvalidStopPointerIndex erro
}

test "KeccakBuiltinRunner: finalStack should return InvalidStopPointer error if stop pointer offset is not cells used" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -925,7 +925,7 @@ test "KeccakBuiltinRunner: finalStack should return InvalidStopPointer error if
}

test "KeccakBuiltinRunner: finalStack should return stop pointer address and update stop_ptr" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -967,7 +967,7 @@ test "KeccakBuiltinRunner: finalStack should return stop pointer address and upd
}

test "KeccakBuiltinRunner: deduceMemoryCell memory valid" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -1016,7 +1016,7 @@ test "KeccakBuiltinRunner: deduceMemoryCell memory valid" {
}

test "KeccakBuiltinRunner: deduceMemoryCell non relocatable address should return null" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -1050,7 +1050,7 @@ test "KeccakBuiltinRunner: deduceMemoryCell non relocatable address should retur
}

test "KeccakBuiltinRunner: deduceMemoryCell offset less than input cell length should return null" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -1080,7 +1080,7 @@ test "KeccakBuiltinRunner: deduceMemoryCell offset less than input cell length s
}

test "KeccakBuiltinRunner: deduceMemoryCell memory cell expected integer" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -1111,7 +1111,7 @@ test "KeccakBuiltinRunner: deduceMemoryCell memory cell expected integer" {
}

test "KeccakBuiltinRunner: deduceMemoryCell missing input cells" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -1142,7 +1142,7 @@ test "KeccakBuiltinRunner: deduceMemoryCell missing input cells" {
}

test "KeccakBuiltinRunner: deduceMemoryCell input cell" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down Expand Up @@ -1173,7 +1173,7 @@ test "KeccakBuiltinRunner: deduceMemoryCell input cell" {
}

test "KeccakBuiltinRunner: deduceMemoryCell get memory error" {
var keccak_instance_def = try KeccakInstanceDef.default(std.testing.allocator);
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);

var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
Expand Down Expand Up @@ -1202,10 +1202,10 @@ test "KeccakBuiltinRunner: deduceMemoryCell get memory error" {
}

test "KeccakBuiltinRunner: deduceMemoryCell memory int larger than bits" {
var _state_rep = ArrayList(u32).init(std.testing.allocator);
defer _state_rep.deinit();
try _state_rep.appendNTimes(1, 8);
var keccak_instance_def = KeccakInstanceDef.init(2048, _state_rep);
var state_rep = ArrayList(u32).init(std.testing.allocator);
defer state_rep.deinit();
try state_rep.appendNTimes(1, 8);
var keccak_instance_def = KeccakInstanceDef.init(2048, state_rep);
var keccak_builtin = KeccakBuiltinRunner.init(
std.testing.allocator,
&keccak_instance_def,
Expand Down
8 changes: 4 additions & 4 deletions src/vm/types/builtins_instance_def.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub const BuiltinsInstanceDef = struct {
.range_check = RangeCheckInstanceDef.init(),
.ecdsa = EcdsaInstanceDef.init(2048),
.bitwise = BitwiseInstanceDef.init(16),
.ec_op = EcOpInstanceDef.from(1024),
.ec_op = EcOpInstanceDef.init(1024),
.keccak = KeccakInstanceDef.init(2048, state_rep_keccak),
.poseidon = PoseidonInstanceDef.init(256),
};
Expand All @@ -111,7 +111,7 @@ pub const BuiltinsInstanceDef = struct {
.range_check = RangeCheckInstanceDef.from(null, 8),
.ecdsa = EcdsaInstanceDef.init(null),
.bitwise = BitwiseInstanceDef.init(null),
.ec_op = EcOpInstanceDef.from(null),
.ec_op = EcOpInstanceDef.init(null),
.keccak = null,
.poseidon = null,
};
Expand Down Expand Up @@ -253,12 +253,12 @@ test "BuiltinsInstanceDef: builtins all Cairo" {
);
try expectEqual(
@as(u32, 16),
actual.keccak.?._instance_per_component,
actual.keccak.?.instance_per_component,
);
try expectEqualSlices(
u32,
state_rep_keccak_expected.items,
actual.keccak.?._state_rep.items,
actual.keccak.?.state_rep.items,
);

try expectEqual(
Expand Down
92 changes: 83 additions & 9 deletions src/vm/types/ec_op_instance_def.zig
Original file line number Diff line number Diff line change
@@ -1,33 +1,107 @@
/// Each EC operation P + m * Q = R contains 7 cells:
/// P_x, P_y, Q_x, Q_y, m, R_x, R_y.
const std = @import("std");
const expectEqual = std.testing.expectEqual;

/// Represents the number of cells in each EC operation P + m * Q = R, which is 7 cells.
/// These cells are: P_x, P_y, Q_x, Q_y, m, R_x, R_y.
pub const CELLS_PER_EC_OP: u32 = 7;
/// Number of input cells per EC operation

/// Represents the number of input cells per EC operation, which is 5 cells.
/// These cells are: P_x, P_y, Q_x, Q_y, m.
pub const INPUT_CELLS_PER_EC_OP: u32 = 5;

/// Represents a EC Operation Instance Definition.
/// Represents an EC Operation Instance Definition.
pub const EcOpInstanceDef = struct {
const Self = @This();

/// Ratio
/// The ratio associated with the instance.
ratio: ?u32,
/// Size of coefficient.
/// The height of the scalar in bits.
scalar_height: u32,
/// Scalar bits
/// The number of bits in the scalar.
scalar_bits: u32,

pub fn init() Self {
/// Initializes an instance with default values.
///
/// Returns:
/// An instance of EC Operation Definition with default values.
pub fn initDefault() Self {
return .{
.ratio = 256,
.scalar_height = 256,
.scalar_bits = 252,
};
}

pub fn from(ratio: ?u32) Self {
/// Initializes an instance with the specified ratio.
///
/// Arguments:
/// - `ratio`: The ratio to associate with the instance.
///
/// Returns:
/// An instance of EC Operation Definition with the specified ratio.
pub fn init(ratio: ?u32) Self {
return .{
.ratio = ratio,
.scalar_height = 256,
.scalar_bits = 252,
};
}

/// Retrieves the number of cells per built-in EC operation.
///
/// Arguments:
/// - `self`: Pointer to the EC Operation Instance Definition.
///
/// Returns:
/// The number of cells per built-in EC operation.
pub fn cellsPerBuiltin(_: *const Self) u32 {
return CELLS_PER_EC_OP;
}

/// Retrieves the number of range check units per built-in EC operation.
///
/// Arguments:
/// - `self`: Pointer to the EC Operation Instance Definition.
///
/// Returns:
/// The number of range check units per built-in EC operation.
pub fn rangeCheckUnitsPerBuiltin(_: *const Self) u32 {
return 0;
}
};

test "EcOpInstanceDef: init function should return an EcOp instance def with provided ratio" {
try expectEqual(
EcOpInstanceDef{
.ratio = 8,
.scalar_height = 256,
.scalar_bits = 252,
},
EcOpInstanceDef.init(8),
);
}

test "EcOpInstanceDef: initDefault function should return the default EcOp instance def" {
try expectEqual(
EcOpInstanceDef{
.ratio = 256,
.scalar_height = 256,
.scalar_bits = 252,
},
EcOpInstanceDef.initDefault(),
);
}

test "EcOpInstanceDef: cellsPerBuiltin function should return CELLS_PER_EC_OP" {
try expectEqual(
CELLS_PER_EC_OP,
EcOpInstanceDef.initDefault().cellsPerBuiltin(),
);
}

test "EcOpInstanceDef: rangeCheckUnitsPerBuiltin function should return 0" {
try expectEqual(
@as(u32, 0),
EcOpInstanceDef.initDefault().rangeCheckUnitsPerBuiltin(),
);
}
Loading

0 comments on commit fd67919

Please sign in to comment.