Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases for addition, subtraction, multiplication, and modulo in the secp256k1 finite field #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions test/bigint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,7 @@ const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);

function bigint_to_array(n: number, k: number, x: bigint) {
let mod: bigint = 1n;
for (var idx = 0; idx < n; idx++) {
mod = mod * 2n;
}

let ret: bigint[] = [];
var x_temp: bigint = x;
for (var idx = 0; idx < k; idx++) {
ret.push(x_temp % mod);
x_temp = x_temp / mod;
}
return ret;
}
import { bigint_to_array } from './utils'

describe("BigMod n = 2, k = 2 exhaustive", function() {
this.timeout(1000 * 1000);
Expand Down
37 changes: 37 additions & 0 deletions test/circuits/test_secp256k1_fp_add.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pragma circom 2.0.2;

include "../../circuits/bigint.circom";
include "../../circuits/secp256k1_func.circom";

template Secp256k1_Fp_Add(n, k) {
signal input a[k];
signal input b[k];
signal output out[k];

component adder = BigAdd(n, k);
for (var i = 0; i < k; i ++) {
adder.a[i] <== a[i];
adder.b[i] <== b[i];
}

component mod = BigMod(n, k);

for (var i = 0; i < k + 1; i ++) {
mod.a[i] <== adder.out[i];
}

for (var i = k + 1; i < k * 2; i ++) {
mod.a[i] <== 0;
}

mod.b[0] <== 18446744069414583343;
mod.b[1] <== 18446744073709551615;
mod.b[2] <== 18446744073709551615;
mod.b[3] <== 18446744073709551615;

for (var i = 0; i < k; i ++) {
out[i] <== mod.mod[i];
}
}

component main {public [a, b]} = Secp256k1_Fp_Add(64, 4);
29 changes: 29 additions & 0 deletions test/circuits/test_secp256k1_fp_mod.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pragma circom 2.0.2;

include "../../circuits/bigint.circom";

template Secp256k1_Fp_Mod() {
var n = 64;
var k = 4;

signal input a[k];
signal output out[k];

component m = BigMod(n, k);

for (var i = 0; i < k; i ++) {
m.a[i] <== a[i];
m.a[k + i] <== 0;
}

m.b[0] <== 18446744069414583343;
m.b[1] <== 18446744073709551615;
m.b[2] <== 18446744073709551615;
m.b[3] <== 18446744073709551615;

for (var i = 0; i < k; i ++) {
out[i] <== m.mod[i];
}
}

component main {public [a]} = Secp256k1_Fp_Mod();
25 changes: 25 additions & 0 deletions test/circuits/test_secp256k1_fp_mul.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pragma circom 2.0.2;

include "../../circuits/bigint.circom";

template Secp256k1_Fp_Mul(n, k) {
signal input a[k];
signal input b[k];
signal output out[k];

component mul_mod_p = BigMultModP(n, k);
for (var i = 0; i < k; i ++) {
mul_mod_p.a[i] <== a[i];
mul_mod_p.b[i] <== b[i];
}
mul_mod_p.p[0] <== 18446744069414583343;
mul_mod_p.p[1] <== 18446744073709551615;
mul_mod_p.p[2] <== 18446744073709551615;
mul_mod_p.p[3] <== 18446744073709551615;

for (var i = 0; i < k; i ++) {
out[i] <== mul_mod_p.out[i];
}
}

component main {public [a, b]} = Secp256k1_Fp_Mul(64, 4);
27 changes: 27 additions & 0 deletions test/circuits/test_secp256k1_fp_sub.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pragma circom 2.0.2;

include "../../circuits/bigint.circom";
include "../../circuits/secp256k1_func.circom";

template Secp256k1_Fp_Sub(n, k) {
signal input a[k];
signal input b[k];
signal output out[k];

component sub = BigSubModP(n, k);
for (var i = 0; i < k; i ++) {
sub.a[i] <== a[i];
sub.b[i] <== b[i];
}

sub.p[0] <== 18446744069414583343;
sub.p[1] <== 18446744073709551615;
sub.p[2] <== 18446744073709551615;
sub.p[3] <== 18446744073709551615;

for (var i = 0; i < k; i ++) {
out[i] <== sub.out[i];
}
}

component main {public [a, b]} = Secp256k1_Fp_Sub(64, 4);
42 changes: 1 addition & 41 deletions test/ecdsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,7 @@ const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);

function bigint_to_tuple(x: bigint) {
let mod: bigint = 2n ** 64n;
let ret: [bigint, bigint, bigint, bigint] = [0n, 0n, 0n, 0n];

var x_temp: bigint = x;
for (var idx = 0; idx < ret.length; idx++) {
ret[idx] = x_temp % mod;
x_temp = x_temp / mod;
}
return ret;
}

function bigint_to_array(n: number, k: number, x: bigint) {
let mod: bigint = 1n;
for (var idx = 0; idx < n; idx++) {
mod = mod * 2n;
}

let ret: bigint[] = [];
var x_temp: bigint = x;
for (var idx = 0; idx < k; idx++) {
ret.push(x_temp % mod);
x_temp = x_temp / mod;
}
return ret;
}

// converts x = sum of a[i] * 2 ** (small_stride * i) for 0 <= 2 ** small_stride - 1
// to: sum of a[i] * 2 ** (stride * i)
function get_strided_bigint(stride: bigint, small_stride: bigint, x: bigint) {
var ret: bigint = 0n;
var exp: bigint = 0n;
while (x > 0) {
var mod: bigint = x % (2n ** small_stride);
ret = ret + mod * (2n ** (stride * exp));
x = x / (2n ** small_stride);
exp = exp + 1n;
}
return ret;
}
import { bigint_to_array, bigint_to_tuple, get_strided_bigint } from './utils'

describe("ECDSAPrivToPub", function () {
this.timeout(1000 * 1000);
Expand Down
16 changes: 1 addition & 15 deletions test/secp256k1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,7 @@ const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);

function bigint_to_array(n: number, k: number, x: bigint) {
let mod: bigint = 1n;
for (var idx = 0; idx < n; idx++) {
mod = mod * 2n;
}

let ret: bigint[] = [];
var x_temp: bigint = x;
for (var idx = 0; idx < k; idx++) {
ret.push(x_temp % mod);
x_temp = x_temp / mod;
}
return ret;
}
import { bigint_to_array } from './utils'

describe("Secp256k1AddUnequal", function () {
this.timeout(1000 * 1000);
Expand Down
Loading