refactor: update function signatures to include raises annotations for better error handling

This commit is contained in:
Dmitriy Ryajov 2025-05-28 19:31:02 -06:00
parent f142321d7f
commit 1291cf0e62
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
3 changed files with 33 additions and 29 deletions

View File

@ -112,16 +112,16 @@ func fromBigUInt*(big: BigUInt): F =
#-------------------------------------------------------------------------------
proc fmapUno[S,T]( fun: ((S) {.gcsafe.} -> T) , node: UnoOpNode[S]): UnoOpNode[T] =
proc fmapUno[S,T]( fun: ((S) {.raises: [], gcsafe.} -> T) , node: UnoOpNode[S]): UnoOpNode[T] =
UnoOpNode[T]( op: node.op, arg1: fun(node.arg1) )
proc fmapDuo[S,T]( fun: ((S) {.gcsafe.} -> T) , node: DuoOpNode[S]): DuoOpNode[T] =
proc fmapDuo[S,T]( fun: ((S) {.raises: [], gcsafe.} -> T) , node: DuoOpNode[S]): DuoOpNode[T] =
DuoOpNode[T]( op: node.op, arg1: fun(node.arg1), arg2: fun(node.arg2) )
proc fmapTres[S,T]( fun: ((S) {.gcsafe.} -> T) , node: TresOpNode[S]): TresOpNode[T] =
proc fmapTres[S,T]( fun: ((S) {.raises: [], gcsafe.} -> T) , node: TresOpNode[S]): TresOpNode[T] =
TresOpNode[T]( op: node.op, arg1: fun(node.arg1), arg2: fun(node.arg2), arg3: fun(node.arg3) )
proc fmap* [S,T]( fun: ((S) {.gcsafe.} -> T) , node: Node[S]): Node[T] =
proc fmap* [S,T]( fun: ((S) {.raises: [], gcsafe.} -> T) , node: Node[S]): Node[T] =
case node.kind:
of Input: Node[T](kind: Input , inp: node.inp )
of Const: Node[T](kind: Const , kst: node.kst )

View File

@ -1,4 +1,6 @@
{.push raises: [].}
import std/bitops
import std/tables
@ -90,7 +92,7 @@ func smallShiftRightB(x: B, k: int): B =
func shiftLeftF*( x: F, kbig: B ) : F
func shiftRightF*( x: F, kbig: B ) : F
func shiftLeftF*( x: F, kbig: B ) : F =
func shiftLeftF*( x: F, kbig: B ) : F {.raises: [].} =
if (isZeroB(kbig)):
return x
elif bool(kbig >= halfPrimePlus1):
@ -173,12 +175,12 @@ func evalTresOpNode(op: TresOp, x: F, y: F, z: F): F =
#-------------------------------------------------------------------------------
func evalNode*( inputs: Table[int,F] , node: Node[F] ): F =
func evalNode*( inputs: Table[int,F] , node: Node[F] ): F {.raises: KeyError.} =
case node.kind:
of Input: return inputs[int(node.inp.idx)]
of Const: return fromBigUInt(node.kst.bigVal)
of Uno: return evalUnoOpNode( node.uno.op , node.uno.arg1 )
of Duo: return evalDuoOpNode( node.duo.op , node.duo.arg1 , node.duo.arg2 )
of Tres: return evalTresOpNode(node.tres.op, node.tres.arg1, node.tres.arg2, node.tres.arg3 )
of Input: inputs[int(node.inp.idx)]
of Const: fromBigUInt(node.kst.bigVal)
of Uno: evalUnoOpNode( node.uno.op , node.uno.arg1 )
of Duo: evalDuoOpNode( node.duo.op , node.duo.arg1 , node.duo.arg2 )
of Tres: evalTresOpNode(node.tres.op, node.tres.arg1, node.tres.arg2, node.tres.arg3 )
#-------------------------------------------------------------------------------

View File

@ -1,4 +1,6 @@
{.push raises: [].}
import std/tables
import std/strformat
@ -15,7 +17,7 @@ proc expandInputs*(circuitInputs: seq[(string, SignalDescription)] , inputs: Inp
let k: int = int(desc.length)
let o: int = int(desc.offset)
assert( inputs.hasKey(key) , "input signal `" & key & "` not present" )
let list: seq[F] = inputs[key]
let list: seq[F] = try: inputs[key] except KeyError as exc: raiseAssert(exc.msg)
assert( list.len == k , "input signal `" & key & "` has unexpected size" )
for i in 0..<k:
table[o + i] = list[i]
@ -23,7 +25,7 @@ proc expandInputs*(circuitInputs: seq[(string, SignalDescription)] , inputs: Inp
return table
# note: this contains temporary values which are not present in the actual witness
proc generateFullComputation*(graph: Graph, inputs: Inputs): seq[F] {.gcsafe.} =
proc generateFullComputation*(graph: Graph, inputs: Inputs): seq[F] {.gcsafe, raises: [KeyError].} =
let sequence : seq[Node[uint32]] = graph.nodes
let graphMeta : GraphMetaData = graph.meta
@ -57,7 +59,7 @@ proc generateFullComputation*(graph: Graph, inputs: Inputs): seq[F] {.gcsafe.} =
echo " "
]#
proc generateWitness*(graph: Graph, inputs: Inputs): seq[F] {.gcsafe.} =
proc generateWitness*(graph: Graph, inputs: Inputs): seq[F] {.gcsafe, raises: [KeyError].} =
let mapping: seq[uint32] = graph.meta.witnessMapping.mapping
let pre_witness = generateFullComputation(graph, inputs)
var output: seq[F] = newSeq[F](mapping.len)