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) ) 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) ) 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) ) 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: case node.kind:
of Input: Node[T](kind: Input , inp: node.inp ) of Input: Node[T](kind: Input , inp: node.inp )
of Const: Node[T](kind: Const , kst: node.kst ) of Const: Node[T](kind: Const , kst: node.kst )

View File

@ -1,4 +1,6 @@
{.push raises: [].}
import std/bitops import std/bitops
import std/tables import std/tables
@ -14,7 +16,7 @@ func bigIntBitwiseComplement(x: B): B =
var bytes1 : seq[byte] = newSeq[byte](32) var bytes1 : seq[byte] = newSeq[byte](32)
var bytes2 : seq[byte] = newSeq[byte](32) var bytes2 : seq[byte] = newSeq[byte](32)
marshal(bytes1, x, littleEndian) marshal(bytes1, x, littleEndian)
for i in 0..<32: for i in 0..<32:
bytes2[i] = bitxor( bytes1[i] , 0xff ) bytes2[i] = bitxor( bytes1[i] , 0xff )
var output : B var output : B
unmarshal(output, bytes2, littleEndian) unmarshal(output, bytes2, littleEndian)
@ -26,7 +28,7 @@ func bigIntBitwiseAnd(x, y: B): B =
var bytes3 : seq[byte] = newSeq[byte](32) var bytes3 : seq[byte] = newSeq[byte](32)
marshal(bytes1, x, littleEndian) marshal(bytes1, x, littleEndian)
marshal(bytes2, y, littleEndian) marshal(bytes2, y, littleEndian)
for i in 0..<32: for i in 0..<32:
bytes3[i] = bitand( bytes1[i] , bytes2[i] ) bytes3[i] = bitand( bytes1[i] , bytes2[i] )
var output : B var output : B
unmarshal(output, bytes3, littleEndian) unmarshal(output, bytes3, littleEndian)
@ -38,7 +40,7 @@ func bigIntBitwiseOr(x, y: B): B =
var bytes3 : seq[byte] = newSeq[byte](32) var bytes3 : seq[byte] = newSeq[byte](32)
marshal(bytes1, x, littleEndian) marshal(bytes1, x, littleEndian)
marshal(bytes2, y, littleEndian) marshal(bytes2, y, littleEndian)
for i in 0..<32: for i in 0..<32:
bytes3[i] = bitor( bytes1[i] , bytes2[i] ) bytes3[i] = bitor( bytes1[i] , bytes2[i] )
var output : B var output : B
unmarshal(output, bytes3, littleEndian) unmarshal(output, bytes3, littleEndian)
@ -50,7 +52,7 @@ func bigIntBitwiseXor(x, y: B): B =
var bytes3 : seq[byte] = newSeq[byte](32) var bytes3 : seq[byte] = newSeq[byte](32)
marshal(bytes1, x, littleEndian) marshal(bytes1, x, littleEndian)
marshal(bytes2, y, littleEndian) marshal(bytes2, y, littleEndian)
for i in 0..<32: for i in 0..<32:
bytes3[i] = bitxor( bytes1[i] , bytes2[i] ) bytes3[i] = bitxor( bytes1[i] , bytes2[i] )
var output : B var output : B
unmarshal(output, bytes3, littleEndian) unmarshal(output, bytes3, littleEndian)
@ -61,20 +63,20 @@ func bigIntBitwiseXor(x, y: B): B =
func applyFieldMask(big : B) : F = func applyFieldMask(big : B) : F =
return bigToF( bigIntBitwiseAnd( fieldMask, big ) ) return bigToF( bigIntBitwiseAnd( fieldMask, big ) )
func fieldComplement(x: F): F = func fieldComplement(x: F): F =
let big1 = fToBig(x) let big1 = fToBig(x)
let comp = bigIntBitwiseComplement( big1 ) let comp = bigIntBitwiseComplement( big1 )
return applyFieldMask(comp) return applyFieldMask(comp)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
func fieldNegateB(x : B): B = func fieldNegateB(x : B): B =
if bool(isZero(x)): if bool(isZero(x)):
return x return x
else: else:
return fieldPrime - x return fieldPrime - x
func smallShiftRightB(x: B, k: int): B = func smallShiftRightB(x: B, k: int): B =
if (k == 0): if (k == 0):
return x return x
elif (k < 64): elif (k < 64):
@ -88,9 +90,9 @@ func smallShiftRightB(x: B, k: int): B =
return smallShiftRightB(y, k-63) return smallShiftRightB(y, k-63)
func shiftLeftF*( x: F, kbig: B ) : F func shiftLeftF*( x: F, kbig: B ) : F
func shiftRightF*( 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)): if (isZeroB(kbig)):
return x return x
elif bool(kbig >= halfPrimePlus1): elif bool(kbig >= halfPrimePlus1):
@ -104,7 +106,7 @@ func shiftLeftF*( x: F, kbig: B ) : F =
let _ = y.double() let _ = y.double()
return applyFieldMask( y ) return applyFieldMask( y )
func shiftRightF*( x: F, kbig: B ) : F = func shiftRightF*( x: F, kbig: B ) : F =
if (isZeroB(kbig)): if (isZeroB(kbig)):
return x # WTF constantine ?!?!?! return x # WTF constantine ?!?!?!
if bool(kbig >= halfPrimePlus1): if bool(kbig >= halfPrimePlus1):
@ -117,7 +119,7 @@ func shiftRightF*( x: F, kbig: B ) : F =
return bigToF( smallShiftRightB( y , k ) ) return bigToF( smallShiftRightB( y , k ) )
#[ #[
proc shiftSanityCheck*() = proc shiftSanityCheck*() =
let x: F = intToF(12345678903) let x: F = intToF(12345678903)
let k: B = uintToB(8) let k: B = uintToB(8)
let nk: B = fieldPrime - k let nk: B = fieldPrime - k
@ -136,14 +138,14 @@ proc shiftSanityCheck*() =
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
func evalUnoOpNode(op: UnoOp, x: F): F = func evalUnoOpNode(op: UnoOp, x: F): F =
case op: case op:
of Neg: return negF(x) of Neg: return negF(x)
of Id: return x of Id: return x
of LNot: return boolToF( not (fToBool x) ) of LNot: return boolToF( not (fToBool x) )
of Bnot: return fieldComplement(x) of Bnot: return fieldComplement(x)
func evalDuoOpNode(op: DuoOp, x: F, y: F): F = func evalDuoOpNode(op: DuoOp, x: F, y: F): F =
case op: case op:
of Mul: return x * y of Mul: return x * y
of Div: return if isZeroF(y): zeroF else: x / y of Div: return if isZeroF(y): zeroF else: x / y
@ -166,19 +168,19 @@ func evalDuoOpNode(op: DuoOp, x: F, y: F): F =
of Band: return bigToF( bigIntBitwiseAnd( fToBig(x) , fToBig(y) ) ) of Band: return bigToF( bigIntBitwiseAnd( fToBig(x) , fToBig(y) ) )
of Bxor: return bigToF( bigIntBitwiseXor( fToBig(x) , fToBig(y) ) ) of Bxor: return bigToF( bigIntBitwiseXor( fToBig(x) , fToBig(y) ) )
func evalTresOpNode(op: TresOp, x: F, y: F, z: F): F = func evalTresOpNode(op: TresOp, x: F, y: F, z: F): F =
case op: case op:
of TernCond: of TernCond:
return (if fToBool(x): y else: z) return (if fToBool(x): y else: z)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
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: case node.kind:
of Input: return inputs[int(node.inp.idx)] of Input: inputs[int(node.inp.idx)]
of Const: return fromBigUInt(node.kst.bigVal) of Const: fromBigUInt(node.kst.bigVal)
of Uno: return evalUnoOpNode( node.uno.op , node.uno.arg1 ) of Uno: evalUnoOpNode( node.uno.op , node.uno.arg1 )
of Duo: return evalDuoOpNode( node.duo.op , node.duo.arg1 , node.duo.arg2 ) of Duo: 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 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/tables
import std/strformat import std/strformat
@ -15,7 +17,7 @@ proc expandInputs*(circuitInputs: seq[(string, SignalDescription)] , inputs: Inp
let k: int = int(desc.length) let k: int = int(desc.length)
let o: int = int(desc.offset) let o: int = int(desc.offset)
assert( inputs.hasKey(key) , "input signal `" & key & "` not present" ) 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" ) assert( list.len == k , "input signal `" & key & "` has unexpected size" )
for i in 0..<k: for i in 0..<k:
table[o + i] = list[i] table[o + i] = list[i]
@ -23,7 +25,7 @@ proc expandInputs*(circuitInputs: seq[(string, SignalDescription)] , inputs: Inp
return table return table
# note: this contains temporary values which are not present in the actual witness # 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 sequence : seq[Node[uint32]] = graph.nodes
let graphMeta : GraphMetaData = graph.meta let graphMeta : GraphMetaData = graph.meta
@ -57,7 +59,7 @@ proc generateFullComputation*(graph: Graph, inputs: Inputs): seq[F] {.gcsafe.} =
echo " " 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 mapping: seq[uint32] = graph.meta.witnessMapping.mapping
let pre_witness = generateFullComputation(graph, inputs) let pre_witness = generateFullComputation(graph, inputs)
var output: seq[F] = newSeq[F](mapping.len) var output: seq[F] = newSeq[F](mapping.len)