From 930c56947e7ef1be14b33618dcb10136fc5d04a4 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sat, 22 Feb 2020 20:19:04 -0700 Subject: [PATCH] Move polynomial functionality to separate file --- client.rkt | 15 +-------------- poly.rkt | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 poly.rkt diff --git a/client.rkt b/client.rkt index c716954..96e6ee7 100644 --- a/client.rkt +++ b/client.rkt @@ -33,20 +33,7 @@ [#:struct (exn:fail:network:http:error exn:fail:network) ([code : Any] [type : Any])]) (require/typed crypto [crypto-random-bytes (Natural -> Bytes)]) - -(define num-bytes (assert (/ 1024 8) natural?)) - -(define (gen) - (let* ([bstr (crypto-random-bytes num-bytes)]) - (cast (bytes->integer bstr #f #t 0 num-bytes) Natural))) - -(define (random-poly [degree : Natural] [constant : Natural]) - (let ([coefficients (build-vector degree (λ (_) (gen)))]) - (λ ([x : Natural]) : Natural - (cast (+ constant - (for/sum : Integer ([i degree]) - (* (expt x (add1 i)) (vector-ref coefficients i)))) - Natural)))) +(require "poly.rkt") (define (natural->hex-string [n : Natural]) (bytes->hex-string diff --git a/poly.rkt b/poly.rkt new file mode 100644 index 0000000..251498f --- /dev/null +++ b/poly.rkt @@ -0,0 +1,25 @@ +#lang typed/racket +(require/typed binaryio + [bytes->integer (->* (Bytes Boolean) (Boolean Natural Natural) Integer)] + [integer->bytes (->* (Integer Natural Boolean) (Boolean Bytes Natural) Bytes)] + [integer-bytes-length (Integer Boolean -> Natural)]) +(require/typed crypto + [crypto-random-bytes (Natural -> Bytes)]) + +(define-type Poly (Natural -> Natural)) + +(define num-bytes (assert (/ 1024 8) natural?)) + +(define (gen) + (let* ([bstr (crypto-random-bytes num-bytes)]) + (cast (bytes->integer bstr #f #t 0 num-bytes) Natural))) + +(define (random-poly [degree : Natural] [constant : Natural]) : Poly + (let ([coefficients (build-vector degree (λ (_) (gen)))]) + (λ ([x : Natural]) : Natural + (cast (+ constant + (for/sum : Integer ([i degree]) + (* (expt x (add1 i)) (vector-ref coefficients i)))) + Natural)))) + +(provide (all-defined-out)) -- 2.38.4