diff --git a/codex/contracts/proofs.nim b/codex/contracts/proofs.nim index 3b84a2e7..a7a59351 100644 --- a/codex/contracts/proofs.nim +++ b/codex/contracts/proofs.nim @@ -10,13 +10,20 @@ type G1Point* = object x*: UInt256 y*: UInt256 + # A field element F_{p^2} encoded as `real + i * imag` + Fp2Element* = object + real*: UInt256 + imag*: UInt256 G2Point* = object - x*: array[2, UInt256] - y*: array[2, UInt256] + x*: Fp2Element + y*: Fp2Element func solidityType*(_: type G1Point): string = solidityType(G1Point.fieldTypes) +func solidityType*(_: type Fp2Element): string = + solidityType(Fp2Element.fieldTypes) + func solidityType*(_: type G2Point): string = solidityType(G2Point.fieldTypes) @@ -26,6 +33,9 @@ func solidityType*(_: type Groth16Proof): string = func encode*(encoder: var AbiEncoder, point: G1Point) = encoder.write(point.fieldValues) +func encode*(encoder: var AbiEncoder, element: Fp2Element) = + encoder.write(element.fieldValues) + func encode*(encoder: var AbiEncoder, point: G2Point) = encoder.write(point.fieldValues) diff --git a/codex/slots/proofs/backends/converters.nim b/codex/slots/proofs/backends/converters.nim index b405ab1f..60c64f5c 100644 --- a/codex/slots/proofs/backends/converters.nim +++ b/codex/slots/proofs/backends/converters.nim @@ -54,14 +54,14 @@ func toG1*(g: CircomG1): G1Point = func toG2*(g: CircomG2): G2Point = G2Point( - x: [ - UInt256.fromBytesLE(g.x[0]), - UInt256.fromBytesLE(g.x[1]) - ], - y: [ - UInt256.fromBytesLE(g.y[0]), - UInt256.fromBytesLE(g.y[1]) - ]) + x: Fp2Element( + real: UInt256.fromBytesLE(g.x[0]), + imag: UInt256.fromBytesLE(g.x[1]) + ), + y: Fp2Element( + real: UInt256.fromBytesLE(g.y[0]), + imag: UInt256.fromBytesLE(g.y[1]) + )) func toGroth16Proof*(proof: CircomProof): Groth16Proof = Groth16Proof( diff --git a/tests/examples.nim b/tests/examples.nim index c70d0dbb..bb506438 100644 --- a/tests/examples.nim +++ b/tests/examples.nim @@ -78,8 +78,8 @@ proc example(_: type G1Point): G1Point = proc example(_: type G2Point): G2Point = G2Point( - x: [UInt256.example, UInt256.example], - y: [UInt256.example, UInt256.example] + x: Fp2Element(real: UInt256.example, imag: UInt256.example), + y: Fp2Element(real: UInt256.example, imag: UInt256.example) ) proc example*(_: type Groth16Proof): Groth16Proof =