summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2011-08-16 15:48:06 +0200
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2011-08-16 15:48:06 +0200
commit8a5aa2f4b6b3f26daa886faceae6fe6547ab2187 (patch)
tree45ff4f3dca5240807e515537b9e4f5eabf85af6e
parent93515cf43ce16974c7bef1d1d8332e3c8cfe6fac (diff)
Salsa20: Use a producer function for generating the output sequence.
-rw-r--r--salsa-chacha.rkt52
1 files changed, 30 insertions, 22 deletions
diff --git a/salsa-chacha.rkt b/salsa-chacha.rkt
index 17af9c8..e5a087d 100644
--- a/salsa-chacha.rkt
+++ b/salsa-chacha.rkt
@@ -21,15 +21,18 @@
(provide salsa20)
-(require/typed racket/stream
- [empty-stream #;(All (a) (Sequenceof a))
- (Sequenceof Byte)])
-
(require/typed racket
[sequence-generate (All (a)
(Sequenceof a)
-> (values (-> Boolean)
- (-> a)))])
+ (-> a)))]
+ #;[in-producer (All (a)
+ ((-> Any)
+ Any
+ -> (Sequenceof a)))]
+ [in-producer ((-> (U Byte Symbol))
+ Symbol
+ -> (Sequenceof Byte))])
(require/typed "typed-stream.rkt"
[lazy-functional-stream-append (All (a)
@@ -167,20 +170,25 @@
(: salsa20 (Bytes Bytes (Sequenceof Byte) -> (Sequenceof Byte)))
(define (salsa20 k v m)
(let-values ([(next? next) (sequence-generate m)])
- (let: loop : (Sequenceof Byte)
- ([i : Word 0])
- (if (next?)
- (let ([64bytes
- (let: inner-loop : Bytes
- ([k : Integer 0]
- [bytes : (Listof Byte) (list)])
- (if (and (next?) (< k 64))
- (inner-loop (add1 k) (cons (next) bytes))
- (list->bytes bytes)))]
- [i-code
- (integer->bytes/size i 'little-endian 8)])
- (lazy-functional-stream-append (ann (bytes-xor 64bytes
- (salsa20k k (bytes-append v i-code)))
- (Sequenceof Byte))
- (λ () (loop (add1 i)))))
- empty-stream))))
+ (let: ([i : Word 0]
+ [buffer : (Listof Byte) (list)])
+ (in-producer
+ (λ ()
+ (when (null? buffer)
+ (let ([64bytes
+ (let: inner-loop : Bytes
+ ([k : Integer 0]
+ [bytes : (Listof Byte) (list)])
+ (if (and (next?) (< k 64))
+ (inner-loop (add1 k) (cons (next) bytes))
+ (list->bytes bytes)))]
+ [i-code
+ (integer->bytes/size i 'little-endian 8)])
+ (set! buffer (bytes->list (bytes-xor 64bytes
+ (salsa20k k (bytes-append v i-code)))))))
+ (if (null? buffer)
+ 'end-of-sequence
+ (let ([byte (first buffer)])
+ (set! buffer (rest buffer))
+ byte)))
+ 'end-of-sequence))))