bn.ss
;; mzcrypto: crypto library for mzscheme
;; Copyright (C) 2007 Dimitris Vyzovitis <[email protected]>
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 2.1 of the License, or (at your option) any later version.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
;; USA

(module bn mzscheme
  (require (lib "foreign.ss"))
  (require "libcrypto.ss" "error.ss" "util.ss")
  (provide (all-defined))
  
  (define/alloc BN)
  (define/ffi (BN_add_word _pointer _ulong) -> _int : check-error)
  (define/ffi (BN_dup _pointer) -> _pointer : pointer/error)
  (define/ffi (BN_num_bits _pointer) -> _int)
  (define/ffi (BN_bn2bin _pointer _bytes) -> _int)
  (define/ffi (BN_bin2bn (bs : _bytes) (_int = (bytes-length bs)) 
                         (_pointer = #f) ) 
    -> _pointer : pointer/error)

  (define (bn-size bn)
    (ceiling (/ (BN_num_bits bn) 8)))

  (define (bn->bytes bn)
    (let ((bs (make-bytes (bn-size bn))))
      (shrink-bytes bs (BN_bn2bin bn bs))))
  
  (define (bytes->bn bs)
    (BN_bin2bn bs))
)