<commit_before>;; Freecoin - digital social currency toolkit

;; part of Decentralized Citizen Engagement Technologies (D-CENT)
;; R&D funded by the European Commission (FP7/CAPS 610349)

;; Based on Secret Share Java implementation by <NAME>
;; Copyright (C) 2015 <NAME> <<EMAIL>>

;; Shamir's Secret Sharing algorithm was invented by <NAME>
;; <NAME> (1979), "How to share a secret", Communications of the ACM 22 (11): 612–613
;; <NAME>. (1997), The Art of Computer Programming, II: Seminumerical Algorithms: 505

;; 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 3 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

(ns freecoin.fxc-test
  (:use midje.sweet)
  (:require
   [freecoin.fxc :as fxc]
   [freecoin.random :as rand]
   [freecoin.params :as param]
   [freecoin.secretshare :as ssss]

   [freecoin.utils :as util]

   [buddy.hashers :as hashers]
   [clojure.pprint :as pp]

   )
  )



(fact "Secret FXC codec"

  ;; manual creation of an nxt address to intercept it in clear
  (def fake {:ah (:integer (rand/create (:length param/encryption)))
             :al (:integer (rand/create (:length param/encryption)))
             })
  (def nxtpass (fxc/render-slice param/encryption "STUB" (:ah fake) (:al fake) 0))

  (def secret (fxc/create-secret param/encryption "STUB" (:ah fake) (:al fake)))

  (pp/pprint (format "%x FXC %x" (:ah fake) (:al fake)))
  (pp/pprint nxtpass)
  
  (fact "cookie is first slice" (first (:slices secret)) => (:cookie secret))

  (fact "reversable extraction / hash / conversion for AH"
    (fxc/extract-ahal param/encryption (:cookie secret) "ah")
    =>
    (fxc/extract-int param/encryption (first (:slices secret)) "ah")
    )

  (fact "reversable extraction / hash / conversion for AL"
    (fxc/extract-ahal param/encryption (:cookie secret) "al")
    =>
    (fxc/extract-int param/encryption (first (:slices secret)) "al")
    )

  (fact "unlocking secret"
    (let [quorum (fxc/extract-quorum param/encryption secret (:cookie secret))
          ah (ssss/shamir-combine (:ah quorum))
          al (ssss/shamir-combine (:al quorum))]
      ah => (:ah fake)
      al => (:al fake)
      (fxc/render-slice param/encryption "STUB" ah al 0) => nxtpass
      (util/log! 'ACK 'unlocking-secret-test
                 ["Quorum size: " (count (get-in quorum [:ah :shares]))
                  "expected: " (get-in quorum [:ah :header :quorum])]
                 )<commit_msg>restored clear test of secretshare<commit_after>;; Freecoin - digital social currency toolkit

;; part of Decentralized Citizen Engagement Technologies (D-CENT)
;; R&D funded by the European Commission (FP7/CAPS 610349)

;; Based on Secret Share Java implementation by <NAME>
;; Copyright (C) 2015 <NAME> <<EMAIL>>

;; Shamir's Secret Sharing algorithm was invented by <NAME>
;; <NAME> (1979), "How to share a secret", Communications of the ACM 22 (11): 612–613
;; <NAME>. (1997), The Art of Computer Programming, II: Seminumerical Algorithms: 505

;; 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 3 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

(ns freecoin.fxc-test
  (:use midje.sweet)
  (:require
   [freecoin.fxc :as fxc]
   [freecoin.random :as rand]
   [freecoin.params :as param]
   [freecoin.secretshare :as ssss]

   [freecoin.utils :as util]

   [buddy.hashers :as hashers]
   [clojure.pprint :as pp]

   )
  )



(fact "Secret FXC codec"

  ;; manual creation of an nxt address to intercept it in clear
  (def fake {:ah (:integer (rand/create (:length param/encryption)))
             :al (:integer (rand/create (:length param/encryption)))
             })
  (def nxtpass (fxc/render-slice param/encryption "STUB" (:ah fake) (:al fake) 0))

  (def secret (fxc/create-secret param/encryption "STUB" (:ah fake) (:al fake)))


  (pp/pprint nxtpass)
  (pp/pprint secret)
  
  (fact "cookie is first slice" (first (:slices secret)) => (:cookie secret))

  (fact "reversable extraction / hash / conversion for AH"
    (fxc/extract-ahal param/encryption (:cookie secret) "ah")
    =>
    (fxc/extract-int param/encryption (first (:slices secret)) "ah")
    )

  (fact "reversable extraction / hash / conversion for AL"
    (fxc/extract-ahal param/encryption (:cookie secret) "al")
    =>
    (fxc/extract-int param/encryption (first (:slices secret)) "al")
    )

  (fact "unlocking secret"
    (let [quorum (fxc/extract-quorum param/encryption secret (:cookie secret))
          ah (ssss/shamir-combine (:ah quorum))
          al (ssss/shamir-combine (:al quorum))]
      ah => (:ah fake)
      al => (:al fake)
      (fxc/render-slice param/encryption "STUB" ah al 0) => nxtpass
      (util/log! 'ACK 'unlocking-secret-test
                 ["Quorum size: " (count (get-in quorum [:ah :shares]))
                  "expected: " (get-in quorum [:ah :header :quorum])]
                 )
{'fraction_non_alphanumeric': 0.1217018469656992, 'fraction_numerical': 0.016160949868073878, 'mean_word_length': 3.3853940708604484, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2330834', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1848, 'n_words': 715}