Skip to content

Commit

Permalink
Do not use binprot serialization when converting bigint fields, use Z…
Browse files Browse the repository at this point in the history
…arith directly
  • Loading branch information
tizoc committed Nov 27, 2023
1 parent 6fcd939 commit b570c8f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/lib/crypto/kimchi_backend/common/dune
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
base.caml
ppx_inline_test.config
bignum.bigint
zarith
base.base_internalhash_types
;; local libraries
tuple_lib
Expand Down
32 changes: 10 additions & 22 deletions src/lib/crypto/kimchi_backend/common/field.ml
Original file line number Diff line number Diff line change
Expand Up @@ -179,28 +179,16 @@ module Make (F : Input_intf) :
let of_sexpable = of_bigint
end)

let zero = of_int 0

let to_bignum_bigint n =
(* For non-zero values, conversion is done by creating the bin_prot representation
of the [Bignum_bigit.t] value, and then parsing it with bin_prot. *)
match compare n zero with
| 0 ->
Bignum_bigint.zero
| c ->
(* Tag for positive values is 1, negative is 2:
https://github.com/janestreet/bignum/blob/6c63419787a4e209e85befd3d823fff2790677e0/bigint/src/bigint.ml#L27-L30 *)
let tag_byte = if c > 0 then '\x01' else '\x02' in
let bytes = to_bytes n in
let len = Bytes.length bytes in
let size_byte = Char.of_int_exn len in
let buf = Bytes.create (2 + len) in
(* First byte is the tag, second the amount of bytes *)
Bytes.unsafe_set buf 0 tag_byte ;
Bytes.unsafe_set buf 1 size_byte ;
(* Copy the bytes representation of the value, skip the tag and size bytes *)
Bytes.unsafe_blit ~src:bytes ~src_pos:0 ~dst_pos:2 ~len ~dst:buf ;
Bin_prot.Reader.of_bytes Bignum_bigint.Stable.V1.bin_reader_t buf
let to_bignum_bigint =
let zero = of_int 0 in
let one = of_int 1 in
fun n ->
if equal n zero then Bignum_bigint.zero
else if equal n one then Bignum_bigint.one
else
Bytes.unsafe_to_string
~no_mutation_while_string_reachable:(to_bytes n)
|> Z.of_bits |> Bignum_bigint.of_zarith_bigint

let hash_fold_t s x = Bignum_bigint.hash_fold_t s (to_bignum_bigint x)

Expand Down

0 comments on commit b570c8f

Please sign in to comment.