<commit_before>#load "utils.fs"

open Utils
open System
open System.Numerics

let rnd = new Random()
let ((e, n), (d, _)) = Utils.genRSAKeys rnd 128
let k = n.ToByteArray() |> Array.length
    
(* Padding and Oracle *)

let parityOracle d n c : bool =
    let m' = Utils.decryptRSA d n c
    let mArr = m'.ToByteArray() |> Array.rev
    let pad = repeatArr 0uy (k - (Array.length mArr))
    let mArr' = Array.append pad mArr
    mArr'.[0..1] = [|0uy; 2uy|]

let genNonZeroRandArr (size: int) : byte [] =
    let rnd = Random()
    [|for _ in 1..size do yield rnd.Next 254 |> (+) 1 |> byte|]

let padPKCS15 (m: string) : byte [] =
    let mArr = Utils.strToBytes m
    let PS = genNonZeroRandArr (k - 3 - (Array.length mArr))
    Array.concat [| [| 0uy; 2uy |]; PS; [| 0uy |]; mArr |] |> Array.rev

(* Padding and Oracle Test *)

let padded = padPKCS15 "kick it, CC"
let c = padded |> BigInteger |> Utils.encryptRSA e n
let m  = Utils.decryptRSA d n c
let mStr = m.ToByteArray() |> Array.rev |> Utils.bytesToStr
let testParityOracle = parityOracle d n c

(* Parity Oracle Decryption *)

let B = BigInteger.Pow (BigInteger 2, (8 * (k-2)))
let zero = BigInteger 0
let one = BigInteger 1
let two = BigInteger 2
let three = BigInteger 3
let M = [| (two * B, three * B - one) |]

let checkOracle s =
    (c * BigInteger.ModPow (s, e, n)) % n |> parityOracle d n

let initSearch =
    let rec search s =
        match checkOracle s with
        | true -> s
        | _    -> search (s + one)
    search (n / (three * B))<commit_msg>minor refactoring<commit_after>#load "utils.fs"

open Utils
open System
open System.Numerics

let rnd = new Random()
let ((e, n), (d, _)) = Utils.genRSAKeys rnd 128
let k = n.ToByteArray() |> Array.length
    
(* Padding and Oracle *)

let parityOracle d n c : bool =
    let m = Utils.decryptRSA d n c
    let mArr = m.ToByteArray() |> Array.rev
    let pad = repeatArr 0uy (k - (Array.length mArr))
    let mArr' = Array.append pad mArr
    mArr'.[0..1] = [|0uy; 2uy|]

let genNonZeroRandArr (size: int) : byte [] =
    let rnd = Random()
    [|for _ in 1..size do yield rnd.Next 254 |> (+) 1 |> byte|]

let padPKCS15 (m: string) : byte [] =
    let mArr = Utils.strToBytes m
    let PS = genNonZeroRandArr (k - 3 - (Array.length mArr))
    Array.concat [| [| 0uy; 2uy |]; PS; [| 0uy |]; mArr |] |> Array.rev

(* Padding and Oracle Test *)

let padded = padPKCS15 "kick it, CC"
let c = padded |> BigInteger |> Utils.encryptRSA e n
let m  = Utils.decryptRSA d n c
let mStr = m.ToByteArray() |> Array.rev |> Utils.bytesToStr
let testParityOracle = parityOracle d n c

(* Parity Oracle Decryption *)

let B = BigInteger.Pow (BigInteger 2, (8 * (k-2)))
let zero, one, two, three = (BigInteger 0, BigInteger 1,
                             BigInteger 2, BigInteger 3)
let M = [| (two * B, three * B - one) |]

let checkOracle s =
    (c * BigInteger.ModPow (s, e, n)) % n |> parityOracle d n

let initSearch =
    let rec search s =
        match checkOracle s with
        | true -> s
        | _    -> search (s + one)
    search (n / (three * B))
{'fraction_non_alphanumeric': 0.14099641810485183, 'fraction_numerical': 0.01823510257245197, 'mean_word_length': 2.7056694813027744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '2700826', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1152, 'n_words': 387}