Skip to content

Commit

Permalink
Tidy up the Groth16 example
Browse files Browse the repository at this point in the history
  • Loading branch information
kwxm committed Mar 16, 2023
1 parent edfabad commit 7771982
Showing 1 changed file with 94 additions and 59 deletions.
Expand Up @@ -6,6 +6,11 @@

{-# OPTIONS_GHC -Wno-name-shadowing #-}

-- | Approximations of the sort of computations involving BLS12-381 primitives
-- that one might wish to perform on the chain. Real on-chain code will have
-- extra overhead, but these examples help to give us an idea of the sort of
-- computation that can feasibly be carried out within the validation budget
-- limits.
module PlutusBenchmark.BLS12_381.Common ( UProg
, UTerm
, checkGroth16Verify_Haskell
Expand All @@ -18,8 +23,6 @@ module PlutusBenchmark.BLS12_381.Common ( UProg
, mkUncompressAndAddG2Script
, toAnonDeBruijnProg
)


where
import PlutusCore (DefaultFun, DefaultUni)
import PlutusTx qualified as Tx
Expand All @@ -29,6 +32,7 @@ import PlutusTx.Prelude as Tx hiding (sort, (*))

import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
import Data.Word (Word8)
import Hedgehog.Internal.Gen qualified as G
import Hedgehog.Internal.Range qualified as R
import System.IO.Unsafe (unsafePerformIO)
Expand Down Expand Up @@ -174,92 +178,123 @@ mkPairingScript p1 p2 q1 q2 =

---------------- Groth16 verification ----------------

-- The data here is derived from
-- https://github.com/achimcc/groth16-example/blob/main/src/lib.rs
{- | An example of the on-chain computation required for verification of a Groth16
proof. The data here is derived from
https://github.com/achimcc/groth16-example/blob/main/src/lib.rs -}

-- Wrappers for serialised group elements for slightly better type safety
newtype SerialisedG1Element = SerialisedG1Element { g1 :: BuiltinByteString }
deriving newtype (Tx.Lift DefaultUni)

mkG1Element :: [Word8] -> SerialisedG1Element
mkG1Element = SerialisedG1Element . toBuiltin . BS.pack

newtype SerialisedG2Element = SerialisedG2Element { g2 :: BuiltinByteString }
deriving newtype (Tx.Lift DefaultUni)

mkG2Element :: [Word8] -> SerialisedG2Element
mkG2Element = SerialisedG2Element . toBuiltin . BS.pack

scalar :: Integer
scalar = 11090173236178880413184798967381823895855059959147020707603928894861818263064
scalar = 0x1884d0cbcc5947434e46d19b3e904e18a8ee8d0d39ce9d315f3b00e338c8f618

-- Lots of group elements for input to the computation

alpha :: SerialisedG1Element
alpha = SerialisedG1Element $ toBuiltin $ BS.pack [ 183, 29, 177, 250, 95, 65,
54, 46, 147, 2, 91, 53, 86, 215, 110, 173, 18, 37, 207, 89, 13, 28,
219, 158, 56, 42, 31, 235, 183, 150, 61, 205, 36, 165, 30, 24, 223, 4,
171, 34, 27, 236, 175, 41, 22, 159, 175, 37]

alpha0 :: BuiltinByteString
alpha0 = toBuiltin $ BS.pack [ 183, 29, 177, 250, 95, 65,
54, 46, 147, 2, 91, 53, 86, 215, 110, 173, 18, 37, 207, 89, 13, 28,
219, 158, 56, 42, 31, 235, 183, 150, 61, 205, 36, 165, 30, 24, 223, 4,
171, 34, 27, 236, 175, 41, 22, 159, 175, 37]

alpha = mkG1Element [ 0xb7, 0x1d, 0xb1, 0xfa, 0x5f, 0x41, 0x36, 0x2e
, 0x93, 0x02, 0x5b, 0x35, 0x56, 0xd7, 0x6e, 0xad
, 0x12, 0x25, 0xcf, 0x59, 0x0d, 0x1c, 0xdb, 0x9e
, 0x38, 0x2a, 0x1f, 0xeb, 0xb7, 0x96, 0x3d, 0xcd
, 0x24, 0xa5, 0x1e, 0x18, 0xdf, 0x04, 0xab, 0x22
, 0x1b, 0xec, 0xaf, 0x29, 0x16, 0x9f, 0xaf, 0x25 ]

beta :: SerialisedG2Element
beta = SerialisedG2Element $ toBuiltin $ BS.pack [
179, 162, 107, 11, 71, 18, 231, 141, 93, 113, 120, 109, 150, 19, 42, 124, 88, 80, 35,
163, 102, 50, 202, 218, 68, 23, 26, 195, 244, 93, 181, 36, 195, 246, 87, 12, 138, 63,
125, 236, 53, 174, 26, 195, 48, 155, 5, 221, 11, 48, 109, 180, 247, 79, 217, 236, 66,
28, 167, 12, 84, 66, 93, 146, 46, 172, 76, 64, 59, 0, 219, 145, 111, 222, 223, 6, 91,
220, 224, 14, 206, 23, 185, 122, 78, 151, 23, 62, 77, 89, 137, 129, 142, 223, 170, 76]
beta = mkG2Element [ 0xb3, 0xa2, 0x6b, 0x0b, 0x47, 0x12, 0xe7, 0x8d
, 0x5d, 0x71, 0x78, 0x6d, 0x96, 0x13, 0x2a, 0x7c
, 0x58, 0x50, 0x23, 0xa3, 0x66, 0x32, 0xca, 0xda
, 0x44, 0x17, 0x1a, 0xc3, 0xf4, 0x5d, 0xb5, 0x24
, 0xc3, 0xf6, 0x57, 0x0c, 0x8a, 0x3f, 0x7d, 0xec
, 0x35, 0xae, 0x1a, 0xc3, 0x30, 0x9b, 0x05, 0xdd
, 0x0b, 0x30, 0x6d, 0xb4, 0xf7, 0x4f, 0xd9, 0xec
, 0x42, 0x1c, 0xa7, 0x0c, 0x54, 0x42, 0x5d, 0x92
, 0x2e, 0xac, 0x4c, 0x40, 0x3b, 0x00, 0xdb, 0x91
, 0x6f, 0xde, 0xdf, 0x06, 0x5b, 0xdc, 0xe0, 0x0e
, 0xce, 0x17, 0xb9, 0x7a, 0x4e, 0x97, 0x17, 0x3e
, 0x4d, 0x59, 0x89, 0x81, 0x8e, 0xdf, 0xaa, 0x4c ]

gamma :: SerialisedG2Element
gamma = SerialisedG2Element $ toBuiltin $ BS.pack [
181, 172, 184, 0, 205, 73, 237, 140, 189, 219, 244, 145, 161, 252, 248, 171, 252, 147,
240, 157, 56, 187, 178, 236, 182, 176, 142, 35, 164, 100, 44, 229, 156, 155, 3, 134,
83, 154, 195, 206, 205, 251, 102, 169, 240, 39, 252, 33, 15, 37, 149, 16, 117, 100, 68,
188, 94, 239, 101, 79, 77, 6, 18, 181, 214, 55, 95, 149, 38, 177, 185, 102, 206, 83,
184, 241, 37, 148, 225, 179, 153, 208, 130, 49, 207, 230, 194, 105, 164, 74, 168, 213,
135, 242, 54, 157]
gamma = mkG2Element [ 0xb5, 0xac, 0xb8, 0x00, 0xcd, 0x49, 0xed, 0x8c
, 0xbd, 0xdb, 0xf4, 0x91, 0xa1, 0xfc, 0xf8, 0xab
, 0xfc, 0x93, 0xf0, 0x9d, 0x38, 0xbb, 0xb2, 0xec
, 0xb6, 0xb0, 0x8e, 0x23, 0xa4, 0x64, 0x2c, 0xe5
, 0x9c, 0x9b, 0x03, 0x86, 0x53, 0x9a, 0xc3, 0xce
, 0xcd, 0xfb, 0x66, 0xa9, 0xf0, 0x27, 0xfc, 0x21
, 0x0f, 0x25, 0x95, 0x10, 0x75, 0x64, 0x44, 0xbc
, 0x5e, 0xef, 0x65, 0x4f, 0x4d, 0x06, 0x12, 0xb5
, 0xd6, 0x37, 0x5f, 0x95, 0x26, 0xb1, 0xb9, 0x66
, 0xce, 0x53, 0xb8, 0xf1, 0x25, 0x94, 0xe1, 0xb3
, 0x99, 0xd0, 0x82, 0x31, 0xcf, 0xe6, 0xc2, 0x69
, 0xa4, 0x4a, 0xa8, 0xd5, 0x87, 0xf2, 0x36, 0x9d ]

delta :: SerialisedG2Element
delta = SerialisedG2Element $ toBuiltin $ BS.pack [
179, 170, 121, 123, 175, 163, 154, 72, 246, 248, 124, 36, 131, 200, 148, 194, 129, 200,
7, 130, 28, 71, 48, 31, 251, 117, 90, 207, 207, 210, 44, 35, 35, 206, 223, 99, 73, 199,
254, 221, 50, 0, 164, 174, 85, 134, 49, 229, 1, 210, 153, 235, 147, 19, 92, 7, 207,
105, 76, 161, 24, 209, 179, 134, 73, 5, 41, 198, 15, 87, 147, 92, 239, 168, 159, 202,
250, 19, 168, 63, 132, 32, 123, 118, 254, 7, 141, 200, 89, 212, 2, 116, 61, 70, 140,
21]
delta = mkG2Element [ 0xb3, 0xaa, 0x79, 0x7b, 0xaf, 0xa3, 0x9a, 0x48
, 0xf6, 0xf8, 0x7c, 0x24, 0x83, 0xc8, 0x94, 0xc2
, 0x81, 0xc8, 0x07, 0x82, 0x1c, 0x47, 0x30, 0x1f
, 0xfb, 0x75, 0x5a, 0xcf, 0xcf, 0xd2, 0x2c, 0x23
, 0x23, 0xce, 0xdf, 0x63, 0x49, 0xc7, 0xfe, 0xdd
, 0x32, 0x00, 0xa4, 0xae, 0x55, 0x86, 0x31, 0xe5
, 0x01, 0xd2, 0x99, 0xeb, 0x93, 0x13, 0x5c, 0x07
, 0xcf, 0x69, 0x4c, 0xa1, 0x18, 0xd1, 0xb3, 0x86
, 0x49, 0x05, 0x29, 0xc6, 0x0f, 0x57, 0x93, 0x5c
, 0xef, 0xa8, 0x9f, 0xca, 0xfa, 0x13, 0xa8, 0x3f
, 0x84, 0x20, 0x7b, 0x76, 0xfe, 0x07, 0x8d, 0xc8
, 0x59, 0xd4, 0x02, 0x74, 0x3d, 0x46, 0x8c, 0x15 ]

gamma_abc_1 :: SerialisedG1Element
gamma_abc_1 = SerialisedG1Element $ toBuiltin $ BS.pack [
183, 246, 208, 109, 211, 229, 36, 110, 246, 181, 27, 7, 92, 48, 182, 143, 212, 144,
251, 248, 94, 2, 5, 247, 159, 160, 77, 129, 19, 49, 146, 19, 148, 99, 181, 232, 239,
178, 44, 57, 239, 61, 209, 197, 9, 32, 21, 184]
gamma_abc_1 = mkG1Element [ 0xb7, 0xf6, 0xd0, 0x6d, 0xd3, 0xe5, 0x24, 0x6e
, 0xf6, 0xb5, 0x1b, 0x07, 0x5c, 0x30, 0xb6, 0x8f
, 0xd4, 0x90, 0xfb, 0xf8, 0x5e, 0x02, 0x05, 0xf7
, 0x9f, 0xa0, 0x4d, 0x81, 0x13, 0x31, 0x92, 0x13
, 0x94, 0x63, 0xb5, 0xe8, 0xef, 0xb2, 0x2c, 0x39
, 0xef, 0x3d, 0xd1, 0xc5, 0x09, 0x20, 0x15, 0xb8 ]

gamma_abc_2 :: SerialisedG1Element
gamma_abc_2 = SerialisedG1Element $ toBuiltin $ BS.pack [
162, 230, 55, 219, 255, 82, 161, 228, 168, 197, 217, 133, 179, 65, 31, 197, 253, 68,
175, 96, 126, 66, 146, 62, 171, 180, 122, 216, 118, 225, 240, 43, 91, 224, 52, 173,
175, 115, 149, 42, 232, 175, 254, 229, 245, 24, 65, 222]
gamma_abc_2 = mkG1Element [ 0xa2, 0xe6, 0x37, 0xdb, 0xff, 0x52, 0xa1, 0xe4
, 0xa8, 0xc5, 0xd9, 0x85, 0xb3, 0x41, 0x1f, 0xc5
, 0xfd, 0x44, 0xaf, 0x60, 0x7e, 0x42, 0x92, 0x3e
, 0xab, 0xb4, 0x7a, 0xd8, 0x76, 0xe1, 0xf0, 0x2b
, 0x5b, 0xe0, 0x34, 0xad, 0xaf, 0x73, 0x95, 0x2a
, 0xe8, 0xaf, 0xfe, 0xe5, 0xf5, 0x18, 0x41, 0xde ]

a :: SerialisedG1Element
a = SerialisedG1Element $ toBuiltin $ BS.pack [
160, 91, 229, 15, 171, 87, 149, 187, 135, 132, 57, 58, 80, 69, 249, 135, 71, 23, 58,
210, 135, 245, 94, 33, 52, 113, 189, 85, 151, 69, 85, 20, 82, 69, 60, 76, 58, 57, 231,
200, 131, 16, 132, 159, 60, 122, 31, 195]
a = mkG1Element [ 0xa0, 0x5b, 0xe5, 0x0f, 0xab, 0x57, 0x95, 0xbb
, 0x87, 0x84, 0x39, 0x3a, 0x50, 0x45, 0xf9, 0x87
, 0x47, 0x17, 0x3a, 0xd2, 0x87, 0xf5, 0x5e, 0x21
, 0x34, 0x71, 0xbd, 0x55, 0x97, 0x45, 0x55, 0x14
, 0x52, 0x45, 0x3c, 0x4c, 0x3a, 0x39, 0xe7, 0xc8
, 0x83, 0x10, 0x84, 0x9f, 0x3c, 0x7a, 0x1f, 0xc3 ]

b :: SerialisedG2Element
b = SerialisedG2Element $ toBuiltin $ BS.pack [
173, 99, 72, 182, 183, 179, 76, 134, 191, 55, 167, 72, 205, 45, 130, 162, 80, 223, 198,
72, 70, 117, 102, 136, 37, 161, 111, 125, 166, 160, 77, 52, 36, 17, 62, 50, 92, 231,
52, 236, 68, 149, 96, 130, 192, 160, 110, 95, 24, 104, 225, 241, 166, 229, 89, 185,
254, 129, 241, 169, 1, 248, 166, 52, 27, 48, 28, 69, 178, 93, 48, 128, 251, 197, 3,
147, 83, 216, 247, 27, 85, 11, 39, 78, 196, 192, 124, 112, 205, 17, 83, 86, 44, 49, 76,
151]
b = mkG2Element [ 0xad, 0x63, 0x48, 0xb6, 0xb7, 0xb3, 0x4c, 0x86
, 0xbf, 0x37, 0xa7, 0x48, 0xcd, 0x2d, 0x82, 0xa2
, 0x50, 0xdf, 0xc6, 0x48, 0x46, 0x75, 0x66, 0x88
, 0x25, 0xa1, 0x6f, 0x7d, 0xa6, 0xa0, 0x4d, 0x34
, 0x24, 0x11, 0x3e, 0x32, 0x5c, 0xe7, 0x34, 0xec
, 0x44, 0x95, 0x60, 0x82, 0xc0, 0xa0, 0x6e, 0x5f
, 0x18, 0x68, 0xe1, 0xf1, 0xa6, 0xe5, 0x59, 0xb9
, 0xfe, 0x81, 0xf1, 0xa9, 0x01, 0xf8, 0xa6, 0x34
, 0x1b, 0x30, 0x1c, 0x45, 0xb2, 0x5d, 0x30, 0x80
, 0xfb, 0xc5, 0x03, 0x93, 0x53, 0xd8, 0xf7, 0x1b
, 0x55, 0x0b, 0x27, 0x4e, 0xc4, 0xc0, 0x7c, 0x70
, 0xcd, 0x11, 0x53, 0x56, 0x2c, 0x31, 0x4c, 0x97 ]

c :: SerialisedG1Element
c = SerialisedG1Element $ toBuiltin $ BS.pack [
181, 105, 204, 73, 27, 77, 240, 53, 203, 244, 158, 149, 31, 212, 254, 48, 170, 130, 54,
176, 226, 175, 104, 244, 193, 89, 44, 212, 13, 235, 235, 113, 138, 243, 54, 57, 219,
107, 193, 226, 218, 157, 152, 229, 83, 229, 234, 237]
c = mkG1Element [ 0xb5, 0x69, 0xcc, 0x49, 0x1b, 0x4d, 0xf0, 0x35
, 0xcb, 0xf4, 0x9e, 0x95, 0x1f, 0xd4, 0xfe, 0x30
, 0xaa, 0x82, 0x36, 0xb0, 0xe2, 0xaf, 0x68, 0xf4
, 0xc1, 0x59, 0x2c, 0xd4, 0x0d, 0xeb, 0xeb, 0x71
, 0x8a, 0xf3, 0x36, 0x39, 0xdb, 0x6b, 0xc1, 0xe2
, 0xda, 0x9d, 0x98, 0xe5, 0x53, 0xe5, 0xea, 0xed ]

{-# INLINABLE groth16Verify #-}
groth16Verify
Expand Down

0 comments on commit 7771982

Please sign in to comment.