-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMain.hs
More file actions
74 lines (67 loc) · 1.87 KB
/
Main.hs
File metadata and controls
74 lines (67 loc) · 1.87 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{-# LANGUAGE NamedFieldPuns #-}
module Main where
import Protolude
import Control.Monad.Random (getRandomR)
import Bulletproofs.ArithmeticCircuit
import Data.Pairing.BLS12381 (Fr)
import Data.Field.Galois (rnd)
import Sonic.SRS as SRS
import Sonic.Protocol
sonicProtocol :: ArithCircuit Fr -> Assignment Fr -> Fr -> IO Bool
sonicProtocol circuit assignment x = do
-- Setup for an SRS
srs <- SRS.new <$> randomD n <*> pure x <*> rnd
-- Prover
(proof, rndOracle@RndOracle{..}) <- prove srs assignment circuit
-- Verifier
pure $ verify srs circuit proof rndOracleY rndOracleZ rndOracleYZs
where
-- n: Number of multiplication constraints
n = length $ aL assignment
randomD n = getRandomR (7 * n, 100 * n)
-- 5 linear constraints (q = 5):
-- aO[0] = aO[1]
-- aL[0] = V[0] - z
-- aL[1] = V[2] - z
-- aR[0] = V[1] - z
-- aR[1] = V[3] - z
--
-- 2 multiplication constraints (implicit) (n = 2):
-- aL[0] * aR[0] = aO[0]
-- aL[1] * aR[1] = aO[1]
--
-- 4 input values (m = 4)
arithCircuitExample :: Fr -> Fr -> (ArithCircuit Fr, Assignment Fr)
arithCircuitExample x z =
let wL = [[0, 0]
,[1, 0]
,[0, 1]
,[0, 0]
,[0, 0]]
wR = [[0, 0]
,[0, 0]
,[0, 0]
,[1, 0]
,[0, 1]]
wO = [[1, -1]
,[0, 0]
,[0, 0]
,[0, 0]
,[0, 0]]
cs = [0, 4-z, 9-z, 9-z, 4-z]
aL = [4 - z, 9 - z]
aR = [9 - z, 4 - z]
aO = zipWith (*) aL aR
gateWeights = GateWeights wL wR wO
assignment = Assignment aL aR aO
circuit = ArithCircuit gateWeights witness cs
in (circuit, assignment)
runExample :: IO ()
runExample = do
pX <- rnd
pZ <- rnd
let (arithCircuit, assignment@Assignment{..}) = arithCircuitExample pX pZ
success <- sonicProtocol arithCircuit assignment pX
putText $ "Success: " <> show success
main :: IO ()
main = runExample