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

EC symbolic #396

Merged
merged 13 commits into from
Dec 13, 2024
4 changes: 2 additions & 2 deletions symbolic-base/bench/BenchEC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import ZkFold.Base.Algebra.EllipticCurve.BLS12_381
import ZkFold.Base.Algebra.EllipticCurve.Class
import ZkFold.Base.Algebra.EllipticCurve.Ed25519
import ZkFold.Symbolic.Compiler.ArithmeticCircuit
import ZkFold.Symbolic.Data.Ed25519 ()
import ZkFold.Symbolic.Data.Ed25519
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 (Ed25519 c)
type PtFFA c = Point (AcEd25519 c)

benchOps :: NFData a => String -> a -> (Natural-> a -> a) -> Benchmark
benchOps desc p0 op = env (fromIntegral <$> randomRIO (1 :: Integer, 3)) $ \ ~n ->
Expand Down
4 changes: 2 additions & 2 deletions symbolic-base/src/ZkFold/Base/Algebra/Basic/Field.hs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class IrreduciblePoly f (e :: Symbol) | e -> f where
irreduciblePoly :: Poly f

data Ext2 f (e :: Symbol) = Ext2 f f
deriving (Eq, Show)
deriving (Eq, Show, Generic)

instance Ord f => Ord (Ext2 f e) where
Ext2 a b <= Ext2 c d = [b, a] <= ([d, c] :: [f])
Expand Down Expand Up @@ -271,7 +271,7 @@ instance (Field f, Eq f, IrreduciblePoly f e, Arbitrary f) => Arbitrary (Ext2 f
arbitrary = Ext2 <$> arbitrary <*> arbitrary

data Ext3 f (e :: Symbol) = Ext3 f f f
deriving (Eq, Show)
deriving (Eq, Show, Generic)

instance Ord f => Ord (Ext3 f e) where
Ext3 a b c <= Ext3 d e f = [c, b, a] <= ([f, e, d] :: [f])
Expand Down
51 changes: 23 additions & 28 deletions symbolic-base/src/ZkFold/Base/Algebra/EllipticCurve/BLS12_381.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ instance EllipticCurve BLS12_381_G1 where

type BaseField BLS12_381_G1 = Fq

inf = Inf

gen = Point
gen = point
0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb
0x8b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1

Expand All @@ -93,9 +91,7 @@ instance EllipticCurve BLS12_381_G2 where

type BaseField BLS12_381_G2 = Fq2

inf = Inf

gen = Point
gen = point
(Ext2
0x24aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8
0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e)
Expand Down Expand Up @@ -138,30 +134,30 @@ ofBytes
. foldl' (\n w8 -> n * 256 + fromIntegral w8) 0

instance Binary (Point BLS12_381_G1) where
put Inf = foldMap putWord8 (bitReverse8 (bit 1) : replicate 95 0)
put (Point x y) = foldMap putWord8 (bytesOf 48 x <> bytesOf 48 y)
put (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
byte <- bitReverse8 <$> getWord8
let compressed = testBit byte 0
infinite = testBit byte 1
if infinite then do
skip (if compressed then 47 else 95)
return Inf
return inf
else do
let byteXhead = bitReverse8 $ clearBit (clearBit (clearBit byte 0) 1) 2
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 (pointCompressed x bigY))
else do
bytesY <- replicateM 48 getWord8
let y = ofBytes bytesY
return (Point x y)
return (point x y)

instance Binary (PointCompressed BLS12_381_G1) where
put InfCompressed =
foldMap putWord8 (bitReverse8 (bit 0 .|. bit 1) : replicate 47 0)
put (PointCompressed x bigY) =
put (PointCompressed 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
bytes = bytesOf 48 x
Expand All @@ -172,23 +168,22 @@ instance Binary (PointCompressed BLS12_381_G1) where
infinite = testBit byte 1
if infinite then do
skip (if compressed then 47 else 95)
return InfCompressed
return inf
else do
let byteXhead = bitReverse8 $ clearBit (clearBit (clearBit byte 0) 1) 2
bytesXtail <- replicateM 47 getWord8
let x = ofBytes (byteXhead:bytesXtail)
bigY = testBit byte 2
if compressed then return (PointCompressed x bigY)
if compressed then return (pointCompressed x bigY)
else do
bytesY <- replicateM 48 getWord8
let y :: Fq = ofBytes bytesY
bigY' = y > negate y
return (PointCompressed x bigY')
return (pointCompressed x bigY')

instance Binary (Point BLS12_381_G2) where
put Inf =
foldMap putWord8 (bitReverse8 (bit 1) : replicate 191 0)
put (Point (Ext2 x0 x1) (Ext2 y0 y1)) =
put (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
<> bytesOf 48 x0
Expand All @@ -202,25 +197,25 @@ instance Binary (Point BLS12_381_G2) where
infinite = testBit byte 1
if infinite then do
skip (if compressed then 95 else 191)
return Inf
return inf
else do
let byteX1head = bitReverse8 $ clearBit (clearBit (clearBit byte 0) 1) 2
bytesX1tail <- replicateM 47 getWord8
bytesX0 <- replicateM 48 getWord8
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 (pointCompressed (Ext2 x0 x1) bigY))
else do
bytesY1 <- replicateM 48 getWord8
bytesY0 <- replicateM 48 getWord8
let y0 = ofBytes bytesY0
y1 = ofBytes bytesY1
return (Point (Ext2 x0 x1) (Ext2 y0 y1))
return (point (Ext2 x0 x1) (Ext2 y0 y1))

instance Binary (PointCompressed BLS12_381_G2) where
put InfCompressed = foldMap putWord8 (bitReverse8 (bit 0 .|. bit 1) : replicate 95 0)
put (PointCompressed (Ext2 x0 x1) bigY) =
put (PointCompressed (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
bytes = bytesOf 48 x1 <> bytesOf 48 x0
Expand All @@ -232,7 +227,7 @@ instance Binary (PointCompressed BLS12_381_G2) where
infinite = testBit byte 1
if infinite then do
skip (if compressed then 95 else 191)
return InfCompressed
return inf
else do
let byteX1head = bitReverse8 $ clearBit (clearBit (clearBit byte 0) 1) 2
bytesX1tail <- replicateM 47 getWord8
Expand All @@ -241,15 +236,15 @@ instance Binary (PointCompressed BLS12_381_G2) where
x0 = ofBytes bytesX0
x = Ext2 x0 x1
bigY = testBit byte 2
if compressed then return (PointCompressed (Ext2 x0 x1) bigY)
if compressed then return (pointCompressed (Ext2 x0 x1) 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 (pointCompressed x bigY')

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

Expand Down
22 changes: 10 additions & 12 deletions symbolic-base/src/ZkFold/Base/Algebra/EllipticCurve/BN254.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ data BN254_G1
instance EllipticCurve BN254_G1 where
type ScalarField BN254_G1 = Fr
type BaseField BN254_G1 = Fp
inf = Inf
gen = Point 1 2
gen = point 1 2
add = addPoints
mul = pointMul

Expand All @@ -86,8 +85,7 @@ data BN254_G2
instance EllipticCurve BN254_G2 where
type ScalarField BN254_G2 = Fr
type BaseField BN254_G2 = Fp2
inf = Inf
gen = Point
gen = point
(Ext2 0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed
0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2)
(Ext2 0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa
Expand Down Expand Up @@ -134,23 +132,23 @@ instance Pairing BN254_G1 BN254_G2 where
------------------------------ Encoding ----------------------------------------

instance Binary (Point BN254_G1) where
put Inf = put (Point @BN254_G1 zero zero)
put (Point xp yp) = put xp >> put yp
put (Point xp yp isInf) =
if isInf then put @(Point BN254_G1) (point zero zero) else put xp >> put yp
get = do
xp <- get
yp <- get
return $
if xp == zero && yp == zero
then Inf
else Point xp yp
then inf
else point xp yp

instance Binary (Point BN254_G2) where
put Inf = put (Point @BN254_G2 zero zero)
put (Point xp yp) = put xp >> put yp
put (Point xp yp isInf) =
if isInf then put @(Point BN254_G2) (point zero zero) else put xp >> put yp
get = do
xp <- get
yp <- get
return $
if xp == zero && yp == zero
then Inf
else Point xp yp
then inf
else point xp yp
Loading
Loading