Skip to content
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

Kwxm/bls12-381/prototype (PLT-192, PLT-1557, PLT-1554, etc). #5231

Merged
merged 230 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 170 commits
Commits
Show all changes
230 commits
Select commit Hold shift + click to select a range
0020b29
Initial version of BLS pairing builtins
kwxm Dec 13, 2022
7e0de43
WIP
kwxm Dec 13, 2022
3927e15
WIP
kwxm Dec 14, 2022
4ade080
WIP: implementations of GT operations
kwxm Dec 14, 2022
56589d9
Tidy up instances
kwxm Dec 14, 2022
a592101
More-or-less complete implementation for UPLC
kwxm Dec 15, 2022
26b01ec
Initial costing benchmarks for BLS builtins
kwxm Dec 15, 2022
1bb2753
Update benchmarks
kwxm Dec 15, 2022
099ba8c
Update R code
kwxm Dec 16, 2022
bf30d3f
Forgot source files
kwxm Dec 17, 2022
4956ed9
Wrong denotation for GT_mul
kwxm Dec 17, 2022
b1b1b0d
Partial updates to CreateBuiltinCostModel
kwxm Dec 17, 2022
2354352
Merge branch 'master' into kwxm/BLS12_381/prototype
kwxm Dec 17, 2022
a81447e
Fix typos in function names
kwxm Dec 20, 2022
3cfe6db
Update memory models for BLS12_381 builtins
kwxm Dec 20, 2022
51bbb93
Update memory models for BLS12_381 builtins
kwxm Dec 20, 2022
dd3b6d3
WIP
kwxm Dec 20, 2022
48a0f18
Rename BLS (de)serialise -> (un)compress
kwxm Dec 20, 2022
aaa8ec9
Reformat
kwxm Dec 20, 2022
378264f
inline-r workaround; corrections to cost model generation code
kwxm Dec 20, 2022
39d1804
inline-r workaround
kwxm Dec 20, 2022
236b2cb
Update cost model tests for BLS functions
kwxm Dec 21, 2022
c977559
Update benching results and cost model file for BLS
kwxm Dec 21, 2022
0b0048e
Update benching results and cost model file for BLS
kwxm Dec 21, 2022
c17d0b8
Update comment
kwxm Dec 21, 2022
cd8c1c5
Update comment
kwxm Dec 21, 2022
994b478
Add some extra stuff for the benefit of the QuickCheck shrinker
kwxm Dec 21, 2022
fd18459
Add new builtins to plutus-tx
kwxm Dec 22, 2022
1c219e7
Add a few Haskell BLS examples
kwxm Dec 22, 2022
ba59233
WIP
kwxm Dec 28, 2022
1a18aff
Update cabal file
kwxm Jan 5, 2023
7e812f2
Merge master
kwxm Jan 5, 2023
6216a1a
WIP
kwxm Jan 5, 2023
b102e4a
Update BLS throughput benchmarks
kwxm Jan 9, 2023
54087c7
Update cost model for uncompress vs deserialise
kwxm Jan 10, 2023
b337f23
Update BSL benchmark program
kwxm Jan 10, 2023
196f6ac
Merge branch 'master' into kwxm/BLS12_381/prototype
kwxm Feb 10, 2023
a1e1c48
WIP
kwxm Feb 10, 2023
00c48ee
Nix weirdness
kwxm Feb 10, 2023
c276129
Updates after merge
kwxm Feb 11, 2023
5b4a6fb
Add Groth16 verification example
kwxm Feb 16, 2023
d5c5660
Tidying up; get rid of SourceSpans
kwxm Feb 17, 2023
c292e0e
Minor updates
kwxm Feb 17, 2023
aaf2abc
Minor updates
kwxm Feb 17, 2023
e75b9a2
Add proper Criterion benchmarks
kwxm Feb 17, 2023
8de27a1
Tidying up
kwxm Feb 17, 2023
4d43ca9
Moved file
kwxm Feb 17, 2023
fdf8f6f
Forgot cabal file
kwxm Feb 17, 2023
c3bea5f
Fix typo
kwxm Feb 17, 2023
640fdd7
Update comment
kwxm Feb 19, 2023
9365d43
Merge branch 'master' into kwxm/BLS12_381/prototype
kwxm Mar 2, 2023
2ae1323
Fix cabal version constraints
kwxm Mar 2, 2023
4b22cbe
Add missing cases for geqStep
kwxm Mar 2, 2023
fd8f5c0
Update deriving methods
kwxm Mar 3, 2023
05a9f37
WIP: property tests
kwxm Mar 3, 2023
a5c5386
Add Plutus versions of most of the property tests
kwxm Mar 4, 2023
384add7
Tidy up the test code a bit
kwxm Mar 4, 2023
fa3ae48
Use folds for repeated addition; adjust sizes of test inputs
kwxm Mar 4, 2023
b9ba88c
Better folding
kwxm Mar 5, 2023
0f25b60
Update FFI code to new version
kwxm Mar 5, 2023
3a13bbc
Name change: millerLoop -> pairing
kwxm Mar 5, 2023
0b854bd
Reorganise files
kwxm Mar 5, 2023
8041694
Tidy up
kwxm Mar 5, 2023
bc76516
Tidy up
kwxm Mar 5, 2023
5f5f705
More tidying up
kwxm Mar 5, 2023
7ef1fe1
Add comment
kwxm Mar 5, 2023
8d67d51
Abstraction for BLS property tests
kwxm Mar 6, 2023
efd3d1f
Tidying up
kwxm Mar 6, 2023
40b55ae
WIP
Mar 10, 2023
52c8d63
Incorporate Inigo's updates
Mar 10, 2023
0242dd3
Banish Hedgehog
Mar 11, 2023
9a761c2
Add conformance tests for BLS12-381 constants
Mar 12, 2023
b8c0a95
Add BLS12-381 addition conformance tests
Mar 12, 2023
5cd1e06
Add BLS12-381 equality conformance tests
Mar 12, 2023
cbde413
Add BLS12-381 negation conformance tests
Mar 12, 2023
4d4280d
Update comment on cofactors
Mar 12, 2023
086b83a
More conformance tests
Mar 12, 2023
ffe0130
Correct test name
Mar 12, 2023
94176b7
Add BLS12-381 scalar multiplication conformance tests
Mar 12, 2023
a4140ff
Remark about source of data for Groth16 verification example
Mar 12, 2023
d423a47
Add BLS12-381 pairing operation conformance tests
Mar 12, 2023
1963ff8
Update comment in BLS12-381 peoperty tests
Mar 12, 2023
251ef9c
Update comment in BLS12-381 peoperty tests
Mar 12, 2023
9cc3f61
Typo in file name
Mar 12, 2023
e451055
Fix types in bls-sizes executable
Mar 15, 2023
fa4b195
Update names in costing benchmark CSV file
Mar 15, 2023
82237c5
Update names of built-in types and functions in plutus-core
Mar 15, 2023
fc02f08
Update names in conformance tests
Mar 15, 2023
885747e
Update BLS names in plutus-tx-plugin
Mar 15, 2023
edfabad
Remove parser for MlResult; fix Groth16 example
Mar 16, 2023
7771982
Tidy up the Groth16 example
Mar 16, 2023
f1581d5
Merge branch 'master' into kwxm/BLS12_381/prototype
Mar 16, 2023
fa29bc1
Update versions in plutus-benchmark.cabal
Mar 16, 2023
e17860c
applyCode -> unsafeApplyCode after PLT-1552
Mar 16, 2023
80db5e7
Update comment
Mar 16, 2023
2a2659e
Minor formatting updates
Mar 16, 2023
8385fbf
Make plutus-metatheory work with the BLS builtins to some extent
Mar 16, 2023
0370055
Fix incorrect test
Mar 16, 2023
06d78c1
Exclude failing BLS12-381 Agda tests
Mar 16, 2023
575f35d
Exclude failing BLS12-381 Agda tests
Mar 16, 2023
616608c
Add property test for periodicity of scalar multiplication
Mar 16, 2023
73a3278
Minor code rearrangement
Mar 16, 2023
067b444
Import scalarPeriod for tests
Mar 16, 2023
e0bd999
Add more property tests for BLS compression
Mar 17, 2023
3d89f5d
Add conformance tests for BLS scalar mulitplication periodicity
Mar 17, 2023
9affbaa
Add descirptive comments to the BLS conformance tests
Mar 17, 2023
d238be1
Improve printing of known builtins when parser encounters an unknown one
Mar 18, 2023
b8d6690
Reorganise files containing cryptographic functions
Mar 18, 2023
37321b8
Reorganise Crypto files
Mar 18, 2023
8d5080e
hashToCurve -> hashToGroup
Mar 18, 2023
eda34de
Adjust spacing in print-builtin-signatures
Mar 18, 2023
2ffe630
Justification
Mar 18, 2023
aeaec35
Attempt to update to work with iohk-nix version of libblst
Mar 20, 2023
ea3064b
Merge branch 'master' into kwxm/BLS12_381/prototype
Mar 20, 2023
51b244f
Merge Mauro's metatheory updates
Mar 20, 2023
82e659a
Update to new version of BLS bindings
Mar 20, 2023
5acda2d
Update to new version of BLS bindings
Mar 20, 2023
98caea1
Update to new version of BLS bindings
Mar 20, 2023
de4ca72
Merge Mauro's metatheory improvements
Mar 20, 2023
a91d8a3
Still trying to get libblst to work with nix
Mar 20, 2023
9a2e840
More informative BLS names in metatheory
Mar 20, 2023
79bdd9e
Update some comments
Mar 21, 2023
c67dda5
Get BSL sizes from blst
Mar 21, 2023
4521caf
Pairing.pairing -> Pairing.millerLoop
Mar 22, 2023
9c6b92a
Backpatch cost model
Mar 22, 2023
2319ad0
Turn on immediate warnings in R
Mar 22, 2023
311d000
Merge branch 'master' into kwxm/BLS12_381/prototype
Mar 22, 2023
18cf9f6
Restore golden budget results after cost model backpatch
Mar 22, 2023
6585a98
Attempt to fix plututs-ledger-api version tests
Mar 22, 2023
0e1c8c5
Restore comment
Mar 22, 2023
5dfd770
Add comment
Mar 22, 2023
02d6bf3
Fix name of plutus-tx-plugin-tests
Mar 22, 2023
fe45053
Improve comments
Mar 23, 2023
6cc73e3
Extend comments
Mar 23, 2023
6d969d8
Reformat comments
Mar 23, 2023
a89677e
Add comments to ignore cbits
Mar 23, 2023
bf07f99
Comments on costing benchmarks
Mar 23, 2023
f25bceb
Tidying up
Mar 23, 2023
930f578
Add some more comments
Mar 23, 2023
dfbf702
Add changelog entries for BLS12-381 modifications
Mar 23, 2023
5292de0
Remove unnecessary changelog directories from unversioned packages
Mar 24, 2023
2038ca0
Update plutus-core/plutus-core/src/PlutusCore/Parser/Builtin.hs
kwxm Mar 24, 2023
1c182de
Address some PR comments
Mar 24, 2023
b0c8f7f
Crypto -> PlutusCore.Crypto for stuff we've defined
Mar 24, 2023
6916df4
Address some more PR comments
Mar 24, 2023
100f301
Update metatheory for package name change
Mar 24, 2023
8c74593
Update metatheory for package name change
Mar 24, 2023
50d0e93
Merge branch 'master' into kwxm/BLS12_381/prototype
Apr 3, 2023
a520986
Update version numbers in cabal file
Apr 3, 2023
6f0698a
Update BLS branch to work with merged version of BLS bindings in card…
Apr 3, 2023
a71be07
Remove commented-out Haskell code in plutus-metatheory
Apr 3, 2023
483cdcd
Missing cases in metatheory
Apr 3, 2023
a077b9d
Missing cases in metatheory
Apr 3, 2023
ce46a04
Missing cases in metatheory
Apr 3, 2023
1187e3f
Missing cases in metatheory
Apr 3, 2023
9316d19
Missing cases in metatheory
Apr 3, 2023
228de87
Merge branch 'master' into kwxm/BLS12_381/prototype
Apr 11, 2023
17f6a86
Reorder constructors in ledger-api cost model interface
Apr 11, 2023
a345dc7
Update the comments about wrapping BLS12-381 types
Apr 12, 2023
00e0be0
Remove Haskell property tests for BLS12-381 (tested in cardano-base)
Apr 16, 2023
6970cbd
Refactoring
Apr 16, 2023
3a7ed24
Refactoring
Apr 16, 2023
f121dd6
Remove some stuff that was left in accidentally
Apr 16, 2023
8957ee1
Remove empty lines
Apr 16, 2023
d83721a
Merge branch 'master' into kwxm/BLS12_381/prototype
Apr 16, 2023
5fa04f5
Resolve merge problems
Apr 16, 2023
50e5040
Fix comment
Apr 16, 2023
c1191cb
Delete unused boilerplate from changelog entry
Apr 16, 2023
5fc950c
Merge branch 'master' into kwxm/BLS12_381/prototype
Apr 17, 2023
6630d15
Update cabal file
Apr 17, 2023
45ff422
Fix alignment
Apr 17, 2023
16bf911
Address some PR comments
Apr 18, 2023
c9fd616
Address some PR comments
Apr 18, 2023
f2294bc
fromIntegral -> fromSatInt
Apr 18, 2023
18f28a0
GHC.Tick -> GHC.HpcTick
Apr 18, 2023
7f897b8
Trying to get rid of wrong version of Expr.hs in plutus-tx-plugin
Apr 19, 2023
1ac1ae6
Add missing golden files
Apr 19, 2023
332773f
Fix weird results in TypeSynthesis/Golden/Bls12*
Apr 19, 2023
8b0db0b
Try to fix blst
michaelpj Apr 19, 2023
aed2c6c
Merge branch 'master' into kwxm/BLS12_381/prototype
kwxm Apr 29, 2023
ccdac92
Add BLS builtins to metatheory
kwxm Apr 30, 2023
2b1b382
Fix Untyped/CEK.lagda; make Agda conformance tests pass
kwxm Apr 30, 2023
10c5e47
Correct spacing
kwxm Apr 30, 2023
1f93366
Remove some remaining merge conflicts
kwxm Apr 30, 2023
4aee59e
Correct spacing
kwxm Apr 30, 2023
bb5b70b
Correct formatting
kwxm Apr 30, 2023
9f1ccb6
Correct formatting
kwxm Apr 30, 2023
4360585
Some renaming
kwxm Apr 30, 2023
610ee51
Merge branch 'master' into kwxm/BLS12_381/prototype
kwxm May 2, 2023
2bae2c7
Update flake
kwxm May 2, 2023
748b666
Merge branch 'master' into kwxm/BLS12_381/prototype
angerman May 11, 2023
8f9b2d7
Bump plutus-core at el from 1.5 -> 1.6
angerman May 11, 2023
0460f4e
Bump haskell.nix, iohk-nix, CHaP
angerman May 11, 2023
bf30c36
Merge branch 'master' into kwxm/BLS12_381/prototype
angerman May 11, 2023
fa8b323
bump cardano-base
angerman May 12, 2023
d71023e
Fix missing blst symbols.
angerman May 12, 2023
5f136e7
Agda fixes
kwxm May 12, 2023
2920bc5
liftCode -> liftCodeDef
kwxm May 12, 2023
f408615
Remove superlfuous dependencies
kwxm May 12, 2023
fe62965
Add DST argument to hashToGroup builtins
kwxm May 12, 2023
513f4e6
Address a couple of PR comments
kwxm May 14, 2023
eed55b5
bump haskell.nix
angerman May 15, 2023
4f94d7f
Add NOINLINE to listOfSizedByteStrings
kwxm May 15, 2023
946ee31
Address some PR comments
kwxm May 15, 2023
2b9ac9f
Merge branch 'kwxm/BLS12_381/prototype' of github.com:input-output-hk…
kwxm May 15, 2023
af2e99d
Bump haskell.nix
hamishmack May 16, 2023
e5bfc61
Merge branch 'kwxm/BLS12_381/prototype' of github.com:input-output-hk…
kwxm May 16, 2023
2c733ab
Error type for overlong DSTs
kwxm May 16, 2023
85ec23d
Error type for overlong DSTs
kwxm May 16, 2023
b3eb0f3
Merge branch 'kwxm/BLS12_381/prototype' of github.com:input-output-hk…
kwxm May 17, 2023
3a859cc
Stuff about shrinking
kwxm May 19, 2023
3d10333
Make CI happy. x is unused.
angerman May 22, 2023
7daa1d5
plutus windows cross 8.10
angerman May 25, 2023
198816d
Finish incomplete test
kwxm May 26, 2023
0e97668
Typo
kwxm May 26, 2023
2b0f142
Address PR comments
kwxm May 26, 2023
75c8cfe
Fix parser for bls12_381_mlresult type
kwxm May 26, 2023
19563fa
WIP updating things
michaelpj May 26, 2023
651f525
bump iohk-nix
michaelpj May 26, 2023
aa41b37
Improve hash collision tests
kwxm May 26, 2023
310cfe0
Merge branch 'kwxm/BLS12_381/prototype' of github.com:input-output-hk…
kwxm May 26, 2023
57dc567
Update benching.csv to new format to make merge easier
kwxm May 26, 2023
fcc4551
Merge branch 'master' into kwxm/BLS12_381/prototype
kwxm May 30, 2023
9a07a5a
Resolve some remaining conflicts
kwxm May 30, 2023
e88aa44
Update ciJobs.nix
kwxm May 30, 2023
17ff8f3
bump haskell.nix
angerman May 31, 2023
df8f19b
Update nix/cells/automation/ciJobs.nix
angerman May 31, 2023
e67e029
Update nix/cells/automation/ciJobs.nix
angerman May 31, 2023
29a78e3
Update plutus-metatheory/src/Algorithmic/Erasure.lagda
angerman Jun 1, 2023
646b7e6
Merge branch 'master' into kwxm/BLS12_381/prototype
angerman Jun 1, 2023
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
8 changes: 8 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ repository cardano-haskell-packages
c00aae8461a256275598500ea0e187588c35a5d5d7454fb57eac18d9edb86a56
d4a35cd3121aa00d18544bb0ac01c3e1691d618f462c46129271bccf39f7e8ee

-- A temporary source-repository-package so that we get the BLS bindings in cardano-base.
-- FIXME: delete this when the version in CHaP is updated.
Copy link
Contributor

Choose a reason for hiding this comment

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

who's making sure this happens? Inigo?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please get this into CHaP, before we add SRPs again? Chap is under our control. @lehins, I believe you are currenlty in charge of cardano-base or am I mistaken?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we're not going to merge this with a s-r-p. Doing this for a pre-release is fine though.

source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-base
tag: 56a71b150b7ff7fb6d6bf588f3e9d88822c9048c
subdir: cardano-crypto-class

-- See CONTRIBUTING for some Nix commands you will need to run if you
-- update either of these.

Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion nix/cells/plutus/library/make-plutus-project.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ let

inputMap = { "https://input-output-hk.github.io/cardano-haskell-packages" = inputs.CHaP; };
# No source-repository-packages right now
sha256map = { };
sha256map = {
"https://github.com/input-output-hk/cardano-base"."56a71b150b7ff7fb6d6bf588f3e9d88822c9048c" =
"sha256-mTRYnpjNATU8W9th9KTNDairJDnFiq12E58t6evHMD8";
};

# TODO: move this into the cabal.project using the new conditional support?
# Configuration settings needed for cabal configure to work when cross compiling
Expand Down Expand Up @@ -156,6 +159,7 @@ let
cardano-crypto-class.components.library.pkgconfig = lib.mkForce [
[ pkgs.libsodium-vrf pkgs.secp256k1 ]
];
cardano-crypto-class.components.library.libs = lib.mkForce [ pkgs.blst ];
Copy link
Contributor Author

@kwxm kwxm Mar 24, 2023

Choose a reason for hiding this comment

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

This gives you warnings about a missing dynamic library, which is misleading because it's a static library and it isn't missing. I've asked our nix experts and they think there might be something genuinely wrong here. It's not clear how to resolve this: maybe blst needs a pkg-config file somewhere (not something we can do in the plutus repository).

Copy link
Contributor

Choose a reason for hiding this comment

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

This does nothing at the moment, right? Since we're not using the code from cardano-crypto-class?

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, since we're vendoring things into plutus-core, shouldn't this be set for plutus-core?

Copy link
Contributor Author

@kwxm kwxm Mar 24, 2023

Choose a reason for hiding this comment

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

This does nothing at the moment, right? Since we're not using the code from cardano-crypto-class?

No, this is for the libblst.a C library, which needs to be available at link time, like libsodium and the like.

The stuff in cardano-crypto-class is the FFI between Haskell and that library. Maybe (?) we'll not need that linewhen the FFI stuff is merged, but for the time being you can't build this branch without it (unless you have your own copy of libblst.a and set a shell variable to point to it).

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, I think I see what you mean now. It does look as if that line refers to something that doesn't currently exist, but I think that without it you can't build this branch. Could you try building it with and without that line and see what happens? I made that modification after the discussion on Slack.

Copy link
Contributor

Choose a reason for hiding this comment

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

pkg-config logic should be in the most recent iohk-nix.

};
})

Expand Down
61 changes: 61 additions & 0 deletions plutus-benchmark/bls-benchmarks/bench/Bench.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- editorconfig-checker-disable-file
{- | Plutus benchmarks measuring actual execution times of some BSL12-381
operations, mainly intended to give us an idea of what we can do within the
on-chain execution limits. -}
module Main where

import Criterion.Main

import PlutusBenchmark.BLS12_381.Common
import PlutusBenchmark.Common (benchTermCek)
import PlutusTx.Prelude qualified as Tx
import UntypedPlutusCore qualified as UPLC

benchProgCek :: UProg -> Benchmarkable
benchProgCek (UPLC.Program _ _ t) = benchTermCek t

benchHashAndAddG1 :: Integer -> Benchmark
benchHashAndAddG1 n =
let prog = mkHashAndAddG1Script (listOfSizedByteStrings n 4)
in bench (show n) $ benchProgCek prog
effectfully marked this conversation as resolved.
Show resolved Hide resolved

benchHashAndAddG2 :: Integer -> Benchmark
benchHashAndAddG2 n =
let prog = mkHashAndAddG2Script (listOfSizedByteStrings n 4)
in bench (show n) $ benchProgCek prog
effectfully marked this conversation as resolved.
Show resolved Hide resolved

benchUncompressAndAddG1 :: Integer -> Benchmark
benchUncompressAndAddG1 n =
let prog = mkUncompressAndAddG1Script (listOfSizedByteStrings n 4)
in bench (show n) $ benchProgCek prog

benchUncompressAndAddG2 :: Integer -> Benchmark
benchUncompressAndAddG2 n =
let prog = mkUncompressAndAddG2Script (listOfSizedByteStrings n 4)
in bench (show n) $ benchProgCek prog

benchPairing :: Benchmark
benchPairing =
case listOfSizedByteStrings 4 4 of
[b1, b2, b3, b4] ->
let p1 = Tx.bls12_381_G1_hashToGroup $ Tx.toBuiltin b1
p2 = Tx.bls12_381_G2_hashToGroup $ Tx.toBuiltin b2
q1 = Tx.bls12_381_G1_hashToGroup $ Tx.toBuiltin b3
q2 = Tx.bls12_381_G2_hashToGroup $ Tx.toBuiltin b4
prog = mkPairingScript p1 p2 q1 q2
in bench "pairing" $ benchProgCek prog
_ -> error "Unexpected list returned by listOfSizedByteStrings"

benchGroth16Verify :: Benchmark
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should also add some budget tests for these so we can see how much they end up costing deterministically?

benchGroth16Verify = bench "groth16Verify" $ benchProgCek mkGroth16VerifyScript

main :: IO ()
main = do
defaultMain [
bgroup "hashAndAddG1" $ fmap benchHashAndAddG1 [0, 10..150]
, bgroup "hashAndAddG2" $ fmap benchHashAndAddG2 [0, 10..150]
, bgroup "uncompressAndAddG1" $ fmap benchUncompressAndAddG1 [0, 10..150]
, bgroup "uncompressAndAddG2" $ fmap benchUncompressAndAddG2 [0, 10..150]
, benchPairing
, benchGroth16Verify
]
161 changes: 161 additions & 0 deletions plutus-benchmark/bls-benchmarks/exe/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
-- editorconfig-checker-disable-file
{-# LANGUAGE NumericUnderscores #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
Copy link
Contributor

Choose a reason for hiding this comment

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

do w need to turn these off?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do w need to turn these off?

Apparently not. Maybe left over from earlier experiments.


{- | Print out the costs of various test scripts involving the BLS12_381
Copy link
Contributor

Choose a reason for hiding this comment

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

Right, so I think what I'm suggesting is basically to make this into a test suite with budget tests instead. Maybe the output is less easy to read, IDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could also check in the output as a golden file.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could also check in the output as a golden file.

Big 👍 from me on that.

primitives. Most of these work on varying numbers of inputs so that we can
get an idea of what we can do within the on-chain execution limits.
-}
module Main (main)

where

import PlutusBenchmark.BLS12_381.Common

import PlutusCore.Evaluation.Machine.ExBudget (ExBudget (exBudgetCPU, exBudgetMemory))
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults qualified as PLC
import PlutusCore.Evaluation.Machine.ExMemory (ExCPU (..), ExMemory (..))
import PlutusTx.Prelude as Tx hiding (sort, (*))
import UntypedPlutusCore qualified as UPLC
import UntypedPlutusCore.Evaluation.Machine.Cek qualified as Cek

import Data.ByteString qualified as BS
import Flat qualified
import Text.Printf (printf)

import Prelude (Double, IO, Integral, String, fromIntegral, mapM_, show, (*), (/))

-- Protocol parameters (November 2022)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is duplicated from somewhere else, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, from plutus-benchmark/ed25519-throughput/Main.hs. There's a ticket to put these somewhere global, but it hasn't been done yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

grumble


-- | This is the "maximum transaction size". We're just comparing the size of
-- the script with this, so our results may be a little optimistic if the
-- transaction includes other stuff (I'm not sure exactly what "maximum
-- transaction size" means).
max_tx_size :: Integer
max_tx_size = 16384

max_tx_ex_steps :: Integer
max_tx_ex_steps = 10_000_000_000

max_tx_ex_mem :: Integer
max_tx_ex_mem = 14_000_000

-------------------------------- Printing --------------------------------

-- Printing utilities
percentage :: (Integral a, Integral b) => a -> b -> Double
percentage a b =
let a' = fromIntegral a :: Double
b' = fromIntegral b :: Double
in (a'/b' * 100)
effectfully marked this conversation as resolved.
Show resolved Hide resolved

percentTxt :: (Integral a, Integral b) => a -> b -> String
percentTxt a b = printf "(%.1f%%)" (percentage a b)

-- | Evaluate a script and return the CPU and memory costs (according to the cost model)
evaluate :: UProg -> (Integer, Integer)
evaluate (UPLC.Program _ _ prog) =
case Cek.runCekDeBruijn PLC.defaultCekParameters Cek.tallying Cek.noEmitter prog of
(_res, Cek.TallyingSt _ budget, _logs) ->
let ExCPU cpu = exBudgetCPU budget
ExMemory mem = exBudgetMemory budget
in (fromIntegral cpu, fromIntegral mem)

-- | Evaluate a script and print out the serialised size and the CPU and memory
-- usage, both as absolute values and percentages of the maxima specified in the
-- protocol parameters.
printStatistics :: Integer -> UProg -> IO ()
Copy link
Contributor

Choose a reason for hiding this comment

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

Right, this is very similar to ed25519-throughput. Would it be bad to just merge them and have one thing with all the statistics? Or just make them all budget tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

still wondering about the budget tests

printStatistics n script = do
let serialised = Flat.flat (UPLC.UnrestrictedProgram $ toAnonDeBruijnProg script)
size = BS.length serialised
(cpu, mem) = evaluate script
-- BS.writeFile "output" serialised
-- printf "%s\n" $ show $ PP.prettyClassicDebug script
printf " %3s %7d %8s %15d %8s %15d %8s \n"
(if n > 0 then show n else "-")
Copy link
Contributor

Choose a reason for hiding this comment

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

Make it Maybe Integer instead of using -1?

size (percentTxt size max_tx_size)
cpu (percentTxt cpu max_tx_ex_steps)
mem (percentTxt mem max_tx_ex_mem)

------------------------------- Examples ---------------------------------

printCosts_HashAndAddG1 :: Integer -> IO ()
printCosts_HashAndAddG1 n =
let script = mkHashAndAddG1Script (listOfSizedByteStrings n 4)
in printStatistics n script


printCosts_HashAndAddG2 :: Integer -> IO ()
printCosts_HashAndAddG2 n =
let script = mkHashAndAddG2Script (listOfSizedByteStrings n 4)
in printStatistics n script


printCosts_UncompressAndAddG1 :: Integer -> IO ()
printCosts_UncompressAndAddG1 n =
let script = mkUncompressAndAddG1Script (listOfSizedByteStrings n 4)
in printStatistics n script

printCosts_UncompressAndAddG2 :: Integer -> IO ()
printCosts_UncompressAndAddG2 n =
let script = mkUncompressAndAddG2Script (listOfSizedByteStrings n 4)
in printStatistics n script

printCosts_Pairing :: IO ()
printCosts_Pairing = do
let p1 = Tx.bls12_381_G1_hashToGroup $ toBuiltin $ BS.pack [0x23, 0x43, 0x56, 0xf2]
p2 = Tx.bls12_381_G2_hashToGroup $ toBuiltin $ BS.pack [0x10, 0x00, 0xff, 0x88]
q1 = Tx.bls12_381_G1_hashToGroup $ toBuiltin $ BS.pack [0x11, 0x22, 0x33, 0x44]
q2 = Tx.bls12_381_G2_hashToGroup $ toBuiltin $ BS.pack [0xa0, 0xb1, 0xc2, 0xd3]
script = mkPairingScript p1 p2 q1 q2
printStatistics (-1) script

printCosts_Groth16Verify :: IO ()
printCosts_Groth16Verify = do
let script = mkGroth16VerifyScript
printStatistics (-1) script

printHeader :: IO ()
printHeader = do
printf " n script size CPU usage Memory usage\n"
printf " ----------------------------------------------------------------------\n"

main :: IO ()
main = do

printf "Hash n bytestrings onto G1 and add points\n\n"
printHeader
mapM_ printCosts_HashAndAddG1 [0, 10..150]
printf "\n\n"

printf "Hash n bytestrings onto G2 and add points\n\n"
printHeader
mapM_ printCosts_HashAndAddG2 [0, 10..150]
printf "\n\n"

printf "Uncompress n G1 points and add the results\n\n"
printHeader
mapM_ printCosts_UncompressAndAddG1 [0, 10..150]
printf "\n\n"

printf "Uncompress n G2 points and add the results\n\n"
printHeader
mapM_ printCosts_UncompressAndAddG2 [0, 10..150]
printf "\n\n"

printf "Apply pairing to two pairs of points in G1 x G2 and run finalVerify on the results\n\n"
printHeader
printCosts_Pairing
printf "\n\n"

printf "Groth16 verification example\n\n"
printHeader
printCosts_Groth16Verify
printf "\n"

if checkGroth16Verify_Haskell
then printf "Groth16Verify succeeded\n"
else printf "Groth16Verify failed\n"


Loading