aboutsummaryrefslogtreecommitdiff
path: root/gnu/gnunet/hashcode.scm
diff options
context:
space:
mode:
authorMaxime Devos <maximedevos@telenet.be>2023-01-29 23:36:29 +0100
committerMaxime Devos <maximedevos@telenet.be>2023-01-30 00:17:27 +0100
commit656f43559f488fbee4a4ebd13421136bfe32bcfc (patch)
tree4ee5a150472ed8c04e9e624184b2edb91e3b5bcf /gnu/gnunet/hashcode.scm
parent6cc094bc15814353a462b118cf8e203270bd91d0 (diff)
downloadgnunet-scheme-656f43559f488fbee4a4ebd13421136bfe32bcfc.tar.gz
gnunet-scheme-656f43559f488fbee4a4ebd13421136bfe32bcfc.zip
records: New API for record types, specialised to bytevector slices.
This simplifies many record definitions and is less prone to errors -- if there is an error in the generation code, it will likely impact multiple record types, so tests for one record type also partially test other record types. It also reduces the amount of code to be written -- reducing boilerplate, in other words. * gnu/gnunet/utils/records.scm: New module. * Makefile.am (modules): Add it. * gnu/gnunet/cadet/client.scm: Use it. * gnu/gnunet/hashcode.scm: Likewise. * gnu/gnunet/fs/uri.scm: Likewise.
Diffstat (limited to 'gnu/gnunet/hashcode.scm')
-rw-r--r--gnu/gnunet/hashcode.scm67
1 files changed, 27 insertions, 40 deletions
diff --git a/gnu/gnunet/hashcode.scm b/gnu/gnunet/hashcode.scm
index 2137755..d1b9c6b 100644
--- a/gnu/gnunet/hashcode.scm
+++ b/gnu/gnunet/hashcode.scm
@@ -1,6 +1,6 @@
1;#!r6rs 1;#!r6rs
2;; This file is part of scheme-GNUnet, a partial Scheme port of GNUnet. 2;; This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
3;; Copyright (C) 2006--2020, 2022 GNUnet e.V. 3;; Copyright (C) 2006--2020, 2022--2023 GNUnet e.V.
4;; 4;;
5;; GNUnet is free software: you can redistribute it and/or modify it 5;; GNUnet is free software: you can redistribute it and/or modify it
6;; under the terms of the GNU Affero General Public License as published 6;; under the terms of the GNU Affero General Public License as published
@@ -29,7 +29,10 @@
29 copy-hashcode:512 copy-hashcode:256) 29 copy-hashcode:512 copy-hashcode:256)
30 (import (rnrs base) 30 (import (rnrs base)
31 (gnu gnunet utils bv-slice) 31 (gnu gnunet utils bv-slice)
32 (rnrs records syntactic)) 32 (only (gnu gnunet hashcode struct)
33 /hashcode:512 /hashcode:256)
34 (only (gnu gnunet utils records)
35 define-record-type*))
33 36
34 (define hashcode:512-bit-length 512) 37 (define hashcode:512-bit-length 512)
35 (define hashcode:256-bit-length 256) 38 (define hashcode:256-bit-length 256)
@@ -38,51 +41,35 @@
38 41
39 ;; A 512-bit hashcode. These are the default length for GNUnet, 42 ;; A 512-bit hashcode. These are the default length for GNUnet,
40 ;; using SHA-512. 43 ;; using SHA-512.
41 (define-record-type (<hashcode:512> make-hashcode:512/share hashcode:512?) 44 (define-record-type* (<hashcode:512> hashcode:512?)
42 (fields (immutable slice hashcode:512->slice)) 45 #:network-structure /hashcode:512
43 (opaque #t) 46 #:read-only-slice-wrapper #true
44 (sealed #t) 47 #:unwrap hashcode:512->slice
45 (protocol 48 #:constructor (make-hashcode:512/share
46 (lambda (%make) 49 "Make a hashcode, containing @var{slice} (a readable
47 (lambda (slice)
48 "Make a hashcode, containing @var{slice} (a readable
49@code{/hashcode:512} bytevector slice). @var{slice} may not be mutated 50@code{/hashcode:512} bytevector slice). @var{slice} may not be mutated
50while the constructed hashcode is in use." 51while the constructed hashcode is in use.")
51 (assert (= (slice-length slice) hashcode:512-u8-length)) 52 #:constructor/copy make-hashcode:512
52 (%make (slice/read-only slice)))))) 53 #:copy (copy-hashcode:512
53 54 "Make a copy of the hashcode:512 @var{hashcode:512}. This can be useful if
54 (define (make-hashcode:512 slice)
55 "Make a hashcode, containing @var{slice} (a readable @code{/hashcode:512}
56bytevector slice). @var{slice} may not be mutated while the constructed
57hashcode is in use."
58 (make-hashcode:512/share (slice-copy/read-only slice)))
59
60 (define (copy-hashcode:512 hashcode:512)
61 "Make a copy of the hashcode:512 @var{hashcode:512}. This can be useful if
62the slice used during the construction of @var{hashcode:512} is potentially 55the slice used during the construction of @var{hashcode:512} is potentially
63going to be mutated while a hashcode will still be in use." 56going to be mutated while a hashcode will still be in use."))
64 (make-hashcode:512 (hashcode:512->slice hashcode:512)))
65 57
66 ;; A 256-bit hashcode. Used under special conditions, like when space 58 ;; A 256-bit hashcode. Used under special conditions, like when space
67 ;; is critical and security is not impacted by it. 59 ;; is critical and security is not impacted by it.
68 (define-record-type (<hashcode:256> make-hashcode:256/share hashcode:256?) 60 (define-record-type* (<hashcode:256> hashcode:256?)
69 (fields (immutable slice hashcode:256->slice)) 61 #:network-structure /hashcode:256
70 (opaque #t) 62 #:read-only-slice-wrapper #true
71 (sealed #t) 63 #:unwrap hashcode:256->slice
72 (protocol 64 #:constructor (make-hashcode:256/share
73 (lambda (%make) 65 "Make a short hashcode, containing @var{slice} (a readable
74 (lambda (slice)
75 "Make a short hashcode, containing @var{slice} (a readable
76@code{/hashcode:256} bytevector slice). @var{slice} may not be mutated 66@code{/hashcode:256} bytevector slice). @var{slice} may not be mutated
77while the constructed short hashcode is in use." 67while the constructed short hashcode is in use.")
78 (assert (= (slice-length slice) hashcode:256-u8-length)) 68 #:constructor/copy make-hashcode:256
79 (%make (slice/read-only slice)))))) 69 #:copy (copy-hashcode:256
80 70 "Make a copy of the hashcode:256 @var{hashcode:256}. This can be useful if
81 (define (copy-hashcode:256 hashcode:256)
82 "Make a copy of the hashcode:256 @var{hashcode:256}. This can be useful if
83the slice used during the construction of @var{hashcode:256} is potentially 71the slice used during the construction of @var{hashcode:256} is potentially
84going to be mutated while a hashcode will still be in use." 72going to be mutated while a hashcode will still be in use."))
85 (make-hashcode:256 (hashcode:256->slice hashcode:256)))
86 73
87 (define (bv->hashcode:512 bv) 74 (define (bv->hashcode:512 bv)
88 "Read a hashcode from a bytevector (deprecated)." 75 "Read a hashcode from a bytevector (deprecated)."