Nim: export witness file

This commit is contained in:
Balazs Komuves 2025-03-15 14:06:24 +01:00
parent ec327944e2
commit b832b20442
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
2 changed files with 63 additions and 1 deletions

View File

@ -0,0 +1,48 @@
import std/streams
import pkg/constantine/math/io/io_bigints
import ./field
#-------------------------------------------------------------------------------
proc writeBigInt(s: Stream, big: B) =
var bytes : seq[byte] = newSeq[byte](32)
marshal(bytes, big, littleEndian)
# s.write(bytes) # ??!?!! go home Nim, you are drunk
for b in bytes: s.write(b)
proc writeFelt(s: Stream, x: F) =
s.writeBigInt(fToBig(x))
#-------------------------------------------------------------------------------
proc writeHeader(s: Stream, witnessLen: int) =
# global header
s.write("wtns") # magic word "wtns"
s.write(uint32(2)) # version
s.write(uint32(2)) # number of sections
# section 1
s.write(uint32(1) ) # section id
s.write(uint64(0x28)) # section length
s.write(uint32(32) ) # 32 bytes per field element
s.writeBigInt(fieldPrime) # the field prime
s.write(uint32(witnessLen)) # number of witness elements
let nbytes: uint64 = 32 * uint64(witnessLen)
# section 2
s.write(uint32(2) ) # section id
s.write(nbytes) # section length
#-------------------------------------------------------------------------------
proc exportWitness*(filepath: string, witness: seq[F]) =
var stream = newFileStream(filepath, fmWrite)
stream.writeHeader(witness.len)
for i in 0..<witness.len:
stream.writeFelt( witness[i] )
#-------------------------------------------------------------------------------

View File

@ -1,16 +1,30 @@
import std/sequtils
import circom_witnessgen/field
import circom_witnessgen/graph
import circom_witnessgen/load
import circom_witnessgen/input_json
import circom_witnessgen/export_wtns
#-------------------------------------------------------------------------------
const graph_file: string = "../tmp/graph4.bin"
const input_file: string = "../tmp/input4.json"
const wtns_file: string = "../tmp/nim4.wtns"
#-------------------------------------------------------------------------------
when isMainModule:
echo "\nloading in " & input_file
let inp = loadInputJSON(input_file)
printInputs(inp)
# printInputs(inp)
echo "\nloading in " & graph_file
let gr = loadGraph(graph_file)
# echo $gr
let us: seq[int] = @[1,2,3,4,5,6,7]
let wtns: seq[F] = us.map(intToF);
exportWitness(wtns_file, wtns)