Skip to content

Commit 39391f7

Browse files
make compilation
1 parent fe0a659 commit 39391f7

File tree

3 files changed

+119
-108
lines changed

3 files changed

+119
-108
lines changed

waku/waku_rln_relay/group_manager/on_chain/group_manager.nim

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,19 @@ method generateProof*(
355355
return err("Failed to convert identity secret to field element")
356356
freeVec(identitySecretVec)
357357

358-
# Convert user message limit to CFr
359-
var userMessageLimitCFr: CFr
360-
if not ffi_cfr_from_uint(g.userMessageLimit.get(), addr userMessageLimitCFr):
358+
# Convert user message limit to CFr using new API
359+
let userMessageLimitCFrPtr = uint_to_cfr(g.userMessageLimit.get().uint32)
360+
if userMessageLimitCFrPtr == nil:
361361
return err("Failed to convert user message limit to field element")
362+
let userMessageLimitCFr = userMessageLimitCFrPtr[]
363+
cfr_free(userMessageLimitCFrPtr)
362364

363-
# Convert message ID to CFr
364-
var messageIdCFr: CFr
365-
if not ffi_cfr_from_uint(messageId, addr messageIdCFr):
365+
# Convert message ID to CFr using new API
366+
let messageIdCFrPtr = uint_to_cfr(messageId.uint32)
367+
if messageIdCFrPtr == nil:
366368
return err("Failed to convert message ID to field element")
369+
let messageIdCFr = messageIdCFrPtr[]
370+
cfr_free(messageIdCFrPtr)
367371

368372
# Convert merkle proof path elements to CFr
369373
var pathElementsCFr: seq[CFr]
@@ -430,34 +434,13 @@ method generateProof*(
430434
return err("Failed to generate proof")
431435

432436
# Convert ffi_RLNProof to RateLimitProof format
433-
# Serialize CFr fields to bytes
437+
# Serialize CFr fields to bytes using new API
434438
var proofVec: Vec[uint8]
435-
var rootVec: Vec[uint8]
436-
var extNullVec: Vec[uint8]
437-
var shareXVec: Vec[uint8]
438-
var shareYVec: Vec[uint8]
439-
var nullifierVec: Vec[uint8]
440-
441-
if not ffi_cfr_serialize(addr ffi2Proof.root, addr rootVec):
442-
return err("Failed to serialize proof root")
443-
if not ffi_cfr_serialize(addr ffi2Proof.external_nullifier, addr extNullVec):
444-
freeVec(rootVec)
445-
return err("Failed to serialize external nullifier")
446-
if not ffi_cfr_serialize(addr ffi2Proof.share_x, addr shareXVec):
447-
freeVec(rootVec)
448-
freeVec(extNullVec)
449-
return err("Failed to serialize share_x")
450-
if not ffi_cfr_serialize(addr ffi2Proof.share_y, addr shareYVec):
451-
freeVec(rootVec)
452-
freeVec(extNullVec)
453-
freeVec(shareXVec)
454-
return err("Failed to serialize share_y")
455-
if not ffi_cfr_serialize(addr ffi2Proof.nullifier, addr nullifierVec):
456-
freeVec(rootVec)
457-
freeVec(extNullVec)
458-
freeVec(shareXVec)
459-
freeVec(shareYVec)
460-
return err("Failed to serialize nullifier")
439+
var rootVec = cfr_to_bytes_le(addr ffi2Proof.root)
440+
var extNullVec = cfr_to_bytes_le(addr ffi2Proof.external_nullifier)
441+
var shareXVec = cfr_to_bytes_le(addr ffi2Proof.share_x)
442+
var shareYVec = cfr_to_bytes_le(addr ffi2Proof.share_y)
443+
var nullifierVec = cfr_to_bytes_le(addr ffi2Proof.nullifier)
461444

462445
# Convert Vec to fixed-size arrays
463446
var

waku/waku_rln_relay/rln/rln_interface.nim

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ type ffi_MerkleProof* = object
5555

5656
#-------------------------------- Identity Generation -----------------------------------------
5757

58-
proc ffi_key_gen*(
59-
output: ptr ffi_IdentityCredential
60-
): bool {.importc: "ffi_key_gen".}
58+
proc ffi_key_gen*(output: ptr ffi_IdentityCredential): bool {.importc: "ffi_key_gen".}
6159

6260
## Generates identity trapdoor, identity nullifier, identity secret hash and id commitment
6361
## Output is written directly to the ffi_IdentityCredential struct
@@ -199,54 +197,89 @@ proc ffi_hash_to_field_be*(
199197
## Hashes arbitrary bytes to a field element (big-endian)
200198
## Returns true on success, false on failure
201199

202-
proc ffi_poseidon_hash*(
203-
inputs: ptr Vec[CFr], output: ptr CFr
204-
): bool {.importc: "ffi_poseidon_hash".}
200+
proc ffi_poseidon_hash_pair*(
201+
a: ptr CFr, b: ptr CFr
202+
): ptr CFr {.importc: "ffi_poseidon_hash_pair".}
205203

206-
## Computes Poseidon hash of field elements
204+
## Computes Poseidon hash of two field elements
207205
## Used for identity secret hash and external nullifier
208-
## Returns true on success, false on failure
206+
## Returns pointer to boxed CFr result (must be freed with cfr_free)
209207

210208
#-------------------------------- Serialization Utilities -----------------------------------------
211209

212-
proc ffi_cfr_serialize*(
213-
fr: ptr CFr, output: ptr Vec[uint8]
214-
): bool {.importc: "ffi_cfr_serialize".}
210+
proc cfr_to_bytes_le*(cfr: ptr CFr): Vec[uint8] {.importc: "cfr_to_bytes_le".}
215211

216-
## Serializes a field element to bytes
217-
## Returns true on success, false on failure
212+
## Serializes a field element to bytes (little-endian)
213+
## Returns Vec[uint8] containing the serialized bytes
218214

219-
proc ffi_cfr_deserialize*(
220-
bytes: ptr Vec[uint8], output: ptr CFr
221-
): bool {.importc: "ffi_cfr_deserialize".}
215+
proc cfr_to_bytes_be*(cfr: ptr CFr): Vec[uint8] {.importc: "cfr_to_bytes_be".}
222216

223-
## Deserializes bytes to a field element
224-
## Returns true on success, false on failure
217+
## Serializes a field element to bytes (big-endian)
218+
## Returns Vec[uint8] containing the serialized bytes
225219

226-
proc ffi_vec_cfr_serialize*(
227-
vec: ptr Vec[CFr], output: ptr Vec[uint8]
228-
): bool {.importc: "ffi_vec_cfr_serialize".}
220+
proc bytes_le_to_cfr*(bytes: ptr Vec[uint8]): ptr CFr {.importc: "bytes_le_to_cfr".}
229221

230-
## Serializes a vector of field elements to bytes
231-
## Returns true on success, false on failure
222+
## Deserializes bytes (little-endian) to a field element
223+
## Returns pointer to boxed CFr (must be freed with cfr_free)
232224

233-
proc ffi_vec_cfr_deserialize*(
234-
bytes: ptr Vec[uint8], output: ptr Vec[CFr]
235-
): bool {.importc: "ffi_vec_cfr_deserialize".}
225+
proc bytes_be_to_cfr*(bytes: ptr Vec[uint8]): ptr CFr {.importc: "bytes_be_to_cfr".}
236226

237-
## Deserializes bytes to a vector of field elements
238-
## Returns true on success, false on failure
227+
## Deserializes bytes (big-endian) to a field element
228+
## Returns pointer to boxed CFr (must be freed with cfr_free)
229+
230+
proc vec_cfr_to_bytes_le*(
231+
vec: ptr Vec[CFr]
232+
): Vec[uint8] {.importc: "vec_cfr_to_bytes_le".}
233+
234+
## Serializes a vector of field elements to bytes (little-endian)
235+
## Returns Vec[uint8] containing the serialized bytes
236+
237+
proc vec_cfr_to_bytes_be*(
238+
vec: ptr Vec[CFr]
239+
): Vec[uint8] {.importc: "vec_cfr_to_bytes_be".}
240+
241+
## Serializes a vector of field elements to bytes (big-endian)
242+
## Returns Vec[uint8] containing the serialized bytes
243+
244+
## CResult type for functions that can return errors
245+
type CResult*[T, E] = object
246+
ok*: ptr T
247+
err*: ptr E
239248

240-
proc ffi_cfr_from_uint*(
241-
value: uint64, output: ptr CFr
242-
): bool {.importc: "ffi_cfr_from_uint".}
249+
proc bytes_le_to_vec_cfr*(
250+
bytes: ptr Vec[uint8]
251+
): CResult[ptr Vec[CFr], cstring] {.importc: "bytes_le_to_vec_cfr".}
252+
253+
## Deserializes bytes (little-endian) to a vector of field elements
254+
## Returns CResult with either ok (ptr Vec[CFr]) or err (cstring)
255+
256+
proc bytes_be_to_vec_cfr*(
257+
bytes: ptr Vec[uint8]
258+
): CResult[ptr Vec[CFr], cstring] {.importc: "bytes_be_to_vec_cfr".}
259+
260+
## Deserializes bytes (big-endian) to a vector of field elements
261+
## Returns CResult with either ok (ptr Vec[CFr]) or err (cstring)
262+
263+
proc uint_to_cfr*(value: uint32): ptr CFr {.importc: "uint_to_cfr".}
243264

244265
## Creates a field element from an unsigned integer
245-
## Returns true on success, false on failure
266+
## Returns pointer to boxed CFr (must be freed with cfr_free)
267+
## Note: Takes uint32, not uint64
246268

247-
proc ffi_cfr_zero*(output: ptr CFr): bool {.importc: "ffi_cfr_zero".}
269+
proc cfr_zero*(): ptr CFr {.importc: "cfr_zero".}
248270
## Creates a zero field element
249-
## Returns true on success, false on failure
271+
## Returns pointer to boxed CFr (must be freed with cfr_free)
272+
273+
proc cfr_one*(): ptr CFr {.importc: "cfr_one".}
274+
## Creates a one field element
275+
## Returns pointer to boxed CFr (must be freed with cfr_free)
276+
277+
proc cfr_free*(cfr: ptr CFr) {.importc: "cfr_free".}
278+
## Frees a boxed CFr
279+
## Must be called for all CFr pointers returned by the API
280+
281+
proc cfr_debug*(cfr: ptr CFr): cstring {.importc: "cfr_debug".}
282+
## Returns debug string representation of CFr
250283

251284
#-------------------------------- Helper Procedures -----------------------------------------
252285

waku/waku_rln_relay/rln/wrappers.nim

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,11 @@ proc membershipKeyGen*(): RlnRelayResult[IdentityCredential] =
3333
return err("error in key generation")
3434

3535
# Serialize CFr fields to bytes for compatibility with existing code
36-
var
37-
idTrapdoorVec: Vec[uint8]
38-
idNullifierVec: Vec[uint8]
39-
idSecretHashVec: Vec[uint8]
40-
idCommitmentVec: Vec[uint8]
41-
42-
if not ffi_cfr_serialize(addr credential.identity_trapdoor, addr idTrapdoorVec):
43-
return err("failed to serialize identity trapdoor")
44-
if not ffi_cfr_serialize(addr credential.identity_nullifier, addr idNullifierVec):
45-
return err("failed to serialize identity nullifier")
46-
if not ffi_cfr_serialize(addr credential.identity_secret_hash, addr idSecretHashVec):
47-
return err("failed to serialize identity secret hash")
48-
if not ffi_cfr_serialize(addr credential.id_commitment, addr idCommitmentVec):
49-
return err("failed to serialize id commitment")
36+
# New API returns Vec directly instead of using output parameter
37+
var idTrapdoorVec = cfr_to_bytes_le(addr credential.identity_trapdoor)
38+
var idNullifierVec = cfr_to_bytes_le(addr credential.identity_nullifier)
39+
var idSecretHashVec = cfr_to_bytes_le(addr credential.identity_secret_hash)
40+
var idCommitmentVec = cfr_to_bytes_le(addr credential.id_commitment)
5041

5142
# Convert Vec to seq
5243
let identityCredential = IdentityCredential(
@@ -151,11 +142,8 @@ proc sha256*(data: openArray[byte]): RlnRelayResult[MerkleNode] =
151142
freeVec(inputVec)
152143
return err("error in sha256 hash")
153144

154-
# Serialize CFr to bytes
155-
var outputVec: Vec[uint8]
156-
if not ffi_cfr_serialize(addr outputCFr, addr outputVec):
157-
freeVec(inputVec)
158-
return err("error serializing hash output")
145+
# Serialize CFr to bytes using new API
146+
var outputVec = cfr_to_bytes_le(addr outputCFr)
159147

160148
# Convert to MerkleNode (32 bytes)
161149
var output: MerkleNode
@@ -188,31 +176,32 @@ proc poseidon*(data: seq[seq[byte]]): RlnRelayResult[array[32, byte]] =
188176
inputCFrs.add(cfr)
189177
freeVec(itemVec)
190178

191-
# Create Vec[CFr] for input
192-
var inputVec = newVec(inputCFrs)
193-
var outputCFr: CFr
194-
195-
let hashSuccess = ffi_poseidon_hash(addr inputVec, addr outputCFr)
179+
# New API only supports hashing pairs, so we need to hash iteratively
180+
# For now, if we have more than 2 elements, we'll need to implement tree hashing
181+
if inputCFrs.len != 2:
182+
return err("poseidon hash currently only supports 2 inputs with new API")
196183

197-
if not hashSuccess:
198-
freeVec(inputVec)
184+
# Hash the pair using new API
185+
let outputCFrPtr = ffi_poseidon_hash_pair(addr inputCFrs[0], addr inputCFrs[1])
186+
if outputCFrPtr == nil:
199187
return err("error in poseidon hash")
200188

201-
# Serialize output to bytes
202-
var outputVec: Vec[uint8]
203-
if not ffi_cfr_serialize(addr outputCFr, addr outputVec):
204-
freeVec(inputVec)
205-
return err("error serializing hash output")
189+
# Dereference the pointer to get the CFr value
190+
let outputCFr = outputCFrPtr[]
191+
192+
# Free the boxed CFr
193+
cfr_free(outputCFrPtr)
194+
195+
# Serialize output to bytes using new API
196+
var outputVec = cfr_to_bytes_le(addr outputCFr)
206197

207198
var output: array[32, byte]
208199
if outputVec.len >= 32:
209200
copyMem(addr output[0], outputVec.data, 32)
210201
else:
211-
freeVec(inputVec)
212202
freeVec(outputVec)
213203
return err("hash output too short")
214204

215-
freeVec(inputVec)
216205
freeVec(outputVec)
217206

218207
return ok(output)
@@ -292,15 +281,19 @@ proc generateProof*(
292281
return err("failed to convert identity secret")
293282
freeVec(identitySecretVec)
294283

295-
# Convert user message limit to CFr
296-
var userMessageLimitCFr: CFr
297-
if not ffi_cfr_from_uint(userMessageLimit, addr userMessageLimitCFr):
284+
# Convert user message limit to CFr using new API
285+
let userMessageLimitCFrPtr = uint_to_cfr(userMessageLimit.uint32)
286+
if userMessageLimitCFrPtr == nil:
298287
return err("failed to convert user message limit")
288+
let userMessageLimitCFr = userMessageLimitCFrPtr[]
289+
cfr_free(userMessageLimitCFrPtr)
299290

300-
# Convert message ID to CFr
301-
var messageIdCFr: CFr
302-
if not ffi_cfr_from_uint(messageId, addr messageIdCFr):
291+
# Convert message ID to CFr using new API
292+
let messageIdCFrPtr = uint_to_cfr(messageId.uint32)
293+
if messageIdCFrPtr == nil:
303294
return err("failed to convert message ID")
295+
let messageIdCFr = messageIdCFrPtr[]
296+
cfr_free(messageIdCFrPtr)
304297

305298
# Convert merkle proof path elements to CFr
306299
var pathElementsCFr: seq[CFr]
@@ -322,9 +315,11 @@ proc generateProof*(
322315
freeVec(externalNullifierVec)
323316

324317
# Compute x (Shamir share x-coordinate) - simplified for example
325-
var xCFr: CFr
326-
if not ffi_cfr_zero(addr xCFr):
318+
let xCFrPtr = cfr_zero()
319+
if xCFrPtr == nil:
327320
return err("failed to create x coordinate")
321+
let xCFr = xCFrPtr[]
322+
cfr_free(xCFrPtr)
328323

329324
# Build witness input struct
330325
var witness = ffi_RLNWitnessInput(

0 commit comments

Comments
 (0)