diff --git a/nim/circom_witnessgen/export_wtns.nim b/nim/circom_witnessgen/export_wtns.nim new file mode 100644 index 0000000..ebe7512 --- /dev/null +++ b/nim/circom_witnessgen/export_wtns.nim @@ -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..