Skip to content

Commit

Permalink
fix indent
Browse files Browse the repository at this point in the history
  • Loading branch information
agnxsh authored and mratsim committed Jan 14, 2024
1 parent 3996b76 commit 85227e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 351 deletions.
52 changes: 23 additions & 29 deletions constantine/eth_verkle_ipa/common_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,35 @@ import


func generate_random_points* [EC_P](points: var openArray[EC_P], ipaTranscript: var IpaTranscript, num_points: uint64) =
## generate_random_points generates random points on the curve with the hardcoded VerkleSeed
var points_found : seq[EC_P]
var incrementer : uint64 = 0
## generate_random_points generates random points on the curve with the hardcoded VerkleSeed -> VerkleSeed
var incrementer: uint64 = 0
var idx: int = 0
while true:
var ctx : sha256
ctx.init()
ctx.update(VerkleSeed)
ctx.update(incrementer.toBytes(bigEndian))
var hash : array[32, byte]
ctx.finish(hash)
ctx.clear()

var x {.noInit.}: Fp[Banderwagon]
var t {.noInit.}: matchingBigInt(Banderwagon)

t.unmarshal(hash, bigEndian)
x.fromBig(t)
while uint64(len(points)) != num_points:

incrementer = incrementer + 1
var digest : IpaTranscript.H
digest.init()
digest.update(VerkleSeed)

digest.update(incrementer.toBytes(bigEndian))
var hash {.noInit.} : array[IpaTranscript.H.digestSize(), byte]
digest.finish(hash)

var x_arr {.noInit.}: array[32, byte]
x_arr.marshal(x, bigEndian)
var x {.noInit.}: EC_P

let stat1 = x.deserialize(hash)
doAssert stat1 == cttCodecEcc_Success, "Deserialization Failure!"
incrementer = incrementer + 1

var x_p {.noInit.} : EC_P
let stat2 = x_p.deserialize(x_arr)
if stat2 == cttCodecEcc_Success:
points_found.add(x_p)
points[idx] = points_found[idx]
idx = idx + 1
var x_as_Bytes {.noInit.} : array[IpaTranscript.H.digestSize(), byte]
let stat2 = x_as_Bytes.serialize(x)
doAssert stat2 == cttCodecEcc_Success, "Serialization Failure!"


if uint64(points_found.len) == num_points:
break
var point_found {.noInit.} : EC_P
let stat3 = point_found.deserialize(x_as_Bytes)

doAssert stat3 == cttCodecEcc_Success, "Deserialization Failure!"
points[idx] = point_found
idx = idx + 1

# ############################################################
#
Expand Down
Binary file added tests/t_ethereum_verkle_ipa_primitives
Binary file not shown.
57 changes: 0 additions & 57 deletions tests/t_ethereum_verkle_ipa_primitives.nim
Original file line number Diff line number Diff line change
Expand Up @@ -147,63 +147,6 @@ suite "Barycentric Form Tests":

testBarycentricPrecomputeCoefficients()

# ############################################################
#
# Test for Random Point Generation and CRS Consistency
#
# ############################################################

suite "Random Elements Generation and CRS Consistency":
test "Test for Generating Random Points and Checking the 1st and 256th point with the Verkle Spec":

proc testGenPoints()=
var ipaConfig {.noInit.} : IPASettings
var ipaTranscript {.noInit.} : IpaTranscript[sha256, 32]
discard ipaConfig.genIPAConfig(ipaTranscript)

var basisPoints {.noInit.} : array[256, EC_P]
basisPoints.generate_random_points(ipaTranscript, 256)

var arr_byte {.noInit.} : array[256, array[32, byte]]
discard arr_byte.serializeBatch(basisPoints)

doAssert arr_byte[0].toHex() == "0x01587ad1336675eb912550ec2a28eb8923b824b490dd2ba82e48f14590a298a0", "Failed to generate the 1st point!"
doAssert arr_byte[255].toHex() == "0x3de2be346b539395b0c0de56a5ccca54a317f1b5c80107b0802af9a62276a4d8", "Failed to generate the 256th point!"

testGenPoints()

# ############################################################
#
# Test for Computing the Correct Vector Commitment
#
# ############################################################
## Test vectors are in this link, as bigint strings
## https://github.com/jsign/verkle-test-vectors/blob/main/crypto/001_vector_commitment.json#L5-L261

suite "Computing the Correct Vector Commitment":
test "Test for Vector Commitments from Verkle Test Vectors by @Ignacio":
proc testVectorComm() =
var ipaConfig: IPASettings
var ipaTranscript: IpaTranscript[sha256, 32]
let stat1 = ipaConfig.genIPAConfig(ipaTranscript)

var basisPoints : array[256, EC_P]
basisPoints.generate_random_points(ipaTranscript, 256)


var test_scalars {.noInit.}: array[256, Fr[Banderwagon]]
for i in 0 ..< 256:
test_scalars[i].fromHex(testScalarsHex[i])

var commitment {.noInit.} : EC_P
commitment.pedersen_commit_varbasis(basisPoints, basisPoints.len, test_scalars, test_scalars.len)

var arr22 {.noInit.} : Bytes
let stat33 = arr22.serialize(commitment)

doAssert "0x524996a95838712c4580220bb3de453d76cffd7f732f89914d4417bc8e99b513" == arr22.toHex(), "bit string does not match expected"
testVectorComm()

# ############################################################
#
# Test for Transcript and Challenge Scalar
Expand Down
Loading

0 comments on commit 85227e0

Please sign in to comment.