-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to galois-field-0.4.0. Remove overlapping instances (#17)
- Loading branch information
1 parent
95450b3
commit ca31452
Showing
4 changed files
with
6 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,68 +1,18 @@ | ||
{-# LANGUAGE TypeFamilies #-} | ||
-- | Prime field with characteristic _q, over which the elliptic curve | ||
-- is defined and the other finite field extensions. | ||
-- | ||
-- * Fq | ||
-- * Fq2 := Fq[u]/u^2 + 1 | ||
-- * Fq6 := Fq2[v]/v^3 - (9 + u) | ||
-- * Fq12 := Fq6[w]/w^2 - v | ||
|
||
module Bulletproofs.Fq | ||
( Fq | ||
, PF | ||
, fqRandom | ||
, fqPow | ||
, fqSqrt | ||
, toInt | ||
) where | ||
|
||
import Protolude | ||
|
||
import Crypto.Random (MonadRandom) | ||
import Crypto.Number.Generate (generateMax) | ||
import Math.NumberTheory.Moduli.Class (powMod) | ||
import PrimeField (PrimeField(..), toInt) | ||
import Pairing.Modular | ||
import PrimeField (PrimeField(..)) | ||
import Bulletproofs.Curve | ||
|
||
|
||
------------------------------------------------------------------------------- | ||
-- Types | ||
------------------------------------------------------------------------------- | ||
|
||
-- | Prime field @Fq@ with characteristic @_q@ | ||
type Fq = PrimeField 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141 | ||
|
||
-- | Type family to extract the characteristic of the prime field | ||
type family PF a where | ||
PF (PrimeField k) = k | ||
|
||
------------------------------------------------------------------------------- | ||
-- Instances | ||
------------------------------------------------------------------------------- | ||
|
||
instance Ord Fq where | ||
compare = on compare toInt | ||
|
||
------------------------------------------------------------------------------- | ||
-- Random | ||
------------------------------------------------------------------------------- | ||
|
||
fqRandom :: MonadRandom m => m Fq | ||
fqRandom = fromInteger <$> generateMax _q | ||
|
||
------------------------------------------------------------------------------- | ||
-- Y for X | ||
------------------------------------------------------------------------------- | ||
|
||
fqPow :: Integral e => Fq -> e -> Fq | ||
fqPow a b = fromInteger (withQ (modUnOp (toInt a) (flip powMod b))) | ||
{-# INLINE fqPow #-} | ||
|
||
fqSqrt :: Bool -> Fq -> Maybe Fq | ||
fqSqrt largestY a = do | ||
(y1, y2) <- withQM (modUnOpMTup (toInt a) bothSqrtOf) | ||
return (fromInteger ((if largestY then max else min) y1 y2)) | ||
|
||
fqYforX :: Fq -> Bool -> Maybe Fq | ||
fqYforX x largestY = fqSqrt largestY (x `fqPow` 3 + fromInteger _b) |
This file contains 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 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 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