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

ZK primitive ops #1052

Merged
merged 47 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
862c9b0
Wip arith
jmcardon Sep 21, 2022
1d62060
wip pairings + twists
jmcardon Sep 26, 2022
ff2eb78
pairing functions
jmcardon Sep 27, 2022
4b1c551
miller loop
jmcardon Sep 28, 2022
bf5cbbc
WIP pairing spec
jmcardon Sep 28, 2022
a33dc37
Begin writing ZK spec tests
jwiegley Sep 29, 2022
27c2ddd
Merge branch 'jose/zk-ops' into johnw/zk-spec
jwiegley Sep 29, 2022
9093885
wip new ZK impl
jmcardon Oct 11, 2022
9dd9e00
Extension impl + miller loop
jmcardon Oct 11, 2022
bcebb5d
Merge branch 'jose/zk-ops' into johnw/zk-spec
jmcardon Oct 11, 2022
c5252b2
wip fix zk ops
jmcardon Oct 11, 2022
6e0f717
wip galois-field replacement
jmcardon Oct 11, 2022
413f115
miller loop for bn254
jmcardon Oct 12, 2022
eddcaf6
Merge branch 'jose/zk-ops' into jj/zk-spec2
jmcardon Oct 12, 2022
19aae76
fix pairing impl
jmcardon Oct 12, 2022
cba893e
remove galois field
jmcardon Oct 12, 2022
2aa5546
rename Fq
jmcardon Oct 12, 2022
887862c
remove points
jmcardon Oct 12, 2022
378bfb0
Simplify tests/ZkSpec.hs
jwiegley Oct 12, 2022
bd01882
wip bounds
jmcardon Oct 12, 2022
a8e8edc
Several optimizations in Pairing.hs
jwiegley Oct 13, 2022
04ce39b
Use explicit exports
jwiegley Oct 13, 2022
1fbb643
Only minor changes
jwiegley Oct 14, 2022
d7fb6de
Minor whitespace fix
jwiegley Oct 14, 2022
c730950
Minor whitespace fix
jwiegley Oct 14, 2022
dbddb91
Add an implementation comment
jwiegley Oct 14, 2022
fbd1edf
Use unsafeSlice rather than slice
jwiegley Oct 14, 2022
894a952
Inline all the things
jwiegley Oct 14, 2022
09828ff
wip more tests
jmcardon Oct 15, 2022
475cd6b
Merge branch 'jose/zk-ops' of github.com:kadena-io/pact into jose/zk-ops
jmcardon Oct 15, 2022
cfd2a94
pairing basic tests
jmcardon Oct 15, 2022
3a95c12
Hedgehog test for pairing property
jmcardon Oct 18, 2022
a5d1757
pairing check
jmcardon Oct 18, 2022
a4e9821
zk test
jmcardon Oct 19, 2022
7360914
wip point add native
jmcardon Oct 20, 2022
4929cad
wip natives
jmcardon Oct 24, 2022
0bdb7f6
Use INLINABLE instead of INLINE for the sake of GHC memory
jwiegley Oct 24, 2022
ac8ea30
pairing check, scalar mult and addition
jmcardon Oct 24, 2022
6352dd1
Merge branch 'jose/zk-ops' of github.com:kadena-io/pact into jose/zk-ops
jmcardon Oct 24, 2022
305ee91
remove pairing lib, pact repl file for zk-snark proof verif
jmcardon Oct 24, 2022
2b9919c
g1 and g2 in pairing contract, update type of pairing-check
jmcardon Oct 26, 2022
5839782
remove commented stubs
jmcardon Jan 11, 2023
2c3e643
fix merge conflicts
jmcardon Jan 11, 2023
fa13939
gas for zk pairs
jmcardon Jan 11, 2023
b27305e
zk gas model spec
jmcardon Jan 11, 2023
ac1a6fb
address removing todos
jmcardon Feb 2, 2023
45936d8
Merge branch 'master' into jose/zk-ops
jmcardon Feb 3, 2023
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
33 changes: 33 additions & 0 deletions docs/en/pact-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,39 @@ Validate that PRINCIPAL unambiguously identifies GUARD.
(enforce (validate-principal (read-keyset 'keyset) account) "Invalid account ID")
```

## Zk {#Zk}

### pairing-check {#pairing-check}

*points-g1*&nbsp;`[<a>]` *points-g2*&nbsp;`[<b>]` *&rarr;*&nbsp;`bool`


Perform pairing and final exponentiation points in G1 and G2 in BN254, check if the result is 1


### point-add {#point-add}

*type*&nbsp;`string` *point1*&nbsp;`<a>` *point2*&nbsp;`<a>` *&rarr;*&nbsp;`<a>`


Add two points together that lie on the curve BN254. Point addition either in Fq or in Fq2
```lisp
pact> (point-add 'g1 {'x: 1, 'y: 2} {'x: 1, 'y: 2})
{"x": 1368015179489954701390400359078579693043519447331113978918064868415326638035,"y": 9918110051302171585080402603319702774565515993150576347155970296011118125764}
```


### scalar-mult {#scalar-mult}

*type*&nbsp;`string` *point1*&nbsp;`<a>` *scalar*&nbsp;`integer` *&rarr;*&nbsp;`<a>`


Multiply a point that lies on the curve BN254 by an integer value
```lisp
pact> (scalar-mult 'g1 {'x: 1, 'y: 2} 2)
{"x": 1368015179489954701390400359078579693043519447331113978918064868415326638035,"y": 9918110051302171585080402603319702774565515993150576347155970296011118125764}
```

## REPL-only functions {#repl-lib}

The following functions are loaded automatically into the interactive REPL, or within script files with a `.repl` extension. They are not available for blockchain-based execution.
Expand Down
23 changes: 23 additions & 0 deletions golden/gas-model/golden
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@
- 1
- - (describe-keyset "some-loaded-keyset")
- 101
- - (point-add 'g1 {'x:1, 'y:2} {'x:1, 'y:2})
- 6
- - |-
(point-add 'g2
{ 'x: [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634]
, 'y: [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531]}
{ 'x: [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634]
, 'y: [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531]})
- 31
- - (create-user-guard (accounts.enforce-true))
- 1
- - (txlog accounts.accounts 0)
Expand Down Expand Up @@ -1010,6 +1019,14 @@
- 3
- - (take ["a1"] smallOjectMap)
- 3
- - (scalar-mult 'g1 {'x:1, 'y:2} 10)
- 361
- - |-
(scalar-mult 'g2
{ 'x: [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634]
, 'y: [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531]}
10)
- 1451
- - (constantly 0 "firstIgnore")
- 1
- - (constantly 0 "firstIgnore" "secondIgnore")
Expand Down Expand Up @@ -1053,6 +1070,12 @@
{ "balance": 10.0 }
)
- 125
- - |-
(pairing-check [{'x:1, 'y:2}]
[{ 'x: [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634]
, 'y: [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531]}]
)
- 15361
- - (resume longBinding a1)
- 10002
- - (resume medBinding a1)
Expand Down
12 changes: 12 additions & 0 deletions pact.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ library
Pact.Native.Ops
Pact.Native.Keysets
Pact.Native.Decrypt
Pact.Native.Pairing
Pact.Parse
Pact.PersistPactDb
Pact.Persist
Expand Down Expand Up @@ -195,6 +196,10 @@ library
, vector >= 0.11.0.0 && < 0.13
, vector-algorithms >= 0.7
, vector-space >= 0.10.4 && < 0.17
, groups
, semirings
, mod >= 0.1.2 && < 0.2
, poly >= 0.5.0 && < 0.6
, time

-- GHCJS
Expand Down Expand Up @@ -422,6 +427,8 @@ test-suite hspec
RemoteVerifySpec
TypecheckSpec
PactCLISpec
ZkSpec
PairingSpec
Utils

build-depends:
Expand All @@ -432,11 +439,16 @@ test-suite hspec
, exceptions
, hedgehog >= 1.0.1 && < 1.2
, hspec-golden >= 0.1.0.2
, hspec-expectations
, hspec-hedgehog
, groups
, http-client
, hw-hspec-hedgehog == 0.1.*
, intervals
, mmorph
, neat-interpolation
, semirings
, prettyprinter
, sbv
, servant-client
, temporary >= 1.3
Expand Down
36 changes: 36 additions & 0 deletions src-ghc/Pact/GasModel/GasTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ allTests = HM.fromList
, ("is-principal", isPrincipalTests)
, ("typeof-principal", typeofPrincipalTests)

-- ZK pairing for curve BN254
, ("point-add", pointAddTests)
, ("scalar-mult", scalarMulTests)
, ("pairing-check", pairingCheckTests)

-- Non-native concepts to benchmark
, ("use", useTests)
, ("module", moduleTests)
Expand Down Expand Up @@ -1964,3 +1969,34 @@ typeofPrincipalTests = createGasUnitTests
, "pred" A..= ("keys-all" :: T.Text)
]
]

scalarMulTests :: NativeDefName -> GasUnitTests
scalarMulTests = defGasUnitTests allExprs
where
scalarMulG1 = [text| (scalar-mult 'g1 {'x:1, 'y:2} 10) |]
scalarMulG2 = [text| (scalar-mult 'g2
{ 'x: [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634]
, 'y: [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531]}
10)|]
allExprs = fmap defPactExpression [scalarMulG1, scalarMulG2]


pointAddTests :: NativeDefName -> GasUnitTests
pointAddTests = defGasUnitTests allExprs
where
pointAddG1 = [text| (point-add 'g1 {'x:1, 'y:2} {'x:1, 'y:2}) |]
pointAddG2 = [text| (point-add 'g2
{ 'x: [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634]
, 'y: [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531]}
{ 'x: [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634]
, 'y: [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531]})|]
allExprs = fmap defPactExpression [pointAddG1, pointAddG2]

pairingCheckTests :: NativeDefName -> GasUnitTests
pairingCheckTests = defGasUnitTests allExprs
where
pairingCheck = [text| (pairing-check [{'x:1, 'y:2}]
[{ 'x: [10857046999023057135944570762232829481370756359578518086990519993285655852781, 11559732032986387107991004021392285783925812861821192530917403151452391805634]
, 'y: [8495653923123431417604973247489272438418190587263600148770280649306958101930, 4082367875863433681332203403145435568316851327593401208105741076214120093531]}]
)|]
allExprs = fmap defPactExpression [pairingCheck]
27 changes: 26 additions & 1 deletion src/Pact/Gas/Table.hs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ defaultGasTable =
,("txids", 100000)
,("txlog", 100000)


-- Zk entries
-- TODO: adjust gas, this is purely for testing purposes
,("scalar-mult", 1)
,("point-add", 1)
,("pairing-check", 1)
]

{-# NOINLINE defaultGasTable #-}
Expand Down Expand Up @@ -281,13 +285,34 @@ tableGasModel gasConfig =
GMakeList2 len msz ->
let glen = fromIntegral len
in glen + maybe 0 ((* glen) . intCost) msz
GZKArgs arg -> case arg of
PointAdd g -> pointAddGas g
ScalarMult g -> scalarMulGas g
Pairing np -> pairingGas np
in GasModel
{ gasModelName = "table"
, gasModelDesc = "table-based cost model"
, runGasModel = run
}
{-# INLINE tableGasModel #-}

pointAddGas :: ZKGroup -> Gas
pointAddGas = \case
ZKG1 -> 5
ZKG2 -> 30

scalarMulGas :: ZKGroup -> Gas
scalarMulGas = \case
ZKG1 -> 360
ZKG2 -> 1450

pairingGas :: Int -> Gas
pairingGas npairs
| npairs > 0 = fromIntegral (npairs * slope + intercept)
| otherwise = 100
where
slope = 3760
intercept = 11600

perByteFactor :: Rational
perByteFactor = 1%10
Expand Down
2 changes: 2 additions & 0 deletions src/Pact/Native.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import Pact.Native.Keysets
import Pact.Native.Ops
import Pact.Native.SPV
import Pact.Native.Time
import Pact.Native.Pairing(zkDefs)
import Pact.Parse
import Pact.Runtime.Utils(lookupFreeVar)
import Pact.Types.Hash
Expand All @@ -117,6 +118,7 @@ natives =
, spvDefs
, decryptDefs
, guardDefs
, zkDefs
]


Expand Down
Loading