Skip to content

Conversation

@kwxm
Copy link
Contributor

@kwxm kwxm commented May 6, 2025

Fixes https://github.com/IntersectMBO/plutus-private/issues/1448
Fixes https://github.com/IntersectMBO/plutus-private/issues/1554

This adds costing for expModInteger. Here are the costs of the builtin with inputs of various sizes, as a percentage of the maximum execution budget. For cryptographic purposes you might go up to about 512 bits, so it doesn't look as if it's prohibitively expensive.

Size in bytes Size in bits Percentage of maximum budget
16 128 0.5%
32 256 0.11%
64 512 0.49%
128 1024 3.07%
256 2048 21%

@kwxm kwxm added Builtins Crypto Costing Anything relating to costs, fees, gas, etc. labels May 6, 2025
@kwxm kwxm requested review from effectfully and zliu41 May 6, 2025 09:37
@kwxm kwxm temporarily deployed to github-pages May 6, 2025 09:37 — with GitHub Actions Inactive
, paramFindFirstSetBit = Id $ ModelOneArgumentConstantCost 1
, paramRipemd_160 = Id $ hashMemModel Hash.ripemd_160
, paramExpModInteger = Id $ ModelThreeArgumentsConstantCost 100000000000 -- FIXME: stub
, paramExpModInteger = Id $ ModelThreeArgumentsLinearInZ identityFunction
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait! This is the size in bytes, not words, so it'll be 8 times bigger than it should be. The size of the result will be the same as the size of the modulus, but we're using a non-standard measure here.

data ExpModCostingFunction = ExpModCostingFunction
{ coefficient00 :: Coefficient00
, coefficient11 :: Coefficient11
, coefficient12 :: Coefficient12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed so specific that it'd be pointless to give it a more general name. We could have made it a general cubic function in two variables, but that would have ten coefficients and we'd only be using three.

{-# INLINE evaluateExpModCostingFunction #-}

-- | s * (x - y) + I
{- In principle we could use ModelConstantOrOneArgument here, but that would
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


{- | Calculate a 'CostingInteger' for the size of the given 'Integer', measured in
8-bit bytes. -}
memoryUsageBytes :: Integer -> CostingInteger
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because using the standard measure would give very coarse-grained results and this is much more accurate. Now I'm worried that it's jsut adding more complexity. Maybe we should go for inaccurate but simple.

@kwxm kwxm temporarily deployed to github-pages May 6, 2025 09:49 — with GitHub Actions Inactive
b = sizeOf y
c = sizeOf z
in c00 + c11 * b * c + c12 * b * c * c
-- ^ THIS IS INCOMPLETE: the real costing function branches if a > 5*c; however we measure
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fudged this a bit to get the PR open. I'll come back and improve it a bit later.

@kwxm kwxm temporarily deployed to github-pages May 6, 2025 09:54 — with GitHub Actions Inactive
(Coefficient00 c00) (Coefficient11 c11) (Coefficient12 c12))
aa ee mm = if aa <= 5*mm
then cost0
else cost0 + (cost0 `dividedBy` 2)
Copy link
Contributor Author

@kwxm kwxm May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ie, 1.5 times the cost. It seems a bit risky to multiply saturated integers by fractions though.

I'm a bit worried about this being hard-coded since it's not easy to adjust and it'll have to be reproduced in eg the metatheory. Maybe the penalty factor should go in the cost model parameters along with the cofficients.

@kwxm kwxm temporarily deployed to github-pages May 6, 2025 10:06 — with GitHub Actions Inactive
Copy link
Member

@zliu41 zliu41 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks sensible!

evaluateExpModCostingFunction
(ExpModCostingFunction
(Coefficient00 c00) (Coefficient11 c11) (Coefficient12 c12))
aa ee mm = if aa <= 5*mm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was aa <= mm - how did you arrive at 5*mm?

Copy link
Contributor Author

@kwxm kwxm May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was aa <= mm - how did you arrive at 5*mm?

I thought I'd be generous and allow callers to go a little bit over the limit: if you're calculating (ab)^e mod m for example, ab might be close to twice the size of m. I'm thinking that I should probably just make it aa <= mm though.

-> BuiltinResult Natural
expModIntegerDenotation
(IntegerCostedByNumBytes a)
(IntegerCostedByNumBytes b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b -> e for consistency?

@kwxm kwxm temporarily deployed to github-pages May 6, 2025 21:47 — with GitHub Actions Inactive
@kwxm kwxm added the No Changelog Required Add this to skip the Changelog Check label May 6, 2025
@kwxm
Copy link
Contributor Author

kwxm commented May 8, 2025

I'm closing this in favour of #7080.

@kwxm kwxm closed this May 8, 2025
@kwxm kwxm deleted the kwxm/costing/expModInteger-2 branch May 8, 2025 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Builtins Costing Anything relating to costs, fees, gas, etc. Crypto No Changelog Required Add this to skip the Changelog Check

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants