-
Notifications
You must be signed in to change notification settings - Fork 44
/
RangeProof.hs
50 lines (43 loc) · 1.73 KB
/
RangeProof.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Example.RangeProof where
import Protolude
import Control.Monad.Random (MonadRandom, getRandomR)
import Data.Curve.Weierstrass.SECP256K1 (Fr)
import Data.Field.Galois (rnd)
import qualified Bulletproofs.RangeProof as RP
import qualified Bulletproofs.MultiRangeProof as MRP
import Bulletproofs.Utils (commit)
testSingleRangeProof :: Integer -> (Fr, Fr) -> IO Bool
testSingleRangeProof upperBound (v, vBlinding) = do
let vCommit = commit v vBlinding
-- Prover
proofE <- runExceptT $ RP.generateProof upperBound (v, vBlinding)
-- Verifier
case proofE of
Left err -> panic $ show err
Right [email protected]{..}
-> pure $ RP.verifyProof upperBound vCommit proof
testMultiRangeProof :: Integer -> [(Fr, Fr)] -> IO Bool
testMultiRangeProof upperBound vsAndvBlindings = do
let vCommits = fmap (uncurry commit) vsAndvBlindings
-- Prover
proofE <- runExceptT $ MRP.generateProof upperBound vsAndvBlindings
-- Verifier
case proofE of
Left err -> panic $ show err
Right [email protected]{..}
-> pure $ MRP.verifyProof upperBound vCommits proof
setupV :: MonadRandom m => Integer -> m (Fr, Fr)
setupV n = do
v <- fromInteger <$> getRandomR (1, 2^n - 1) -- value that needs to be in a certain range
vBlinding <- rnd -- blinding value
pure (v, vBlinding)
runExamples :: IO ()
runExamples = do
n <- (2 ^) <$> getRandomR (0 :: Integer, 7)
let upperBound = 2 ^ n
(v, vBlinding) <- setupV n
singleRangeProof <- testSingleRangeProof upperBound (v, vBlinding)
putText $ "Single-range proof success: " <> show singleRangeProof
vsAndvBlindings <- replicateM 5 (setupV n)
testMultiRangeProof <- testMultiRangeProof upperBound vsAndvBlindings
putText $ "Multi-range proof success: " <> show singleRangeProof