jamestimothymeech /  RNG

Created
Maintained by jamestimothymeech
This project used Microsoft Bing Chat to generate Amaranth hardware description language scripts for a random number generator design. Amaranth is an open-source Python-based hardware description language that can generate Verilog. We used this approach to quickly and efficiently generate a hardware permuted congruential generator pseudo random number generator design, simulate the design, run the simulated output of the design through the Dieharder test suite, and finally produce the Verilog for the design to connect to the Caravel wishbone. We hardened the design using the Openlane flow and integrated it with the Caravel wishbone.  |   https://github.com/JamesTimothyMeech/RNG
Members 1
JamesTimothyMeech committed 5 months ago

RNG Project

Main Idea and High-Level Project Overview

This project is based on the idea that large language models are good at generating code in popular high-level languages such as Python. Large language models are comparatively bad at generating code in languages that describe hardware such as Verilog. Therefore this project leverages Amaranth, a Python hardware description language that can generate Verilog to quickly and efficiently generate a hardware pseudorandom number generator design, simulate the design, run the simulated output of the design through the Dieharder test suite, and finally produce the verilog for the design to connect to the Caravel wishbone and hardend. The image below shows a human drawn diagram of the random number generator design generated by Microsoft Bing Chat using the Amaranth hardware description language. We used Microsoft Bing Chat for this project because we had free access to the enterprise version and it can search the internet for (hopefully) up-to-date information about the open-source projects we are leveraging to produce our design in this project. It is unlikely that I would have been able to complete this project on my own, even using Amaranth as had to learn how to use it, partly from Microsoft Bing Chat during the project. We committed the original unedited conversations with Microsoft Bing Chat which we recorded using the Bing Chat History plugin for the Google Chrome browser. We then edited the files to remove formatting errors such as all code blocks being printed twice. We changed the names of the files generated by the Amranth scripts to make the documentation of the project easier to follow. We will share a write-up that provides a more in-depth analysis of the project on arxiv soon. to the enterprise version and it can search the internet for (hopefully) up-to-date information about the open-source projects we are leveraging to produce our design in this project. It is unlikely that I would have been able to complete this project on my own, even using Amaranth as had to learn how to use it, partly from Microsoft Bing Chat during the project. We committed the original unedited conversations with Microsoft Bing Chat which we recorded using the Bing Chat History plugin for the Google Chrome browser. We then edited the files to remove formatting errors such as all code blocks being printed twice. We changed the names of the files generated by the Amranth scripts to make the documentation of the project easier to follow. A write-up that provides a more in-depth analysis of the project is avaliable here: https://arxiv.org/abs/2311.03489. We will extend the write-up with more results and analysis as they become avaliable. Circuit

Initial Design Generation and Simulation

Initially, we explored the feasibility of the project by having the conversation recorded in the Initial_Design_Generation_and_Simulation.md file in the Chat-Logs directory. Microsoft Bing Chat was able to generate a Python script containing successfully simulated and produced verilog for a random number generator design. The .vcd files and screenshots of the simulation results can be found in the Simulations directory. The generated Verilog can be found in the Generated-Verilog directory.

Design Refinement and Simulation

Important signals such as seed, state, multiplier, and increment did not appear in the simulation generated in the conversation in the Initial_Design_Generation_and_Simulation.md file so we had a second conversation which can be found in the Refine_Initial_Design_and_Simulation.md file. The purpose of this conversation was to make these signals appear in both the simulated results and the generated Verilog module. The .vcd files and screenshots of the simulation results can be found in the Simulations directory. The generated Verilog can be found in the Generated-Verilog directory.

Random Number Generator Testing Using The Dieharder Test Suite

We had a conversation in the Random_Number_Generator_Dieharder_Simulation.md file to use Amaranth to create a simulation of the random number generator which would print the random numbers produced by the generator to a .txt file which we could use as input to the Dieharder test suite to verify the quality of the random number generator. Microsoft Bing Chat was able to successfully produce a Python script that created a text file containing the output of the random number generator. We had the script not write the simulation results to a .vcd file to increase the simulation speed, even with this improvement the Dieharder test suite takes multiple days to run over the simulated random number generator output. Testing a file of random numbers is undesirable as the files have to be very large ~ 250 GB so as not to be re-wound by the Dieharder test suite. For this reason, we wanted to pipe the output of the random number generator directly into the Dieharder test suite by running the terminal command python3 test-edited.py | dieharder -a -g 200. For this reason, we manually edited the Python script generated by Microsoft Bing Chat to write the line sys.stdout.buffer.write(struct.pack('>I', int(integer))) in place of where the Microsoft Bing Chat generated script wrote to the text file and import sys and struct. A sample of the text file generated (test.txt) and the output of the Dieharder tests can be found in the Simulations directory. As of now, the tests are not completed but we saved the current partial results in the test-results.txt file in the simulation directory. Two tests have failed already but as we will discuss in the arxiv writeup, even good random number generators can fail some tests.

Testing Random Number Generator Design on Icebreaker FPGA

We attempted to have Microsoft Bing Chat generate a Python script to deploy the random number generator design to an Icebreaker FPGA but it was not able to do so. Instead, we hand-edited the refined.py file to produce the fpga.py file which we used to test the design on the Icebreaker FPGA. We verified the design was functional by slowing the design down using a counter and connecting the Icebreaker LEDs to six of the random number generator output signals. We also measured the signals with a logic analyzer at a higher clock speed without slowing the design down with a counter. We attempted to break out more of the output signals to the Icebreaker PMOD connectors but we were unable to get this working.

Creating and Simulating Wishbone Interface

We had a conversation in the Create_and_Simulate_Wishbone.md file to add a wishbone interface to the random number generator to allow it to connect to the wishbone bus in Caravel. We include the Verilog generated when testing the connection of the output of the signal of the random number generator to the Wishbone bus. We assume that the subsequent Python files wishbone-test-seed.py, wishbone-test-multiplier.py, and wishbone-test-increment.py will all produce the same Verilog as we only asked Microsoft Bing Chat to change the simulation, not the design. The Verilog file does not include the correct ports to connect to the wishbone bust but we will solve that in the next section. We include .vcd files and screenshots for each of the four wishbone-test-*.py files in the Simulation-Results directory.

Creating The Final Design With Correct Ports

We had a conversation in the Final_Verilog_Design_Generation.md file to create the final.py Python script which we used to generate a Verilog module with the correct ports to connect to the Caravel wishbone bus. We then changed the name of the Verilog module to RNG and manually integrated it into the Caravel user_proj_example and hardened the design as described in the Efabless tutorial video. We edited the macro.cfg file to mprj 100 200 N to prevent the make user_project_wrapper command from failing.

Final Design With Wisbone Acks and Safety Settings Changes

To meet the original deadline of the project we hurriedly deleted the ack port of the Wishbone interface because our Amaranth design did not have any logic to drive it and this caused errors when running Openlane. After the competition deadline was extended we had one last conversation with Microsoft Bing Chat to add acks to the wishbone bus. The conversation can be found in the Final_Verilog_Design_Generation_With_Acks_and_Safety_Settings.md file in the Chat-Logs folder. Microsoft Bing Chat was able to add acks to the Wishbone bus but it was only able to do this after being reminded that it had generated Amranth in the past. We attempted to get it to generate simulations to test the final design but it refused to do so even after being reminded that it had generated Amaranth in the past. We believe this is because the safety settings of Microsoft Bing Chat have been changed to make it more hesitant to generate code for the user. In the absence of new test simulations, we used the old proc functions and simulation code that Microsoft Bing Chat generated to test the first version of the Wishbone bus to test the new version with acks included. The results of these tests can be found in the Simulation-Results/Final+Acks directory. The Python scripts used to generate these results can be found in the Amaranth-Python-Scripts/Final+Acks directory. The image below shows a diagram of how we connected the random number generator to the Caravel wishbone using an Amaranth generated wishbone interface to program and sample from the random number generator.

Project

Future Work and Project Direction

In future work, we want to implement the functionality to re-seed the random number generator using a true random number generator such as https://betrusted.io/avalanche-noise.html either inside or outside the ASIC. This would enable the best of both worlds, the high speed of a pseudorandom number generator and the security of a truly random number generator. The rate of re-seeding will be a tradeoff between security and random number generation speed. We may also need to remove one or both of the multiplier and increment values from the Wishbone bus for security purposes.

Acknowledgments and Thanks

Thanks to Joseph Dean (jpdean) for suggesting that I use Microsoft Bing chat when ChatGPT had trouble generating Amaranth code due to the training data cut-off date. Thanks to Emilio Baungarten and Anton Maurovic (algofoogle) for helping me harden the design when I was rushing to complete my submission before the deadline in the early hours of the morning.

                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright [yyyy] [name of copyright owner]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.