additional api with global kzg settings

This commit is contained in:
jangko 2023-02-09 10:02:06 +07:00
parent d0af4143e2
commit 4a52112a98
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
6 changed files with 171 additions and 7 deletions

View File

@ -8,8 +8,9 @@
# those terms.
import
kzg4844/kzg
kzg4844/kzg,
kzg4844/kzg_ex
export
kzg
kzg,
kzg_ex

101
kzg4844/kzg_ex.nim Normal file
View File

@ -0,0 +1,101 @@
# nim-kzg4844
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import
stew/results,
./kzg
export
results,
kzg
type
Kzg* = object
Bytes32 = array[32, byte]
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
##############################################################
# Private helpers
##############################################################
var gCtx: KzgCtx
const
GlobalCtxErr = "kzg global context not loaded"
template setupCtx(body: untyped): untyped =
let res = body
if res.isErr:
return err(res.error)
gCtx = res.get
ok()
template verifyCtx(body: untyped): untyped =
if gCtx.isNil:
return err(GlobalCtxErr)
body
##############################################################
# Public functions
##############################################################
proc loadTrustedSetup*(_: type Kzg,
input: File): Result[void, string] =
setupCtx:
kzg.loadTrustedSetup(input)
proc loadTrustedSetup*(_: type Kzg,
fileName: string): Result[void, string] =
setupCtx:
kzg.loadTrustedSetup(fileName)
proc loadTrustedSetup*(_: type Kzg, g1: openArray[G1Data],
g2: openArray[G2Data]):
Result[void, string] =
setupCtx:
kzg.loadTrustedSetup(g1, g2)
proc loadTrustedSetupFromString*(_: type Kzg,
input: string): Result[void, string] =
setupCtx:
kzg.loadTrustedSetupFromString(input)
proc toCommitment*(blob: KzgBlob):
Result[KzgCommitment, string] =
verifyCtx:
gCtx.toCommitment(blob)
proc computeProof*(blobs: openArray[KzgBlob]):
Result[KzgProof, string] =
verifyCtx:
gCtx.computeProof(blobs)
proc verifyProof*(blobs: openArray[KzgBlob],
commitments: openArray[KzgCommitment],
proof: KzgProof): Result[void, string] =
verifyCtx:
gCtx.verifyProof(blobs, commitments, proof)
proc computeProof*(blob: KzgBlob,
z: Bytes32): Result[KzgProof, string] =
verifyCtx:
gCtx.computeProof(blob, z)
proc verifyProof*(commitment: KzgCommitment,
z: Bytes32, # Input Point
y: Bytes32, # Claimed Value
proof: KzgProof): Result[void, string] =
verifyCtx:
gCtx.verifyProof(commitment, z, y, proof)
{. pop .}

View File

@ -9,4 +9,5 @@
import
test_abi,
test_kzg
test_kzg,
test_kzg_ex

View File

@ -31,9 +31,6 @@ proc createKateBlobs(ctx: KzgCtx, n: int): KateBlobs =
result.kates.add(res.get)
suite "verify proof (high-level)":
const
trustedSetup = staticRead("../" & trustedSetupFile)
var ctx: KzgCtx
test "load trusted setup from string":

63
tests/test_kzg_ex.nim Normal file
View File

@ -0,0 +1,63 @@
# nim-kzg4844
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
{.used.}
import
std/[sysrand],
unittest2,
../kzg4844/kzg_ex,
./types
proc createKateBlobs(n: int): KateBlobs =
var blob: KzgBlob
for i in 0..<n:
discard urandom(blob)
for i in 0..<len(blob):
# don't overflow modulus
if blob[i] > MAX_TOP_BYTE and i %% BYTES_PER_FIELD_ELEMENT == 31:
blob[i] = MAX_TOP_BYTE
result.blobs.add(blob)
for i in 0..<n:
let res = toCommitment(result.blobs[i])
doAssert res.isOk
result.kates.add(res.get)
suite "verify proof (extended version)":
test "load trusted setup from string":
let res = Kzg.loadTrustedSetupFromString(trustedSetup)
check res.isOk
test "verify proof success":
let kb = createKateBlobs(nblobs)
let pres = computeProof(kb.blobs)
check pres.isOk
let res = verifyProof(kb.blobs, kb.kates, pres.get)
check res.isOk
test "verify proof failure":
let kb = createKateBlobs(nblobs)
let pres = computeProof(kb.blobs)
check pres.isOk
let other = createKateBlobs(nblobs)
let badProof = computeProof(other.blobs)
check badProof.isOk
let res = verifyProof(kb.blobs, kb.kates, badProof.get)
check res.isErr
test "verify proof":
let kp = computeProof(blob, inputPoint)
check kp.isOk
check kp.get == proof
let res = verifyProof(commitment, inputPoint, claimedValue, kp.get)
check res.isOk

View File

@ -27,3 +27,4 @@ const
inputPoint* = hexToByteArray[32]("AD5570F5A3810B7AF9D4B24BC1C2EA670245DB2EAA49AAE654B8F7393A9A6214")
claimedValue* = hexToByteArray[32]("17F10B3E7BFC7894E4150626380E226D7B1D0085550067787F259A7F55BB5D36")
nblobs* = 5
trustedSetup* = staticRead("../" & trustedSetupFile)