Skip to content

Commit

Permalink
feat: now sage generates input data
Browse files Browse the repository at this point in the history
  • Loading branch information
LesterEvSe committed Aug 9, 2024
1 parent 64f6066 commit 7574e1b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 68 deletions.
2 changes: 1 addition & 1 deletion crates/plume/src/constants.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::bigint::Secpk1Fq;

global MSG_LEN = 32;
global MSG_LEN =32;
global PLUME_MSG_LEN = MSG_LEN + 33; // Because a point is added, which is compressed to 33 bytes

global ZERO = Secpk1Fq::from_le_bytes(&[0]);
Expand Down
4 changes: 2 additions & 2 deletions crates/use/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ pub fn main(msg: [u8; MSG_LEN], c: [u8; 32], s: [u8; 32], pk: [[u8; 32]; 2], nul

// Choose your plume variant and comment the other

plume_v1(msg, c, s, pk, nullifier);
// plume_v2(msg, c, s, pk, nullifier);
// plume_v1(msg, c, s, pk, nullifier);
plume_v2(msg, c, s, pk, nullifier);
}
8 changes: 4 additions & 4 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Tests

Plume, which is written in sage. Generates test values `r`, `sk` and `msg` 32 bytes long, then runs `nargo check`, fills in the required values in `Prover.toml`, and finally runs the commands `nargo prove` and `nargo verify`.
Plume, which is written in sage. Generates test values `r`, `sk` of 32 bytes and msg of arbitrary length, then runs `nargo check`, fills in the required values in `Prover.toml` and changes the files `main.nr` and `constants.nr`.

## Build and Run

1. Install SageMath by following [these instructions](https://doc.sagemath.org/html/en/installation/index.html).
2. To start, select the version of plume you want to test and uncomment the corresponding line in [main.nr](../crates/use/src/main.nr), and comment out the other.
3. Change the MSG_LEN value in the [constants](../crates/plume/src/constants.nr) file to 32.
4. Run test.sage script of the appropriate version using the command `sage test.sage v1` or `sage test.sage v2`.
2. To start, select the version of plume you want to run "v1" or "v2", then the number of bytes for msg (non-negative number). For example, `sage gen_data.sage v2 32`.

After that, you can run the program without interfering with the Noir code.
97 changes: 97 additions & 0 deletions tests/gen_data.sage
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import sys
import subprocess
import toml
load('test_gen.sage')

# Run a shell command and print the output
def run_command(command):
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.stdout:
print(result.stdout, end='')

if result.returncode != 0:
if result.stderr:
print(result.stderr)
raise subprocess.CalledProcessError(result.returncode, command)

def format_array(arr):
return "[" + ", ".join(f'"{item}"' for item in arr) + "]"

def format_double_array(pk):
return "[" + ", ".join(format_array(subarr) for subarr in pk) + "]"

def update_prover_toml(filepath, version1: bool, msg_len: int):
data = toml.load(filepath)
(msg, c, s, pk, nullifier) = plume_generate_test_case(version1, msg_len)

data['c'] = c
data['msg'] = msg
data['nullifier'] = nullifier
data['pk'] = pk
data['s'] = s

c_str = format_array(c)
msg_str = format_array(msg)
nullifier_str = format_double_array(nullifier)
pk_str = format_double_array(pk)
s_str = format_array(s)

with open(filepath, 'w') as f:
f.write(f'c = {c_str}\n')
f.write(f'msg = {msg_str}\n')
f.write(f'nullifier = {nullifier_str}\n')
f.write(f'pk = {pk_str}\n')
f.write(f's = {s_str}\n')

def update_plume_version(is_v1: bool):
path = "../crates/use/src/"
p1_line = 13
p2_line = 14

with open(path + 'main.nr', 'r') as file:
lines = file.readlines()

if is_v1:
lines[p1_line] = " plume_v1(msg, c, s, pk, nullifier);\n"
lines[p2_line] = " // plume_v2(msg, c, s, pk, nullifier);\n"
else:
lines[p1_line] = " // plume_v1(msg, c, s, pk, nullifier);\n"
lines[p2_line] = " plume_v2(msg, c, s, pk, nullifier);\n"

with open(path + 'main.nr', 'w') as file:
file.writelines(lines)


def update_MSG_LEN_variable(msg_len: int):
path = "../crates/plume/src/"
with open(path + 'constants.nr', 'r') as file:
lines = file.readlines()

MSG_LEN_line = 2
lines[MSG_LEN_line] = lines[MSG_LEN_line][:-4] + str(msg_len) + ";\n"

with open(path + 'constants.nr', 'w') as file:
file.writelines(lines)


# Take MSG_LEN number and plume version (v1 or v2)
if __name__ == "__main__":
run_command("nargo check")

prover_toml_path = "../crates/use/Prover.toml"
if len(sys.argv) != 3:
print("Error: incorrect number of arguments")
sys.exit()

versions = ["v1", "v2"]

if sys.argv[1] not in versions:
print("Error:", sys.argv[1], "incorrect version.")
sys.exit()

is_v1 = sys.argv[1] == versions[0]
msg_len = int(sys.argv[2])

update_plume_version(is_v1)
update_MSG_LEN_variable(msg_len)
update_prover_toml(prover_toml_path, is_v1, msg_len)
57 changes: 0 additions & 57 deletions tests/test.sage

This file was deleted.

8 changes: 4 additions & 4 deletions tests/test_gen.sage
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def get_G():
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
)

def generate_random_r_sk_msg():
def generate_random_r_sk_msg(msg_len: int):
r = os.urandom(32)
sk = os.urandom(32)
msg = os.urandom(32)
msg = os.urandom(msg_len)
return (list(r), list(sk), list(msg))

def plume_generate_test_case(version1):
(r, sk, msg) = generate_random_r_sk_msg()
def plume_generate_test_case(version1: bool, msg_len: int):
(r, sk, msg) = generate_random_r_sk_msg(msg_len)
r = bytes_to_num(r)
sk = bytes_to_num(sk)

Expand Down

0 comments on commit 7574e1b

Please sign in to comment.