-
Notifications
You must be signed in to change notification settings - Fork 8
Group elements generator #608
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
Merged
Merged
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1aa5496
Add group-elements-generator exe
vlasin daf19be
Add BLS G2 support
vlasin 6fce43c
Add precomputed group elements
vlasin 57f5b19
Remove the precomputed group elements
vlasin ef19bc3
Update symbolic-base.cabal
vlasin 62ff74a
Update GroupElementsGenerator.hs
vlasin e0c14f9
stylish-haskell auto-commit
vlasin ce7eced
Fix bug in `getSecretParams`
vlasin c041690
Merge branch 'vlasin-group-elements-generator' of https://github.com/…
vlasin 26d8489
Fix generator and setup elements
vlasin 51599b5
Update CRS for `UtxoAccumulator`
vlasin ed8d810
stylish-haskell auto-commit
vlasin ee0c3c9
Add JSON instances
vlasin ed14089
Merge branch 'vlasin-group-elements-generator' of https://github.com/…
vlasin c76289a
stylish-haskell auto-commit
vlasin 35499de
Fix newly introduced Plonkup setup bug
vlasin 1da3432
Update UtxoAccumulator.hs
vlasin 863be46
Fix bug
vlasin 3c6e2fe
Update UtxoAccumulator.hs
vlasin 395f4f7
Naming adjustments
vlasin e29d631
Merge branch 'main' into vlasin-group-elements-generator
vlasin 1d4330c
Use options parser
vlasin 616551b
stylish-haskell auto-commit
vlasin f6cd1e8
Update Main.hs
vlasin 8915b0c
stylish-haskell auto-commit
vlasin 4ea43a2
Use `TypeAbstractions`
vlasin 1df1952
Use GHC flag to pass CI
vlasin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
module Main (main) where | ||
|
||
import Data.Aeson (ToJSON, encode) | ||
import qualified Data.ByteString.Lazy.Char8 as BL | ||
import Data.List (intercalate) | ||
import Numeric.Natural (Natural) | ||
import Options.Applicative | ||
import Prelude | ||
|
||
import ZkFold.Algebra.Class (FromConstant (..), Scale (..)) | ||
import ZkFold.Algebra.EllipticCurve.BLS12_381 (BLS12_381_G1_Point, BLS12_381_G2_Point) | ||
import ZkFold.Algebra.EllipticCurve.BN254 (BN254_G1_Point, BN254_G2_Point) | ||
import ZkFold.Algebra.EllipticCurve.Class (CyclicGroup (..)) | ||
|
||
-- | Supported groups | ||
data Group = BN254_G1 | BN254_G2 | BLS12_381_G1 | BLS12_381_G2 | ||
deriving (Show, Eq, Enum, Bounded) | ||
|
||
-- | Output format | ||
data OutputFormat = Plain | JSON | ||
deriving (Eq, Show) | ||
|
||
-- | Canonical string name for each group | ||
groupName :: Group -> String | ||
groupName BN254_G1 = "bn254-g1" | ||
groupName BN254_G2 = "bn254-g2" | ||
groupName BLS12_381_G1 = "bls12381-g1" | ||
groupName BLS12_381_G2 = "bls12381-g2" | ||
|
||
-- | Parse group from string | ||
parseGroup :: String -> Maybe Group | ||
parseGroup s = lookup s [(groupName g, g) | g <- [minBound .. maxBound :: Group]] | ||
|
||
-- | CLI options | ||
data Options = Options | ||
{ optGroup :: Group | ||
, optSeed :: Natural | ||
, optLength :: Natural | ||
, optFormat :: OutputFormat | ||
} | ||
|
||
optionsParser :: Parser Options | ||
optionsParser = | ||
let groupList = map groupName [minBound .. maxBound :: Group] | ||
groupListStr = intercalate " | " groupList | ||
groupHelp = "Group name: one of { " <> groupListStr <> " }" | ||
in Options | ||
<$> option (maybeReader parseGroup) | ||
(long "group" <> short 'g' <> metavar "GROUP" <> help groupHelp) | ||
<*> option auto | ||
(long "seed" <> short 's' <> metavar "SEED" <> help "Exponent seed (natural number)") | ||
<*> option auto | ||
(long "length" <> short 'l' <> metavar "LENGTH" <> help "Number of elements to generate") | ||
<*> flag Plain JSON | ||
(long "json" <> help "Output as JSON array of points") | ||
|
||
runGroupElementsGeneric :: forall pt. | ||
( Scale (ScalarFieldOf pt) pt | ||
, FromConstant Natural (ScalarFieldOf pt) | ||
, Show pt | ||
, ToJSON pt | ||
) | ||
=> Options -> pt -> IO () | ||
runGroupElementsGeneric opts g = | ||
let x = optSeed opts | ||
n = optLength opts | ||
fmt = optFormat opts | ||
group = optGroup opts | ||
exps = [x ^ k | k <- [0..n-1]] | ||
points = [scale (fromConstant @_ @(ScalarFieldOf pt) e) g | e <- exps] | ||
in case fmt of | ||
Plain -> do | ||
putStrLn $ "Generated group points (" <> groupName group <> ") :" | ||
mapM_ print points | ||
JSON -> do | ||
let fname = groupName group <> "_n" <> show n <> ".json" | ||
BL.writeFile fname (encode points) | ||
putStrLn $ "Wrote JSON to " <> fname | ||
|
||
main :: IO () | ||
main = do | ||
opts <- execParser $ info (optionsParser <**> helper) | ||
( fullDesc | ||
<> progDesc "Generate group elements by multiplying the generator point by powers of a seed (from 0 to LENGTH-1)." | ||
<> header "Group Elements Generator" | ||
) | ||
case optGroup opts of | ||
BN254_G1 -> runGroupElementsGeneric opts (pointGen :: BN254_G1_Point) | ||
BN254_G2 -> runGroupElementsGeneric opts (pointGen :: BN254_G2_Point) | ||
BLS12_381_G1 -> runGroupElementsGeneric opts (pointGen :: BLS12_381_G1_Point) | ||
BLS12_381_G2 -> runGroupElementsGeneric opts (pointGen :: BLS12_381_G2_Point) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of storing just a name of a group, you can directly store the required operations, like this:
But that's just a suggestion, the code looks 🔥 🔥 🔥 as is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, apparently, this approach requires
TypeAbstractions
to work, which requires GHC 9.14.When we upgrade to that version, we can use something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's strange, the extension page says that it's available since 9.8.
And the following basic example runs fine for me on GHC 9.6: