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

Elliptic Curve hierarchy #429

Merged
merged 71 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 70 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
db63f1f
EC hierarchy
echatav Dec 30, 2024
35e2c34
Update Class2.hs
echatav Dec 30, 2024
a98e258
Update Class2.hs
echatav Dec 30, 2024
f385ef2
Secp256k1
echatav Dec 30, 2024
606695f
more point types ...
echatav Dec 31, 2024
3842551
pointGen
echatav Dec 31, 2024
328760f
changes
echatav Dec 31, 2024
8108fc5
naming
echatav Dec 31, 2024
86bdca4
Update Class.hs
echatav Dec 31, 2024
1b3b6ea
Update Class.hs
echatav Dec 31, 2024
6db7e04
discriminant
echatav Dec 31, 2024
cef77f1
Update Class.hs
echatav Dec 31, 2024
63f7685
Update Class.hs
echatav Dec 31, 2024
6fe254f
some twisted edwards stuff
echatav Dec 31, 2024
d57aedb
Ed25519
echatav Dec 31, 2024
698b916
remove things
echatav Dec 31, 2024
973f454
PairingCurves
echatav Dec 31, 2024
f0343bc
field parameterize point types
echatav Dec 31, 2024
7f3388c
Update Class.hs
echatav Dec 31, 2024
fd10464
symmetric curves
echatav Jan 1, 2025
101ee6a
Pasta
echatav Jan 1, 2025
030baae
pairing & BN254 curve
echatav Jan 2, 2025
4b3bc99
BLS12-381
echatav Jan 3, 2025
402af0f
Update BLS12_381.hs
echatav Jan 3, 2025
836562e
stylish-haskell auto-commit
echatav Jan 6, 2025
c338387
simplify pasta
echatav Jan 6, 2025
a4957c5
simplify pairings
echatav Jan 6, 2025
039bb1e
stylish-haskell auto-commit
echatav Jan 6, 2025
51c2e51
Update symbolic-base.cabal
echatav Jan 6, 2025
5ff8f11
Merge branch 'eitan-ec-hierarchy' of https://github.com/zkFold/zkfold…
echatav Jan 6, 2025
50ac4d2
Update Class.hs
echatav Jan 6, 2025
982173f
squashing...
echatav Jan 6, 2025
6963fbd
Revert "squashing..."
echatav Jan 6, 2025
b66e54b
remove some point type
echatav Jan 6, 2025
128525d
Update Class.hs
echatav Jan 6, 2025
6e963cf
Update Class.hs
echatav Jan 6, 2025
777a163
Update BLS12_381.hs
echatav Jan 6, 2025
ebce2a8
Update Ed25519.hs
echatav Jan 6, 2025
f532e01
naming
echatav Jan 7, 2025
f162ff4
push through Proof Protocol curve hierarchy integration
echatav Jan 8, 2025
80aea4a
stylish-haskell auto-commit
echatav Jan 8, 2025
402cbc0
ed25519
echatav Jan 9, 2025
d9c99b9
stylish-haskell auto-commit
echatav Jan 9, 2025
4f579a0
symbolic data and input instances for point types
echatav Jan 10, 2025
d4af28e
Update ECDSA.hs
echatav Jan 10, 2025
0f1656e
Update ECDSA.hs
echatav Jan 10, 2025
d34d7de
Merge branch 'eitan-ec-hierarchy' of https://github.com/zkFold/zkfold…
echatav Jan 10, 2025
a36191f
stylish-haskell auto-commit
echatav Jan 10, 2025
9aae732
remove
echatav Jan 13, 2025
53098fd
type families
echatav Jan 13, 2025
8938bcc
integration
echatav Jan 13, 2025
d02a212
integration work
echatav Jan 13, 2025
ff07c6c
integration
echatav Jan 13, 2025
f562bd9
Update BN254.hs
echatav Jan 13, 2025
aea0ffb
testing
echatav Jan 14, 2025
c936f52
stylish-haskell auto-commit
echatav Jan 14, 2025
1c26de6
test integration
echatav Jan 14, 2025
b1182c5
more test integration
echatav Jan 14, 2025
e2e824e
indenting
echatav Jan 14, 2025
bf97a41
Update Plonk.hs
echatav Jan 14, 2025
1234ed3
symbolic secp256k1
echatav Jan 14, 2025
1909886
Update Class.hs
echatav Jan 14, 2025
1f8fbf8
stylish-haskell auto-commit
echatav Jan 14, 2025
e8a6f29
finish integration
echatav Jan 20, 2025
50012cb
Merge branch 'main' into ec-merge
echatav Jan 20, 2025
5126bed
stylish-haskell auto-commit
echatav Jan 20, 2025
7d96f15
Update FFA.hs
echatav Jan 20, 2025
0125060
Update BenchEC.hs
echatav Jan 20, 2025
4931a67
stylish-haskell auto-commit
echatav Jan 20, 2025
11fd977
comment out failing IVC test
echatav Jan 22, 2025
000d2b1
Merge branch 'main' into eitan-ec-hierarchy
TurtlePU Jan 24, 2025
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
7 changes: 3 additions & 4 deletions symbolic-base/bench/BenchEC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Basic.Field
import ZkFold.Base.Algebra.Basic.Number
import ZkFold.Base.Algebra.EllipticCurve.BLS12_381
import ZkFold.Base.Algebra.EllipticCurve.Class
import ZkFold.Base.Algebra.EllipticCurve.Ed25519
import ZkFold.Base.Algebra.EllipticCurve.Ed25519 hiding (Ed25519_Point)
import ZkFold.Symbolic.Compiler.ArithmeticCircuit
import ZkFold.Symbolic.Data.Ed25519
import ZkFold.Symbolic.Data.Ed25519 (Ed25519_Point)
import ZkFold.Symbolic.Data.FFA
import ZkFold.Symbolic.Interpreter

type I = Interpreter (Zp BLS12_381_Scalar)
type A = ArithmeticCircuit (Zp BLS12_381_Scalar) U1 U1
type PtFFA c = Point (AcEd25519 c)
type PtFFA c = Ed25519_Point c

benchOps :: NFData a => String -> a -> (Natural-> a -> a) -> Benchmark
benchOps desc p0 op = env (fromIntegral <$> randomRIO (1 :: Integer, 3)) $ \ ~n ->
Expand Down
139 changes: 78 additions & 61 deletions symbolic-base/src/ZkFold/Base/Algebra/EllipticCurve/BLS12_381.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

module ZkFold.Base.Algebra.EllipticCurve.BLS12_381 where

import Control.DeepSeq (NFData)
import Control.Monad
import Data.Bits
import Data.Foldable
import Data.Word
import GHC.Generics (Generic)
import Prelude hiding (Num (..), (/), (^))

import ZkFold.Base.Algebra.Basic.Class
Expand All @@ -24,6 +22,8 @@ import ZkFold.Base.Algebra.EllipticCurve.Class
import ZkFold.Base.Algebra.EllipticCurve.Pairing
import ZkFold.Base.Algebra.Polynomials.Univariate
import ZkFold.Base.Data.ByteString
import qualified ZkFold.Symbolic.Data.Conditional as Symbolic
import qualified ZkFold.Symbolic.Data.Eq as Symbolic

-------------------------------- Introducing Fields ----------------------------------

Expand Down Expand Up @@ -57,56 +57,70 @@ instance IrreduciblePoly Fq6 IP3 where
in toPoly [e, zero, one]
type Fq12 = Ext2 Fq6 IP3

------------------------------------ BLS12-381 G1 ------------------------------------

data BLS12_381_G1
deriving (Generic, NFData)

instance EllipticCurve BLS12_381_G1 where
type ScalarField BLS12_381_G1 = Fr
------------------------------------- BLS12-381 --------------------------------------

instance Field field => WeierstrassCurve "BLS12-381" field where
weierstrassB = fromConstant (4 :: Natural)

type BLS12_381_Point baseField = Weierstrass "BLS12-381" (Point Bool baseField)

type BLS12_381_CompressedPoint baseField =
Weierstrass "BLS12-381" (CompressedPoint Bool baseField)

instance
( Symbolic.Conditional Bool field
, Symbolic.Eq Bool field
, FiniteField field
, Ord field
) => Compressible Bool (BLS12_381_Point field) where
type Compressed (BLS12_381_Point field) = BLS12_381_CompressedPoint field
pointCompressed x yBit = Weierstrass (CompressedPoint x yBit False)
compress (Weierstrass (Point x y isInf)) =
if isInf then pointInf
else pointCompressed @Bool @(BLS12_381_Point field) x (y > negate y)
decompress (Weierstrass (CompressedPoint x bigY isInf)) =
if isInf then pointInf else
let b = weierstrassB @"BLS12-381"
q = order @field
sqrt_ z = z ^ ((q + 1) `Prelude.div` 2)
y' = sqrt_ (x * x * x + b)
y'' = negate y'
y = if bigY then max y' y'' else min y' y''
in pointXY x y

type BaseField BLS12_381_G1 = Fq

pointGen = pointXY
0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb
0x8b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1
------------------------------------ BLS12-381 G1 ------------------------------------

add = addPoints
type BLS12_381_G1_Point = BLS12_381_Point Fq

mul = pointMul
type BLS12_381_G1_CompressedPoint = BLS12_381_CompressedPoint Fq

instance WeierstrassCurve BLS12_381_G1 where
weierstrassA = zero
instance CyclicGroup BLS12_381_G1_Point where
type ScalarFieldOf BLS12_381_G1_Point = Fr
pointGen = pointXY
0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb
0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb

weierstrassB = fromConstant (4 :: Natural)
instance Scale Fr BLS12_381_G1_Point where
scale n x = scale (toConstant n) x

------------------------------------ BLS12-381 G2 ------------------------------------

data BLS12_381_G2
deriving (Generic, NFData)

instance EllipticCurve BLS12_381_G2 where
type BLS12_381_G2_Point = BLS12_381_Point Fq2

type ScalarField BLS12_381_G2 = Fr
type BLS12_381_G2_CompressedPoint = BLS12_381_CompressedPoint Fq2

type BaseField BLS12_381_G2 = Fq2
instance CyclicGroup BLS12_381_G2_Point where
type ScalarFieldOf BLS12_381_G2_Point = Fr
pointGen = pointXY
(Ext2
0x24aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8
0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e)
(Ext2
0xce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801
0x606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be)

pointGen = pointXY
(Ext2
0x24aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8
0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e)
(Ext2
0xce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801
0x606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be)

add = addPoints

mul = pointMul

instance WeierstrassCurve BLS12_381_G2 where
weierstrassA = zero

weierstrassB = fromConstant (4 :: Natural)
instance Scale Fr BLS12_381_G2_Point where
scale n x = scale (toConstant n) x

------------------------------------ Encoding ------------------------------------

Expand All @@ -133,8 +147,8 @@ ofBytes
= fromConstant @Natural
. foldl' (\n w8 -> n * 256 + fromIntegral w8) 0

instance Binary (Point BLS12_381_G1) where
put (Point x y isInf) =
instance Binary BLS12_381_G1_Point where
put (Weierstrass (Point x y isInf)) =
if isInf then foldMap putWord8 (bitReverse8 (bit 1) : replicate 95 0)
else foldMap putWord8 (bytesOf 48 x <> bytesOf 48 y)
get = do
Expand All @@ -149,14 +163,16 @@ instance Binary (Point BLS12_381_G1) where
bytesXtail <- replicateM 47 getWord8
let x = ofBytes (byteXhead:bytesXtail)
bigY = testBit byte 2
if compressed then return (decompress (pointCompressed x bigY))
if compressed then return $
decompress @Bool @BLS12_381_G1_Point
(pointCompressed @Bool @BLS12_381_G1_Point x bigY)
else do
bytesY <- replicateM 48 getWord8
let y = ofBytes bytesY
return (pointXY x y)

instance Binary (CompressedPoint BLS12_381_G1) where
put (CompressedPoint x bigY isInf) =
instance Binary BLS12_381_G1_CompressedPoint where
put (Weierstrass (CompressedPoint x bigY isInf)) =
if isInf then foldMap putWord8 (bitReverse8 (bit 0 .|. bit 1) : replicate 47 0) else
let
flags = bitReverse8 $ if bigY then bit 0 .|. bit 2 else bit 0
Expand All @@ -174,15 +190,15 @@ instance Binary (CompressedPoint BLS12_381_G1) where
bytesXtail <- replicateM 47 getWord8
let x = ofBytes (byteXhead:bytesXtail)
bigY = testBit byte 2
if compressed then return (pointCompressed x bigY)
else do
pointCompressed @Bool @BLS12_381_G1_Point x <$>
if compressed then return bigY else do
bytesY <- replicateM 48 getWord8
let y :: Fq = ofBytes bytesY
bigY' = y > negate y
return (pointCompressed x bigY')
return bigY'

instance Binary (Point BLS12_381_G2) where
put (Point (Ext2 x0 x1) (Ext2 y0 y1) isInf) =
instance Binary BLS12_381_G2_Point where
put (Weierstrass (Point (Ext2 x0 x1) (Ext2 y0 y1) isInf)) =
if isInf then foldMap putWord8 (bitReverse8 (bit 1) : replicate 191 0) else
let
bytes = bytesOf 48 x1
Expand All @@ -205,16 +221,18 @@ instance Binary (Point BLS12_381_G2) where
let x1 = ofBytes (byteX1head:bytesX1tail)
x0 = ofBytes bytesX0
bigY = testBit byte 2
if compressed then return (decompress (pointCompressed (Ext2 x0 x1) bigY))
if compressed then return $
decompress @Bool @BLS12_381_G2_Point
(pointCompressed @Bool @BLS12_381_G2_Point (Ext2 x0 x1) bigY)
else do
bytesY1 <- replicateM 48 getWord8
bytesY0 <- replicateM 48 getWord8
let y0 = ofBytes bytesY0
y1 = ofBytes bytesY1
return (pointXY (Ext2 x0 x1) (Ext2 y0 y1))

instance Binary (CompressedPoint BLS12_381_G2) where
put (CompressedPoint (Ext2 x0 x1) bigY isInf) =
instance Binary BLS12_381_G2_CompressedPoint where
put (Weierstrass (CompressedPoint (Ext2 x0 x1) bigY isInf)) =
if isInf then foldMap putWord8 (bitReverse8 (bit 0 .|. bit 1) : replicate 95 0) else
let
flags = bitReverse8 $ if bigY then bit 0 .|. bit 2 else bit 0
Expand All @@ -236,22 +254,22 @@ instance Binary (CompressedPoint BLS12_381_G2) where
x0 = ofBytes bytesX0
x = Ext2 x0 x1
bigY = testBit byte 2
if compressed then return (pointCompressed (Ext2 x0 x1) bigY)
else do
pointCompressed @Bool @BLS12_381_G2_Point x <$>
if compressed then return bigY else do
bytesY1 <- replicateM 48 getWord8
bytesY0 <- replicateM 48 getWord8
let y0 = ofBytes bytesY0
y1 = ofBytes bytesY1
y :: Fq2 = Ext2 y0 y1
bigY' = y > negate y
return (pointCompressed x bigY')
return bigY'

--------------------------------------- Pairing ---------------------------------------

-- | An image of a pairing is a cyclic multiplicative subgroup of @'Fq12'@
-- of order @'BLS12_381_Scalar'@.
newtype BLS12_381_GT = BLS12_381_GT Fq12
deriving newtype (Eq, Show, MultiplicativeSemigroup, MultiplicativeMonoid)
deriving newtype (Eq, Show, MultiplicativeSemigroup, MultiplicativeMonoid, Symbolic.Eq Bool)

instance Exponent BLS12_381_GT Natural where
BLS12_381_GT a ^ p = BLS12_381_GT (a ^ p)
Expand All @@ -264,11 +282,10 @@ deriving via (NonZero Fq12) instance MultiplicativeGroup BLS12_381_GT
instance Finite BLS12_381_GT where
type Order BLS12_381_GT = BLS12_381_Scalar

instance Pairing BLS12_381_G1 BLS12_381_G2 where
type TargetGroup BLS12_381_G1 BLS12_381_G2 = BLS12_381_GT
instance Pairing BLS12_381_G1_Point BLS12_381_G2_Point BLS12_381_GT where
pairing a b
= BLS12_381_GT
$ finalExponentiation @BLS12_381_G2
$ finalExponentiation @Fr
$ millerAlgorithmBLS12 param a b
where
param = [-1
Expand Down
Loading
Loading