Continue working on new builtin cost model updates

This commit is contained in:
microproofs 2024-11-25 10:03:59 +07:00
parent befbb6ec18
commit 4839273449
No known key found for this signature in database
GPG Key ID: 14F93C84DE6AFD17
2 changed files with 103 additions and 93 deletions

View File

@ -344,12 +344,12 @@ pub struct BuiltinCosts {
// bitwise // bitwise
integer_to_byte_string: CostingFun<ThreeArguments>, integer_to_byte_string: CostingFun<ThreeArguments>,
byte_string_to_integer: CostingFun<TwoArguments>, byte_string_to_integer: CostingFun<TwoArguments>,
and_byte_string: CostingFun<TwoArguments>, and_byte_string: CostingFun<ThreeArguments>,
or_byte_string: CostingFun<TwoArguments>, or_byte_string: CostingFun<ThreeArguments>,
xor_byte_string: CostingFun<TwoArguments>, xor_byte_string: CostingFun<ThreeArguments>,
complement_byte_string: CostingFun<OneArgument>, complement_byte_string: CostingFun<OneArgument>,
read_bit: CostingFun<TwoArguments>, read_bit: CostingFun<TwoArguments>,
write_bits: CostingFun<TwoArguments>, write_bits: CostingFun<ThreeArguments>,
replicate_byte: CostingFun<TwoArguments>, replicate_byte: CostingFun<TwoArguments>,
shift_byte_string: CostingFun<TwoArguments>, shift_byte_string: CostingFun<TwoArguments>,
rotate_byte_string: CostingFun<TwoArguments>, rotate_byte_string: CostingFun<TwoArguments>,
@ -803,16 +803,16 @@ impl BuiltinCosts {
mem: TwoArguments::ConstantCost(30000000000), mem: TwoArguments::ConstantCost(30000000000),
}, },
and_byte_string: CostingFun { and_byte_string: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
or_byte_string: CostingFun { or_byte_string: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
xor_byte_string: CostingFun { xor_byte_string: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
complement_byte_string: CostingFun { complement_byte_string: CostingFun {
cpu: OneArgument::ConstantCost(30000000000), cpu: OneArgument::ConstantCost(30000000000),
@ -823,8 +823,8 @@ impl BuiltinCosts {
mem: TwoArguments::ConstantCost(30000000000), mem: TwoArguments::ConstantCost(30000000000),
}, },
write_bits: CostingFun { write_bits: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
replicate_byte: CostingFun { replicate_byte: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: TwoArguments::ConstantCost(30000000000),
@ -1297,16 +1297,16 @@ impl BuiltinCosts {
mem: TwoArguments::ConstantCost(30000000000), mem: TwoArguments::ConstantCost(30000000000),
}, },
and_byte_string: CostingFun { and_byte_string: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
or_byte_string: CostingFun { or_byte_string: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
xor_byte_string: CostingFun { xor_byte_string: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
complement_byte_string: CostingFun { complement_byte_string: CostingFun {
cpu: OneArgument::ConstantCost(30000000000), cpu: OneArgument::ConstantCost(30000000000),
@ -1317,8 +1317,8 @@ impl BuiltinCosts {
mem: TwoArguments::ConstantCost(30000000000), mem: TwoArguments::ConstantCost(30000000000),
}, },
write_bits: CostingFun { write_bits: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
replicate_byte: CostingFun { replicate_byte: CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: TwoArguments::ConstantCost(30000000000),
@ -1841,34 +1841,34 @@ impl BuiltinCosts {
}), }),
}, },
and_byte_string: CostingFun { and_byte_string: CostingFun {
cpu: TwoArguments::LinearInXAndY(TwoVariableLinearSize { cpu: ThreeArguments::LinearInYandZ(TwoVariableLinearSize {
intercept: 100181, intercept: 100181,
slope1: 726, slope1: 726,
slope2: 719, slope2: 719,
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: ThreeArguments::LinearInMaxYZ(LinearSize {
intercept: 0, intercept: 0,
slope: 1, slope: 1,
}), }),
}, },
or_byte_string: CostingFun { or_byte_string: CostingFun {
cpu: TwoArguments::LinearInXAndY(TwoVariableLinearSize { cpu: ThreeArguments::LinearInYandZ(TwoVariableLinearSize {
intercept: 100181, intercept: 100181,
slope1: 726, slope1: 726,
slope2: 719, slope2: 719,
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: ThreeArguments::LinearInMaxYZ(LinearSize {
intercept: 0, intercept: 0,
slope: 1, slope: 1,
}), }),
}, },
xor_byte_string: CostingFun { xor_byte_string: CostingFun {
cpu: TwoArguments::LinearInXAndY(TwoVariableLinearSize { cpu: ThreeArguments::LinearInYandZ(TwoVariableLinearSize {
intercept: 100181, intercept: 100181,
slope1: 726, slope1: 726,
slope2: 719, slope2: 719,
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: ThreeArguments::LinearInMaxYZ(LinearSize {
intercept: 0, intercept: 0,
slope: 1, slope: 1,
}), }),
@ -1888,41 +1888,41 @@ impl BuiltinCosts {
mem: TwoArguments::ConstantCost(1), mem: TwoArguments::ConstantCost(1),
}, },
write_bits: CostingFun { write_bits: CostingFun {
cpu: TwoArguments::LinearInY(LinearSize { cpu: ThreeArguments::LinearInY(LinearSize {
intercept: 281145, intercept: 281145,
slope: 18848, slope: 18848,
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: ThreeArguments::LinearInX(LinearSize {
intercept: 0, intercept: 0,
slope: 1, slope: 1,
}), }),
}, },
replicate_byte: CostingFun { replicate_byte: CostingFun {
cpu: TwoArguments::LinearInY(LinearSize { cpu: TwoArguments::LinearInX(LinearSize {
intercept: 180194, intercept: 180194,
slope: 159, slope: 159,
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: TwoArguments::LinearInX(LinearSize {
intercept: 1, intercept: 1,
slope: 1, slope: 1,
}), }),
}, },
shift_byte_string: CostingFun { shift_byte_string: CostingFun {
cpu: TwoArguments::LinearInY(LinearSize { cpu: TwoArguments::LinearInX(LinearSize {
intercept: 158519, intercept: 158519,
slope: 8942, slope: 8942,
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: TwoArguments::LinearInX(LinearSize {
intercept: 0, intercept: 0,
slope: 1, slope: 1,
}), }),
}, },
rotate_byte_string: CostingFun { rotate_byte_string: CostingFun {
cpu: TwoArguments::LinearInY(LinearSize { cpu: TwoArguments::LinearInX(LinearSize {
intercept: 159378, intercept: 159378,
slope: 8813, slope: 8813,
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: TwoArguments::LinearInX(LinearSize {
intercept: 0, intercept: 0,
slope: 1, slope: 1,
}), }),
@ -2567,34 +2567,40 @@ impl BuiltinCosts {
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()), .cost(args[0].to_ex_mem(), args[1].to_ex_mem()),
}, },
DefaultFunction::AndByteString => ExBudget { DefaultFunction::AndByteString => ExBudget {
mem: self mem: self.and_byte_string.mem.cost(
.and_byte_string args[0].to_ex_mem(),
.mem args[1].to_ex_mem(),
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()), args[2].to_ex_mem(),
cpu: self ),
.and_byte_string cpu: self.and_byte_string.cpu.cost(
.cpu args[0].to_ex_mem(),
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()), args[1].to_ex_mem(),
args[2].to_ex_mem(),
),
}, },
DefaultFunction::OrByteString => ExBudget { DefaultFunction::OrByteString => ExBudget {
mem: self mem: self.or_byte_string.mem.cost(
.or_byte_string args[0].to_ex_mem(),
.mem args[1].to_ex_mem(),
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()), args[2].to_ex_mem(),
cpu: self ),
.or_byte_string cpu: self.or_byte_string.cpu.cost(
.cpu args[0].to_ex_mem(),
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()), args[1].to_ex_mem(),
args[2].to_ex_mem(),
),
}, },
DefaultFunction::XorByteString => ExBudget { DefaultFunction::XorByteString => ExBudget {
mem: self mem: self.xor_byte_string.mem.cost(
.xor_byte_string args[0].to_ex_mem(),
.mem args[1].to_ex_mem(),
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()), args[2].to_ex_mem(),
cpu: self ),
.xor_byte_string cpu: self.xor_byte_string.cpu.cost(
.cpu args[0].to_ex_mem(),
.cost(args[0].to_ex_mem(), args[1].to_ex_mem()), args[1].to_ex_mem(),
args[2].to_ex_mem(),
),
}, },
DefaultFunction::ComplementByteString => ExBudget { DefaultFunction::ComplementByteString => ExBudget {
mem: self.complement_byte_string.mem.cost(args[0].to_ex_mem()), mem: self.complement_byte_string.mem.cost(args[0].to_ex_mem()),
@ -4739,11 +4745,11 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
}, },
and_byte_string: match version { and_byte_string: match version {
Language::PlutusV1 | Language::PlutusV2 => CostingFun { Language::PlutusV1 | Language::PlutusV2 => CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
Language::PlutusV3 => CostingFun { Language::PlutusV3 => CostingFun {
cpu: TwoArguments::LinearInXAndY(TwoVariableLinearSize { cpu: ThreeArguments::LinearInYandZ(TwoVariableLinearSize {
intercept: *cost_map intercept: *cost_map
.get("andByteString-cpu-arguments-intercept") .get("andByteString-cpu-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4754,7 +4760,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
.get("andByteString-cpu-arguments-slope2") .get("andByteString-cpu-arguments-slope2")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: ThreeArguments::LinearInMaxYZ(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("andByteString-memory-arguments-intercept") .get("andByteString-memory-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4766,11 +4772,11 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
}, },
or_byte_string: match version { or_byte_string: match version {
Language::PlutusV1 | Language::PlutusV2 => CostingFun { Language::PlutusV1 | Language::PlutusV2 => CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
Language::PlutusV3 => CostingFun { Language::PlutusV3 => CostingFun {
cpu: TwoArguments::LinearInXAndY(TwoVariableLinearSize { cpu: ThreeArguments::LinearInYandZ(TwoVariableLinearSize {
intercept: *cost_map intercept: *cost_map
.get("orByteString-cpu-arguments-intercept") .get("orByteString-cpu-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4781,7 +4787,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
.get("orByteString-cpu-arguments-slope2") .get("orByteString-cpu-arguments-slope2")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: ThreeArguments::LinearInMaxYZ(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("orByteString-memory-arguments-intercept") .get("orByteString-memory-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4793,11 +4799,11 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
}, },
xor_byte_string: match version { xor_byte_string: match version {
Language::PlutusV1 | Language::PlutusV2 => CostingFun { Language::PlutusV1 | Language::PlutusV2 => CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
Language::PlutusV3 => CostingFun { Language::PlutusV3 => CostingFun {
cpu: TwoArguments::LinearInXAndY(TwoVariableLinearSize { cpu: ThreeArguments::LinearInYandZ(TwoVariableLinearSize {
intercept: *cost_map intercept: *cost_map
.get("xorByteString-cpu-arguments-intercept") .get("xorByteString-cpu-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4808,7 +4814,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
.get("xorByteString-cpu-arguments-slope2") .get("xorByteString-cpu-arguments-slope2")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: ThreeArguments::LinearInMaxYZ(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("xorByteString-memory-arguments-intercept") .get("xorByteString-memory-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4862,11 +4868,11 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
}, },
write_bits: match version { write_bits: match version {
Language::PlutusV1 | Language::PlutusV2 => CostingFun { Language::PlutusV1 | Language::PlutusV2 => CostingFun {
cpu: TwoArguments::ConstantCost(30000000000), cpu: ThreeArguments::ConstantCost(30000000000),
mem: TwoArguments::ConstantCost(30000000000), mem: ThreeArguments::ConstantCost(30000000000),
}, },
Language::PlutusV3 => CostingFun { Language::PlutusV3 => CostingFun {
cpu: TwoArguments::LinearInY(LinearSize { cpu: ThreeArguments::LinearInY(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("writeBits-cpu-arguments-intercept") .get("writeBits-cpu-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4874,7 +4880,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
.get("writeBits-cpu-arguments-slope") .get("writeBits-cpu-arguments-slope")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: ThreeArguments::LinearInX(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("writeBits-memory-arguments-intercept") .get("writeBits-memory-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4890,7 +4896,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
mem: TwoArguments::ConstantCost(30000000000), mem: TwoArguments::ConstantCost(30000000000),
}, },
Language::PlutusV3 => CostingFun { Language::PlutusV3 => CostingFun {
cpu: TwoArguments::LinearInY(LinearSize { cpu: TwoArguments::LinearInX(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("replicateByte-cpu-arguments-intercept") .get("replicateByte-cpu-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4898,7 +4904,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
.get("replicateByte-cpu-arguments-slope") .get("replicateByte-cpu-arguments-slope")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: TwoArguments::LinearInX(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("replicateByte-memory-arguments-intercept") .get("replicateByte-memory-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4914,7 +4920,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
mem: TwoArguments::ConstantCost(30000000000), mem: TwoArguments::ConstantCost(30000000000),
}, },
Language::PlutusV3 => CostingFun { Language::PlutusV3 => CostingFun {
cpu: TwoArguments::LinearInY(LinearSize { cpu: TwoArguments::LinearInX(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("shiftByteString-cpu-arguments-intercept") .get("shiftByteString-cpu-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4922,7 +4928,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
.get("shiftByteString-cpu-arguments-slope") .get("shiftByteString-cpu-arguments-slope")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: TwoArguments::LinearInX(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("shiftByteString-memory-arguments-intercept") .get("shiftByteString-memory-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4938,7 +4944,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
mem: TwoArguments::ConstantCost(30000000000), mem: TwoArguments::ConstantCost(30000000000),
}, },
Language::PlutusV3 => CostingFun { Language::PlutusV3 => CostingFun {
cpu: TwoArguments::LinearInY(LinearSize { cpu: TwoArguments::LinearInX(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("rotateByteString-cpu-arguments-intercept") .get("rotateByteString-cpu-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -4946,7 +4952,7 @@ pub fn initialize_cost_model(version: &Language, costs: &[i64]) -> CostModel {
.get("rotateByteString-cpu-arguments-slope") .get("rotateByteString-cpu-arguments-slope")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
}), }),
mem: TwoArguments::LinearInY(LinearSize { mem: TwoArguments::LinearInX(LinearSize {
intercept: *cost_map intercept: *cost_map
.get("rotateByteString-memory-arguments-intercept") .get("rotateByteString-memory-arguments-intercept")
.unwrap_or(&30000000000), .unwrap_or(&30000000000),
@ -5126,6 +5132,8 @@ pub enum ThreeArguments {
LinearInZ(LinearSize), LinearInZ(LinearSize),
QuadraticInZ(QuadraticFunction), QuadraticInZ(QuadraticFunction),
LiteralInYorLinearInZ(LinearSize), LiteralInYorLinearInZ(LinearSize),
LinearInMaxYZ(LinearSize),
LinearInYandZ(TwoVariableLinearSize),
} }
impl ThreeArguments { impl ThreeArguments {
@ -5144,6 +5152,8 @@ impl ThreeArguments {
y y
} }
} }
ThreeArguments::LinearInMaxYZ(linear_size) => todo!(),
ThreeArguments::LinearInYandZ(two_variable_linear_size) => todo!(),
} }
} }
} }

View File

@ -273,19 +273,19 @@ impl DefaultFunction {
DefaultFunction::Bls12_381_FinalVerify => 2, DefaultFunction::Bls12_381_FinalVerify => 2,
DefaultFunction::IntegerToByteString => 3, DefaultFunction::IntegerToByteString => 3,
DefaultFunction::ByteStringToInteger => 2, DefaultFunction::ByteStringToInteger => 2,
DefaultFunction::AndByteString => 2, DefaultFunction::AndByteString => 3,
DefaultFunction::OrByteString => 2, DefaultFunction::OrByteString => 3,
DefaultFunction::XorByteString => 2, DefaultFunction::XorByteString => 3,
DefaultFunction::ComplementByteString => todo!(), DefaultFunction::ComplementByteString => 1,
DefaultFunction::ReadBit => todo!(), DefaultFunction::ReadBit => 2,
DefaultFunction::WriteBits => todo!(), DefaultFunction::WriteBits => 3,
DefaultFunction::ReplicateByte => todo!(), DefaultFunction::ReplicateByte => 2,
DefaultFunction::ShiftByteString => todo!(), DefaultFunction::ShiftByteString => 2,
DefaultFunction::RotateByteString => todo!(), DefaultFunction::RotateByteString => 2,
DefaultFunction::CountSetBits => todo!(), DefaultFunction::CountSetBits => 1,
DefaultFunction::FindFirstSetBit => todo!(), DefaultFunction::FindFirstSetBit => 1,
DefaultFunction::Ripemd_160 => todo!(), DefaultFunction::Ripemd_160 => 1,
DefaultFunction::ExpModInteger => todo!(), DefaultFunction::ExpModInteger => 3,
} }
} }